github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/v6/isolated/set_space_role_command_test.go (about) 1 package isolated 2 3 import ( 4 "code.cloudfoundry.org/cli/integration/helpers" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 . "github.com/onsi/gomega/gbytes" 8 . "github.com/onsi/gomega/gexec" 9 ) 10 11 var _ = Describe("set-space-role command", func() { 12 Describe("help text and argument validation", func() { 13 When("--help flag is set", func() { 14 It("Displays command usage to output", func() { 15 session := helpers.CF("set-space-role", "--help") 16 Eventually(session).Should(Say("NAME:")) 17 Eventually(session).Should(Say("set-space-role - Assign a space role to a user")) 18 Eventually(session).Should(Say("USAGE:")) 19 Eventually(session).Should(Say("cf set-space-role USERNAME ORG SPACE ROLE")) 20 Eventually(session).Should(Say("ROLES:")) 21 Eventually(session).Should(Say("'SpaceManager' - Invite and manage users, and enable features for a given space")) 22 Eventually(session).Should(Say("'SpaceDeveloper' - Create and manage apps and services, and see logs and reports")) 23 Eventually(session).Should(Say("'SpaceAuditor' - View logs, reports, and settings on this space")) 24 Eventually(session).Should(Say("OPTIONS:")) 25 Eventually(session).Should(Say(`--client\s+Treat USERNAME as the client-id of a \(non-user\) service account`)) 26 Eventually(session).Should(Say("SEE ALSO:")) 27 Eventually(session).Should(Say("space-users")) 28 Eventually(session).Should(Exit(0)) 29 }) 30 }) 31 32 When("the role does not exist", func() { 33 It("prints a useful error, prints help text, and exits 1", func() { 34 session := helpers.CF("set-space-role", "some-user", "some-org", "some-space", "NotARealRole") 35 Eventually(session.Err).Should(Say(`Incorrect Usage: ROLE must be "SpaceManager", "SpaceDeveloper" and "SpaceAuditor"`)) 36 Eventually(session).Should(Say(`NAME:`)) 37 Eventually(session).Should(Say(`\s+set-space-role - Assign a space role to a user`)) 38 Eventually(session).Should(Say(`USAGE:`)) 39 Eventually(session).Should(Say(`\s+cf set-space-role USERNAME ORG SPACE ROLE`)) 40 Eventually(session).Should(Say(`ROLES:`)) 41 Eventually(session).Should(Say(`\s+'SpaceManager' - Invite and manage users, and enable features for a given space`)) 42 Eventually(session).Should(Say(`\s+'SpaceDeveloper' - Create and manage apps and services, and see logs and reports`)) 43 Eventually(session).Should(Say(`\s+'SpaceAuditor' - View logs, reports, and settings on this space`)) 44 Eventually(session).Should(Say("OPTIONS:")) 45 Eventually(session).Should(Say(`--client\s+Treat USERNAME as the client-id of a \(non-user\) service account`)) 46 Eventually(session).Should(Say(`SEE ALSO:`)) 47 Eventually(session).Should(Say(`\s+space-users`)) 48 Eventually(session).Should(Exit(1)) 49 }) 50 }) 51 52 When("too few arguments are passed", func() { 53 It("prints a useful error, prints help text, and exits 1", func() { 54 session := helpers.CF("set-space-role", "not-enough", "arguments") 55 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SPACE` and `ROLE` were not provided")) 56 Eventually(session).Should(Say(`NAME:`)) 57 Eventually(session).Should(Say(`\s+set-space-role - Assign a space role to a user`)) 58 Eventually(session).Should(Say(`USAGE:`)) 59 Eventually(session).Should(Say(`\s+cf set-space-role USERNAME ORG SPACE ROLE`)) 60 Eventually(session).Should(Say(`ROLES:`)) 61 Eventually(session).Should(Say(`\s+'SpaceManager' - Invite and manage users, and enable features for a given space`)) 62 Eventually(session).Should(Say(`\s+'SpaceDeveloper' - Create and manage apps and services, and see logs and reports`)) 63 Eventually(session).Should(Say(`\s+'SpaceAuditor' - View logs, reports, and settings on this space`)) 64 Eventually(session).Should(Say("OPTIONS:")) 65 Eventually(session).Should(Say(`--client\s+Treat USERNAME as the client-id of a \(non-user\) service account`)) 66 Eventually(session).Should(Say(`SEE ALSO:`)) 67 Eventually(session).Should(Say(`\s+space-users`)) 68 Eventually(session).Should(Exit(1)) 69 }) 70 }) 71 72 When("too many arguments are passed", func() { 73 It("prints a useful error, prints help text, and exits 1", func() { 74 session := helpers.CF("set-space-role", "some-user", "some-org", "some-space", "SpaceAuditor", "some-extra-argument") 75 Eventually(session).Should(Say("Incorrect Usage. Requires USERNAME, ORG, SPACE, ROLE as arguments")) 76 Eventually(session).Should(Say(`NAME:`)) 77 Eventually(session).Should(Say(`\s+set-space-role - Assign a space role to a user`)) 78 Eventually(session).Should(Say(`USAGE:`)) 79 Eventually(session).Should(Say(`\s+cf set-space-role USERNAME ORG SPACE ROLE`)) 80 Eventually(session).Should(Say(`ROLES:`)) 81 Eventually(session).Should(Say(`\s+'SpaceManager' - Invite and manage users, and enable features for a given space`)) 82 Eventually(session).Should(Say(`\s+'SpaceDeveloper' - Create and manage apps and services, and see logs and reports`)) 83 Eventually(session).Should(Say(`\s+'SpaceAuditor' - View logs, reports, and settings on this space`)) 84 Eventually(session).Should(Say("OPTIONS:")) 85 Eventually(session).Should(Say(`--client\s+Treat USERNAME as the client-id of a \(non-user\) service account`)) 86 Eventually(session).Should(Exit(1)) 87 }) 88 }) 89 }) 90 91 When("logged in as admin", func() { 92 var ( 93 orgName string 94 spaceName string 95 ) 96 97 BeforeEach(func() { 98 helpers.LoginCF() 99 orgName = ReadOnlyOrg 100 spaceName = ReadOnlySpace 101 }) 102 103 When("the --client flag is passed", func() { 104 var clientID string 105 106 BeforeEach(func() { 107 clientID, _ = helpers.SkipIfClientCredentialsNotSet() 108 }) 109 110 When("the client exists", func() { 111 It("sets the org role for the client", func() { 112 session := helpers.CF("set-space-role", clientID, orgName, spaceName, "SpaceAuditor", "--client") 113 Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user %s in org %s / space %s as admin...", clientID, orgName, spaceName)) 114 Eventually(session).Should(Say("OK")) 115 Eventually(session).Should(Exit(0)) 116 }) 117 118 When("the active user lacks permissions to look up clients", func() { 119 BeforeEach(func() { 120 helpers.SwitchToSpaceRole(orgName, spaceName, "SpaceManager") 121 }) 122 123 It("prints an appropriate error and exits 1", func() { 124 session := helpers.CF("set-space-role", clientID, orgName, spaceName, "SpaceAuditor", "--client") 125 Eventually(session).Should(Say("FAILED")) 126 Eventually(session).Should(Say("Server error, status code: 403: Access is denied. You do not have privileges to execute this command.")) 127 Eventually(session).Should(Exit(1)) 128 }) 129 }) 130 }) 131 132 When("the targeted client does not exist", func() { 133 var badClientID string 134 135 BeforeEach(func() { 136 badClientID = "nonexistent-client" 137 }) 138 139 It("fails with an appropriate error message", func() { 140 session := helpers.CF("set-space-role", badClientID, orgName, spaceName, "SpaceAuditor", "--client") 141 Eventually(session).Should(Say("FAILED")) 142 Eventually(session).Should(Say("Client nonexistent-client not found")) 143 Eventually(session).Should(Exit(1)) 144 }) 145 }) 146 }) 147 148 When("the user exists", func() { 149 var username string 150 151 BeforeEach(func() { 152 username, _ = helpers.CreateUser() 153 }) 154 155 When("the passed role is lowercase", func() { 156 It("sets the space role for the user", func() { 157 session := helpers.CF("set-space-role", username, orgName, spaceName, "spaceauditor") 158 Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user %s in org %s / space %s as admin...", username, orgName, spaceName)) 159 Eventually(session).Should(Say("OK")) 160 Eventually(session).Should(Exit(0)) 161 }) 162 }) 163 164 It("sets the space role for the user", func() { 165 session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceAuditor") 166 Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user %s in org %s / space %s as admin...", username, orgName, spaceName)) 167 Eventually(session).Should(Say("OK")) 168 Eventually(session).Should(Exit(0)) 169 }) 170 171 When("the logged in user has insufficient permissions", func() { 172 BeforeEach(func() { 173 helpers.SwitchToSpaceRole(orgName, spaceName, "SpaceAuditor") 174 }) 175 176 It("prints out the error message from CC API and exits 1", func() { 177 session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceAuditor") 178 Eventually(session).Should(Say("FAILED")) 179 Eventually(session).Should(Say("Server error, status code: 403, error code: 10003, message: You are not authorized to perform the requested action")) 180 Eventually(session).Should(Exit(1)) 181 }) 182 }) 183 184 When("the user already has the desired role", func() { 185 BeforeEach(func() { 186 session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceDeveloper") 187 Eventually(session).Should(Say("Assigning role RoleSpaceDeveloper to user %s in org %s / space %s as admin...", username, orgName, spaceName)) 188 Eventually(session).Should(Exit(0)) 189 }) 190 191 It("is idempotent", func() { 192 session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceDeveloper") 193 Eventually(session).Should(Say("Assigning role RoleSpaceDeveloper to user %s in org %s / space %s as admin...", username, orgName, spaceName)) 194 Eventually(session).Should(Exit(0)) 195 }) 196 }) 197 198 When("the org does not exist", func() { 199 It("prints an appropriate error and exits 1", func() { 200 session := helpers.CF("set-space-role", username, "invalid-org", spaceName, "SpaceAuditor") 201 Eventually(session).Should(Say("FAILED")) 202 Eventually(session).Should(Say("Organization invalid-org not found")) 203 Eventually(session).Should(Exit(1)) 204 }) 205 }) 206 207 When("the space does not exist", func() { 208 It("prints an appropriate error and exits 1", func() { 209 session := helpers.CF("set-space-role", username, orgName, "invalid-space", "SpaceAuditor") 210 Eventually(session).Should(Say("FAILED")) 211 Eventually(session).Should(Say("Space invalid-space not found")) 212 Eventually(session).Should(Exit(1)) 213 }) 214 }) 215 }) 216 217 When("the user does not exist", func() { 218 It("prints an appropriate error and exits 1", func() { 219 session := helpers.CF("set-space-role", "not-exists", orgName, spaceName, "SpaceAuditor") 220 Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user not-exists in org %s / space %s as admin...", orgName, spaceName)) 221 Eventually(session).Should(Say("FAILED")) 222 Eventually(session).Should(Say("Server error, status code: 404, error code: 20003, message: The user could not be found: not-exists")) 223 Eventually(session).Should(Exit(1)) 224 }) 225 }) 226 }) 227 })