github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/user.go (about) 1 package helpers 2 3 import ( 4 "encoding/json" 5 "time" 6 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/gexec" 9 ) 10 11 type User struct { 12 GUID string 13 Username string 14 Origin string 15 CreatedAt time.Time 16 } 17 18 // ListUsers returns all the users in the targeted environment. 19 func GetUsers() []User { 20 var userPagesResponse struct { 21 NextURL *string `json:"next_url"` 22 Resources []struct { 23 Metadata struct { 24 GUID string `json:"guid"` 25 CreatedAt time.Time `json:"created_at"` 26 } `json:"metadata"` 27 Entity struct { 28 Username string `json:"username"` 29 } `json:"entity"` 30 } `json:"resources"` 31 } 32 33 var allUsers []User 34 nextURL := "/v2/users?results-per-page=50" 35 36 for { 37 session := CF("curl", nextURL) 38 Eventually(session).Should(Exit(0)) 39 40 err := json.Unmarshal(session.Out.Contents(), &userPagesResponse) 41 Expect(err).NotTo(HaveOccurred()) 42 for _, resource := range userPagesResponse.Resources { 43 allUsers = append(allUsers, User{ 44 GUID: resource.Metadata.GUID, 45 CreatedAt: resource.Metadata.CreatedAt, 46 Username: resource.Entity.Username, 47 }) 48 } 49 50 if userPagesResponse.NextURL == nil { 51 break 52 } 53 nextURL = *userPagesResponse.NextURL 54 } 55 56 return allUsers 57 } 58 59 func GetUsersV3() []User { 60 var userPagesResponse struct { 61 Pagination struct { 62 NextURL *string `json:"next_url"` 63 } `json:"pagination"` 64 Resources []struct { 65 GUID string `json:"guid"` 66 Origin string `json:"origin"` 67 Username string `json:"username"` 68 } `json:"resources"` 69 } 70 71 var allUsers []User 72 nextURL := "/v3/users?per_page=50" 73 74 for { 75 session := CF("curl", nextURL) 76 Eventually(session).Should(Exit(0)) 77 78 err := json.Unmarshal(session.Out.Contents(), &userPagesResponse) 79 Expect(err).NotTo(HaveOccurred()) 80 for _, resource := range userPagesResponse.Resources { 81 allUsers = append(allUsers, User{ 82 Origin: resource.Origin, 83 Username: resource.Username, 84 }) 85 } 86 87 if userPagesResponse.Pagination.NextURL == nil { 88 break 89 } 90 nextURL = *userPagesResponse.Pagination.NextURL 91 } 92 93 return allUsers 94 } 95 96 // CreateUser creates a user with a random username and password and returns both. 97 func CreateUser() (string, string) { 98 username := NewUsername() 99 password := RandomName() 100 101 // env := map[string]string{ 102 // "NEW_USER_PASSWORD": password, 103 // } 104 105 // session := CFWithEnv(env, "create-user", username, "$NEW_USER_PASSWORD") 106 session := CF("create-user", username, password) 107 Eventually(session).Should(Exit(0)) 108 109 return username, password 110 } 111 112 // DeleteUser deletes the user specified by username. 113 func DeleteUser(username string) { 114 session := CF("delete-user", username, "-f") 115 Eventually(session).Should(Exit(0)) 116 } 117 118 // CreateUserInOrgRole creates a user with a random username and password and gives them the specified role within 119 // a specific org. The new user's username and password are returned. 120 func CreateUserInOrgRole(org, role string) (string, string) { 121 username, password := CreateUser() 122 123 session := CF("set-org-role", username, org, role) 124 Eventually(session).Should(Exit(0)) 125 126 return username, password 127 } 128 129 // CreateUserInSpaceRole creates a user with a random username and password and gives them the specified role within 130 // a specific space. The new user's username and password are returned. 131 func CreateUserInSpaceRole(org, space, role string) (string, string) { 132 username, password := CreateUser() 133 134 session := CF("set-space-role", username, org, space, role) 135 Eventually(session).Should(Exit(0)) 136 137 return username, password 138 }