|
1 | 1 | package user_test |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - |
6 | 4 | "github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal" |
7 | 5 | . "github.com/onsi/ginkgo/v2" |
8 | 6 | . "github.com/onsi/gomega" |
9 | | - "github.com/softlayer/softlayer-go/datatypes" |
10 | 7 | "github.com/softlayer/softlayer-go/session" |
11 | | - "github.com/softlayer/softlayer-go/sl" |
| 8 | + "strings" |
12 | 9 |
|
13 | 10 | "github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/user" |
14 | 11 | "github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata" |
15 | 12 | "github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers" |
16 | 13 | ) |
17 | 14 |
|
18 | | -var _ = Describe("User List", func() { |
| 15 | +var _ = Describe("sl user list", func() { |
19 | 16 | var ( |
20 | | - fakeUI *terminal.FakeUI |
21 | | - fakeUserManager *testhelpers.FakeUserManager |
22 | | - cliCommand *user.ListCommand |
23 | | - fakeSession *session.Session |
24 | | - slCommand *metadata.SoftlayerCommand |
| 17 | + fakeUI *terminal.FakeUI |
| 18 | + // fakeUserManager *testhelpers.FakeUserManager |
| 19 | + cliCommand *user.ListCommand |
| 20 | + fakeSession *session.Session |
| 21 | + slCommand *metadata.SoftlayerCommand |
| 22 | + fakeHandler *testhelpers.FakeTransportHandler |
25 | 23 | ) |
26 | 24 | BeforeEach(func() { |
27 | 25 | fakeUI = terminal.NewFakeUI() |
28 | | - fakeUserManager = new(testhelpers.FakeUserManager) |
29 | | - fakeSession = testhelpers.NewFakeSoftlayerSession([]string{}) |
| 26 | + // fakeUserManager = new(testhelpers.FakeUserManager) |
| 27 | + fakeSession = testhelpers.NewFakeSoftlayerSession(nil) |
| 28 | + fakeHandler = testhelpers.GetSessionHandler(fakeSession) |
30 | 29 | slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession) |
31 | 30 | cliCommand = user.NewListCommand(slCommand) |
32 | 31 | cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.") |
33 | | - cliCommand.UserManager = fakeUserManager |
34 | | - |
35 | | - testListUser := []datatypes.User_Customer{ |
36 | | - datatypes.User_Customer{ |
37 | | - Id: sl.Int(5555), |
38 | | - Username: sl.String("ATestUser"), |
39 | | - Email: sl.String("user2@email.com"), |
40 | | - DisplayName: sl.String("DisplayedName"), |
41 | | - ExternalBindingCount: sl.Uint(123), |
42 | | - ApiAuthenticationKeyCount: sl.Uint(123456), |
43 | | - UserStatus: &datatypes.User_Customer_Status{ |
44 | | - Name: sl.String("ACTIVE"), |
45 | | - }, |
46 | | - }, |
47 | | - datatypes.User_Customer{ |
48 | | - Id: sl.Int(5556), |
49 | | - Username: sl.String("ATestUser2"), |
50 | | - Email: sl.String("user2@email.com"), |
51 | | - DisplayName: sl.String("DisplayedName2"), |
52 | | - ExternalBindingCount: sl.Uint(1234), |
53 | | - ApiAuthenticationKeyCount: sl.Uint(1234567), |
54 | | - }, |
55 | | - } |
56 | | - fakeUserManager.ListUsersReturns(testListUser, nil) |
| 32 | + }) |
| 33 | + AfterEach(func() { |
| 34 | + // Clear API call logs and any errors that might have been set after every test |
| 35 | + fakeHandler.ClearApiCallLogs() |
| 36 | + fakeHandler.ClearErrors() |
57 | 37 | }) |
58 | 38 |
|
59 | | - Describe("user list ", func() { |
60 | | - Context("user list with unknown column", func() { |
61 | | - It("return error", func() { |
62 | | - err := testhelpers.RunCobraCommand(cliCommand.Command, "--column", "noExist") |
63 | | - Expect(err).To(HaveOccurred()) |
64 | | - Expect(err.Error()).To(ContainSubstring("Incorrect Usage: --column noExist is not supported.")) |
65 | | - }) |
| 39 | + Describe("Usage Errors ", func() { |
| 40 | + It("return error", func() { |
| 41 | + err := testhelpers.RunCobraCommand(cliCommand.Command, "--column", "noExist") |
| 42 | + Expect(err).To(HaveOccurred()) |
| 43 | + Expect(err.Error()).To(ContainSubstring("Incorrect Usage: --column noExist is not supported.")) |
66 | 44 | }) |
| 45 | + }) |
67 | 46 |
|
68 | | - Context("user list fatal error", func() { |
69 | | - It("return error", func() { |
70 | | - fakeUserManager.ListUsersReturns([]datatypes.User_Customer{}, errors.New("Internal server error")) |
71 | | - err := testhelpers.RunCobraCommand(cliCommand.Command) |
72 | | - Expect(err).To(HaveOccurred()) |
73 | | - Expect(err.Error()).To(ContainSubstring("Failed to list users.")) |
74 | | - }) |
| 47 | + Describe("API Errors", func() { |
| 48 | + It("SoftLayer_Account::getUsers API Error", func() { |
| 49 | + fakeHandler.AddApiError("SoftLayer_Account", "getUsers", 500, "Internal Server Error") |
| 50 | + err := testhelpers.RunCobraCommand(cliCommand.Command) |
| 51 | + Expect(err).To(HaveOccurred()) |
| 52 | + Expect(err.Error()).To(ContainSubstring("Failed to list users.")) |
75 | 53 | }) |
| 54 | + }) |
76 | 55 |
|
77 | | - Context("user list", func() { |
78 | | - It("return users list", func() { |
79 | | - err := testhelpers.RunCobraCommand(cliCommand.Command) |
80 | | - Expect(err).NotTo(HaveOccurred()) |
81 | | - Expect(fakeUI.Outputs()).To(ContainSubstring("id username email displayName 2FA classicAPIKey")) |
82 | | - Expect(fakeUI.Outputs()).To(ContainSubstring("5555 ATestUser user2@email.com DisplayedName yes yes")) |
83 | | - Expect(fakeUI.Outputs()).To(ContainSubstring("5556 ATestUser2 user2@email.com DisplayedName2 yes yes")) |
84 | | - }) |
| 56 | + Describe("Happy Path", func() { |
| 57 | + It("return users list", func() { |
| 58 | + err := testhelpers.RunCobraCommand(cliCommand.Command) |
| 59 | + Expect(err).NotTo(HaveOccurred()) |
| 60 | + // Remove whitespace to make testing output a bit less rigid |
| 61 | + trimmed := strings.ReplaceAll(fakeUI.Outputs(), " ", "") |
| 62 | + Expect(trimmed).To(ContainSubstring("idusernameemaildisplayName2FAclassicAPIKeyvpn")) |
| 63 | + Expect(trimmed).To(ContainSubstring("1468361IBM27821sdfasdfasd@one.comazzz---")) |
| 64 | + Expect(trimmed).To(ContainSubstring("1468362IBM27832sdfasdfasd@two.comaccc-yesYes")) |
| 65 | + Expect(trimmed).To(ContainSubstring("1468363IBM27843sdfasdfasd@three.comabc--No")) |
85 | 66 | }) |
86 | | - |
87 | | - Context("user list with column", func() { |
88 | | - It("return users list", func() { |
89 | | - err := testhelpers.RunCobraCommand(cliCommand.Command, "--column", "username") |
90 | | - Expect(err).NotTo(HaveOccurred()) |
91 | | - Expect(fakeUI.Outputs()).To(ContainSubstring("username")) |
92 | | - Expect(fakeUI.Outputs()).To(ContainSubstring("ATestUser")) |
93 | | - Expect(fakeUI.Outputs()).To(ContainSubstring("ATestUser2")) |
94 | | - }) |
| 67 | + It("return users list simple columns", func() { |
| 68 | + err := testhelpers.RunCobraCommand(cliCommand.Command, "--column=id", "--column=displayName") |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + // Remove whitespace to make testing output a bit less rigid |
| 71 | + trimmed := strings.ReplaceAll(fakeUI.Outputs(), " ", "") |
| 72 | + Expect(trimmed).To(ContainSubstring("iddisplayName")) |
| 73 | + Expect(trimmed).To(ContainSubstring("1468361azzz")) |
| 74 | + Expect(trimmed).To(ContainSubstring("1468362accc")) |
| 75 | + Expect(trimmed).To(ContainSubstring("1468363abc")) |
95 | 76 | }) |
96 | | - |
97 | | - Context("user list in format json", func() { |
98 | | - It("return users list", func() { |
99 | | - err := testhelpers.RunCobraCommand(cliCommand.Command, "--output", "json") |
100 | | - Expect(err).NotTo(HaveOccurred()) |
101 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"apiAuthenticationKeyCount": 123456,`)) |
102 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"displayName": "DisplayedName",`)) |
103 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"email": "user2@email.com",`)) |
104 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"externalBindingCount": 123,`)) |
105 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"id": 5555,`)) |
106 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"userStatus": {`)) |
107 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"name": "ACTIVE"`)) |
108 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`},`)) |
109 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"username": "ATestUser"`)) |
110 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"apiAuthenticationKeyCount": 1234567,`)) |
111 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"displayName": "DisplayedName2",`)) |
112 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"email": "user2@email.com",`)) |
113 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"externalBindingCount": 1234,`)) |
114 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"id": 5556,`)) |
115 | | - Expect(fakeUI.Outputs()).To(ContainSubstring(`"username": "ATestUser2"`)) |
116 | | - }) |
| 77 | + It("return users list with just username", func() { |
| 78 | + err := testhelpers.RunCobraCommand(cliCommand.Command, "--column", "username") |
| 79 | + Expect(err).NotTo(HaveOccurred()) |
| 80 | + Expect(fakeUI.Outputs()).To(ContainSubstring("username")) |
| 81 | + Expect(fakeUI.Outputs()).To(ContainSubstring("IBM27821")) |
| 82 | + Expect(fakeUI.Outputs()).To(ContainSubstring("IBM27832")) |
| 83 | + }) |
| 84 | + It("return users list with json output", func() { |
| 85 | + err := testhelpers.RunCobraCommand(cliCommand.Command, "--output", "json") |
| 86 | + Expect(err).NotTo(HaveOccurred()) |
| 87 | + Expect(fakeUI.Outputs()).To(ContainSubstring(`"email": "sdfasdfasd@three.com",`)) |
| 88 | + Expect(fakeUI.Outputs()).To(ContainSubstring(`"displayName": "azzz",`)) |
| 89 | + Expect(fakeUI.Outputs()).To(ContainSubstring(`"apiAuthenticationKeyCount": 1,`)) |
117 | 90 | }) |
118 | 91 | }) |
119 | 92 | }) |
0 commit comments