Skip to content

Commit

Permalink
test: login and registration for sms
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Sep 27, 2024
1 parent 3a51321 commit dfa858c
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 69 deletions.
36 changes: 8 additions & 28 deletions courier/courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,22 @@ type (
}

courier struct {
courierChannels map[string]Channel
deps Dependencies
failOnDispatchError bool
backoff backoff.BackOff
deps Dependencies
failOnDispatchError bool
backoff backoff.BackOff
newEmailTemplateFromMessage func(d template.Dependencies, msg Message) (EmailTemplate, error)
}
)

func NewCourier(ctx context.Context, deps Dependencies) (Courier, error) {
return NewCourierWithCustomTemplates(ctx, deps, NewEmailTemplateFromMessage)
}

func NewCourierWithCustomTemplates(ctx context.Context, deps Dependencies, newEmailTemplateFromMessage func(d template.Dependencies, msg Message) (EmailTemplate, error)) (Courier, error) {
cs, err := deps.CourierConfig().CourierChannels(ctx)
if err != nil {
return nil, err
}
channels := make(map[string]Channel, len(cs))
for _, c := range cs {
switch c.Type {
case "smtp":
ch, err := NewSMTPChannelWithCustomTemplates(deps, c.SMTPConfig, newEmailTemplateFromMessage)
if err != nil {
return nil, err
}
channels[ch.ID()] = ch
case "http":
channels[c.ID] = newHttpChannel(c.ID, c.RequestConfig, deps)
default:
return nil, errors.Errorf("unknown courier channel type: %s", c.Type)
}
}

func NewCourierWithCustomTemplates(_ context.Context, deps Dependencies, newEmailTemplateFromMessage func(d template.Dependencies, msg Message) (EmailTemplate, error)) (Courier, error) {
return &courier{
deps: deps,
backoff: backoff.NewExponentialBackOff(),
courierChannels: channels,
deps: deps,
backoff: backoff.NewExponentialBackOff(),
newEmailTemplateFromMessage: newEmailTemplateFromMessage,
}, nil
}

Expand Down
36 changes: 33 additions & 3 deletions courier/courier_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ import (
"github.com/pkg/errors"
)

func (c *courier) channels(ctx context.Context, id string) (Channel, error) {
cs, err := c.deps.CourierConfig().CourierChannels(ctx)
if err != nil {
return nil, err

Check warning on line 15 in courier/courier_dispatcher.go

View check run for this annotation

Codecov / codecov/patch

courier/courier_dispatcher.go#L15

Added line #L15 was not covered by tests
}

for _, channel := range cs {
if channel.ID != id {
continue
}
switch channel.Type {
case "smtp":
courierChannel, err := NewSMTPChannelWithCustomTemplates(c.deps, channel.SMTPConfig, c.newEmailTemplateFromMessage)
if err != nil {
return nil, err

Check warning on line 26 in courier/courier_dispatcher.go

View check run for this annotation

Codecov / codecov/patch

courier/courier_dispatcher.go#L26

Added line #L26 was not covered by tests
}
return courierChannel, nil
case "http":
return newHttpChannel(channel.ID, channel.RequestConfig, c.deps), nil
default:
return nil, errors.Errorf("unknown courier channel type: %s", channel.Type)

Check warning on line 32 in courier/courier_dispatcher.go

View check run for this annotation

Codecov / codecov/patch

courier/courier_dispatcher.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}
}

return nil, errors.Errorf("no courier channels configured")
}

func (c *courier) DispatchMessage(ctx context.Context, msg Message) error {
logger := c.deps.Logger().
WithField("message_id", msg.ID).
Expand All @@ -24,9 +51,9 @@ func (c *courier) DispatchMessage(ctx context.Context, msg Message) error {
return err
}

channel, ok := c.courierChannels[msg.Channel.String()]
if !ok {
return errors.Errorf("message %s has unknown channel %q", msg.ID.String(), msg.Channel)
channel, err := c.channels(ctx, msg.Channel.String())
if err != nil {
return err
}

logger = logger.
Expand Down Expand Up @@ -80,6 +107,9 @@ func (c *courier) DispatchQueue(ctx context.Context) error {
logger.
Warnf(`Message was abandoned because it did not deliver after %d attempts`, msg.SendCount)
} else if err := c.DispatchMessage(ctx, msg); err != nil {
logger.
WithError(err).
Warn(`Unable to dispatch message.`)
if err := c.deps.CourierPersister().RecordDispatch(ctx, msg.ID, CourierMessageDispatchStatusFailed, err); err != nil {
logger.
WithError(err).
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/mock/httptarget/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/ory/mock

go 1.22.1

require (
github.com/julienschmidt/httprouter v1.3.0
github.com/ory/graceful v0.1.3
)

require github.com/pkg/errors v0.9.1 // indirect
16 changes: 16 additions & 0 deletions test/e2e/mock/httptarget/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/ory/graceful v0.1.3 h1:FaeXcHZh168WzS+bqruqWEw/HgXWLdNv2nJ+fbhxbhc=
github.com/ory/graceful v0.1.3/go.mod h1:4zFz687IAF7oNHHiB586U4iL+/4aV09o/PYLE34t2bA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
79 changes: 79 additions & 0 deletions test/e2e/mock/httptarget/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package main

import (
"cmp"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"

"github.com/julienschmidt/httprouter"

"github.com/ory/graceful"
)

var (
documentsLock sync.RWMutex
documents = make(map[string][]byte)
)

func main() {
port := cmp.Or(os.Getenv("PORT"), "4471")
server := graceful.WithDefaults(&http.Server{Addr: fmt.Sprintf(":%s", port)})
register(server)
if err := graceful.Graceful(server.ListenAndServe, server.Shutdown); err != nil {
log.Fatalln(err)
}
}

func register(server *http.Server) {
router := httprouter.New()

router.GET("/health", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
_, _ = w.Write([]byte("OK"))
})

router.GET("/documents/:id", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.ByName("id")

documentsLock.RLock()
doc, ok := documents[id]
documentsLock.RUnlock()

if ok {
_, _ = w.Write(doc)
} else {
w.WriteHeader(http.StatusNotFound)
}
})

router.PUT("/documents/:id", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
documentsLock.Lock()
defer documentsLock.Unlock()
id := ps.ByName("id")

body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

documents[id] = body
})

router.DELETE("/documents/:id", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
documentsLock.Lock()
defer documentsLock.Unlock()
id := ps.ByName("id")

delete(documents, id)
w.WriteHeader(http.StatusNoContent)
})

server.Handler = router
}
Loading

0 comments on commit dfa858c

Please sign in to comment.