-
Notifications
You must be signed in to change notification settings - Fork 653
fix: add test for all sms providers #676
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
edd7f32
refactor: use custom HttpClient interface
kangmingtay e1beb19
test: add test for twilio SendSms
kangmingtay 8539746
use gock for mocking requests
kangmingtay 54ba0c6
Merge branch 'master' into km/test-sms-provider
kangmingtay e8c5455
test twilio error edge cases
kangmingtay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| package sms_provider | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/netlify/gotrue/conf" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/stretchr/testify/suite" | ||
| "gopkg.in/h2non/gock.v1" | ||
| ) | ||
|
|
||
| var handleApiRequest func(*http.Request) (*http.Response, error) | ||
|
|
||
| type SmsProviderTestSuite struct { | ||
| suite.Suite | ||
| Config *conf.GlobalConfiguration | ||
| } | ||
|
|
||
| type MockHttpClient struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *MockHttpClient) Do(req *http.Request) (*http.Response, error) { | ||
| return handleApiRequest(req) | ||
| } | ||
|
|
||
| func TestSmsProvider(t *testing.T) { | ||
| ts := &SmsProviderTestSuite{ | ||
| Config: &conf.GlobalConfiguration{ | ||
| Sms: conf.SmsProviderConfiguration{ | ||
| Twilio: conf.TwilioProviderConfiguration{ | ||
| AccountSid: "test_account_sid", | ||
| AuthToken: "test_auth_token", | ||
| MessageServiceSid: "test_message_service_id", | ||
| }, | ||
| Messagebird: conf.MessagebirdProviderConfiguration{ | ||
| AccessKey: "test_access_key", | ||
| Originator: "test_originator", | ||
| }, | ||
| Vonage: conf.VonageProviderConfiguration{ | ||
| ApiKey: "test_api_key", | ||
| ApiSecret: "test_api_secret", | ||
| From: "test_from", | ||
| }, | ||
| Textlocal: conf.TextlocalProviderConfiguration{ | ||
| ApiKey: "test_api_key", | ||
| Sender: "test_sender", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| suite.Run(t, ts) | ||
| } | ||
|
|
||
| func (ts *SmsProviderTestSuite) TestTwilioSendSms() { | ||
| defer gock.Off() | ||
| provider, err := NewTwilioProvider(ts.Config.Sms.Twilio) | ||
| require.NoError(ts.T(), err) | ||
|
|
||
| twilioProvider, ok := provider.(*TwilioProvider) | ||
| require.Equal(ts.T(), true, ok) | ||
|
|
||
| phone := "123456789" | ||
| message := "This is the sms code: 123456" | ||
|
|
||
| body := url.Values{ | ||
| "To": {"+" + phone}, | ||
| "Channel": {"sms"}, | ||
| "From": {twilioProvider.Config.MessageServiceSid}, | ||
| "Body": {message}, | ||
| } | ||
|
|
||
| cases := []struct { | ||
| Desc string | ||
| TwilioResponse *gock.Response | ||
| ExpectedError error | ||
| }{ | ||
| { | ||
| Desc: "Successfully sent sms", | ||
| TwilioResponse: gock.New(twilioProvider.APIPath).Post(""). | ||
| MatchHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(twilioProvider.Config.AccountSid+":"+twilioProvider.Config.AuthToken))). | ||
| MatchType("url").BodyString(body.Encode()). | ||
| Reply(200).JSON(SmsStatus{ | ||
| To: "+" + phone, | ||
| From: twilioProvider.Config.MessageServiceSid, | ||
| Status: "sent", | ||
| Body: message, | ||
| }), | ||
| ExpectedError: nil, | ||
| }, | ||
| { | ||
| Desc: "Sms status is failed / undelivered", | ||
| TwilioResponse: gock.New(twilioProvider.APIPath).Post(""). | ||
| MatchHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(twilioProvider.Config.AccountSid+":"+twilioProvider.Config.AuthToken))). | ||
| MatchType("url").BodyString(body.Encode()). | ||
| Reply(200).JSON(SmsStatus{ | ||
| ErrorMessage: "failed to send sms", | ||
| ErrorCode: "401", | ||
| Status: "failed", | ||
| }), | ||
| ExpectedError: fmt.Errorf("twilio error: %v %v", "failed to send sms", "401"), | ||
| }, | ||
| { | ||
| Desc: "Non-2xx status code returned", | ||
| TwilioResponse: gock.New(twilioProvider.APIPath).Post(""). | ||
| MatchHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(twilioProvider.Config.AccountSid+":"+twilioProvider.Config.AuthToken))). | ||
| MatchType("url").BodyString(body.Encode()). | ||
| Reply(500).JSON(twilioErrResponse{ | ||
| Code: 500, | ||
| Message: "Internal server error", | ||
| MoreInfo: "error", | ||
| Status: 500, | ||
| }), | ||
| ExpectedError: &twilioErrResponse{ | ||
| Code: 500, | ||
| Message: "Internal server error", | ||
| MoreInfo: "error", | ||
| Status: 500, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| ts.Run(c.Desc, func() { | ||
| err = twilioProvider.SendSms(phone, message) | ||
| require.Equal(ts.T(), c.ExpectedError, err) | ||
| }) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another good check at the end of a test is I also find its default logging a little unclear, so I created a helper to print out unmatched requests. |
||
| } | ||
|
|
||
| func (ts *SmsProviderTestSuite) TestMessagebirdSendSms() { | ||
| defer gock.Off() | ||
| provider, err := NewMessagebirdProvider(ts.Config.Sms.Messagebird) | ||
| require.NoError(ts.T(), err) | ||
|
|
||
| messagebirdProvider, ok := provider.(*MessagebirdProvider) | ||
| require.Equal(ts.T(), true, ok) | ||
|
|
||
| phone := "123456789" | ||
| message := "This is the sms code: 123456" | ||
| body := url.Values{ | ||
| "originator": {messagebirdProvider.Config.Originator}, | ||
| "body": {message}, | ||
| "recipients": {phone}, | ||
| "type": {"sms"}, | ||
| "datacoding": {"unicode"}, | ||
| } | ||
| gock.New(messagebirdProvider.APIPath).Post("").MatchHeader("Authorization", "AccessKey "+messagebirdProvider.Config.AccessKey).MatchType("url").BodyString(body.Encode()).Reply(200).JSON(MessagebirdResponse{ | ||
| Recipients: MessagebirdResponseRecipients{ | ||
| TotalSentCount: 1, | ||
| }, | ||
| }) | ||
|
|
||
| err = messagebirdProvider.SendSms(phone, message) | ||
| require.NoError(ts.T(), err) | ||
| } | ||
|
|
||
| func (ts *SmsProviderTestSuite) TestVonageSendSms() { | ||
| defer gock.Off() | ||
| provider, err := NewVonageProvider(ts.Config.Sms.Vonage) | ||
| require.NoError(ts.T(), err) | ||
|
|
||
| vonageProvider, ok := provider.(*VonageProvider) | ||
| require.Equal(ts.T(), true, ok) | ||
|
|
||
| phone := "123456789" | ||
| message := "This is the sms code: 123456" | ||
|
|
||
| body := url.Values{ | ||
| "from": {vonageProvider.Config.From}, | ||
| "to": {phone}, | ||
| "text": {message}, | ||
| "api_key": {vonageProvider.Config.ApiKey}, | ||
| "api_secret": {vonageProvider.Config.ApiSecret}, | ||
| } | ||
|
|
||
| gock.New(vonageProvider.APIPath).Post("").MatchType("url").BodyString(body.Encode()).Reply(200).JSON(VonageResponse{ | ||
| Messages: []VonageResponseMessage{ | ||
| {Status: "0"}, | ||
| }, | ||
| }) | ||
|
|
||
| err = vonageProvider.SendSms(phone, message) | ||
| require.NoError(ts.T(), err) | ||
| } | ||
|
|
||
| func (ts *SmsProviderTestSuite) TestTextLocalSendSms() { | ||
| defer gock.Off() | ||
| provider, err := NewTextlocalProvider(ts.Config.Sms.Textlocal) | ||
| require.NoError(ts.T(), err) | ||
|
|
||
| textlocalProvider, ok := provider.(*TextlocalProvider) | ||
| require.Equal(ts.T(), true, ok) | ||
|
|
||
| phone := "123456789" | ||
| message := "This is the sms code: 123456" | ||
| body := url.Values{ | ||
| "sender": {textlocalProvider.Config.Sender}, | ||
| "apikey": {textlocalProvider.Config.ApiKey}, | ||
| "message": {message}, | ||
| "numbers": {phone}, | ||
| } | ||
|
|
||
| gock.New(textlocalProvider.APIPath).Post("").MatchType("url").BodyString(body.Encode()).Reply(200).JSON(TextlocalResponse{ | ||
| Status: "success", | ||
| Errors: []TextlocalError{}, | ||
| }) | ||
|
|
||
| err = textlocalProvider.SendSms(phone, message) | ||
| require.NoError(ts.T(), err) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually use
defer gock.OffAll()so that unmatched requests don't carry over to the next test. https://pkg.go.dev/github.com/h2non/gock#OffAll