-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_test.go
87 lines (61 loc) · 2.34 KB
/
admin_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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/emicklei/go-restful"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoginInAsAdmin(t *testing.T) {
buildWebservice(true)
getHttpReq, _ := http.NewRequest("GET", "/me", nil)
getHttpReq.Header.Set("Authorization", basicAuthEncode("admin", "pass"))
httpWriter := httptest.NewRecorder()
restful.DefaultContainer.ServeHTTP(httpWriter, getHttpReq)
assert.Equal(t, 200, httpWriter.Code)
}
func TestAdminUsersPage(t *testing.T) {
buildWebservice(true)
getHttpReq, _ := http.NewRequest("GET", "/admin/user", nil)
getHttpReq.Header.Set("Authorization", basicAuthEncode("admin", "pass"))
httpWriter := httptest.NewRecorder()
restful.DefaultContainer.ServeHTTP(httpWriter, getHttpReq)
require.Equal(t, 200, httpWriter.Code)
var users []UserWithConnections
json.Unmarshal(httpWriter.Body.Bytes(), &users)
require.Len(t, users, 1)
assert.Equal(t, "admin", users[0].User.Username)
assert.Len(t, users[0].Connections, 0)
}
func TestAdminUsersPageNotAccessibleUnlessAdmin(t *testing.T) {
buildWebservice(true)
registerUser(t, "viktor", "pass")
getHttpReq, _ := http.NewRequest("GET", "/admin/user", nil)
getHttpReq.Header.Set("Authorization", basicAuthEncode("viktor", "pass"))
httpWriter := httptest.NewRecorder()
restful.DefaultContainer.ServeHTTP(httpWriter, getHttpReq)
require.Equal(t, 401, httpWriter.Code)
}
func TestAdminUsersPageListsConnections(t *testing.T) {
buildWebservice(true)
//add some connections
registerUser(t, "viktor", "pass")
user2Id := registerUser(t, "user2", "pass")
addConnection(t, "viktor", "pass", user2Id)
getHttpReq, _ := http.NewRequest("GET", "/admin/user", nil)
getHttpReq.Header.Set("Authorization", basicAuthEncode("admin", "pass"))
httpWriter := httptest.NewRecorder()
restful.DefaultContainer.ServeHTTP(httpWriter, getHttpReq)
require.Equal(t, 200, httpWriter.Code)
var users []UserWithConnections
json.Unmarshal(httpWriter.Body.Bytes(), &users)
require.Len(t, users, 3)
assert.Equal(t, "admin", users[0].User.Username)
assert.Len(t, users[0].Connections, 0)
assert.Equal(t, "viktor", users[1].User.Username)
require.Len(t, users[1].Connections, 1)
assert.Equal(t, "user2", users[1].Connections[0].Username)
assert.Equal(t, user2Id, users[1].Connections[0].Id)
}