github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/user_test.go (about) 1 //go:build user || functional || ALL 2 3 /* 4 * Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 5 */ 6 7 package govcd 8 9 import ( 10 "fmt" 11 "strings" 12 13 "github.com/vmware/go-vcloud-director/v2/types/v56" 14 15 . "gopkg.in/check.v1" 16 ) 17 18 /* 19 TODO: Add test for takeOwnership. 20 21 This is more complicated than it looks, because it requires the following: 22 23 Either: 24 1a. Separate connection with a newly created user [requires test enhancement] 25 2a. Creation of entities with new user (vapp/catalog/catalog items) 26 27 OR 28 1b. create entities with the user that runs the tests 29 2b. change ownership of such entities to the new user [requires new feature] 30 31 3. Check that the user is the intended one (this is currently doable, because we can 32 inspect the Owner structure of the entity being created) 33 34 4. Try deleting the user that owns the new entities 35 5. get an error 36 6. take ownership from the user 37 7. delete the user and see the operation succeed 38 8. Check that the new entities belong to the current user 39 9. Delete the new entities 40 */ 41 42 // Checks that the default roles are available from the organization 43 func (vcd *TestVCD) Test_GetRoleReference(check *C) { 44 adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 45 check.Assert(err, IsNil) 46 check.Assert(adminOrg, NotNil) 47 Roles := []string{ 48 OrgUserRoleOrganizationAdministrator, 49 OrgUserRoleVappUser, 50 OrgUserRoleCatalogAuthor, 51 OrgUserRoleConsoleAccessOnly, 52 } 53 for _, roleName := range Roles { 54 roleReference, err := adminOrg.GetRoleReference(roleName) 55 check.Assert(err, IsNil) 56 check.Assert(roleReference, NotNil) 57 check.Assert(roleReference.Name, Equals, roleName) 58 check.Assert(roleReference.HREF, Not(Equals), "") 59 } 60 } 61 62 // Checks that we can retrieve a user by name or ID 63 func (vcd *TestVCD) Test_GetUserByNameOrId(check *C) { 64 adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 65 check.Assert(err, IsNil) 66 check.Assert(adminOrg, NotNil) 67 68 // We get the list of users from the organization 69 var userRefs []types.Reference 70 for _, userRef := range adminOrg.AdminOrg.Users.User { 71 userRefs = append(userRefs, *userRef) 72 } 73 74 // Using the list above, we first try to get each user by name 75 for _, userRef := range userRefs { 76 user, err := adminOrg.GetUserByName(userRef.Name, false) 77 check.Assert(err, IsNil) 78 check.Assert(user, NotNil) 79 check.Assert(user.User.Name, Equals, userRef.Name) 80 81 // Then we try to get the same user by ID 82 user, err = adminOrg.GetUserById(userRef.ID, false) 83 check.Assert(err, IsNil) 84 check.Assert(user, NotNil) 85 check.Assert(user.User.Name, Equals, userRef.Name) 86 87 // Then we try to get the same user by Name or ID combined 88 user, err = adminOrg.GetUserByNameOrId(userRef.ID, true) 89 check.Assert(err, IsNil) 90 check.Assert(user, NotNil) 91 check.Assert(user.User.Name, Equals, userRef.Name) 92 93 user, err = adminOrg.GetUserByNameOrId(userRef.Name, false) 94 check.Assert(err, IsNil) 95 check.Assert(user, NotNil) 96 check.Assert(user.User.Name, Equals, userRef.Name) 97 } 98 } 99 100 // This test creates 5 users using 5 available roles, 101 // Then updates each of them with a different role, 102 // Furthermore, disables, and then enables the users again 103 // and finally deletes all of them 104 func (vcd *TestVCD) Test_UserCRUD(check *C) { 105 vcd.checkSkipWhenApiToken(check) 106 adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 107 check.Assert(err, IsNil) 108 check.Assert(adminOrg, NotNil) 109 110 type userTestData struct { 111 name string // name of the user. Note: only lowercase letters allowed 112 roleName string // the role this user is created with 113 secondRole string // The role to which we change using Update() 114 } 115 userData := []userTestData{ 116 { 117 name: "test_user_admin", 118 roleName: OrgUserRoleOrganizationAdministrator, 119 secondRole: OrgUserRoleVappAuthor, 120 }, 121 { 122 name: "test_user_vapp_author", 123 roleName: OrgUserRoleVappAuthor, 124 secondRole: OrgUserRoleVappUser, 125 }, 126 { 127 name: "test_user_vapp_user", 128 roleName: OrgUserRoleVappUser, 129 secondRole: OrgUserRoleConsoleAccessOnly, 130 }, 131 { 132 name: "test_user_console_access", 133 roleName: OrgUserRoleConsoleAccessOnly, 134 secondRole: OrgUserRoleCatalogAuthor, 135 }, 136 { 137 name: "test_user_catalog_author", 138 roleName: OrgUserRoleCatalogAuthor, 139 secondRole: OrgUserRoleOrganizationAdministrator, 140 }, 141 } 142 143 quotaDeployed := 10 144 quotaStored := 10 145 for _, ud := range userData { 146 quotaDeployed += 2 147 quotaStored += 2 148 fmt.Printf("# Creating user %s with role %s\n", ud.name, ud.roleName) 149 // Uncomment the following lines to see creation request and response 150 // enableDebugShowRequest() 151 // enableDebugShowResponse() 152 var userDefinition = OrgUserConfiguration{ 153 Name: ud.name, 154 Password: "user_pass", 155 RoleName: ud.roleName, 156 ProviderType: OrgUserProviderIntegrated, 157 DeployedVmQuota: quotaDeployed, 158 StoredVmQuota: quotaStored, 159 FullName: strings.ReplaceAll(ud.name, "_", " "), 160 Description: "user " + strings.ReplaceAll(ud.name, "_", " "), 161 IsEnabled: true, 162 IsExternal: false, 163 IM: "TextIM", 164 EmailAddress: "somename@somedomain.com", 165 Telephone: "999 888-7777", 166 } 167 168 user, err := adminOrg.CreateUserSimple(userDefinition) 169 // disableDebugShowRequest() 170 // disableDebugShowResponse() 171 check.Assert(err, IsNil) 172 173 AddToCleanupList(ud.name, "user", user.AdminOrg.AdminOrg.Name, check.TestName()) 174 check.Assert(user.User, NotNil) 175 check.Assert(user.User.Name, Equals, ud.name) 176 check.Assert(user.GetRoleName(), Equals, ud.roleName) 177 check.Assert(user.User.IsEnabled, Equals, true) 178 check.Assert(user.User.FullName, Equals, userDefinition.FullName) 179 check.Assert(user.User.EmailAddress, Equals, userDefinition.EmailAddress) 180 check.Assert(user.User.IM, Equals, userDefinition.IM) 181 check.Assert(user.User.Telephone, Equals, userDefinition.Telephone) 182 check.Assert(user.User.StoredVmQuota, Equals, userDefinition.StoredVmQuota) 183 check.Assert(user.User.DeployedVmQuota, Equals, userDefinition.DeployedVmQuota) 184 check.Assert(user.User.IsExternal, Equals, userDefinition.IsExternal) 185 186 // change DeployedVmQuota and StoredVmQuota to 0 and assert 187 // this will make DeployedVmQuota and StoredVmQuota unlimited 188 user.User.DeployedVmQuota = 0 189 user.User.StoredVmQuota = 0 190 err = user.Update() 191 check.Assert(err, IsNil) 192 193 // Get the user from API again 194 user, err = adminOrg.GetUserByHref(user.User.Href) 195 check.Assert(err, IsNil) 196 check.Assert(user.User.DeployedVmQuota, Equals, 0) 197 check.Assert(user.User.StoredVmQuota, Equals, 0) 198 199 err = user.Disable() 200 check.Assert(err, IsNil) 201 check.Assert(user.User.IsEnabled, Equals, false) 202 203 fmt.Printf("# Updating user %s with role %s\n", ud.name, ud.secondRole) 204 err = user.ChangeRole(ud.secondRole) 205 check.Assert(err, IsNil) 206 check.Assert(user.GetRoleName(), Equals, ud.secondRole) 207 208 err = user.Enable() 209 check.Assert(err, IsNil) 210 check.Assert(user.User.IsEnabled, Equals, true) 211 err = user.ChangePassword("new_pass") 212 check.Assert(err, IsNil) 213 } 214 215 var enableMap = map[bool]string{ 216 true: "enabled", 217 false: "disabled", 218 } 219 for _, ud := range userData { 220 user, err := adminOrg.GetUserByNameOrId(ud.name, true) 221 check.Assert(err, IsNil) 222 223 fmt.Printf("# deleting user %s (%s - %s)\n", ud.name, user.GetRoleName(), enableMap[user.User.IsEnabled]) 224 // uncomment the following two lines to see the deletion request and response 225 // enableDebugShowRequest() 226 // enableDebugShowResponse() 227 err = user.Delete(true) 228 // disableDebugShowRequest() 229 // disableDebugShowResponse() 230 check.Assert(err, IsNil) 231 user, err = adminOrg.GetUserByNameOrId(user.User.ID, true) 232 check.Assert(err, NotNil) 233 // Tests both the error directly and the function IsNotFound 234 check.Assert(err, Equals, ErrorEntityNotFound) 235 check.Assert(IsNotFound(err), Equals, true) 236 // Expect a null pointer when user is not found 237 check.Assert(user, IsNil) 238 } 239 } 240 241 func init() { 242 testingTags["user"] = "user_test.go" 243 }