github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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("ValidateClientUser", func() {
   271  		var (
   272  			clientID string
   273  			err      error
   274  		)
   275  
   276  		BeforeEach(func() {
   277  			clientID = "client-id"
   278  			err = nil
   279  		})
   280  
   281  		JustBeforeEach(func() {
   282  			err = client.ValidateClientUser(clientID)
   283  		})
   284  
   285  		When("no errors occur", func() {
   286  			BeforeEach(func() {
   287  				uaaServer.AppendHandlers(
   288  					CombineHandlers(
   289  						verifyRequestHost(TestUAAResource),
   290  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   291  						VerifyHeaderKV("Content-Type", "application/json"),
   292  						RespondWith(http.StatusOK, "Response{}"),
   293  					))
   294  
   295  				It("gets users by username", func() {
   296  					Expect(err).NotTo(HaveOccurred())
   297  				})
   298  			})
   299  		})
   300  
   301  		When("a generic error occurs", func() {
   302  			var response string
   303  
   304  			BeforeEach(func() {
   305  				clientID = "client-id"
   306  
   307  				response = `{
   308  					"error_description": "Invalid filter expression"
   309  				}`
   310  
   311  				uaaServer.AppendHandlers(
   312  					CombineHandlers(
   313  						verifyRequestHost(TestUAAResource),
   314  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   315  						VerifyHeaderKV("Content-Type", "application/json"),
   316  						RespondWith(http.StatusBadRequest, response),
   317  					))
   318  			})
   319  
   320  			It("returns the error", func() {
   321  				Expect(err).To(MatchError(RawHTTPStatusError{
   322  					StatusCode:  400,
   323  					RawResponse: []byte(response),
   324  				}))
   325  			})
   326  		})
   327  
   328  		When("a user-not-found error occurs", func() {
   329  			var response string
   330  
   331  			BeforeEach(func() {
   332  				clientID = "client-id"
   333  
   334  				response = `{
   335  					"error_description": "User not found"
   336  				}`
   337  
   338  				uaaServer.AppendHandlers(
   339  					CombineHandlers(
   340  						verifyRequestHost(TestUAAResource),
   341  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   342  						VerifyHeaderKV("Content-Type", "application/json"),
   343  						RespondWith(http.StatusNotFound, response),
   344  					))
   345  			})
   346  
   347  			It("returns the error", func() {
   348  				Expect(err).To(MatchError(actionerror.UserNotFoundError{Username: "client-id"}))
   349  			})
   350  		})
   351  
   352  		When("a forbidden error occurs", func() {
   353  			var response string
   354  
   355  			BeforeEach(func() {
   356  				clientID = "client-id"
   357  
   358  				response = `{
   359  					"error_description": "Not authorized"
   360  				}`
   361  
   362  				uaaServer.AppendHandlers(
   363  					CombineHandlers(
   364  						verifyRequestHost(TestUAAResource),
   365  						VerifyRequest(http.MethodGet, "/oauth/clients/client-id"),
   366  						VerifyHeaderKV("Content-Type", "application/json"),
   367  						RespondWith(http.StatusForbidden, response),
   368  					))
   369  			})
   370  
   371  			It("returns the error", func() {
   372  				Expect(err).To(MatchError(InsufficientScopeError{}))
   373  			})
   374  		})
   375  	})
   376  })