-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathrecover_test.go
More file actions
194 lines (154 loc) · 6.18 KB
/
recover_test.go
File metadata and controls
194 lines (154 loc) · 6.18 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package api
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/models"
)
type RecoverTestSuite struct {
suite.Suite
API *API
Config *conf.GlobalConfiguration
}
func TestRecover(t *testing.T) {
api, config, err := setupAPIForTest()
require.NoError(t, err)
ts := &RecoverTestSuite{
API: api,
Config: config,
}
defer api.db.Close()
suite.Run(t, ts)
}
func (ts *RecoverTestSuite) SetupTest() {
models.TruncateAll(ts.API.db)
// Create user
u, err := models.NewUser("", "test@example.com", "password", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error creating test user model")
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new test user")
}
func (ts *RecoverTestSuite) TestRecover_FirstRecovery() {
u, err := models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
u.RecoverySentAt = &time.Time{}
require.NoError(ts.T(), ts.API.db.Update(u))
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": "test@example.com",
}))
// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/recover", &buffer)
req.Header.Set("Content-Type", "application/json")
// Setup response recorder
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusOK, w.Code)
u, err = models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.WithinDuration(ts.T(), time.Now(), *u.RecoverySentAt, 1*time.Second)
}
func (ts *RecoverTestSuite) TestRecover_NoEmailSent() {
recoveryTime := time.Now().UTC().Add(-59 * time.Second)
u, err := models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
u.RecoverySentAt = &recoveryTime
require.NoError(ts.T(), ts.API.db.Update(u))
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": "test@example.com",
}))
// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/recover", &buffer)
req.Header.Set("Content-Type", "application/json")
// Setup response recorder
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusTooManyRequests, w.Code)
u, err = models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
// ensure it did not send a new email
u1 := recoveryTime.Round(time.Second).Unix()
u2 := u.RecoverySentAt.Round(time.Second).Unix()
assert.Equal(ts.T(), u1, u2)
}
func (ts *RecoverTestSuite) TestRecover_NewEmailSent() {
recoveryTime := time.Now().UTC().Add(-20 * time.Minute)
u, err := models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
u.RecoverySentAt = &recoveryTime
require.NoError(ts.T(), ts.API.db.Update(u))
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": "test@example.com",
}))
// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/recover", &buffer)
req.Header.Set("Content-Type", "application/json")
// Setup response recorder
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusOK, w.Code)
u, err = models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
// ensure it sent a new email
assert.WithinDuration(ts.T(), time.Now(), *u.RecoverySentAt, 1*time.Second)
}
func (ts *RecoverTestSuite) TestRecover_NoSideChannelLeak() {
email := "doesntexist@example.com"
_, err := models.FindUserByEmailAndAudience(ts.API.db, email, ts.Config.JWT.Aud)
require.True(ts.T(), models.IsNotFoundError(err), "User with email %s does exist", email)
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": email,
}))
// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/recover", &buffer)
req.Header.Set("Content-Type", "application/json")
// Setup response recorder
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusOK, w.Code)
}
func (ts *RecoverTestSuite) TestRecover_WithApostropheEmail() {
// Apostrophes are valid in the local part of email addresses per RFC 5321.
// Irish/UK names commonly use them (O'Sullivan, O'Brien, etc.).
// See: https://github.com/supabase/auth/issues/2329
email := "joe.o'sullivan@example.com"
// Create user with apostrophe in email
u, err := models.NewUser("", email, "password", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error creating test user model with apostrophe email")
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving test user with apostrophe email")
u, err = models.FindUserByEmailAndAudience(ts.API.db, email, ts.Config.JWT.Aud)
require.NoError(ts.T(), err, "Error finding user with apostrophe email")
u.RecoverySentAt = &time.Time{}
require.NoError(ts.T(), ts.API.db.Update(u))
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": email,
}))
// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/recover", &buffer)
req.Header.Set("Content-Type", "application/json")
// Setup response recorder
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusOK, w.Code)
u, err = models.FindUserByEmailAndAudience(ts.API.db, email, ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.WithinDuration(ts.T(), time.Now(), *u.RecoverySentAt, 1*time.Second)
// Verify the one-time token was created successfully
_, err = models.FindUserByConfirmationOrRecoveryToken(ts.API.db, u.RecoveryToken)
require.NoError(ts.T(), err, "Recovery token should be retrievable for apostrophe email user")
}