github.com/cloudfoundry/cli@v7.1.0+incompatible/api/uaa/user_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/api/uaa"
     8  	"code.cloudfoundry.org/cli/api/uaa/uaafakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("User", func() {
    15  	var (
    16  		client *Client
    17  
    18  		fakeConfig *uaafakes.FakeConfig
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeConfig = NewTestConfig()
    23  
    24  		client = NewTestUAAClientAndStore(fakeConfig)
    25  	})
    26  
    27  	Describe("CreateUser", func() {
    28  		When("no errors occur", func() {
    29  			When("creating user with origin", func() {
    30  				BeforeEach(func() {
    31  					response := `{
    32  					"ID": "new-user-id"
    33  				}`
    34  					uaaServer.AppendHandlers(
    35  						CombineHandlers(
    36  							verifyRequestHost(TestUAAResource),
    37  							VerifyRequest(http.MethodPost, "/Users"),
    38  							VerifyHeaderKV("Content-Type", "application/json"),
    39  							VerifyBody([]byte(`{"userName":"new-user","password":"","origin":"some-origin","name":{"familyName":"new-user","givenName":"new-user"},"emails":[{"value":"new-user","primary":true}]}`)),
    40  							RespondWith(http.StatusOK, response),
    41  						))
    42  				})
    43  
    44  				It("creates a new user", func() {
    45  					user, err := client.CreateUser("new-user", "", "some-origin")
    46  					Expect(err).NotTo(HaveOccurred())
    47  
    48  					Expect(user).To(Equal(User{
    49  						ID: "new-user-id",
    50  					}))
    51  				})
    52  			})
    53  			When("creating user in UAA", func() {
    54  				BeforeEach(func() {
    55  					response := `{
    56  					"ID": "new-user-id"
    57  				}`
    58  					uaaServer.AppendHandlers(
    59  						CombineHandlers(
    60  							verifyRequestHost(TestUAAResource),
    61  							VerifyRequest(http.MethodPost, "/Users"),
    62  							VerifyHeaderKV("Content-Type", "application/json"),
    63  							VerifyBody([]byte(`{"userName":"new-user","password":"new-password","origin":"","name":{"familyName":"new-user","givenName":"new-user"},"emails":[{"value":"new-user","primary":true}]}`)),
    64  							RespondWith(http.StatusOK, response),
    65  						))
    66  				})
    67  
    68  				It("creates a new user", func() {
    69  					user, err := client.CreateUser("new-user", "new-password", "")
    70  					Expect(err).NotTo(HaveOccurred())
    71  
    72  					Expect(user).To(Equal(User{
    73  						ID: "new-user-id",
    74  					}))
    75  				})
    76  			})
    77  		})
    78  
    79  		When("an error occurs", func() {
    80  			var response string
    81  
    82  			BeforeEach(func() {
    83  				response = `{
    84  					"error": "some-error",
    85  					"error_description": "some-description"
    86  				}`
    87  				uaaServer.AppendHandlers(
    88  					CombineHandlers(
    89  						verifyRequestHost(TestUAAResource),
    90  						VerifyRequest(http.MethodPost, "/Users"),
    91  						RespondWith(http.StatusTeapot, response),
    92  					))
    93  			})
    94  
    95  			It("returns the error", func() {
    96  				_, err := client.CreateUser("new-user", "new-password", "")
    97  				Expect(err).To(MatchError(RawHTTPStatusError{
    98  					StatusCode:  http.StatusTeapot,
    99  					RawResponse: []byte(response),
   100  				}))
   101  			})
   102  		})
   103  	})
   104  
   105  	Describe("ListUsers", func() {
   106  		var (
   107  			userName string
   108  			origin   string
   109  			users    []User
   110  			err      error
   111  		)
   112  
   113  		BeforeEach(func() {
   114  			userName = ""
   115  			origin = ""
   116  			users = []User{}
   117  			err = nil
   118  		})
   119  
   120  		JustBeforeEach(func() {
   121  			users, err = client.ListUsers(userName, origin)
   122  		})
   123  
   124  		When("no errors occur", func() {
   125  			When("getting the users by username", func() {
   126  				BeforeEach(func() {
   127  					userName = "existing-user"
   128  					origin = ""
   129  
   130  					response := `{
   131  						"resources": [
   132  							{ "ID": "existing-user-id", "origin": "uaa" }
   133  						]
   134  					}`
   135  
   136  					uaaServer.AppendHandlers(
   137  						CombineHandlers(
   138  							verifyRequestHost(TestUAAResource),
   139  							VerifyRequest(http.MethodGet, "/Users", "filter=userName+eq+%22existing-user%22"),
   140  							VerifyHeaderKV("Content-Type", "application/json"),
   141  							RespondWith(http.StatusOK, response),
   142  						))
   143  				})
   144  
   145  				It("gets users by username", func() {
   146  					Expect(err).NotTo(HaveOccurred())
   147  					Expect(users).To(Equal([]User{
   148  						{ID: "existing-user-id", Origin: "uaa"},
   149  					}))
   150  				})
   151  			})
   152  
   153  			When("getting the user by username and origin", func() {
   154  				BeforeEach(func() {
   155  					userName = "existing-user"
   156  					origin = "ldap"
   157  
   158  					response := `{
   159  						"resources": [
   160  							{ "ID": "existing-user-id" }
   161  						]
   162  					}`
   163  
   164  					uaaServer.AppendHandlers(
   165  						CombineHandlers(
   166  							verifyRequestHost(TestUAAResource),
   167  							VerifyRequest(http.MethodGet, "/Users", "filter=userName+eq+%22existing-user%22+and+origin+eq+%22ldap%22"),
   168  							VerifyHeaderKV("Content-Type", "application/json"),
   169  							RespondWith(http.StatusOK, response),
   170  						))
   171  				})
   172  
   173  				It("gets user by username and origin", func() {
   174  					Expect(err).NotTo(HaveOccurred())
   175  					Expect(users).To(Equal([]User{
   176  						{ID: "existing-user-id"},
   177  					}))
   178  				})
   179  			})
   180  		})
   181  
   182  		When("an error occurs", func() {
   183  			var response string
   184  
   185  			BeforeEach(func() {
   186  				userName = "existing-user"
   187  				origin = "ldap"
   188  
   189  				response = `{
   190  					"error_description": "Invalid filter expression"
   191  				}`
   192  
   193  				uaaServer.AppendHandlers(
   194  					CombineHandlers(
   195  						verifyRequestHost(TestUAAResource),
   196  						VerifyRequest(http.MethodGet, "/Users", "filter=userName+eq+%22existing-user%22+and+origin+eq+%22ldap%22"),
   197  						VerifyHeaderKV("Content-Type", "application/json"),
   198  						RespondWith(http.StatusBadRequest, response),
   199  					))
   200  			})
   201  
   202  			It("returns the error", func() {
   203  				Expect(err).To(MatchError(RawHTTPStatusError{
   204  					StatusCode:  400,
   205  					RawResponse: []byte(response),
   206  				}))
   207  			})
   208  		})
   209  	})
   210  
   211  	Describe("DeleteUser", func() {
   212  		When("no errors occur", func() {
   213  			When("deleting user with origin", func() {
   214  				BeforeEach(func() {
   215  					deleteResponse := `{"ID": "some-user-guid"}`
   216  					uaaServer.AppendHandlers(
   217  						CombineHandlers(
   218  							verifyRequestHost(TestUAAResource),
   219  							VerifyRequest(http.MethodDelete, "/Users/some-user-guid"),
   220  							VerifyHeaderKV("Content-Type", "application/json"),
   221  							RespondWith(http.StatusOK, deleteResponse),
   222  						),
   223  					)
   224  				})
   225  
   226  				It("deletes the user", func() {
   227  					user, err := client.DeleteUser("some-user-guid")
   228  					Expect(err).NotTo(HaveOccurred())
   229  
   230  					Expect(user).To(Equal(User{
   231  						ID: "some-user-guid",
   232  					}))
   233  				})
   234  			})
   235  		})
   236  
   237  		When("an error occurs", func() {
   238  			var response string
   239  
   240  			BeforeEach(func() {
   241  				response = `{
   242  							"error": "some-error",
   243  							"error_description": "some-description"
   244  						}`
   245  			})
   246  
   247  			When("delete user fails", func() {
   248  				BeforeEach(func() {
   249  					uaaServer.AppendHandlers(
   250  						CombineHandlers(
   251  							verifyRequestHost(TestUAAResource),
   252  							VerifyRequest(http.MethodDelete, "/Users/some-user-guid"),
   253  							VerifyHeaderKV("Content-Type", "application/json"),
   254  							RespondWith(http.StatusTeapot, response),
   255  						),
   256  					)
   257  				})
   258  
   259  				It("it errors", func() {
   260  					_, err := client.DeleteUser("some-user-guid")
   261  					Expect(err).To(MatchError(RawHTTPStatusError{
   262  						StatusCode:  http.StatusTeapot,
   263  						RawResponse: []byte(response),
   264  					}))
   265  				})
   266  			})
   267  		})
   268  	})
   269  
   270  	Describe("UpdatePassword", func() {
   271  		When("no errors occur", func() {
   272  			BeforeEach(func() {
   273  				response := `{
   274  					"status": "ok"
   275  				}`
   276  
   277  				uaaServer.AppendHandlers(
   278  					CombineHandlers(
   279  						verifyRequestHost(TestUAAResource),
   280  						VerifyRequest(http.MethodPut, "/Users/some-user/password"),
   281  						VerifyHeaderKV("Content-Type", "application/json"),
   282  						VerifyBody([]byte(`{"oldPassword":"old1","password":"new1"}`)),
   283  						RespondWith(http.StatusOK, response),
   284  					))
   285  			})
   286  
   287  			It("updates password", func() {
   288  				err := client.UpdatePassword("some-user", "old1", "new1")
   289  				Expect(err).NotTo(HaveOccurred())
   290  			})
   291  		})
   292  
   293  		When("an error occurs", func() {
   294  			var response string
   295  
   296  			BeforeEach(func() {
   297  				response = `{
   298  					"error": "some-error",
   299  					"error_description": "some-description"
   300  				}`
   301  
   302  				uaaServer.AppendHandlers(
   303  					CombineHandlers(
   304  						verifyRequestHost(TestUAAResource),
   305  						VerifyRequest(http.MethodPut, "/Users/some-user/password"),
   306  						RespondWith(http.StatusTeapot, response),
   307  					))
   308  			})
   309  
   310  			It("returns the error", func() {
   311  				err := client.UpdatePassword("some-user", "old1", "new1")
   312  				Expect(err).To(MatchError(RawHTTPStatusError{
   313  					StatusCode:  http.StatusTeapot,
   314  					RawResponse: []byte(response),
   315  				}))
   316  			})
   317  		})
   318  	})
   319  
   320  	Describe("ValidateClientUser", func() {
   321  		var (
   322  			clientID string
   323  			err      error
   324  		)
   325  
   326  		BeforeEach(func() {
   327  			clientID = "client-id"
   328  			err = nil
   329  		})
   330  
   331  		JustBeforeEach(func() {
   332  			err = client.ValidateClientUser(clientID)
   333  		})
   334  
   335  		When("no errors occur", func() {
   336  			BeforeEach(func() {
   337  				uaaServer.AppendHandlers(
   338  					CombineHandlers(
   339  						verifyRequestHost(TestUAAResource),
   340  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   341  						VerifyHeaderKV("Content-Type", "application/json"),
   342  						RespondWith(http.StatusOK, "Response{}"),
   343  					))
   344  
   345  				It("gets users by username", func() {
   346  					Expect(err).NotTo(HaveOccurred())
   347  				})
   348  			})
   349  		})
   350  
   351  		When("a generic error occurs", func() {
   352  			var response string
   353  
   354  			BeforeEach(func() {
   355  				clientID = "client-id"
   356  
   357  				response = `{
   358  					"error_description": "Invalid filter expression"
   359  				}`
   360  
   361  				uaaServer.AppendHandlers(
   362  					CombineHandlers(
   363  						verifyRequestHost(TestUAAResource),
   364  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   365  						VerifyHeaderKV("Content-Type", "application/json"),
   366  						RespondWith(http.StatusBadRequest, response),
   367  					))
   368  			})
   369  
   370  			It("returns the error", func() {
   371  				Expect(err).To(MatchError(RawHTTPStatusError{
   372  					StatusCode:  400,
   373  					RawResponse: []byte(response),
   374  				}))
   375  			})
   376  		})
   377  
   378  		When("a user-not-found error occurs", func() {
   379  			var response string
   380  
   381  			BeforeEach(func() {
   382  				clientID = "client-id"
   383  
   384  				response = `{
   385  					"error_description": "User not found"
   386  				}`
   387  
   388  				uaaServer.AppendHandlers(
   389  					CombineHandlers(
   390  						verifyRequestHost(TestUAAResource),
   391  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   392  						VerifyHeaderKV("Content-Type", "application/json"),
   393  						RespondWith(http.StatusNotFound, response),
   394  					))
   395  			})
   396  
   397  			It("returns the error", func() {
   398  				Expect(err).To(MatchError(actionerror.UserNotFoundError{Username: "client-id"}))
   399  			})
   400  		})
   401  
   402  		When("a forbidden error occurs", func() {
   403  			var response string
   404  
   405  			BeforeEach(func() {
   406  				clientID = "client-id"
   407  
   408  				response = `{
   409  					"error_description": "Not authorized"
   410  				}`
   411  
   412  				uaaServer.AppendHandlers(
   413  					CombineHandlers(
   414  						verifyRequestHost(TestUAAResource),
   415  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   416  						VerifyHeaderKV("Content-Type", "application/json"),
   417  						RespondWith(http.StatusForbidden, response),
   418  					))
   419  			})
   420  
   421  			It("returns the error", func() {
   422  				Expect(err).To(MatchError(InsufficientScopeError{}))
   423  			})
   424  		})
   425  	})
   426  })