-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhelpers_test.go
More file actions
179 lines (154 loc) · 3.95 KB
/
helpers_test.go
File metadata and controls
179 lines (154 loc) · 3.95 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
package users
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/rivo/sessions"
)
type MyUser struct {
id string
email string
passwordHash []byte
state int
verificationID string
vidCreated time.Time
passwordToken string
tokenCreated time.Time
}
func (u *MyUser) GetID() interface{} {
return u.id
}
func (u *MyUser) SetID(id interface{}) {
u.id = id.(string)
}
func (u *MyUser) SetState(state int) {
u.state = state
}
func (u *MyUser) GetState() int {
return u.state
}
func (u *MyUser) SetEmail(email string) {
u.email = email
}
func (u *MyUser) GetEmail() string {
return u.email
}
func (u *MyUser) SetPasswordHash(hash []byte) {
u.passwordHash = hash
}
func (u *MyUser) GetPasswordHash() []byte {
return u.passwordHash
}
func (u *MyUser) SetVerificationID(id string, created time.Time) {
u.verificationID = id
u.vidCreated = created
}
func (u *MyUser) GetVerificationID() (string, time.Time) {
return u.verificationID, u.vidCreated
}
func (u *MyUser) SetPasswordToken(id string, created time.Time) {
u.passwordToken = id
u.tokenCreated = created
}
func (u *MyUser) GetPasswordToken() (string, time.Time) {
return u.passwordToken, u.tokenCreated
}
func (u *MyUser) GetRoles() []string {
return nil
}
func TestMain(m *testing.M) {
Config.HTMLTemplateDir = "test"
Config.MailTemplateDir = "test"
Config.Log = log.New(ioutil.Discard, "", 0)
Config.NewUser = func() User {
return &MyUser{
id: sessions.CUID(),
}
}
sessions.AcceptChangingUserAgent = true
os.Exit(m.Run())
}
// Runs a request on the given handler and returns the results. The "get" and
// "post" arguments may be nil. If "post" is not nil, the request method will be
// POST, otherwise GET. If "user" is not nil, they are logged in first.
//
// The function returns the response body as well as the email text that was
// sent (if any).
func runRequest(user User, get, post map[string]string, handler func(response http.ResponseWriter, request *http.Request)) (string, string) {
// Make HTTP request.
method := "GET"
var reqBody string
u := "/test"
if get != nil {
values := url.Values{}
for key, value := range get {
values.Add(key, value)
}
u += "?" + values.Encode()
}
if post != nil {
method = "POST"
values := url.Values{}
for key, value := range post {
values.Add(key, value)
}
reqBody = values.Encode()
}
request := httptest.NewRequest(method, u, strings.NewReader(reqBody))
if post != nil {
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
// Make HTTP response.
response := httptest.NewRecorder()
// Log in user if required.
if user != nil {
sessions.PurgeSessions()
request.AddCookie(&http.Cookie{
Name: "id",
Value: "01234567890123456789----",
})
sessions.Persistence = sessions.ExtendablePersistenceLayer{
LoadSessionFunc: func(id string) (*sessions.Session, error) {
now := time.Now().Format(time.RFC3339)
serialized := fmt.Sprintf(`{"cr":"%s","da":{},"ip":"192.168.178.1:80","la":"%s","rf":"","ua":"0","us":"abcd","v":1}`, now, now)
session := &sessions.Session{}
if err := json.Unmarshal([]byte(serialized), session); err != nil {
panic(err)
}
return session, nil
},
LoadUserFunc: func(id interface{}) (sessions.User, error) {
return user, nil
},
}
}
// Set up email handler.
var email string
Config.SendEmails = true
Config.SendEmail = func(recipient, subject, body string) error {
email = body
return nil
}
// Run the handler.
handler(response, request)
Config.SendEmails = false
if response.Code == 302 {
return "redirect", email
}
return response.Body.String(), strings.TrimSpace(email)
}
// Outputs an error if the expected and computed strings don't match.
func assertString(expected, computed string, t *testing.T) {
t.Helper()
if expected != computed {
t.Errorf(`Expected "%s" but is "%s"`, expected, computed)
}
}