-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_signup_test.go
66 lines (55 loc) · 1.44 KB
/
user_signup_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
package api_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/bhongy/kimidori/authentication/api"
"github.com/bhongy/kimidori/authentication/user"
)
type mockUserService struct {
createdUser user.User
}
func (svc *mockUserService) Signup(username, password string) (user.User, error) {
return svc.createdUser, nil
}
func TestUserSignup(t *testing.T) {
u := user.User{
ID: "fake.id",
Username: "fake.username",
CreatedAt: user.NewTimestamp(time.Now()),
}
body, err := json.Marshal(api.UserSignupRequest{
Username: u.Username,
Password: "fake.password",
})
if err != nil {
t.Fatalf("marshal request body: %v", err)
}
req, err := http.NewRequest("POST", "/does-not-matter", bytes.NewBuffer(body))
if err != nil {
t.Fatalf("create request: %v", err)
}
recorder := httptest.NewRecorder()
userService := mockUserService{createdUser: u}
h := api.UserSignup(&userService)
h.ServeHTTP(recorder, req)
expectedStatus := http.StatusCreated
if status := recorder.Code; status != expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expectedStatus)
}
expectedBody := fmt.Sprintf(
`{"id":"%v","username":"%v","createdAt":"%v"}`,
u.ID,
u.Username,
u.CreatedAt.String(),
)
if body := recorder.Body.String(); body != expectedBody {
t.Errorf("handler returned unexpected body: got %v want %v",
body, expectedBody)
}
}