Skip to content

Commit 2b435eb

Browse files
#854 fixed up unit tests
1 parent f6c65d1 commit 2b435eb

File tree

9 files changed

+1252
-80
lines changed

9 files changed

+1252
-80
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,15 @@ By default, every API call made to the SoftLayer API will load in the approprait
218218
To force errors:
219219

220220
```go
221-
fakeHandler := testhelpers.FakeTransportHandler{}
222-
fakeHandler.AddApiError("SoftLayer_Tag", "getAttachedTagsForCurrentUser", 500, "BAD")
223-
fakeSLSession := &session.Session{TransportHandler: fakeHandler,}
221+
// In the Top level BeforeEach
222+
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
223+
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
224+
225+
// Then in a BeforeEach for the specific test...
226+
BeforeEach(func() {
227+
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject",
228+
500, "Internal Server Error")
229+
})
224230
```
225231

226232
To force a non-default JSON file to be loaded

bin/buildAndDeploy.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ def runTests() -> None:
6262
if re.match(r"fixtrues|vendor", mod) is None:
6363
clean_mods.append(mod)
6464

65+
## GO GENERATE
66+
go_generate = ['go', 'generate', './...']
67+
# Not using the 'real' command here because this looks neater.
68+
69+
print(f'[turquoise2]Running: go generate ./...')
70+
try:
71+
subprocess.run(go_generate, check=True)
72+
except subprocess.CalledProcessError as e:
73+
print(f"[red]>>> Go Generate failed <<<")
74+
sys.exit(e.returncode)
75+
6576
## GO VET
6677
go_vet = ['go', 'vet'] + clean_mods
6778
# Not using the 'real' command here because this looks neater.
@@ -73,16 +84,7 @@ def runTests() -> None:
7384
print(f"[red]>>> Go Vet failed <<<")
7485
sys.exit(e.returncode)
7586

76-
## GO GENERATE
77-
go_generate = ['go', 'generate', './...']
78-
# Not using the 'real' command here because this looks neater.
7987

80-
print(f'[turquoise2]Running: go generate ./...')
81-
try:
82-
subprocess.run(go_generate, check=True)
83-
except subprocess.CalledProcessError as e:
84-
print(f"[red]>>> Go Generate failed <<<")
85-
sys.exit(e.returncode)
8688

8789
## GO TEST
8890
go_test = ['go', 'test'] + clean_mods

plugin/commands/user/details_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package user_test
22

33
import (
44
"errors"
5-
"fmt"
65
"time"
76

87
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
@@ -374,7 +373,6 @@ var _ = Describe("Detail", func() {
374373

375374
Context("user detail with correct id and logins", func() {
376375
It("return a user with logins", func() {
377-
fmt.Println("**")
378376
err := testhelpers.RunCobraCommand(cliCommand.Command, "5555", "--logins")
379377
Expect(err).NotTo(HaveOccurred())
380378
Expect(fakeUI.Outputs()).To(ContainSubstring("name value"))
@@ -401,7 +399,6 @@ var _ = Describe("Detail", func() {
401399

402400
Context("user detail with correct id and events", func() {
403401
It("return a user with events", func() {
404-
fmt.Println("**")
405402
err := testhelpers.RunCobraCommand(cliCommand.Command, "5555", "--events")
406403
Expect(err).NotTo(HaveOccurred())
407404
Expect(fakeUI.Outputs()).To(ContainSubstring("name value"))

plugin/commands/user/permissions_test.go

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package user_test
22

33
import (
4-
"errors"
5-
64
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
75
. "github.com/onsi/ginkgo/v2"
86
. "github.com/onsi/gomega"
9-
"github.com/softlayer/softlayer-go/datatypes"
7+
108
"github.com/softlayer/softlayer-go/session"
11-
"github.com/softlayer/softlayer-go/sl"
129

1310
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/user"
1411
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
@@ -18,46 +15,19 @@ import (
1815
var _ = Describe("User Permissions", func() {
1916
var (
2017
fakeUI *terminal.FakeUI
21-
fakeUserManager *testhelpers.FakeUserManager
2218
cliCommand *user.PermissionsCommand
2319
fakeSession *session.Session
2420
slCommand *metadata.SoftlayerCommand
21+
fakeHandler *testhelpers.FakeTransportHandler
2522
)
2623
BeforeEach(func() {
2724
fakeUI = terminal.NewFakeUI()
28-
fakeUserManager = new(testhelpers.FakeUserManager)
29-
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
25+
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
26+
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
3027
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
3128
cliCommand = user.NewPermissionsCommand(slCommand)
3229
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
33-
cliCommand.UserManager = fakeUserManager
34-
testUser := datatypes.User_Customer{
35-
Roles: []datatypes.User_Permission_Role{
36-
datatypes.User_Permission_Role{
37-
Id: sl.Int(123),
38-
Name: sl.String("role name"),
39-
Description: sl.String("description"),
40-
},
41-
},
42-
Permissions: []datatypes.User_Customer_CustomerPermission_Permission{
43-
datatypes.User_Customer_CustomerPermission_Permission{
44-
KeyName: sl.String("KEY_PERMISSION_1"),
45-
Name: sl.String("Permission 1"),
46-
},
47-
},
48-
}
49-
testAllPermissions := []datatypes.User_Customer_CustomerPermission_Permission{
50-
datatypes.User_Customer_CustomerPermission_Permission{
51-
KeyName: sl.String("KEY_PERMISSION_1"),
52-
Name: sl.String("Permission 1"),
53-
},
54-
datatypes.User_Customer_CustomerPermission_Permission{
55-
KeyName: sl.String("KEY_PERMISSION_2"),
56-
Name: sl.String("Permission 2"),
57-
},
58-
}
59-
fakeUserManager.GetUserReturns(testUser, nil)
60-
fakeUserManager.GetAllPermissionReturns(testAllPermissions, nil)
30+
6131
})
6232

6333
Describe("user permissions ", func() {
@@ -78,17 +48,23 @@ var _ = Describe("User Permissions", func() {
7848
})
7949

8050
Context("user permissions error user", func() {
51+
BeforeEach(func() {
52+
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject",
53+
500, "Internal Server Error")
54+
})
8155
It("return error", func() {
82-
fakeUserManager.GetUserReturns(datatypes.User_Customer{}, errors.New("Internal server error"))
8356
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
8457
Expect(err).To(HaveOccurred())
8558
Expect(err.Error()).To(ContainSubstring("Failed to get user."))
8659
})
8760
})
8861

8962
Context("user permissions error", func() {
63+
BeforeEach(func() {
64+
fakeHandler.AddApiError("SoftLayer_User_Permission_Department", "getAllObjects",
65+
500, "Internal Server Error")
66+
})
9067
It("return error", func() {
91-
fakeUserManager.GetAllPermissionReturns([]datatypes.User_Customer_CustomerPermission_Permission{}, errors.New("Internal server error"))
9268
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
9369
Expect(err).To(HaveOccurred())
9470
Expect(err.Error()).To(ContainSubstring("Failed to get permissions."))
@@ -99,38 +75,17 @@ var _ = Describe("User Permissions", func() {
9975
It("return user permissions", func() {
10076
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
10177
Expect(err).NotTo(HaveOccurred())
102-
Expect(fakeUI.Outputs()).To(ContainSubstring("ID Role Name Description"))
103-
Expect(fakeUI.Outputs()).To(ContainSubstring("123 role name description"))
104-
Expect(fakeUI.Outputs()).To(ContainSubstring("Description KeyName Assigned"))
105-
Expect(fakeUI.Outputs()).To(ContainSubstring("Permission 1 KEY_PERMISSION_1 true"))
106-
Expect(fakeUI.Outputs()).To(ContainSubstring("Permission 2 KEY_PERMISSION_2 false"))
107-
})
108-
})
109-
110-
Context("hide user permissions", func() {
111-
It("return not equal user permissions", func() {
112-
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
113-
Expect(err).NotTo(HaveOccurred())
114-
Expect(fakeUI.Outputs()).NotTo(Equal("ACCOUNT_SUMMARY_VIEW"))
115-
Expect(fakeUI.Outputs()).To(Not(Equal("REQUEST_COMPLIANCE_REPORT")))
116-
Expect(fakeUI.Outputs()).To(Not(Equal("COMPANY_EDIT")))
117-
Expect(fakeUI.Outputs()).To(Not(Equal("ONE_TIME_PAYMENTS")))
118-
Expect(fakeUI.Outputs()).To(Not(Equal("UPDATE_PAYMENT_DETAILS")))
119-
Expect(fakeUI.Outputs()).To(Not(Equal("EU_LIMITED_PROCESSING_MANAGE")))
120-
Expect(fakeUI.Outputs()).To(Not(Equal("TICKET_ADD")))
121-
Expect(fakeUI.Outputs()).To(Not(Equal("TICKET_EDIT")))
122-
Expect(fakeUI.Outputs()).To(Not(Equal("TICKET_SEARCH")))
123-
Expect(fakeUI.Outputs()).To(Not(Equal("TICKET_VIEW")))
124-
Expect(fakeUI.Outputs()).To(Not(Equal("TICKET_VIEW_ALL")))
78+
Expect(fakeUI.Outputs()).To(ContainSubstring("ID Role Name Description"))
79+
Expect(fakeUI.Outputs()).To(ContainSubstring("2 role name description of the role"))
80+
Expect(fakeUI.Outputs()).To(ContainSubstring("ADMINISTRATIVE KeyName Assigned Description"))
81+
Expect(fakeUI.Outputs()).To(ContainSubstring("ACCOUNT_BRAND_ADD false Permission to create sub brands"))
82+
Expect(fakeUI.Outputs()).To(ContainSubstring("ACCOUNT_BILLING_SYSTEM true Permission to access account billing system type determination"))
12583
})
12684
})
12785

12886
Context("user permissions - master account", func() {
12987
It("return user permissions", func() {
130-
fakeUserManager.GetUserReturns(datatypes.User_Customer{
131-
IsMasterUserFlag: sl.Bool(true),
132-
}, nil)
133-
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
88+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345")
13489
Expect(err).NotTo(HaveOccurred())
13590
Expect(fakeUI.Outputs()).To(ContainSubstring("This account is the Master User and has all permissions enabled"))
13691
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"id": 123,
3+
"permissions": [
4+
{
5+
"createDate": null,
6+
"departmentId": 1,
7+
"description": "Permission to access account billing system type determination endpoint",
8+
"id": 5088,
9+
"key": null,
10+
"keyName": "ACCOUNT_BILLING_SYSTEM",
11+
"modifyDate": null,
12+
"name": "Account Billing System"
13+
},
14+
{
15+
"createDate": null,
16+
"departmentId": 3,
17+
"description": "Add new support tickets.",
18+
"id": 3,
19+
"key": "T_7",
20+
"keyName": "TICKET_ADD",
21+
"modifyDate": null,
22+
"name": "Add Tickets"
23+
},
24+
{
25+
"createDate": null,
26+
"departmentId": 4,
27+
"description": "Create, edit and delete SAML authentication records.",
28+
"id": 3664,
29+
"key": "SM_1",
30+
"keyName": "SAML_AUTHENTICATION_MANAGE",
31+
"modifyDate": null,
32+
"name": "Manage SAML Authentication"
33+
}
34+
],
35+
"roles": [
36+
{
37+
"id": 2,
38+
"Name" : "role name",
39+
"Description": "description of the role"
40+
}
41+
],
42+
"isMasterUserFlag": false
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"id": 123,
3+
"permissions": [
4+
{
5+
"createDate": null,
6+
"departmentId": 1,
7+
"description": "Permission to access account billing system type determination endpoint",
8+
"id": 5088,
9+
"key": null,
10+
"keyName": "ACCOUNT_BILLING_SYSTEM",
11+
"modifyDate": null,
12+
"name": "Account Billing System"
13+
},
14+
{
15+
"createDate": null,
16+
"departmentId": 2,
17+
"description": "Add new servers to the account.",
18+
"id": 1267,
19+
"key": "XX_1",
20+
"keyName": "SERVER_ADD",
21+
"modifyDate": null,
22+
"name": "Add Server"
23+
}
24+
],
25+
"roles": [
26+
{
27+
"id": 2,
28+
"Name" : "role name",
29+
"Description": "description of the role"
30+
}
31+
],
32+
"isMasterUserFlag": true
33+
}
Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
1-
{"username": "test", "modifyDate": "2018-10-05T02:00:27+08:00", "localeId": 1, "unsuccessfulLogins": [], "locale": {"friendlyName": "English", "id": 1, "name": "English", "languageTag": "en-US"}, "secondaryPasswordModifyDate": "2018-08-31T21:02:29+08:00", "timezone": {"shortName": "CST", "offset": "-0600", "id": 114, "name": "sdfsdaf", "longName": "(GMT-06:00) America/Dallas - CST"}, "userStatusId": 22, "id": 345234, "accountId": 5342, "pptpVpnAllowedFlag": false, "city": "Markham", "savedId": "7260307", "createDate": "2018-08-31T21:02:26+08:00", "userStatus": {"name": "Active"}, "successfulLogins": [{"successFlag": true, "createDate": "2018-11-09T00:40:58+08:00", "ipAddress": "169.60.96.34", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:25:31+08:00", "ipAddress": "169.60.96.54", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:23:28+08:00", "ipAddress": "169.60.96.38", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:23:26+08:00", "ipAddress": "169.60.96.44", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:23:13+08:00", "ipAddress": "169.60.96.56", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:21:44+08:00", "ipAddress": "169.60.96.46", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:21:21+08:00", "ipAddress": "169.60.96.59", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:21:16+08:00", "ipAddress": "169.60.96.54", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:15:59+08:00", "ipAddress": "169.60.75.102", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-09T00:15:51+08:00", "ipAddress": "169.60.96.44", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-06T02:40:04+08:00", "ipAddress": "169.60.96.34", "userId": 7260307}, {"successFlag": true, "createDate": "2018-11-06T02:39:58+08:00", "ipAddress": "169.51.49.78", "userId": 7260307}, {"successFlag": true, "createDate": "2018-10-05T01:56:46+08:00", "ipAddress": "129.42.208.179", "userId": 7260307}, {"successFlag": true, "createDate": "2018-09-20T04:54:01+08:00", "ipAddress": "129.42.208.182", "userId": 7260307}, {"successFlag": true, "createDate": "2018-09-11T20:27:33+08:00", "ipAddress": "129.42.208.183", "userId": 7260307}], "state": "ON", "parentId": 146836, "postalCode": "L6G1C7", "sslVpnAllowedFlag": true, "vpnManualConfig": false, "email": "[email protected]", "secondaryLoginManagementFlag": true, "timezoneId": 114, "iamId": "IBMid-550000P00Y", "apiAuthenticationKeys": [], "parent": {"username": "IBM278444", "id": 146836}, "companyName": "IBM - BlueMix - Internal Dev", "address1": "8200 Warden Ave", "isMasterUserFlag": false, "ibmIdLink": {"realm": "IBMid", "userId": 7260307, "createDate": "2018-09-11T20:27:28+08:00", "uniqueIdentifier": "IBMid-550000P00Y", "id": 744393, "serviceProvider": {"keyName": "BLUE_ID", "description": "Blue ID", "name": "Blue ID", "id": 348}, "serviceProviderId": 348, "destinationUserAlphanumericId": "tache%2ejohnson@ibm%2ecom", "defaultFlag": 1}, "secondaryPasswordTimeoutDays": 0, "displayName": "Tache J", "firstName": "Tache ", "iamAuthorizationFlag": false, "denyAllResourceAccessOnCreateFlag": false, "lastName": "Johnson", "forumPasswordHash": "unused", "openIdConnectUserName": "[email protected]", "managedByOpenIdConnectFlag": true, "daylightSavingsTimeFlag": false, "country": "CA", "managedByFederationFlag": false}
1+
{
2+
"accountId": 5342,
3+
"address1": "11111111 aaaa Ave",
4+
"apiAuthenticationKeys": [],
5+
"city": "Markham",
6+
"companyName": "IBM - BlueMix - Internal Dev",
7+
"country": "CA",
8+
"createDate": "2018-08-31T21:02:26+08:00",
9+
"daylightSavingsTimeFlag": false,
10+
"denyAllResourceAccessOnCreateFlag": false,
11+
"displayName": "XXX J",
12+
"email": "[email protected]",
13+
"firstName": "XXX ",
14+
"forumPasswordHash": "unused",
15+
"iamAuthorizationFlag": false,
16+
"iamId": "IBMid-550000P00Y",
17+
"ibmIdLink": {
18+
"createDate": "2018-09-11T20:27:28+08:00",
19+
"defaultFlag": 1,
20+
"destinationUserAlphanumericId": "asdfg@ibm%2ecom",
21+
"id": 744393,
22+
"realm": "IBMid",
23+
"serviceProvider": {
24+
"description": "Blue ID",
25+
"id": 348,
26+
"keyName": "BLUE_ID",
27+
"name": "Blue ID"
28+
},
29+
"serviceProviderId": 348,
30+
"uniqueIdentifier": "IBMid-550000P00Y",
31+
"userId": 7260307
32+
},
33+
"id": 345234,
34+
"isMasterUserFlag": false,
35+
"lastName": "ASD",
36+
"locale": {
37+
"friendlyName": "English",
38+
"id": 1,
39+
"languageTag": "en-US",
40+
"name": "English"
41+
},
42+
"localeId": 1,
43+
"managedByFederationFlag": false,
44+
"managedByOpenIdConnectFlag": true,
45+
"modifyDate": "2018-10-05T02:00:27+08:00",
46+
"openIdConnectUserName": "[email protected]",
47+
"parent": {
48+
"id": 146836,
49+
"username": "IBM278444"
50+
},
51+
"parentId": 146836,
52+
"postalCode": "L6G1C7",
53+
"pptpVpnAllowedFlag": false,
54+
"savedId": "7260307",
55+
"secondaryLoginManagementFlag": true,
56+
"secondaryPasswordModifyDate": "2018-08-31T21:02:29+08:00",
57+
"secondaryPasswordTimeoutDays": 0,
58+
"sslVpnAllowedFlag": true,
59+
"state": "ON",
60+
"successfulLogins": [
61+
{
62+
"createDate": "2018-11-09T00:40:58+08:00",
63+
"ipAddress": "169.60.96.34",
64+
"successFlag": true,
65+
"userId": 7260307
66+
},
67+
{
68+
"createDate": "2018-11-09T00:25:31+08:00",
69+
"ipAddress": "169.60.96.54",
70+
"successFlag": true,
71+
"userId": 7260307
72+
}
73+
],
74+
"timezone": {
75+
"id": 114,
76+
"longName": "(GMT-06:00) America/Dallas - CST",
77+
"name": "sdfsdaf",
78+
"offset": "-0600",
79+
"shortName": "CST"
80+
},
81+
"timezoneId": 114,
82+
"unsuccessfulLogins": [],
83+
"userStatus": {
84+
"name": "Active"
85+
},
86+
"userStatusId": 22,
87+
"username": "test",
88+
"vpnManualConfig": false
89+
}

0 commit comments

Comments
 (0)