-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathauth_test.go
100 lines (85 loc) · 2.72 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package nakama
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"testing"
"github.com/nakamauwu/nakama/mailing"
"github.com/nakamauwu/nakama/testutil"
)
func TestService_SendMagicLink(t *testing.T) {
ctx := context.Background()
t.Run("empty_email", func(t *testing.T) {
svc := &Service{}
err := svc.SendMagicLink(ctx, SendMagicLink{})
testutil.WantEq(t, ErrInvalidEmail, err, "error")
})
t.Run("invalid_email", func(t *testing.T) {
svc := &Service{}
err := svc.SendMagicLink(ctx, SendMagicLink{Email: "nope"})
testutil.WantEq(t, ErrInvalidEmail, err, "error")
})
email := testutil.RandStr(t, 10) + "@example.org"
t.Run("empty_redirect_uri", func(t *testing.T) {
svc := &Service{}
err := svc.SendMagicLink(ctx, SendMagicLink{Email: email})
testutil.WantEq(t, ErrInvalidRedirectURI, err, "error")
})
t.Run("non_absolute_redirect_uri", func(t *testing.T) {
svc := &Service{}
err := svc.SendMagicLink(ctx, SendMagicLink{Email: email, RedirectURI: "/nope"})
testutil.WantEq(t, ErrInvalidRedirectURI, err, "error")
})
t.Run("untrusted_redirect_uri", func(t *testing.T) {
svc := &Service{
Origin: &url.URL{
Scheme: "http",
Host: "localhost:3000",
},
}
err := svc.SendMagicLink(ctx, SendMagicLink{Email: email, RedirectURI: "https://example.org/login-callback"})
testutil.WantEq(t, ErrUntrustedRedirectURI, err, "error")
})
redirectURI := "http://localhost:3000/login-callback"
origin := &url.URL{
Scheme: "http",
Host: "localhost:3000",
}
t.Run("sender_send_error", func(t *testing.T) {
errInternal := errors.New("internal error")
svc := &Service{
DB: testDB,
Origin: origin,
Sender: &mailing.SenderMock{
SendFunc: func(to, subject, html, text string) error {
return errInternal
},
},
}
err := svc.SendMagicLink(ctx, SendMagicLink{Email: email, RedirectURI: redirectURI})
testutil.WantEq(t, fmt.Errorf("could not send magic link: %w", errInternal), err, "error")
})
t.Run("ok", func(t *testing.T) {
senderMock := &mailing.SenderMock{
SendFunc: func(to, subject, html, text string) error {
return nil
},
}
svc := &Service{
DB: testDB,
Origin: origin,
Sender: senderMock,
}
err := svc.SendMagicLink(ctx, SendMagicLink{Email: email, RedirectURI: redirectURI})
testutil.WantEq(t, nil, err, "error")
calls := senderMock.SendCalls()
testutil.WantEq(t, 1, len(calls), "calls length")
call := calls[0]
testutil.WantEq(t, email, call.To, "sender send-to")
testutil.WantEq(t, "Login to Nakama", call.Subject, "sender send-subject")
testutil.WantEq(t, "text/html; charset=utf-8", http.DetectContentType([]byte(call.HTML)), "sender send-subject content type")
t.Logf("\nmagic link text:\n%s\n\n", call.Text)
})
}