Skip to content

Commit

Permalink
test: add tests for new sms options
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Feb 23, 2022
1 parent 65e42e5 commit 799fa10
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
13 changes: 8 additions & 5 deletions courier/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package courier
import (
"context"
"encoding/json"
"errors"
"net/http"

"github.com/pkg/errors"

"github.com/ory/herodot"

"github.com/gofrs/uuid"

"github.com/ory/kratos/request"
Expand All @@ -25,10 +28,6 @@ type smsClient struct {
}

func newSMS(ctx context.Context, deps Dependencies) *smsClient {
if !deps.CourierConfig(ctx).CourierSMSEnabled() {
deps.Logger().Error("messages will not be sent - no sms gate server address is set in config")
}

return &smsClient{
RequestConfig: deps.CourierConfig(ctx).CourierSMSRequestConfig(),

Expand Down Expand Up @@ -68,6 +67,10 @@ func (c *courier) QueueSMS(ctx context.Context, t SMSTemplate) (uuid.UUID, error
}

func (c *courier) dispatchSMS(ctx context.Context, msg Message) error {
if !c.deps.CourierConfig(ctx).CourierSMSEnabled() {
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an sms but courier.sms.enabled is set to false!"))
}

tmpl, err := c.smsClient.NewTemplateFromMessage(c.deps, msg)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion courier/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"
"time"

"github.com/ory/kratos/driver/config"

"github.com/gofrs/uuid"
"github.com/pkg/errors"

Expand Down Expand Up @@ -117,7 +119,7 @@ func (c *courier) QueueEmail(ctx context.Context, t EmailTemplate) (uuid.UUID, e

func (c *courier) dispatchEmail(ctx context.Context, msg Message) error {
if c.smtpClient.Host == "" {
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an email but courier.smtp_url is not set!"))
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an email but %s is not set!", config.ViperKeyCourierSMTPURL))
}

from := c.deps.CourierConfig(ctx).CourierSMTPFrom()
Expand Down
15 changes: 15 additions & 0 deletions driver/config/.snapshots/TestCourierSMS-case=configs_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"auth": {
"config": {
"password": "YourPass",
"user": "YourUsername"
},
"type": "basic_auth"
},
"body": "base64://e30=",
"header": {
"Content-Type": "application/x-www-form-urlencoded"
},
"method": "POST",
"url": "https://api.twilio.com/2010-04-01/Accounts/YourAccountID/Messages.json"
}
1 change: 1 addition & 0 deletions driver/config/.snapshots/TestCourierSMS-case=defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
22 changes: 22 additions & 0 deletions driver/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"testing"
"time"

"github.com/ory/x/snapshotx"

"github.com/ghodss/yaml"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -1046,6 +1048,26 @@ func TestChangeMinPasswordLength(t *testing.T) {
})
}

func TestCourierSMS(t *testing.T) {
ctx := context.Background()

t.Run("case=configs set", func(t *testing.T) {
conf, _ := config.New(ctx, logrusx.New("", ""), os.Stderr,
configx.WithConfigFiles("stub/.kratos.courier.sms.yaml"), configx.SkipValidation())
assert.True(t, conf.CourierSMSEnabled())
snapshotx.SnapshotTExcept(t, conf.CourierSMSRequestConfig(), nil)
assert.Equal(t, "+49123456789", conf.CourierSMSFrom())
})

t.Run("case=defaults", func(t *testing.T) {
conf, _ := config.New(ctx, logrusx.New("", ""), os.Stderr, configx.SkipValidation())

assert.False(t, conf.CourierSMSEnabled())
snapshotx.SnapshotTExcept(t, conf.CourierSMSRequestConfig(), nil)
assert.Equal(t, "Ory Kratos", conf.CourierSMSFrom())
})
}

func TestCourierTemplatesConfig(t *testing.T) {
ctx := context.Background()

Expand Down
28 changes: 28 additions & 0 deletions driver/config/stub/.kratos.courier.sms.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
dsn: sqlite://foo.db?mode=memory&_fk=true

selfservice:
default_browser_return_url: http://return-to-3-test.ory.sh/

identity:
default_schema_id: default
schemas:
- id: default
url: base64://ewogICIkaWQiOiAib3J5Oi8vaWRlbnRpdHktdGVzdC1zY2hlbWEiLAogICIkc2NoZW1hIjogImh0dHA6Ly9qc29uLXNjaGVtYS5vcmcvZHJhZnQtMDcvc2NoZW1hIyIsCiAgInRpdGxlIjogIklkZW50aXR5U2NoZW1hIiwKICAidHlwZSI6ICJvYmplY3QiLAogICJwcm9wZXJ0aWVzIjogewogICAgInRyYWl0cyI6IHsKICAgICAgInR5cGUiOiAib2JqZWN0IiwKICAgICAgInByb3BlcnRpZXMiOiB7CiAgICAgICAgIm5hbWUiOiB7CiAgICAgICAgICAidHlwZSI6ICJvYmplY3QiLAogICAgICAgICAgInByb3BlcnRpZXMiOiB7CiAgICAgICAgICAgICJmaXJzdCI6IHsKICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciCiAgICAgICAgICAgIH0sCiAgICAgICAgICAgICJsYXN0IjogewogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfSwKICAgICAgInJlcXVpcmVkIjogWwogICAgICAgICJuYW1lIgogICAgICBdLAogICAgICAiYWRkaXRpb25hbFByb3BlcnRpZXMiOiB0cnVlCiAgICB9CiAgfQp9

courier:
smtp:
connection_uri: smtp://foo:bar@baz/
sms:
enabled: true
from: '+49123456789'
request_config:
url: https://api.twilio.com/2010-04-01/Accounts/YourAccountID/Messages.json
method: POST
body: base64://e30=
header:
'Content-Type': 'application/x-www-form-urlencoded'
auth:
type: basic_auth
config:
user: YourUsername
password: YourPass

0 comments on commit 799fa10

Please sign in to comment.