diff --git a/courier/sms.go b/courier/sms.go index d1e0f1bfc1d9..5d0b28321a95 100644 --- a/courier/sms.go +++ b/courier/sms.go @@ -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 } diff --git a/courier/sms_templates.go b/courier/sms_templates.go index 9446664f54d5..955630ff918b 100644 --- a/courier/sms_templates.go +++ b/courier/sms_templates.go @@ -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) { diff --git a/courier/sms_templates_test.go b/courier/sms_templates_test.go index a713f79abc01..caaf7ae63ee6 100644 --- a/courier/sms_templates_test.go +++ b/courier/sms_templates_test.go @@ -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) @@ -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) diff --git a/courier/sms_test.go b/courier/sms_test.go index e62cf8bb7e12..b705bf0490fa 100644 --- a/courier/sms_test.go +++ b/courier/sms_test.go @@ -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", }, } @@ -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, }) })) @@ -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) } diff --git a/courier/template/courier/builtin/templates/otp/sms.body.gotmpl b/courier/template/courier/builtin/templates/otp/sms.body.gotmpl index 928e1cfb8f38..a630a83b82db 100644 --- a/courier/template/courier/builtin/templates/otp/sms.body.gotmpl +++ b/courier/template/courier/builtin/templates/otp/sms.body.gotmpl @@ -1,3 +1,3 @@ Hi, please verify your account using following code: -{{ .VerificationCode }} +{{ .Code }} diff --git a/courier/template/sms/otp.go b/courier/template/sms/otp.go index 6ebacf8f5cb6..d55f31a93b94 100644 --- a/courier/template/sms/otp.go +++ b/courier/template/sms/otp.go @@ -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 } diff --git a/courier/template/sms/otp_test.go b/courier/template/sms/otp_test.go index 1b7854fbd9a5..51109e465c88 100644 --- a/courier/template/sms/otp_test.go +++ b/courier/template/sms/otp_test.go @@ -1,6 +1,7 @@ package sms_test import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -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) } diff --git a/courier/template/sms/stub.go b/courier/template/sms/stub.go index 00f85cbcfd59..89fb4370dd16 100644 --- a/courier/template/sms/stub.go +++ b/courier/template/sms/stub.go @@ -13,8 +13,8 @@ type ( } TestStubModel struct { - Phone string - Body string + To string + Body string } ) @@ -22,8 +22,8 @@ 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) { diff --git a/courier/template/sms/stub_test.go b/courier/template/sms/stub_test.go new file mode 100644 index 000000000000..95432b3feda9 --- /dev/null +++ b/courier/template/sms/stub_test.go @@ -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) +}