Skip to content

Commit

Permalink
feat: Add user lock & unlock operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Pitsilos committed Oct 13, 2023
1 parent d9e4be0 commit 0d0a1cc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
25 changes: 25 additions & 0 deletions clerk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type User struct {
UnsafeMetadata interface{} `json:"unsafe_metadata"`
LastSignInAt *int64 `json:"last_sign_in_at"`
Banned bool `json:"banned"`
Locked bool `json:"locked"`
ExternalID *string `json:"external_id"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Expand Down Expand Up @@ -304,6 +305,30 @@ func (s *UsersService) Unban(userID string) (*User, error) {
return &response, nil
}

func (s *UsersService) Lock(userID string) (*User, error) {
url := fmt.Sprintf("%s/%s/lock", UsersUrl, userID)
req, _ := s.client.NewRequest(http.MethodPost, url)

var response User
if _, err := s.client.Do(req, &response); err != nil {
return nil, err
}

return &response, nil
}

func (s *UsersService) Unlock(userID string) (*User, error) {
url := fmt.Sprintf("%s/%s/unlock", UsersUrl, userID)
req, _ := s.client.NewRequest(http.MethodPost, url)

var response User
if _, err := s.client.Do(req, &response); err != nil {
return nil, err
}

return &response, nil
}

type ListMembershipsParams struct {
Limit *int
Offset *int
Expand Down
37 changes: 36 additions & 1 deletion clerk/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,40 @@ func TestUsersService_Unban_happyPath(t *testing.T) {
assert.NoError(t, err)
}

func TestUsersService_Lock_happyPath(t *testing.T) {
token := "token"
userID := "test-user-id"

client, mux, _, teardown := setup(token)
defer teardown()

mux.HandleFunc("/users/"+userID+"/lock", func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, http.MethodPost)
testHeader(t, req, "Authorization", "Bearer "+token)
fmt.Fprint(w, dummyUserJson)
})

_, err := client.Users().Lock(userID)
assert.NoError(t, err)
}

func TestUsersService_Unlock_happyPath(t *testing.T) {
token := "token"
userID := "test-user-id"

client, mux, _, teardown := setup(token)
defer teardown()

mux.HandleFunc("/users/"+userID+"/unlock", func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, http.MethodPost)
testHeader(t, req, "Authorization", "Bearer "+token)
fmt.Fprint(w, dummyUserJson)
})

_, err := client.Users().Unlock(userID)
assert.NoError(t, err)
}

const dummyUserJson = `{
"birthday": "",
"created_at": 1610783813,
Expand Down Expand Up @@ -480,7 +514,8 @@ const dummyUserJson = `{
"app_id": 5
},
"last_sign_in_at": 1610783813,
"banned": false
"banned": false,
"locked": false
}`

const dummyUserOAuthAccessTokensJson = `[
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ func TestUsers(t *testing.T) {
t.Fatalf("Users.Unban returned error: %v", err)
}
assert.False(t, updatedUser.Banned)

updatedUser, err = client.Users().Lock(userId)
if err != nil {
t.Fatalf("Users.Ban returned error: %v", err)
}
assert.True(t, updatedUser.Locked)

updatedUser, err = client.Users().Unlock(userId)
if err != nil {
t.Fatalf("Users.Unban returned error: %v", err)
}
assert.False(t, updatedUser.Locked)
}

// Should return all memberships of a user
Expand Down

0 comments on commit 0d0a1cc

Please sign in to comment.