Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimuska139 committed Oct 24, 2019
1 parent 033b759 commit c43404f
Show file tree
Hide file tree
Showing 15 changed files with 1,075 additions and 590 deletions.
550 changes: 0 additions & 550 deletions books_test.go

This file was deleted.

7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type client struct {
tokenLock *sync.RWMutex
}

func NewClient(config Config) *client {
c := &client{config, "", new(sync.RWMutex)}
return c
}

const apiBaseUrl = "https://api.sendpulse.com"

func (c *client) getToken() (string, error) {
Expand Down Expand Up @@ -85,7 +90,7 @@ func (c *client) makeRequest(path string, method string, data map[string]interfa

method = strings.ToUpper(method)

fullPath := fmt.Sprintf(apiBaseUrl+"%s", path)
fullPath := apiBaseUrl + path
req, e := http.NewRequest(method, fullPath, bytes.NewBufferString(q.Encode()))
if e != nil {
return nil, e
Expand Down
99 changes: 98 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,109 @@ package sendpulse

import (
"fmt"
"github.com/icrowley/fake"
"github.com/stretchr/testify/assert"
"gopkg.in/jarcoal/httpmock.v1"
"net/http"
"testing"
)

func TestResponseErrorData(t *testing.T) {
func TestSendpulseError_Error(t *testing.T) {
e := SendpulseError{http.StatusInternalServerError, "http://test.com", "Something went wrong", "Test message"}
assert.Equal(t, fmt.Sprintf("Http code: %d, url: %s, body: %s, message: %s", e.HttpCode, e.Url, e.Body, e.Message), e.Error())
}

func TestGetToken_Stored(t *testing.T) {
config := Config{
UserID: fake.Word(),
Secret: fake.Word(),
Timeout: 0,
}
token := fake.Word()
c := NewClient(config)
c.token = token
result, err := c.getToken()
assert.NoError(t, err)
assert.Equal(t, token, result)
}

func TestGetToken_Error(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", apiBaseUrl+"/oauth/access_token",
httpmock.NewStringResponder(http.StatusInternalServerError,
`Something went wrong`))

config := Config{
UserID: fake.Word(),
Secret: fake.Word(),
Timeout: 0,
}

c := NewClient(config)
token, err := c.getToken()
assert.Error(t, err)
assert.Equal(t, "", token)
}

func TestGetToken_BadJson(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", apiBaseUrl+"/oauth/access_token",
httpmock.NewStringResponder(http.StatusInternalServerError,
`{access_token": "testtoken","token_type": "Bearer","expires_in": 3600}`))

config := Config{
UserID: fake.Word(),
Secret: fake.Word(),
Timeout: 0,
}

c := NewClient(config)
token, err := c.getToken()
assert.Error(t, err)
assert.Equal(t, "", token)
}

func TestGetToken_NoTokenProperty(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", apiBaseUrl+"/oauth/access_token",
httpmock.NewStringResponder(http.StatusInternalServerError,
`{"token_type": "Bearer","expires_in": 3600}`))

config := Config{
UserID: fake.Word(),
Secret: fake.Word(),
Timeout: 0,
}

c := NewClient(config)
token, err := c.getToken()
assert.Error(t, err)
assert.Equal(t, "", token)
}

func TestGetToken_Success(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

token := fake.Word()
httpmock.RegisterResponder("POST", apiBaseUrl+"/oauth/access_token",
httpmock.NewStringResponder(http.StatusOK,
`{"access_token": "`+token+`","token_type": "Bearer","expires_in": 3600}`))

config := Config{
UserID: fake.Word(),
Secret: fake.Word(),
Timeout: 0,
}

c := NewClient(config)
newToken, err := c.getToken()
assert.NoError(t, err)
assert.Equal(t, token, newToken)
}
9 changes: 2 additions & 7 deletions automation360.go → emails_automation360.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func (a *automation360) StartEvent(eventName string, variables map[string]interf
}

body, err := a.Client.makeRequest(path, "POST", variables, true)

if err != nil {
return err
}
Expand All @@ -33,12 +32,8 @@ func (a *automation360) StartEvent(eventName string, variables map[string]interf
}

result, resultExists := respData["result"]
if !resultExists {
return &SendpulseError{http.StatusOK, path, string(body), "'result' not found in response"}
}

if !result.(bool) {
return &SendpulseError{http.StatusOK, path, string(body), "'result' is false"}
if !resultExists || !result.(bool) {
return &SendpulseError{http.StatusOK, path, string(body), "invalid response"}
}

return nil
Expand Down
66 changes: 47 additions & 19 deletions automation360_test.go → emails_automation360_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ func TestAutomation360_StartEvent_NoPhoneAndEmail(t *testing.T) {
assert.False(t, isSPError)
}

func TestAutomation360_StartEvent_EventNotExists(t *testing.T) {
func TestAutomation360_StartEvent_Success(t *testing.T) {
eventName := fake.Word()

path := fmt.Sprintf("/events/name/%s", eventName)
url := apiBaseUrl + path
url := fmt.Sprintf("%s/events/name/%s", apiBaseUrl, eventName)

apiUid := fake.CharactersN(50)
apiSecret := fake.CharactersN(50)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

respBody := `{"error_code": 102,"message": "Event not exists"}`
httpmock.RegisterResponder("POST", url,
httpmock.NewStringResponder(http.StatusBadRequest,
respBody))
httpmock.NewStringResponder(http.StatusOK,
`{"result": true}`))

config := Config{
UserID: apiUid,
Expand All @@ -54,19 +52,43 @@ func TestAutomation360_StartEvent_EventNotExists(t *testing.T) {
spClient.client.token = fake.Word()

variables := make(map[string]interface{})
variables["email"] = fake.EmailAddress()
variables["phone"] = fake.Phone()
variables["name"] = fake.FullName()
err := spClient.Emails.Automation360.StartEvent(eventName, variables)
assert.NoError(t, err)
}

func TestAutomation360_StartEvent_Error(t *testing.T) {
eventName := fake.Word()

url := fmt.Sprintf("%s/events/name/%s", apiBaseUrl, eventName)

apiUid := fake.CharactersN(50)
apiSecret := fake.CharactersN(50)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", url,
httpmock.NewStringResponder(http.StatusNotFound, ""))

config := Config{
UserID: apiUid,
Secret: apiSecret,
Timeout: 5,
}
spClient, _ := ApiClient(config)
spClient.client.token = fake.Word()

variables := make(map[string]interface{})
variables["phone"] = fake.Phone()
variables["name"] = fake.FullName()
err := spClient.Emails.Automation360.StartEvent(eventName, variables)
assert.Error(t, err)
ResponseError, isResponseError := err.(*SendpulseError)
_, isResponseError := err.(*SendpulseError)
assert.True(t, isResponseError)
assert.Equal(t, http.StatusBadRequest, ResponseError.HttpCode)
assert.Equal(t, path, ResponseError.Url)
assert.Equal(t, respBody, ResponseError.Body)
assert.Equal(t, "", ResponseError.Message)
}

func TestAutomation360_StartEvent_WithPhone(t *testing.T) {
func TestAutomation360_StartEvent_BadJson(t *testing.T) {
eventName := fake.Word()

url := fmt.Sprintf("%s/events/name/%s", apiBaseUrl, eventName)
Expand All @@ -78,7 +100,7 @@ func TestAutomation360_StartEvent_WithPhone(t *testing.T) {

httpmock.RegisterResponder("POST", url,
httpmock.NewStringResponder(http.StatusOK,
`{"result": true}`))
`{result": true}`))

config := Config{
UserID: apiUid,
Expand All @@ -89,13 +111,16 @@ func TestAutomation360_StartEvent_WithPhone(t *testing.T) {
spClient.client.token = fake.Word()

variables := make(map[string]interface{})
variables["phone"] = fake.Phone()
variables["email"] = fake.EmailAddress()
variables["name"] = fake.FullName()
err := spClient.Emails.Automation360.StartEvent(eventName, variables)
assert.NoError(t, err)
assert.Error(t, err)

_, isResponseError := err.(*SendpulseError)
assert.True(t, isResponseError)
}

func TestAutomation360_StartEvent_WithEmail(t *testing.T) {
func TestAutomation360_StartEvent_BadResult(t *testing.T) {
eventName := fake.Word()

url := fmt.Sprintf("%s/events/name/%s", apiBaseUrl, eventName)
Expand All @@ -107,7 +132,7 @@ func TestAutomation360_StartEvent_WithEmail(t *testing.T) {

httpmock.RegisterResponder("POST", url,
httpmock.NewStringResponder(http.StatusOK,
`{"result": true}`))
`{"result": false}`))

config := Config{
UserID: apiUid,
Expand All @@ -121,5 +146,8 @@ func TestAutomation360_StartEvent_WithEmail(t *testing.T) {
variables["email"] = fake.EmailAddress()
variables["name"] = fake.FullName()
err := spClient.Emails.Automation360.StartEvent(eventName, variables)
assert.NoError(t, err)
assert.Error(t, err)

_, isResponseError := err.(*SendpulseError)
assert.True(t, isResponseError)
}
20 changes: 16 additions & 4 deletions books.go → emails_books.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type Variable struct {

type Contact struct {
Email string
Status int
Status string
StatusExplain string
Variables map[string]interface{}
Variables []Variable
}

type Email struct {
Expand Down Expand Up @@ -152,9 +152,13 @@ func (b *books) Variables(addressBookId uint) ([]Variable, error) {
}

func (b *books) Emails(addressBookId uint, limit uint, offset uint) ([]Contact, error) {
path := fmt.Sprintf("/addressbooks/%d/emails?limit=%d&offset=%d", addressBookId, limit, offset)
path := fmt.Sprintf("/addressbooks/%d/emails", addressBookId)

body, err := b.Client.makeRequest(path, "GET", nil, true)
data := map[string]interface{}{
"limit": fmt.Sprint(limit),
"offset": fmt.Sprint(offset),
}
body, err := b.Client.makeRequest(path, "GET", data, true)

if err != nil {
return nil, err
Expand Down Expand Up @@ -189,6 +193,14 @@ func (b *books) EmailsTotal(addressBookId uint) (uint, error) {
return uint(total.(float64)), nil
}

/**
Known limitations:
-- Max 10 rps allowed
-- Max 255 chars per variable
-- Sendpulse calls trim function to every variable
-- Sendpulse rejects requests with html tags an \r symbols
-- Sendpulse don't remove previous user variables if user already added to address book before
*/
func (b *books) AddEmails(addressBookId uint, notifications []Email, additionalParams map[string]string, senderEmail string) error {
path := fmt.Sprintf("/addressbooks/%d/emails", addressBookId)

Expand Down
Loading

0 comments on commit c43404f

Please sign in to comment.