Skip to content

Commit

Permalink
feat: enhanced test
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiireshetnik committed Jan 12, 2022
1 parent bc250e4 commit 15e1048
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion courier/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newSMS(ctx context.Context, deps Dependencies) *smsClient {
}

func (c *courier) QueueSMS(ctx context.Context, t SMSTemplate) (uuid.UUID, error) {
recipient, err := t.RecipientPhone()
recipient, err := t.PhoneNumber()
if err != nil {
return uuid.Nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion courier/sms_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type SMSTemplate interface {
json.Marshaler
SMSBody() (string, error)
RecipientPhone() (string, error)
PhoneNumber() (string, error)
}

func SMSTemplateType(t SMSTemplate) (TemplateType, error) {
Expand Down
6 changes: 3 additions & 3 deletions courier/sms_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNewSMSTemplateFromMessage(t *testing.T) {
conf := internal.NewConfigurationWithDefaults(t)
for tmplType, expectedTmpl := range map[courier.TemplateType]courier.SMSTemplate{
courier.TypeVerificationValid: sms.NewOTPMessage(conf, &sms.OTPMessageModel{To: "+12345678901"}),
courier.TypeTestStub: sms.NewTestStub(conf, &sms.TestStubModel{Phone: "+12345678901", Body: "test body"}),
courier.TypeTestStub: sms.NewTestStub(conf, &sms.TestStubModel{To: "+12345678901", Body: "test body"}),
} {
t.Run(fmt.Sprintf("case=%s", tmplType), func(t *testing.T) {
tmplData, err := json.Marshal(expectedTmpl)
Expand All @@ -41,9 +41,9 @@ func TestNewSMSTemplateFromMessage(t *testing.T) {

require.IsType(t, expectedTmpl, actualTmpl)

expectedRecipient, err := expectedTmpl.RecipientPhone()
expectedRecipient, err := expectedTmpl.PhoneNumber()
require.NoError(t, err)
actualRecipient, err := actualTmpl.RecipientPhone()
actualRecipient, err := actualTmpl.PhoneNumber()
require.NoError(t, err)
require.Equal(t, expectedRecipient, actualRecipient)

Expand Down
14 changes: 7 additions & 7 deletions courier/sms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestQueueSMS(t *testing.T) {
expectedSender := "Kratos Test"
expectedSMS := []*sms.TestStubModel{
{
Phone: "+12065550101",
Body: "test-sms-body-1",
To: "+12065550101",
Body: "test-sms-body-1",
},
{
Phone: "+12065550102",
Body: "test-sms-body-2",
To: "+12065550102",
Body: "test-sms-body-2",
},
}

Expand All @@ -54,8 +54,8 @@ func TestQueueSMS(t *testing.T) {

assert.Equal(t, body.From, expectedSender)
actual = append(actual, &sms.TestStubModel{
Phone: body.To,
Body: body.Body,
To: body.To,
Body: body.Body,
})
}))

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestQueueSMS(t *testing.T) {
for i, message := range actual {
expected := expectedSMS[i]

assert.Equal(t, expected.Phone, message.Phone)
assert.Equal(t, expected.To, message.To)
assert.Equal(t, fmt.Sprintf("stub sms body %s\n", expected.Body), message.Body)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Hi, please verify your account using following code:

{{ .VerificationCode }}
{{ .Code }}
2 changes: 1 addition & 1 deletion courier/template/sms/otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewOTPMessage(c template.Config, m *OTPMessageModel) *OTPMessage {
return &OTPMessage{c: c, m: m}
}

func (t *OTPMessage) RecipientPhone() (string, error) {
func (t *OTPMessage) PhoneNumber() (string, error) {
return t.m.To, nil
}

Expand Down
21 changes: 15 additions & 6 deletions courier/template/sms/otp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sms_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,15 +11,23 @@ import (
"github.com/ory/kratos/internal"
)

func TestVerifyValid(t *testing.T) {
func TestNewOTPMessage(t *testing.T) {
conf, _ := internal.NewFastRegistryWithMocks(t)
tpl := sms.NewOTPMessage(conf, &sms.OTPMessageModel{To: "+12345678901"})

rendered, err := tpl.SMSBody()
const (
expectedPhone = "+12345678901"
otp = "012345"
)

tpl := sms.NewOTPMessage(conf, &sms.OTPMessageModel{To: expectedPhone, Code: otp})

expectedBody := fmt.Sprintf("Hi, please verify your account using following code:\n\n%s\n", otp)

actualBody, err := tpl.SMSBody()
require.NoError(t, err)
assert.NotEmpty(t, rendered)
assert.Equal(t, expectedBody, actualBody)

rendered, err = tpl.RecipientPhone()
actualPhone, err := tpl.PhoneNumber()
require.NoError(t, err)
assert.NotEmpty(t, rendered)
assert.Equal(t, expectedPhone, actualPhone)
}
8 changes: 4 additions & 4 deletions courier/template/sms/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ type (
}

TestStubModel struct {
Phone string
Body string
To string
Body string
}
)

func NewTestStub(c template.Config, m *TestStubModel) *TestStub {
return &TestStub{c: c, m: m}
}

func (t *TestStub) RecipientPhone() (string, error) {
return t.m.Phone, nil
func (t *TestStub) PhoneNumber() (string, error) {
return t.m.To, nil
}

func (t *TestStub) SMSBody() (string, error) {
Expand Down
30 changes: 30 additions & 0 deletions courier/template/sms/stub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sms_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/kratos/courier/template/sms"
"github.com/ory/kratos/internal"
)

func TestNewTestStub(t *testing.T) {
conf, _ := internal.NewFastRegistryWithMocks(t)

const (
expectedPhone = "+12345678901"
expectedBody = "test sms"
)

tpl := sms.NewTestStub(conf, &sms.TestStubModel{To: expectedPhone, Body: expectedBody})

actualBody, err := tpl.SMSBody()
require.NoError(t, err)
assert.Equal(t, "stub sms body test sms\n", actualBody)

actualPhone, err := tpl.PhoneNumber()
require.NoError(t, err)
assert.Equal(t, expectedPhone, actualPhone)
}

0 comments on commit 15e1048

Please sign in to comment.