Skip to content

Commit

Permalink
adding Messages tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panperla committed Jan 16, 2024
1 parent 5ee835d commit 3b1b349
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reamaze/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"reflect"
)

// call to messages will allow you to retrieve individual messages for all conversations in the Brand
// GetMessages call to messages will allow you to retrieve individual messages for all conversations in the Brand
// https://www.reamaze.com/api/get_messages
func (c *Client) GetMessages() (*GetMessagesResponse, error) {
var response *GetMessagesResponse
Expand Down
254 changes: 254 additions & 0 deletions reamaze/messages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package reamaze

import (
"io"
"net/http"
"reflect"
"strings"
"testing"
)

func TestClient_GetMessages(t *testing.T) {
type fields struct {
baseURL string
auth string
httpClient *http.Client
}
tests := []struct {
name string
fields fields
want *GetMessagesResponse
wantErr bool
}{
{
name: "Testing correct request",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 Status OK",
Body: io.NopCloser(strings.NewReader(`{}`)),
}
}),
}},

want: &GetMessagesResponse{},
wantErr: false,
},
{
name: "Testing incorrect JSON response",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 Status OK",
Body: io.NopCloser(strings.NewReader(`{`)),
}
}),
}},

want: nil,
wantErr: true,
},
{
name: "Testing incorrect endpoint response",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusUnprocessableEntity,
Status: "422 UnprocessableEntity",
Body: io.NopCloser(strings.NewReader(`{}`)),
}
}),
}},

want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
baseURL: tt.fields.baseURL,
auth: tt.fields.auth,
httpClient: tt.fields.httpClient,
}
got, err := c.GetMessages()
if (err != nil) != tt.wantErr {
t.Errorf("Client.GetMessages() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Client.GetMessages() = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_CreateMessage(t *testing.T) {
type fields struct {
baseURL string
auth string
httpClient *http.Client
}
type args struct {
slug string
req *CreateMessageRequest
}
tests := []struct {
name string
fields fields
args args
want *CreateMessageResponse
wantErr bool
}{
{
name: "Testing correct request",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 Status OK",
Body: io.NopCloser(strings.NewReader(`{}`)),
}
}),
}},
args: args{slug: "dummy", req: &CreateMessageRequest{Message: struct {
Body string "json:\"body\""
Visibility ReamazeVisibility "json:\"visibility,omitempty\""
OriginID string "json:\"origin_id,omitempty\""
User *struct {
Name string "json:\"name,omitempty\""
Email string "json:\"email,omitempty\""
} "json:\"user,omitempty\""
SupressNotification bool "json:\"suppress_notifications,omitempty\""
SupressAutoresolve bool "json:\"suppress_autoresolve,omitempty\""
Attachment string "json:\"attachment,omitempty\""
Attachments []string "json:\"attachments,omitempty\""
}{
Body: "dummy",
}}},
want: &CreateMessageResponse{},
wantErr: false,
},
{
name: "Testing invalid JSON response",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 Status OK",
Body: io.NopCloser(strings.NewReader(`{`)),
}
}),
}},
args: args{slug: "dummy", req: &CreateMessageRequest{Message: struct {
Body string "json:\"body\""
Visibility ReamazeVisibility "json:\"visibility,omitempty\""
OriginID string "json:\"origin_id,omitempty\""
User *struct {
Name string "json:\"name,omitempty\""
Email string "json:\"email,omitempty\""
} "json:\"user,omitempty\""
SupressNotification bool "json:\"suppress_notifications,omitempty\""
SupressAutoresolve bool "json:\"suppress_autoresolve,omitempty\""
Attachment string "json:\"attachment,omitempty\""
Attachments []string "json:\"attachments,omitempty\""
}{
Body: "dummy",
}}},
want: nil,
wantErr: true,
},
{
name: "Testing invalid endpoint response",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusNotFound,
Status: "404 Not Found",
Body: io.NopCloser(strings.NewReader(`{}`)),
}
}),
}},
args: args{slug: "dummy", req: &CreateMessageRequest{Message: struct {
Body string "json:\"body\""
Visibility ReamazeVisibility "json:\"visibility,omitempty\""
OriginID string "json:\"origin_id,omitempty\""
User *struct {
Name string "json:\"name,omitempty\""
Email string "json:\"email,omitempty\""
} "json:\"user,omitempty\""
SupressNotification bool "json:\"suppress_notifications,omitempty\""
SupressAutoresolve bool "json:\"suppress_autoresolve,omitempty\""
Attachment string "json:\"attachment,omitempty\""
Attachments []string "json:\"attachments,omitempty\""
}{
Body: "dummy",
}}},
want: nil,
wantErr: true,
},
{
name: "Testing empty CreateMessageRequest",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 Status OK",
Body: io.NopCloser(strings.NewReader(`{}`)),
}
}),
}},
args: args{slug: "dummy", req: &CreateMessageRequest{}},
want: nil,
wantErr: true,
},
{
name: "Testing empty slug",
fields: fields{baseURL: "https://dummy.reamaze.io", auth: "dummy", httpClient: &http.Client{
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 Status OK",
Body: io.NopCloser(strings.NewReader(`{}`)),
}
}),
}},
args: args{slug: "", req: &CreateMessageRequest{Message: struct {
Body string "json:\"body\""
Visibility ReamazeVisibility "json:\"visibility,omitempty\""
OriginID string "json:\"origin_id,omitempty\""
User *struct {
Name string "json:\"name,omitempty\""
Email string "json:\"email,omitempty\""
} "json:\"user,omitempty\""
SupressNotification bool "json:\"suppress_notifications,omitempty\""
SupressAutoresolve bool "json:\"suppress_autoresolve,omitempty\""
Attachment string "json:\"attachment,omitempty\""
Attachments []string "json:\"attachments,omitempty\""
}{
Body: "dummy",
}}},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
baseURL: tt.fields.baseURL,
auth: tt.fields.auth,
httpClient: tt.fields.httpClient,
}
got, err := c.CreateMessage(tt.args.slug, tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("Client.CreateMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Client.CreateMessage() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3b1b349

Please sign in to comment.