/
regauth.go
50 lines (40 loc) · 1.23 KB
/
regauth.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
package testutil
import (
"encoding/json"
"net/http"
"net/url"
"testing"
"app/api/endpoint"
"app/api/model"
"github.com/stretchr/testify/assert"
)
// Register a user and return the user ID.
func Register(t *testing.T, tr *Request, p endpoint.Core) (userID string) {
// Register the user.
form := url.Values{}
form.Set("first_name", "Foo")
form.Set("last_name", "Barr")
form.Set("email", "fbar@example.com")
form.Set("password", "guess123")
w := tr.SendJSON(t, p, "POST", "/api/v1/register", form)
// Verify the response.
r := new(model.CreatedResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusCreated, w.Code)
assert.NotEmpty(t, r.Body.RecordID)
return r.Body.RecordID
}
// Auth will register a user, login, and return the user token and user ID.
func Auth(t *testing.T, tr *Request, p endpoint.Core) (userToken string, userID string) {
userID = Register(t, tr, p)
// Login with the user.
form := url.Values{}
form.Set("email", "fbar@example.com")
form.Set("password", "guess123")
w := tr.SendJSON(t, p, "POST", "/api/v1/login", form)
r := new(model.LoginResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
return r.Body.Token, userID
}