Commit 5e1c7f37 authored by Joe Rocklin's avatar Joe Rocklin Committed by Sander van Harmelen

Update user block/unblock to use new error handling

parent f4839529
......@@ -17,7 +17,6 @@
package gitlab
import (
"errors"
"fmt"
"time"
)
......@@ -431,7 +430,7 @@ func (s *UsersService) BlockUser(user int, options ...OptionFunc) error {
}
resp, err := s.client.Do(req, nil)
if err != nil {
if err != nil && resp == nil {
return err
}
......@@ -439,9 +438,9 @@ func (s *UsersService) BlockUser(user int, options ...OptionFunc) error {
case 201:
return nil
case 403:
return errors.New("Cannot block a user that is already blocked by LDAP synchronization")
return UserBlockPreventedError
case 404:
return errors.New("User does not exist")
return UserNotFoundError
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
......@@ -459,7 +458,7 @@ func (s *UsersService) UnblockUser(user int, options ...OptionFunc) error {
}
resp, err := s.client.Do(req, nil)
if err != nil {
if err != nil && resp == nil {
return err
}
......@@ -467,9 +466,9 @@ func (s *UsersService) UnblockUser(user int, options ...OptionFunc) error {
case 201:
return nil
case 403:
return errors.New("Cannot unblock a user that is blocked by LDAP synchronization")
return UserUnblockPreventedError
case 404:
return errors.New("User does not exist")
return UserNotFoundError
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
......
......@@ -7,6 +7,81 @@ import (
"testing"
)
func TestBlockUser(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/users/1/block", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusCreated)
})
err := client.Users.BlockUser(1)
if err != nil {
t.Errorf("Users.BlockUser returned error: %v", err)
}
}
func TestBlockUser_UserNotFound(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/users/1/block", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusNotFound)
})
err := client.Users.BlockUser(1)
if err != UserNotFoundError {
t.Errorf("Users.BlockUser error.\nExpected: %+v\n Got: %+v", UserNotFoundError, err)
}
}
func TestBlockUser_BlockPrevented(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/users/1/block", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusForbidden)
})
err := client.Users.BlockUser(1)
if err != UserBlockPreventedError {
t.Errorf("Users.BlockUser error.\nExpected: %+v\n Got: %+v", UserBlockPreventedError, err)
}
}
func TestBlockUser_UnknownError(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/users/1/block", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusTeapot)
})
want := fmt.Errorf("Received unexpected result code: %d", http.StatusTeapot)
err := client.Users.BlockUser(1)
if !reflect.DeepEqual(err, want) {
t.Errorf("Users.BlockUser error.\nExpected: %+v\n Got: %+v", want, err)
}
}
func TestBlockUser_BadResponseFromNet(t *testing.T) {
client := NewClient(nil, "")
client.SetBaseURL("")
want := "Post /users/1/block: unsupported protocol scheme \"\""
err := client.Users.BlockUser(1)
if err.Error() != want {
t.Errorf("Users.BlockUser error.\nExpected: %+v\n Got: %+v", want, err)
}
}
// ------------------------ Unblock user -------------------------
func TestUnblockUser(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment