forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_emails_test.go
156 lines (144 loc) · 3.58 KB
/
user_emails_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package backend
import (
"context"
"reflect"
"testing"
"time"
"github.com/sourcegraph/sourcegraph/cmd/frontend/db"
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/txemail"
"github.com/sourcegraph/sourcegraph/schema"
)
func Test_checkEmailAbuse(t *testing.T) {
ctx := testContext()
cfg := conf.Get()
cfg.EmailSmtp = &schema.SMTPServerConfig{}
conf.Mock(cfg)
defer func() {
cfg.EmailSmtp = nil
conf.Mock(cfg)
}()
envvar.MockSourcegraphDotComMode(true)
defer envvar.MockSourcegraphDotComMode(false)
now := time.Now()
tests := []struct {
name string
mockEmails []*db.UserEmail
hasQuote bool
expAbused bool
expReason string
expErr error
}{
{
name: "no verified email address",
mockEmails: []*db.UserEmail{
{
Email: "[email protected]",
},
},
hasQuote: false,
expAbused: true,
expReason: "a verified email is required before you can add additional email addressed to your account",
expErr: nil,
},
{
name: "reached maximum number of unverified email addresses",
mockEmails: []*db.UserEmail{
{
Email: "[email protected]",
VerifiedAt: &now,
},
{
Email: "[email protected]",
},
{
Email: "[email protected]",
},
{
Email: "[email protected]",
},
},
hasQuote: false,
expAbused: true,
expReason: "too many existing unverified email addresses",
expErr: nil,
},
{
name: "no quota",
mockEmails: []*db.UserEmail{
{
Email: "[email protected]",
VerifiedAt: &now,
},
},
hasQuote: false,
expAbused: true,
expReason: "email address quota exceeded (contact support to increase the quota)",
expErr: nil,
},
{
name: "no abuse",
mockEmails: []*db.UserEmail{
{
Email: "[email protected]",
VerifiedAt: &now,
},
},
hasQuote: true,
expAbused: false,
expReason: "",
expErr: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
db.Mocks.Users.CheckAndDecrementInviteQuota = func(context.Context, int32) (bool, error) {
return test.hasQuote, nil
}
db.Mocks.UserEmails.ListByUser = func(context.Context, db.UserEmailsListOptions) ([]*db.UserEmail, error) {
return test.mockEmails, nil
}
defer func() {
db.Mocks.Users.CheckAndDecrementInviteQuota = nil
db.Mocks.UserEmails.ListByUser = nil
}()
abused, reason, err := checkEmailAbuse(ctx, 1)
if test.expErr != err {
t.Fatalf("err: want %v but got %v", test.expErr, err)
} else if test.expAbused != abused {
t.Fatalf("abused: want %v but got %v", test.expAbused, abused)
} else if test.expReason != reason {
t.Fatalf("reason: want %q but got %q", test.expReason, reason)
}
})
}
}
func TestSendUserEmailVerificationEmail(t *testing.T) {
var sent *txemail.Message
txemail.MockSend = func(ctx context.Context, message txemail.Message) error {
sent = &message
return nil
}
defer func() { txemail.MockSend = nil }()
if err := SendUserEmailVerificationEmail(context.Background(), "[email protected]", "c"); err != nil {
t.Fatal(err)
}
if sent == nil {
t.Fatal("want sent != nil")
}
if want := (txemail.Message{
FromName: "",
To: []string{"[email protected]"},
Template: verifyEmailTemplates,
Data: struct {
Email string
URL string
}{
Email: "[email protected]",
URL: "http://example.com/-/verify-email?code=c&email=a%40example.com",
},
}); !reflect.DeepEqual(*sent, want) {
t.Errorf("got %+v, want %+v", *sent, want)
}
}