github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 a privileged user", func() {
    92  		var (
    93  			privilegedUsername string
    94  			orgName            string
    95  			spaceName          string
    96  		)
    97  
    98  		BeforeEach(func() {
    99  			privilegedUsername = helpers.LoginCF()
   100  			orgName = ReadOnlyOrg
   101  			spaceName = ReadOnlySpace
   102  		})
   103  
   104  		When("the --client flag is passed", func() {
   105  			var clientID string
   106  
   107  			BeforeEach(func() {
   108  				clientID, _ = helpers.SkipIfClientCredentialsNotSet()
   109  			})
   110  
   111  			When("the client exists", func() {
   112  				It("sets the org role for the client", func() {
   113  					session := helpers.CF("set-space-role", clientID, orgName, spaceName, "SpaceAuditor", "--client")
   114  					Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user %s in org %s / space %s as %s...", clientID, orgName, spaceName, privilegedUsername))
   115  					Eventually(session).Should(Say("OK"))
   116  					Eventually(session).Should(Exit(0))
   117  				})
   118  
   119  				When("the active user lacks permissions to look up clients", func() {
   120  					BeforeEach(func() {
   121  						helpers.SwitchToSpaceRole(orgName, spaceName, "SpaceManager")
   122  					})
   123  
   124  					It("prints an appropriate error and exits 1", func() {
   125  						session := helpers.CF("set-space-role", clientID, orgName, spaceName, "SpaceAuditor", "--client")
   126  						Eventually(session).Should(Say("FAILED"))
   127  						Eventually(session).Should(Say("Server error, status code: 403: Access is denied.  You do not have privileges to execute this command."))
   128  						Eventually(session).Should(Exit(1))
   129  					})
   130  				})
   131  			})
   132  
   133  			When("the targeted client does not exist", func() {
   134  				var badClientID string
   135  
   136  				BeforeEach(func() {
   137  					badClientID = "nonexistent-client"
   138  				})
   139  
   140  				It("fails with an appropriate error message", func() {
   141  					session := helpers.CF("set-space-role", badClientID, orgName, spaceName, "SpaceAuditor", "--client")
   142  					Eventually(session).Should(Say("FAILED"))
   143  					Eventually(session).Should(Say("Client nonexistent-client not found"))
   144  					Eventually(session).Should(Exit(1))
   145  				})
   146  			})
   147  		})
   148  
   149  		When("the user exists", func() {
   150  			var username string
   151  
   152  			BeforeEach(func() {
   153  				username, _ = helpers.CreateUser()
   154  			})
   155  
   156  			When("the passed role is lowercase", func() {
   157  				It("sets the space role for the user", func() {
   158  					session := helpers.CF("set-space-role", username, orgName, spaceName, "spaceauditor")
   159  					Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user %s in org %s / space %s as %s...", username, orgName, spaceName, privilegedUsername))
   160  					Eventually(session).Should(Say("OK"))
   161  					Eventually(session).Should(Exit(0))
   162  				})
   163  			})
   164  
   165  			It("sets the space role for the user", func() {
   166  				session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceAuditor")
   167  				Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user %s in org %s / space %s as %s...", username, orgName, spaceName, privilegedUsername))
   168  				Eventually(session).Should(Say("OK"))
   169  				Eventually(session).Should(Exit(0))
   170  			})
   171  
   172  			When("the logged in user has insufficient permissions", func() {
   173  				BeforeEach(func() {
   174  					helpers.SwitchToSpaceRole(orgName, spaceName, "SpaceAuditor")
   175  				})
   176  
   177  				It("prints out the error message from CC API and exits 1", func() {
   178  					session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceAuditor")
   179  					Eventually(session).Should(Say("FAILED"))
   180  					Eventually(session).Should(Say("Server error, status code: 403, error code: 10003, message: You are not authorized to perform the requested action"))
   181  					Eventually(session).Should(Exit(1))
   182  				})
   183  			})
   184  
   185  			When("the user already has the desired role", func() {
   186  				BeforeEach(func() {
   187  					session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceDeveloper")
   188  					Eventually(session).Should(Say("Assigning role RoleSpaceDeveloper to user %s in org %s / space %s as %s...", username, orgName, spaceName, privilegedUsername))
   189  					Eventually(session).Should(Exit(0))
   190  				})
   191  
   192  				It("is idempotent", func() {
   193  					session := helpers.CF("set-space-role", username, orgName, spaceName, "SpaceDeveloper")
   194  					Eventually(session).Should(Say("Assigning role RoleSpaceDeveloper to user %s in org %s / space %s as %s...", username, orgName, spaceName, privilegedUsername))
   195  					Eventually(session).Should(Exit(0))
   196  				})
   197  			})
   198  
   199  			When("the org does not exist", func() {
   200  				It("prints an appropriate error and exits 1", func() {
   201  					session := helpers.CF("set-space-role", username, "invalid-org", spaceName, "SpaceAuditor")
   202  					Eventually(session).Should(Say("FAILED"))
   203  					Eventually(session).Should(Say("Organization invalid-org not found"))
   204  					Eventually(session).Should(Exit(1))
   205  				})
   206  			})
   207  
   208  			When("the space does not exist", func() {
   209  				It("prints an appropriate error and exits 1", func() {
   210  					session := helpers.CF("set-space-role", username, orgName, "invalid-space", "SpaceAuditor")
   211  					Eventually(session).Should(Say("FAILED"))
   212  					Eventually(session).Should(Say("Space invalid-space not found"))
   213  					Eventually(session).Should(Exit(1))
   214  				})
   215  			})
   216  		})
   217  
   218  		When("the user does not exist", func() {
   219  			It("prints an appropriate error and exits 1", func() {
   220  				session := helpers.CF("set-space-role", "not-exists", orgName, spaceName, "SpaceAuditor")
   221  				Eventually(session).Should(Say("Assigning role RoleSpaceAuditor to user not-exists in org %s / space %s as %s...", orgName, spaceName, privilegedUsername))
   222  				Eventually(session).Should(Say("FAILED"))
   223  				Eventually(session).Should(Say("Server error, status code: 404, error code: 20003, message: The user could not be found: not-exists"))
   224  				Eventually(session).Should(Exit(1))
   225  			})
   226  		})
   227  	})
   228  })