Skip to content

Commit

Permalink
Merge pull request #93 from nukosuke/rud-trigger
Browse files Browse the repository at this point in the history
RUD Trigger
  • Loading branch information
nukosuke authored Mar 23, 2019
2 parents 3115767 + 02d1f78 commit d540f1d
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 1 deletion.
43 changes: 43 additions & 0 deletions fixture/GET/trigger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"trigger": {
"url": "https://example.zendesk.com/api/v2/triggers/360056295714.json",
"id": 360056295714,
"title": "Notify requester of received request",
"active": true,
"updated_at": "2018-11-23T16:05:14Z",
"created_at": "2018-11-23T16:05:12Z",
"actions": [
{
"field": "notification_user",
"value": [
"requester_id",
"[Request received]",
"Your request ({{ticket.id}}) has been received and is being reviewed by our support staff.\n\nTo add additional comments, reply to this email."
]
}
],
"conditions": {
"all": [
{
"field": "update_type",
"operator": "is",
"value": "Create"
},
{
"field": "status",
"operator": "is_not",
"value": "solved"
},
{
"field": "ticket_is_public",
"operator": "is",
"value": "public"
}
],
"any": []
},
"description": null,
"position": 0,
"raw_title": "Notify requester of received request"
}
}
43 changes: 43 additions & 0 deletions fixture/PUT/triggers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"trigger": {
"url": "https://example.zendesk.com/api/v2/triggers/360056295714.json",
"id": 360056295714,
"title": "Notify requester of received request",
"active": true,
"updated_at": "2018-11-23T16:05:14Z",
"created_at": "2018-11-23T16:05:12Z",
"actions": [
{
"field": "notification_user",
"value": [
"requester_id",
"[Request received]",
"Your request ({{ticket.id}}) has been received and is being reviewed by our support staff.\n\nTo add additional comments, reply to this email."
]
}
],
"conditions": {
"all": [
{
"field": "update_type",
"operator": "is",
"value": "Create"
},
{
"field": "status",
"operator": "is_not",
"value": "solved"
},
{
"field": "ticket_is_public",
"operator": "is",
"value": "public"
}
],
"any": []
},
"description": null,
"position": 0,
"raw_title": "Notify requester of received request"
}
}
44 changes: 44 additions & 0 deletions zendesk/mock/client.go

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

58 changes: 57 additions & 1 deletion zendesk/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zendesk

import (
"encoding/json"
"fmt"
"time"
)

Expand Down Expand Up @@ -41,9 +42,13 @@ type Trigger struct {
type TriggerAPI interface {
GetTriggers() ([]Trigger, Page, error)
CreateTrigger(trigger Trigger) (Trigger, error)
GetTrigger(id int64) (Trigger, error)
UpdateTrigger(id int64, trigger Trigger) (Trigger, error)
DeleteTrigger(id int64) error
}

// GetTriggers fetch trigger list
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#getting-triggers
func (z *Client) GetTriggers() ([]Trigger, Page, error) {
var data struct {
Triggers []Trigger `json:"triggers"`
Expand All @@ -63,7 +68,7 @@ func (z *Client) GetTriggers() ([]Trigger, Page, error) {
}

// CreateTrigger creates new trigger
// ref: https://developer.zendesk.com/rest_api/docs/core/triggers#create-trigger
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#create-trigger
func (z *Client) CreateTrigger(trigger Trigger) (Trigger, error) {
var data, result struct {
Trigger Trigger `json:"trigger"`
Expand All @@ -81,3 +86,54 @@ func (z *Client) CreateTrigger(trigger Trigger) (Trigger, error) {
}
return result.Trigger, nil
}

// GetTrigger returns the specified trigger
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#getting-triggers
func (z *Client) GetTrigger(id int64) (Trigger, error) {
var result struct {
Trigger Trigger `json:"trigger"`
}

body, err := z.Get(fmt.Sprintf("/triggers/%d.json", id))
if err != nil {
return Trigger{}, err
}

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

// UpdateTrigger updates the specified trigger and returns the updated one
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#update-trigger
func (z *Client) UpdateTrigger(id int64, trigger Trigger) (Trigger, error) {
var data, result struct {
Trigger Trigger `json:"trigger"`
}

data.Trigger = trigger
body, err := z.Put(fmt.Sprintf("/triggers/%d.json", id), data)
if err != nil {
return Trigger{}, err
}

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

return result.Trigger, nil
}

// DeleteTrigger deletes the specified trigger
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#delete-trigger
func (z *Client) DeleteTrigger(id int64) error {
err := z.Delete(fmt.Sprintf("/triggers/%d.json", id))
if err != nil {
return err
}

return nil
}
85 changes: 85 additions & 0 deletions zendesk/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zendesk

import (
"net/http"
"net/http/httptest"
"testing"
)

Expand Down Expand Up @@ -30,3 +31,87 @@ func TestCreateTrigger(t *testing.T) {
t.Fatalf("Failed to send request to create trigger: %s", err)
}
}

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

trg, err := client.GetTrigger(123)
if err != nil {
t.Fatalf("Failed to get trigger: %s", err)
}

expectedID := int64(360056295714)
if trg.ID != expectedID {
t.Fatalf("Returned trigger does not have the expected ID %d. Trigger id is %d", expectedID, trg.ID)
}
}

func TestGetTriggerFailure(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write(nil)
}))

c := newTestClient(mockAPI)
_, err := c.GetTrigger(1234)
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}

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

trg, err := client.UpdateTrigger(123, Trigger{})
if err != nil {
t.Fatalf("Failed to get trigger: %s", err)
}

expectedID := int64(360056295714)
if trg.ID != expectedID {
t.Fatalf("Returned trigger does not have the expected ID %d. Trigger id is %d", expectedID, trg.ID)
}
}

func TestUpdateTriggerFailure(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write(nil)
}))

c := newTestClient(mockAPI)
_, err := c.UpdateTrigger(1234, Trigger{})
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}

func TestDeleteTrigger(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
w.Write(nil)
}))

c := newTestClient(mockAPI)
err := c.DeleteTrigger(1234)
if err != nil {
t.Fatalf("Failed to delete trigger: %s", err)
}
}

func TestDeleteTriggerFailure(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write(nil)
}))

c := newTestClient(mockAPI)
err := c.DeleteTrigger(1234)
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}

0 comments on commit d540f1d

Please sign in to comment.