This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
user_test.go
188 lines (155 loc) · 4.47 KB
/
user_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
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
package gosaas
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"
"github.com/dstpierre/gosaas/model"
)
func logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
dr, err := httputil.DumpRequest(r, true)
if err != nil {
log.Println("unable to dump request", err)
} else {
ctx = context.WithValue(ctx, ContextRequestDump, dr)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func authenticator(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
key, _, err := extractKeyFromRequest(r)
if err != nil {
// we simulate the way the real authenticator middleware
// act by simply doing nothing for public route
mr := ctx.Value(ContextMinimumRole).(model.Roles)
if mr == model.RolePublic {
next.ServeHTTP(w, r.WithContext(ctx))
return
}
http.Error(w, fmt.Sprintf("error returned when extracting key %v", err), http.StatusInternalServerError)
return
}
if key == "unit-test-token" {
a := Auth{
AccountID: 1,
Email: "[email protected]",
Role: model.RoleAdmin,
UserID: 1,
}
ctx = context.WithValue(ctx, ContextAuth, a)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func executeRequest(req *http.Request) (*httptest.ResponseRecorder, *Server) {
req.Header.Set("Content-Type", "application/json")
routes := make(map[string]*Route)
// we use the built-in routes for tests
routes["users"] = newUser()
routes["billing"] = newBilling()
routes["webhooks"] = newWebhook()
mux := &Server{
DB: db,
Logger: logger,
Authenticator: authenticator,
Throttler: Throttler,
RateLimiter: RateLimiter,
Cors: Cors,
StaticDirectory: "/public/",
Routes: routes,
}
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code >= http.StatusBadRequest {
fmt.Printf("error while requesting %s\n%s\n\n", req.URL.Path, string(rec.Body.Bytes()))
}
return rec, mux
}
func Test_Users_SignUp(t *testing.T) {
t.Parallel()
var data = new(struct {
Email string `json:"email"`
Password string `json:"password"`
})
data.Email = "[email protected]"
data.Password = "unit-test"
b, err := json.Marshal(data)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/users/signup", bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
rec, svr := executeRequest(req)
if status := rec.Code; status != http.StatusCreated {
t.Errorf("returns status %v was expecting %v", status, http.StatusCreated)
}
var user model.Account
if err := ParseBody(ioutil.NopCloser(bytes.NewReader(rec.Body.Bytes())), &user); err != nil {
t.Fatal(err)
} else if user.Email != data.Email {
t.Errorf("returns user's email %v differ from added %v", user.Email, data.Email)
}
// we validate that the new user has been saved
acct, err := svr.DB.Users.GetDetail(user.ID)
if err != nil {
t.Fatal(err)
} else if acct.Email != data.Email {
t.Errorf("database email is %s and was expecting %s", acct.Email, data.Email)
}
}
func Test_Users_SignIn(t *testing.T) {
t.Parallel()
fmt.Println("testing signin")
email, pw := "[email protected]", "$2a$10$i32u9lXBMeDGTwe2vrBRde8E8QQ5UW7ndGzWa9n.M/O14/CH9HqlG"
acct, err := db.Users.SignUp(email, pw)
if err != nil {
t.Fatal(err)
}
var data = new(struct {
Email string `json:"email"`
Password string `json:"password"`
})
data.Email = email
data.Password = "unit-test"
b, err := json.Marshal(data)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/users/login", bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
rec, _ := executeRequest(req)
if status := rec.Code; status != http.StatusOK {
t.Errorf("returns status %v was expecting %v", status, http.StatusOK)
}
var user model.User
if err := ParseBody(ioutil.NopCloser(bytes.NewReader(rec.Body.Bytes())), &user); err != nil {
t.Errorf("error while parsing returning JSON: %v", err)
} else if user.Email != acct.Email {
t.Errorf("email was %s was expecting [email protected]", user.Email)
}
}
func Test_Users_Profile(t *testing.T) {
t.Skip()
req, err := http.NewRequest("GET", "/users/profile?key=unit-test-token", nil)
if err != nil {
t.Fatal(err)
}
rec, _ := executeRequest(req)
if status := rec.Code; status != http.StatusOK {
t.Errorf("returns status %v was expecting %v", status, http.StatusOK)
}
}