|
| 1 | +// Copyright © 2018 github.com/devopsctl authors |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "testing" |
| 26 | + |
| 27 | + gitlab "github.com/xanzy/go-gitlab" |
| 28 | +) |
| 29 | + |
| 30 | +func TestDeleteAllMembers(t *testing.T) { |
| 31 | + tg := []struct { |
| 32 | + name string |
| 33 | + args []string |
| 34 | + flagsMap map[string]string |
| 35 | + expect testResult |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "delete all from a project", |
| 39 | + flagsMap: map[string]string{ |
| 40 | + "from-project": "TestDeleteAllMembersProject", |
| 41 | + }, |
| 42 | + expect: pass, |
| 43 | + }, |
| 44 | + } |
| 45 | + t.Run(tg[0].name, func(t *testing.T) { |
| 46 | + // SETUP: |
| 47 | + // create the test project |
| 48 | + gid, _ := getNamespaceID("Group1") |
| 49 | + if _, err := newProject(&gitlab.CreateProjectOptions{ |
| 50 | + Path: gitlab.String(tg[0].flagsMap["from-project"]), |
| 51 | + Name: gitlab.String(tg[0].flagsMap["from-project"]), |
| 52 | + NamespaceID: gitlab.Int(gid), |
| 53 | + }); err != nil { |
| 54 | + tInfo(err) |
| 55 | + } |
| 56 | + |
| 57 | + tg[0].flagsMap["from-project"] = "Group1/" + tg[0].flagsMap["from-project"] |
| 58 | + // add project members |
| 59 | + if _, err := newProjectMember(tg[0].flagsMap["from-project"], |
| 60 | + "john.smith", |
| 61 | + &gitlab.AddProjectMemberOptions{ |
| 62 | + AccessLevel: gitlab.AccessLevel(40), |
| 63 | + }); err != nil { |
| 64 | + tInfo(err) |
| 65 | + } |
| 66 | + |
| 67 | + if _, err := newProjectMember(tg[0].flagsMap["from-project"], |
| 68 | + "john.doe", |
| 69 | + &gitlab.AddProjectMemberOptions{ |
| 70 | + AccessLevel: gitlab.AccessLevel(40), |
| 71 | + }); err != nil { |
| 72 | + tInfo(err) |
| 73 | + } |
| 74 | + |
| 75 | + execT := execTestCmdFlags{ |
| 76 | + t: t, |
| 77 | + cmd: deleteAllMembersCmd, |
| 78 | + flagsMap: tg[0].flagsMap, |
| 79 | + } |
| 80 | + stdout, execResult := execT.executeCommand() |
| 81 | + assertEqualResult(t, tg[0].expect, execResult, stdout) |
| 82 | + |
| 83 | + // delete test project |
| 84 | + if err := deleteProject(tg[0].flagsMap["from-project"]); err != nil { |
| 85 | + tInfo("Removal of test data failure for delete all-members test") |
| 86 | + } |
| 87 | + |
| 88 | + }) |
| 89 | + |
| 90 | + // negative tests |
| 91 | + tt := []struct { |
| 92 | + name string |
| 93 | + flagsMap map[string]string |
| 94 | + expect testResult |
| 95 | + expectOut string |
| 96 | + }{ |
| 97 | + { |
| 98 | + name: "fails when no flag is used", |
| 99 | + flagsMap: map[string]string{ |
| 100 | + "from-project": "", |
| 101 | + }, |
| 102 | + expect: fail, |
| 103 | + expectOut: fmt.Sprint(setAtLeastOneFlagError), |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tc := range tt { |
| 108 | + t.Run(tc.name, func(t *testing.T) { |
| 109 | + execT := execTestCmdFlags{ |
| 110 | + t: t, |
| 111 | + cmd: deleteAllMembersCmd, |
| 112 | + flagsMap: tc.flagsMap, |
| 113 | + } |
| 114 | + stdout, _ := execT.executeCommand() |
| 115 | + assertOutContains(t, stdout, tc.expectOut) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments