(t *testing.T)
| 259 | } |
| 260 | |
| 261 | func TestUserAPI(t *testing.T) { |
| 262 | |
| 263 | // PUT a user |
| 264 | rt := NewRestTester(t, nil) |
| 265 | defer rt.Close() |
| 266 | ctx := rt.Context() |
| 267 | ds := rt.GetSingleDataStore() |
| 268 | c := ds.CollectionName() |
| 269 | s := ds.ScopeName() |
| 270 | |
| 271 | RequireStatus(t, rt.SendAdminRequest("GET", "/db/_user/snej", ""), 404) |
| 272 | |
| 273 | response := rt.SendAdminRequest("PUT", "/db/_user/snej", GetUserPayload(t, "", "letmein", "jens@couchbase.com", ds, []string{"foo", "bar"}, nil)) |
| 274 | RequireStatus(t, response, 201) |
| 275 | |
| 276 | user, err := rt.ServerContext().Database(ctx, "db").Authenticator(ctx).GetUser("snej") |
| 277 | require.NoError(t, err) |
| 278 | require.NotNil(t, user) |
| 279 | // GET the user and make sure the result is OK |
| 280 | response = rt.SendAdminRequest("GET", "/db/_user/snej", "") |
| 281 | RequireStatus(t, response, 200) |
| 282 | |
| 283 | princ := auth.PrincipalConfig{} |
| 284 | require.NoError(t, base.JSONUnmarshal(response.Body.Bytes(), &princ)) |
| 285 | allChans := princ.GetChannels(s, c).ToArray() |
| 286 | adminChans := princ.GetExplicitChannels(s, c).ToArray() |
| 287 | sort.Strings(allChans) |
| 288 | sort.Strings(adminChans) |
| 289 | assert.Equal(t, "snej", *princ.Name) |
| 290 | assert.Equal(t, "jens@couchbase.com", *princ.Email) |
| 291 | assert.Equal(t, []string{"bar", "foo"}, adminChans) |
| 292 | assert.Equal(t, []string{"!", "bar", "foo"}, allChans) |
| 293 | assert.Nil(t, princ.Password) |
| 294 | |
| 295 | // Check the list of all users: |
| 296 | response = rt.SendAdminRequest("GET", "/db/_user/", "") |
| 297 | RequireStatus(t, response, 200) |
| 298 | assert.Equal(t, `["snej"]`, string(response.Body.Bytes())) |
| 299 | |
| 300 | // Check that the actual User object is correct: |
| 301 | user, _ = rt.ServerContext().Database(ctx, "db").Authenticator(ctx).GetUser("snej") |
| 302 | assert.Equal(t, "snej", user.Name()) |
| 303 | assert.Equal(t, "jens@couchbase.com", user.Email()) |
| 304 | assert.Equal(t, channels.TimedSet{"bar": channels.NewVbSimpleSequence(0x1), "foo": channels.NewVbSimpleSequence(0x1)}, user.CollectionExplicitChannels(s, c)) |
| 305 | assert.True(t, user.Authenticate("letmein")) |
| 306 | |
| 307 | // Change the password and verify it: |
| 308 | response = rt.SendAdminRequest("PUT", "/db/_user/snej", GetUserPayload(t, "", "123", "", ds, []string{"foo", "bar"}, nil)) |
| 309 | RequireStatus(t, response, 200) |
| 310 | |
| 311 | user, _ = rt.ServerContext().Database(ctx, "db").Authenticator(ctx).GetUser("snej") |
| 312 | assert.True(t, user.Authenticate("123")) |
| 313 | |
| 314 | // DELETE the user |
| 315 | RequireStatus(t, rt.SendAdminRequest("DELETE", "/db/_user/snej", ""), 200) |
| 316 | RequireStatus(t, rt.SendAdminRequest("GET", "/db/_user/snej", ""), 404) |
| 317 | |
| 318 | // POST a user |
nothing calls this directly
no test coverage detected