Skip to content

Commit

Permalink
feat(smtpmock): refact server and add auth, create command line client (
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Jan 8, 2020
1 parent 08e3ef2 commit b3a6fb7
Show file tree
Hide file tree
Showing 27 changed files with 694 additions and 213 deletions.
Binary file not shown.
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ require (
github.com/marstr/guid v1.1.0 // indirect
github.com/maruel/panicparse v1.3.0
github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b // indirect

github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
github.com/mattn/go-runewidth v0.0.1 // indirect
github.com/mattn/go-sqlite3 v1.9.0
github.com/mattn/go-zglob v0.0.1
Expand Down Expand Up @@ -183,7 +180,6 @@ require (
github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/tebeka/go2xunit v1.4.10 // indirect
github.com/tevino/abool v0.0.0-20170917061928-9b9efcf221b5
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 // indirect
github.com/ugorji/go v1.1.7 // indirect
Expand All @@ -200,7 +196,7 @@ require (
github.com/ziutek/mymysql v1.5.4 // indirect
go.etcd.io/bbolt v1.3.3 // indirect
go.opencensus.io v0.22.0
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a
Expand Down
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,11 @@ github.com/maruel/panicparse v1.3.0 h1:1Ep/RaYoSL1r5rTILHQQbyzHG8T4UP5ZbQTYTo4bd
github.com/maruel/panicparse v1.3.0/go.mod h1:vszMjr5QQ4F5FSRfraldcIA/BCw5xrdLL+zEcU2nRBs=
github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b h1:v29yPGHhOqw7VHEnTeQFAth3SsBrmwc8JfuhNY0G34k=
github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b/go.mod h1:5MWrJXKRQyhQdUCF+vu6U5c4nQpg70vW3eHaU0/AYbU=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-runewidth v0.0.1 h1:+EiaBVXhogb1Klb4tRJ7hYnuGK6PkKOZlK04D/GMOqk=
github.com/mattn/go-runewidth v0.0.1/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
Expand Down Expand Up @@ -528,8 +525,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tebeka/go2xunit v1.4.10 h1:0UO+9YoLpXTZ0DL9XbTmIIibgmKBGiwroo8uhFMSyR0=
github.com/tebeka/go2xunit v1.4.10/go.mod h1:wmc9jKT7KlU4QLU6DNTaIXNnYNOjKKNlp6mjOS0UrqY=
github.com/tevino/abool v0.0.0-20170917061928-9b9efcf221b5 h1:hNna6Fi0eP1f2sMBe/rJicDmaHmoXGe1Ta84FPYHLuE=
github.com/tevino/abool v0.0.0-20170917061928-9b9efcf221b5/go.mod h1:f1SCnEOt6sc3fOJfPQDRDzHOtSXuTtnz0ImG9kPRDV0=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
Expand Down Expand Up @@ -588,6 +583,8 @@ golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 h1:Gv7RPwsi3eZ2Fgewe3CBsuOebPwO27PoXzRpJPsvSSM=
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down
16 changes: 16 additions & 0 deletions tools/smtpmock/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# build stage
FROM golang:1.13.4 AS builder
ADD . $GOPATH/src/github.com/ovh/cds/tools/smtpmock
WORKDIR $GOPATH/src/github.com/ovh/cds/tools/smtpmock
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /tmp/smtpmocksrv github.com/ovh/cds/tools/smtpmock/server
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /tmp/smtpmockcli github.com/ovh/cds/tools/smtpmock/cli

# final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /tmp/smtpmocksrv ./
COPY --from=builder /tmp/smtpmockcli ./
RUN chmod +x ./smtpmocksrv
ENTRYPOINT ["./smtpmocksrv"]
CMD ["start"]
EXPOSE 2023 2024
1 change: 1 addition & 0 deletions tools/smtpmock/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
smtpmockcli
104 changes: 104 additions & 0 deletions tools/smtpmock/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/pkg/errors"
"github.com/urfave/cli/v2"

"github.com/ovh/cds/tools/smtpmock"
)

var client smtpmock.Client

func main() {
flags := []cli.Flag{
&cli.StringFlag{
Name: "api-url",
Value: "http://localhost:2024",
EnvVars: []string{"SMTPMOCK_API_URL"},
},
&cli.StringFlag{
Name: "signin-token",
EnvVars: []string{"SMTPMOCK_SIGNIN_TOKEN"},
},
}

app := &cli.App{
Commands: []*cli.Command{
{
Name: "messages",
Usage: "Get messages",
Action: messages,
},
{
Name: "recipient-messages",
Usage: "Get messages for a recipient",
Action: recipientMessages,
},
{
Name: "recipient-latest-message",
Usage: "Get latest message for a recipient",
Action: recipientLatestMessage,
},
},
Flags: flags,
Before: func(ctx *cli.Context) error {
client = smtpmock.NewClient(ctx.String("api-url"))

token := ctx.String("signin-token")
if token != "" {
if _, err := client.Signin(token); err != nil {
return err
}
}

return nil
},
}

if err := app.Run(os.Args); err != nil {
fmt.Printf("%v\n", err)
}
}

func messages(ctx *cli.Context) error {
ms, err := client.GetMessages()
if err != nil {
return err
}
return printJSON(ms)
}

func recipientMessages(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return errors.New("missing recipient email")
}
ms, err := client.GetRecipientMessages(ctx.Args().First())
if err != nil {
return err
}
return printJSON(ms)
}

func recipientLatestMessage(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return errors.New("missing recipient email")
}
m, err := client.GetRecipientLatestMessage(ctx.Args().First())
if err != nil {
return err
}
return printJSON(m)
}

func printJSON(i interface{}) error {
buf, err := json.Marshal(i)
if err != nil {
return errors.WithStack(err)
}
fmt.Println(string(buf))
return nil
}
97 changes: 97 additions & 0 deletions tools/smtpmock/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package smtpmock

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
)

func NewClient(url string) Client {
return &client{url: url}
}

type Client interface {
Signin(token string) (SigninResponse, error)
GetMessages() ([]Message, error)
GetRecipientMessages(email string) ([]Message, error)
GetRecipientLatestMessage(email string) (Message, error)
}

type client struct {
url string
sessionToken string
}

func (c *client) requestJSON(method string, url string, body io.Reader, data interface{}) error {
req, err := http.NewRequest(method, url, body)
if err != nil {
return errors.WithStack(err)
}

req.Header.Add("Content-Type", "application/json")

if c.sessionToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.sessionToken))
}

res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.WithStack(err)
}
if res.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("error request at %s %s", method, req.URL.String()))
}

buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.WithStack(err)
}

return errors.WithStack(json.Unmarshal(buf, data))
}

func (c *client) Signin(token string) (SigninResponse, error) {
var res SigninResponse

buf, err := json.Marshal(SigninRequest{
SigninToken: token,
})
if err != nil {
return res, errors.WithStack(err)
}

if err := c.requestJSON(http.MethodPost, c.url+"/signin", bytes.NewBuffer(buf), &res); err != nil {
return res, err
}
c.sessionToken = res.SessionToken
return res, nil
}

func (c *client) GetMessages() ([]Message, error) {
var res []Message
if err := c.requestJSON(http.MethodGet, c.url+"/messages", nil, &res); err != nil {
return res, err
}
return res, nil
}

func (c *client) GetRecipientMessages(email string) ([]Message, error) {
var res []Message
if err := c.requestJSON(http.MethodGet, fmt.Sprintf("%s/messages/%s", c.url, email), nil, &res); err != nil {
return res, err
}
return res, nil
}

func (c *client) GetRecipientLatestMessage(email string) (Message, error) {
var res Message
if err := c.requestJSON(http.MethodGet, fmt.Sprintf("%s/messages/%s/latest", c.url, email), nil, &res); err != nil {
return res, err
}
return res, nil
}
1 change: 0 additions & 1 deletion tools/smtpmock/cmd/smtpmockcli/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions tools/smtpmock/cmd/smtpmockcli/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions tools/smtpmock/cmd/smtpmockcli/go.sum

This file was deleted.

24 changes: 0 additions & 24 deletions tools/smtpmock/cmd/smtpmockcli/main.go

This file was deleted.

24 changes: 0 additions & 24 deletions tools/smtpmock/cmd/smtpmockcli/smtpmock.go

This file was deleted.

1 change: 0 additions & 1 deletion tools/smtpmock/cmd/smtpmocksrv/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions tools/smtpmock/cmd/smtpmocksrv/Dockerfile

This file was deleted.

Loading

0 comments on commit b3a6fb7

Please sign in to comment.