Skip to content

Commit

Permalink
Handle GetUser & UpdateUser Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
abebars committed May 12, 2020
1 parent db4cda2 commit ff9654b
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 1 deletion.
67 changes: 67 additions & 0 deletions fixture/GET/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"user": {
"id": 369531345753,
"url": "https://example.zendesk.com/api/v2/users/369531345753.json",
"name": "Sample customer",
"email": "customer@example.com",
"created_at": "2018-11-23T16:05:13Z",
"updated_at": "2018-11-23T16:05:14Z",
"time_zone": "Osaka",
"iana_time_zone": "Asia/Tokyo",
"phone": null,
"shared_phone_number": null,
"photo": {
"url": "https://example.zendesk.com/api/v2/attachments/360255188054.json",
"id": 360255188054,
"file_name": "profile_image_369531345753_9042965.jpg",
"content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965.jpg",
"mapped_content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965.jpg",
"content_type": "image/jpeg",
"size": 2587,
"width": 40,
"height": 40,
"inline": false,
"thumbnails": [
{
"url": "https://example.zendesk.com/api/v2/attachments/360255188074.json",
"id": 360255188074,
"file_name": "profile_image_369531345753_9042965_thumb.jpg",
"content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965_thumb.jpg",
"mapped_content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965_thumb.jpg",
"content_type": "image/jpeg",
"size": 1973,
"width": 32,
"height": 32,
"inline": false
}
]
},
"locale_id": 1,
"locale": "en-US",
"organization_id": null,
"role": "end-user",
"verified": false,
"external_id": null,
"tags": [],
"alias": null,
"active": true,
"shared": false,
"shared_agent": false,
"last_login_at": null,
"two_factor_auth_enabled": false,
"signature": null,
"details": null,
"notes": null,
"role_type": null,
"custom_role_id": null,
"moderator": false,
"ticket_restriction": "requested",
"only_private_comments": false,
"restricted_agent": true,
"suspended": false,
"chat_only": false,
"default_group_id": null,
"report_csv": false,
"user_fields": {}
}
}
67 changes: 67 additions & 0 deletions fixture/PUT/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"user": {
"id": 369531345753,
"url": "https://example.zendesk.com/api/v2/users/369531345753.json",
"name": "Sample customer",
"email": "customer@example.com",
"created_at": "2018-11-23T16:05:13Z",
"updated_at": "2018-11-23T16:05:14Z",
"time_zone": "Osaka",
"iana_time_zone": "Asia/Tokyo",
"phone": null,
"shared_phone_number": null,
"photo": {
"url": "https://example.zendesk.com/api/v2/attachments/360255188054.json",
"id": 360255188054,
"file_name": "profile_image_369531345753_9042965.jpg",
"content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965.jpg",
"mapped_content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965.jpg",
"content_type": "image/jpeg",
"size": 2587,
"width": 40,
"height": 40,
"inline": false,
"thumbnails": [
{
"url": "https://example.zendesk.com/api/v2/attachments/360255188074.json",
"id": 360255188074,
"file_name": "profile_image_369531345753_9042965_thumb.jpg",
"content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965_thumb.jpg",
"mapped_content_url": "https://example.zendesk.com/system/photos/3602/5518/8054/profile_image_369531345753_9042965_thumb.jpg",
"content_type": "image/jpeg",
"size": 1973,
"width": 32,
"height": 32,
"inline": false
}
]
},
"locale_id": 1,
"locale": "en-US",
"organization_id": null,
"role": "end-user",
"verified": false,
"external_id": null,
"tags": [],
"alias": null,
"active": true,
"shared": false,
"shared_agent": false,
"last_login_at": null,
"two_factor_auth_enabled": false,
"signature": null,
"details": null,
"notes": null,
"role_type": null,
"custom_role_id": null,
"moderator": false,
"ticket_restriction": "requested",
"only_private_comments": false,
"restricted_agent": true,
"suspended": false,
"chat_only": false,
"default_group_id": null,
"report_csv": false,
"user_fields": {}
}
}
30 changes: 30 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions zendesk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zendesk
import (
"context"
"encoding/json"
"fmt"
"time"
)

Expand Down Expand Up @@ -81,7 +82,9 @@ func UserRoleText(role int) string {
// UserAPI an interface containing all user related methods
type UserAPI interface {
GetUsers(ctx context.Context, opts *UserListOptions) ([]User, Page, error)
GetUser(ctx context.Context, userId int64) (User, error)
CreateUser(ctx context.Context, user User) (User, error)
UpdateUser(ctx context.Context, userId int64, user User) (User, error)
}

// GetUsers fetch user list
Expand Down Expand Up @@ -136,3 +139,42 @@ func (z *Client) CreateUser(ctx context.Context, user User) (User, error) {
}

// TODO: CreateOrUpdateManyUsers(users []User)

// GetUser get an existing user
// ref: https://developer.zendesk.com/rest_api/docs/support/users#show-user
func (z *Client) GetUser(ctx context.Context, userId int64) (User, error) {
var result struct {
User User `json:"user"`
}

body, err := z.get(ctx, fmt.Sprintf("/users/%d.json", userId))
if err != nil {
return User{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return User{}, err
}
return result.User, nil
}

// UpdateUser update an existing user
// ref: https://developer.zendesk.com/rest_api/docs/support/users#update-user
func (z *Client) UpdateUser(ctx context.Context, userId int64, user User) (User, error) {
var data, result struct {
User User `json:"user"`
}
data.User = user

body, err := z.put(ctx, fmt.Sprintf("/users/%d.json", userId), data)
if err != nil {
return User{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return User{}, err
}
return result.User, nil
}
34 changes: 33 additions & 1 deletion zendesk/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ func TestGetUsers(t *testing.T) {
}

if len(users) != 2 {
t.Fatalf("expected length of triggers is 2, but got %d", len(users))
t.Fatalf("expected length of userss is 2, but got %d", len(users))
}
}

func TestGetUser(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "user.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

user, err := client.GetUser(ctx, 369531345753)
if err != nil {
t.Fatalf("Failed to get user: %s", err)
}

expectedID := int64(369531345753)
if user.ID != expectedID {
t.Fatalf("Returned user does not have the expected ID %d. User id is %d", expectedID, user.ID)
}
}

Expand Down Expand Up @@ -72,3 +88,19 @@ func TestCreateUser(t *testing.T) {
t.Fatal("Failed to create user")
}
}

func TestUpdateUser(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPut, "user.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()

user, err := client.UpdateUser(ctx, 369531345753, User{})
if err != nil {
t.Fatalf("Failed to update user: %s", err)
}

expectedID := int64(369531345753)
if user.ID != expectedID {
t.Fatalf("Returned user does not have the expected ID %d. User id is %d", expectedID, user.ID)
}
}

0 comments on commit ff9654b

Please sign in to comment.