Skip to content

Commit

Permalink
Add test for DeleteUser
Browse files Browse the repository at this point in the history
  • Loading branch information
nukosuke committed Apr 25, 2022
1 parent d9d68e0 commit 75215df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
7 changes: 7 additions & 0 deletions fixture/DELETE/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"user": {
"active": false,
"id": 9873843,
"name": "Roger Wilco II"
}
}
23 changes: 17 additions & 6 deletions zendesk/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestCreateUser(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

user, err := client.CreateUser(ctx, User{
user, err := client.CreateUser(ctx, &User{
Email: "test@example.com",
Name: "testuser",
})
Expand All @@ -120,7 +120,7 @@ func TestCreateOrUpdateUserCreated(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

user, err := client.CreateOrUpdateUser(ctx, User{
user, err := client.CreateOrUpdateUser(ctx, &User{
Email: "test@example.com",
Name: "testuser",
})
Expand All @@ -137,7 +137,7 @@ func TestCreateOrUpdateUserUpdated(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

user, err := client.CreateOrUpdateUser(ctx, User{
user, err := client.CreateOrUpdateUser(ctx, &User{
Email: "test@example.com",
Name: "testuser",
})
Expand All @@ -155,7 +155,7 @@ func TestCreateOrUpdateUserFailure(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

_, err := client.CreateOrUpdateUser(ctx, User{})
_, err := client.CreateOrUpdateUser(ctx, &User{})
if err == nil {
t.Fatal("Client did not return error when api failed")
}
Expand All @@ -166,7 +166,7 @@ func TestUpdateUser(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

user, err := client.UpdateUser(ctx, 369531345753, User{})
user, err := client.UpdateUser(ctx, 369531345753, &User{})
if err != nil {
t.Fatalf("Failed to update user: %s", err)
}
Expand All @@ -182,12 +182,23 @@ func TestUpdateUserFailure(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

_, err := client.UpdateUser(ctx, 369531345753, User{})
_, err := client.UpdateUser(ctx, 369531345753, &User{})
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}

func TestDeleteUser(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodDelete, "users.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()

err := client.DeleteUser(ctx, 1234)
if err != nil {
t.Fatalf("Failed to delete user: %s", err)
}
}

func TestGetUserRelated(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodGet, "user_related.json", http.StatusOK)
client := newTestClient(mockAPI)
Expand Down
7 changes: 4 additions & 3 deletions zendesk/zendesk.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ func (z *Client) delete(ctx context.Context, path string) error {
return err
}

if resp.StatusCode != http.StatusNoContent {
switch {
case http.StatusOK <= resp.StatusCode && resp.StatusCode < http.StatusBadRequest:
return nil
default:
return Error{
body: body,
resp: resp,
}
}

return nil
}

// prepare request sets common request variables such as authn and user agent
Expand Down

0 comments on commit 75215df

Please sign in to comment.