Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create mocks helper #420

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/sendgrid/rest"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/sendgrid/sendgrid-go/helpers/mock"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like adding this dependency in non-dev environments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @thinkingserious, how are you?
Yes, I think the same but is only place where we could intercept the request
I haven't found other place where we could intercept the request with the Mocks.

Where we could intercept the request with the mocks?

Thank you very much and greetings!

Nahuel

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good, thanks for asking. I trust the same is true with you!

I think we should first create the interface per issue #421.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @thinkingserious

I created a SendGrid interface for test integration,
this example is in examples/mocks

package main

import (
	"fmt"
	"os"

	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
	"github.com/sendgrid/sendgrid-go/helpers/mock"
)

func main() {

	// start mocks server
	mock.StartTestServer()

	// add mock value
	mock.Add(&mock.Mock{
		StatusCode: 400,
		Body:       `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`,
	})


	simpleSendMail() // Response with mock data

	// stop mocks server
	mock.StopTestServer()

}

func simpleSendMail() {
	from := mail.NewEmail("Example User", "[email protected]")
	subject := "Sending with Twilio SendGrid is Fun"
	to := mail.NewEmail("Example User", "[email protected]")
	plainTextContent := "and easy to do anywhere, even with Go"
	htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
	message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
	
	// create mock client
	client := sendgrid.NewSendClientMock(os.Getenv("SENDGRID_API_KEY"))

	response, err := client.Send(message)
	if err != nil {
		fmt.Println("Simple Sengird Error: ")
		fmt.Println(err)
		fmt.Println("________________________________")
		fmt.Println()
	} else {
		fmt.Println("Simple Sengird Response: ")
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
		fmt.Println("________________________________")
		fmt.Println()
	}
}

)

// Version is this client library's current version
Expand All @@ -24,6 +25,16 @@ type options struct {
Subuser string
}

// SendGrid is a interface of diferents client type
type SendGrid interface {
Send(email *mail.SGMailV3) (*rest.Response, error)
}

// ClientMock for integration test
type ClientMock struct {
rest.Request
}

// Client is the Twilio SendGrid Go client
type Client struct {
rest.Request
Expand Down Expand Up @@ -58,6 +69,15 @@ func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
return MakeRequest(cl.Request)
}

// Send is a mocked to send mail
func (clm *ClientMock) Send(email *mail.SGMailV3) (*rest.Response, error) {
if mock.IsMocked() && mock.Get() != nil {
return mock.Request()
}

return nil, errors.New("The Mock has not been setting")
}

// DefaultClient is used if no custom HTTP client is defined
var DefaultClient = rest.DefaultClient

Expand All @@ -70,6 +90,11 @@ func API(request rest.Request) (*rest.Response, error) {

// MakeRequest attempts a Twilio SendGrid request synchronously.
func MakeRequest(request rest.Request) (*rest.Response, error) {

if mock.IsMocked() && mock.Get() != nil {
return mock.Request()
}

return DefaultClient.Send(request)
}

Expand Down
121 changes: 121 additions & 0 deletions examples/mocks/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/sendgrid/sendgrid-go/helpers/mock"
)

func main() {

sendMail() // Send Mail
simpleSendMail() // Response Error

// start mocks server
mock.StartTestServer()

// add mock value
mock.Add(&mock.Mock{
StatusCode: 400,
Body: `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`,
})
sendMail() // Response with mock data
simpleSendMail() // Response with mock data

// add mock value
mock.Add(&mock.Mock{
StatusCode: 200,
})
sendMail() // Response with mock data
simpleSendMail() // Response with mock data

// add mock value
mock.Add(&mock.Mock{
Err: errors.New("Has been an mock error"),
})

// stop mocks server
mock.StopTestServer()

sendMail() // Send Mail
simpleSendMail() // Response Error
// start mocks server
mock.StartTestServer()

sendMail() // Response with mock data
simpleSendMail() // Response with mock data

// clear mock
mock.Flush()
sendMail() // Send Mail
simpleSendMail() // Response Error

// stop mocks server
mock.StopTestServer()

}

func sendMail() {
m := mail.NewV3Mail()
content := mail.NewContent("text/html", "<h1>Hello world</h1>This is an example")

from := mail.NewEmail("envalidMail", "[email protected]")
m.SetFrom(from)

m.AddContent(content)

personalization := mail.NewPersonalization()

personalization.AddTos(mail.NewEmail("envalidMail", "[email protected]"))
personalization.Subject = "Example"

m.AddPersonalizations(personalization)

request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
response, err := sendgrid.API(request)

if err != nil {
fmt.Println("Sengird Error: ")
fmt.Println(err)
fmt.Println("________________________________")
fmt.Println()
return
}

fmt.Println("Sengird Response: ")
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
fmt.Println("________________________________")
fmt.Println()
}

func simpleSendMail() {
from := mail.NewEmail("Example User", "[email protected]")
subject := "Sending with Twilio SendGrid is Fun"
to := mail.NewEmail("Example User", "[email protected]")
plainTextContent := "and easy to do anywhere, even with Go"
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClientMock(os.Getenv("SENDGRID_API_KEY"))
response, err := client.Send(message)
if err != nil {
fmt.Println("Simple Sengird Error: ")
fmt.Println(err)
fmt.Println("________________________________")
fmt.Println()
} else {
fmt.Println("Simple Sengird Response: ")
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
fmt.Println("________________________________")
fmt.Println()
}
}
64 changes: 64 additions & 0 deletions helpers/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package mock

import "github.com/sendgrid/rest"

// Mock - Mock struct, has Response Code, Response Body and errors
type Mock struct {
StatusCode int
Body string
Err error
}

var (
mock *Mock
isMocked bool
)

// Add - add mock method
func Add(m *Mock) {
mock = m
}

// Flush - Flush mock method
func Flush() {
mock = nil
}

// Get - Get mock method
func Get() *Mock {
return mock
}

// StartTestServer - start mock server
func StartTestServer() {
isMocked = true
}

// StopTestServer - start mock server
func StopTestServer() {
isMocked = false
}

// IsMocked - return true if the mocks server was started
func IsMocked() bool {
return isMocked
}

// Request - return mock sengrid response and error
func Request() (*rest.Response, error) {

if mock == nil {
panic("There isn't a mock")
}

if mock.Err != nil {
return nil, mock.Err
}

r := &rest.Response{
StatusCode: mock.StatusCode,
Body: mock.Body,
}

return r, nil
}
11 changes: 9 additions & 2 deletions sendgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ func createSendGridRequest(sgOptions sendGridOptions) rest.Request {
}

// NewSendClient constructs a new Twilio SendGrid client given an API key
func NewSendClient(key string) *Client {
func NewSendClient(key string) SendGrid {
request := GetRequest(key, "/v3/mail/send", "")
request.Method = "POST"
return &Client{request}
}

// GetRequestSubuser like NewSendClient but with On-Behalf of Subuser
// @return [Client]
func NewSendClientSubuser(key, subuser string) *Client {
func NewSendClientSubuser(key, subuser string) SendGrid {
request := GetRequestSubuser(key, "/v3/mail/send", "", subuser)
request.Method = "POST"
return &Client{request}
}

// NewSendClientMock constructs a new mock client
func NewSendClientMock(key string) SendGrid {
request := GetRequest(key, "/v3/mail/send", "")
request.Method = "POST"
return &ClientMock{request}
}
110 changes: 110 additions & 0 deletions sendgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sendgrid

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -14,6 +15,7 @@ import (

"github.com/sendgrid/rest"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/sendgrid/sendgrid-go/helpers/mock"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -3742,3 +3744,111 @@ func Test_test_whitelabel_links__link_id__subuser_post(t *testing.T) {
}
assert.Equal(t, 200, response.StatusCode, "Wrong status code returned")
}

// send mail function example to used in Mock test
func sendMailExample() (code int, body string, err error) {
m := mail.NewV3Mail()

m.SetFrom(mail.NewEmail("Invalid From", "[email protected]"))
m.AddContent(mail.NewContent("text/html", "<h1>Hello world</h1>This is an example"))

personalization := mail.NewPersonalization()

// invalid mail
personalization.AddTos(mail.NewEmail("Nahuel", "nlcostamagna"))
personalization.Subject = "Example invalid email"

m.AddPersonalizations(personalization)

request := GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
response, err := API(request)

if err != nil {
return 0, "", err
}

return response.StatusCode, response.Body, nil
}

func TestMockStatusCode400(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
StatusCode: 400,
Body: `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`,
})

m := mock.Get()
code, body, err := sendMailExample()

assert.EqualValues(t, code, 400)
assert.EqualValues(t, m.StatusCode, 400)
assert.EqualValues(t, body, `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`)
assert.EqualValues(t, m.Body, `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`)
assert.Nil(t, err)
assert.Nil(t, m.Err)

mock.StopTestServer()
}

func TestMockStatusCode202(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
StatusCode: 202,
})

m := mock.Get()
code, body, err := sendMailExample()

assert.EqualValues(t, code, 202)
assert.EqualValues(t, m.StatusCode, 202)
assert.EqualValues(t, body, "")
assert.EqualValues(t, m.Body, "")
assert.Nil(t, err)
assert.Nil(t, m.Err)

mock.StopTestServer()
}

func TestMockErr(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
Err: errors.New("Has been an error in TestMockErr"),
})

m := mock.Get()
code, body, err := sendMailExample()

assert.EqualValues(t, err.Error(), "Has been an error in TestMockErr")
assert.EqualValues(t, m.Err.Error(), "Has been an error in TestMockErr")
assert.EqualValues(t, body, "")
assert.EqualValues(t, m.Body, "")
assert.EqualValues(t, code, 0)
assert.EqualValues(t, m.StatusCode, 0)

mock.StopTestServer()
}

func TestMockFlush(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
StatusCode: 202,
})
m1 := mock.Get()
mock.Flush()
m2 := mock.Get()

code, _, err := sendMailExample()

assert.EqualValues(t, m1.StatusCode, 202)
assert.EqualValues(t, code, 401)
assert.Nil(t, m2)
assert.Nil(t, err)

mock.StopTestServer()
}