forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_test.go
169 lines (162 loc) · 4.38 KB
/
users_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
package handlers
import (
"github.com/statping/statping/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"testing"
)
func TestUnAuthenticatedUserRoutes(t *testing.T) {
tests := []HTTPTest{
{
Name: "No Authentication - New User",
URL: "/api/users",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Update User",
URL: "/api/users/1",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - View User",
URL: "/api/users/1",
Method: "GET",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Delete User",
URL: "/api/users/1",
Method: "DELETE",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
}
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
str, t, err := RunHTTPTest(v, t)
t.Logf("Test %s: \n %v\n", v.Name, str)
assert.Nil(t, err)
})
}
}
func TestApiUsersRoutes(t *testing.T) {
form := url.Values{}
form.Add("username", "adminupdated")
form.Add("password", "password12345")
badForm := url.Values{}
badForm.Add("username", "adminupdated")
badForm.Add("password", "wrongpassword")
tests := []HTTPTest{
{
Name: "Check Basic Authentication",
URL: "/api",
Method: "GET",
ExpectedStatus: 401,
BeforeTest: func(t *testing.T) error {
utils.Params.Set("AUTH_USERNAME", "admin")
utils.Params.Set("AUTH_PASSWORD", "admin")
return nil
},
AfterTest: func(t *testing.T) error {
utils.Params.Set("AUTH_USERNAME", "")
utils.Params.Set("AUTH_PASSWORD", "")
return nil
},
},
{
Name: "Statping All Users",
URL: "/api/users",
Method: "GET",
ExpectedStatus: 200,
ResponseLen: 1,
BeforeTest: SetTestENV,
}, {
Name: "Statping Create User",
URL: "/api/users",
HttpHeaders: []string{"Content-Type=application/json"},
Method: "POST",
Body: `{
"username": "adminuser2",
"email": "[email protected]",
"password": "passsword123",
"admin": true
}`,
ExpectedStatus: 200,
ExpectedContains: []string{Success, MethodCreate},
}, {
Name: "Statping View User",
URL: "/api/users/1",
Method: "GET",
ExpectedStatus: 200,
}, {
Name: "Statping Incorrect User ID",
URL: "/api/users/NOinteger",
Method: "GET",
ExpectedStatus: 422,
}, {
Name: "Statping Missing User",
URL: "/api/users/9393939393",
Method: "GET",
ExpectedStatus: 404,
}, {
Name: "Statping Update User",
URL: "/api/users/1",
Method: "POST",
Body: `{
"username": "adminupdated",
"email": "[email protected]",
"password": "password12345",
"admin": true
}`,
ExpectedStatus: 200,
ExpectedContains: []string{Success, MethodUpdate},
}, {
Name: "Statping Delete User",
URL: "/api/users/2",
Method: "DELETE",
ExpectedStatus: 200,
ExpectedContains: []string{Success, MethodDelete},
}, {
Name: "Statping Login User",
URL: "/api/login",
Method: "POST",
Body: form.Encode(),
ExpectedContains: []string{`"token"`},
ExpectedStatus: 200,
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
}, {
Name: "Statping Bad Login User",
URL: "/api/login",
Method: "POST",
Body: badForm.Encode(),
ExpectedContains: []string{`incorrect authentication`},
ExpectedStatus: 200,
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
}, {
Name: "Statping Logout",
URL: "/api/logout",
Method: "GET",
ExpectedStatus: 200,
}, {
Name: "Incorrect JSON POST",
URL: "/api/users",
Body: BadJSON,
ExpectedContains: []string{BadJSONResponse},
BeforeTest: SetTestENV,
Method: "POST",
ExpectedStatus: 422,
},
}
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t)
require.Nil(t, err)
})
}
}