Skip to content

Commit

Permalink
Implement user ban and unban APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
timofurrer committed Mar 2, 2022
1 parent fe1a113 commit f7b4f3f
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
52 changes: 52 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,58 @@ func (s *UsersService) UnblockUser(user int, options ...RequestOptionFunc) error
}
}

// BanUser bans the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/users.html#ban-user
func (s *UsersService) BanUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/ban", user)

req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}

resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}

switch resp.StatusCode {
case 201:
return nil
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}

// UnbanUser unbans the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/users.html#unban-user
func (s *UsersService) UnbanUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/unban", user)

req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}

resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}

switch resp.StatusCode {
case 201:
return nil
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}

// DeactivateUser deactivate the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/users.html#deactivate-user
Expand Down
100 changes: 100 additions & 0 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,106 @@ func TestUnblockUser_UnknownError(t *testing.T) {
}
}

func TestBanUser(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

path := fmt.Sprintf("/%susers/1/block", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusCreated)
})

err := client.Users.BlockUser(1)
if err != nil {
t.Errorf("Users.BlockUser returned error: %v", err)
}
}

func TestBanUser_UserNotFound(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

path := fmt.Sprintf("/%susers/1/ban", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusNotFound)
})

err := client.Users.BanUser(1)
if !errors.Is(err, ErrUserNotFound) {
t.Errorf("Users.BanUser error.\nExpected: %+v\nGot: %+v", ErrUserNotFound, err)
}
}

func TestBanUser_UnknownError(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

path := fmt.Sprintf("/%susers/1/ban", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusTeapot)
})

want := fmt.Sprintf("Received unexpected result code: %d", http.StatusTeapot)

err := client.Users.BanUser(1)
if err.Error() != want {
t.Errorf("Users.BanUSer error.\nExpected: %s\nGot: %v", want, err)
}
}

func TestUnbanUser(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

path := fmt.Sprintf("/%susers/1/unban", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusCreated)
})

err := client.Users.UnbanUser(1)
if err != nil {
t.Errorf("Users.UnbanUser returned error: %v", err)
}
}

func TestUnbanUser_UserNotFound(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

path := fmt.Sprintf("/%susers/1/unban", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusNotFound)
})

err := client.Users.UnbanUser(1)
if !errors.Is(err, ErrUserNotFound) {
t.Errorf("Users.UnbanUser error.\nExpected: %v\nGot: %v", ErrUserNotFound, err)
}
}

func TestUnbanUser_UnknownError(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

path := fmt.Sprintf("/%susers/1/unban", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusTeapot)
})

want := fmt.Sprintf("Received unexpected result code: %d", http.StatusTeapot)

err := client.Users.UnbanUser(1)
if err.Error() != want {
t.Errorf("Users.UnbanUser error.\nExpected: %s\n\tGot: %v", want, err)
}
}

func TestDeactivateUser(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
Expand Down

0 comments on commit f7b4f3f

Please sign in to comment.