Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add user lock, unlock & support user_lockout in instance restri… #167

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Lock returned error: %v", err)
}
assert.True(t, updatedUser.Locked)

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

// Should return all memberships of a user
Expand Down