-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
123 lines (111 loc) · 3.25 KB
/
handler_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
package auth
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHandler(t *testing.T) {
t.Run("method not allowed", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/auth/login", nil)
w := httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode)
})
t.Run("action not provided", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/auth/", nil)
w := httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
})
t.Run("action not supported", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/auth/x", nil)
w := httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
})
}
func TestHandlerActions(t *testing.T) {
t.Run("register", func(t *testing.T) {
body := strings.NewReader(`{
"username": "hello",
"password": "world"
}`)
req := httptest.NewRequest(http.MethodPost, "/auth/register", body)
w := httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
body = strings.NewReader(`{
"username": "hello",
"password": "world"
}`)
t.Log("register same username twice, should return error")
req = httptest.NewRequest(http.MethodPost, "/auth/register", body)
w = httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res = w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusConflict, res.StatusCode)
})
t.Run("login", func(t *testing.T) {
body := strings.NewReader(`{
"username": "hello",
"password": "world"
}`)
req := httptest.NewRequest(http.MethodPost, "/auth/login", body)
w := httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
var resData map[string]string
err = json.Unmarshal(data, &resData)
assert.Nil(t, err)
t.Log("get token: ", resData["token"])
t.Log("login with wrong password")
body = strings.NewReader(`{
"username": "hello",
"password": "world2"
}`)
req = httptest.NewRequest(http.MethodPost, "/auth/login", body)
w = httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res = w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
t.Log("login with wrong username")
body = strings.NewReader(`{
"username": "hello2",
"password": "world"
}`)
req = httptest.NewRequest(http.MethodPost, "/auth/login", body)
w = httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res = w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusNotFound, res.StatusCode)
})
t.Run("logout", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/auth/logout", nil)
w := httptest.NewRecorder()
testHandler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
})
}