-
Notifications
You must be signed in to change notification settings - Fork 18
/
session_test.go
213 lines (176 loc) · 6.93 KB
/
session_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestSession(t *testing.T) {
{
ts := &TestSuite{}
config := testConfig()
ts.Setup(config)
defer ts.Teardown()
ts.CreateTestUser(ts.Server, TEST_USERNAME)
t.Run("Test /session/minecraft/hasJoined", ts.testSessionHasJoined)
t.Run("Test /session/minecraft/join", ts.testSessionJoin)
t.Run("Test /session/minecraft/profile/:id", ts.testSessionProfile)
t.Run("Test /blockedservers", ts.testSessionBlockedServers)
}
}
func (ts *TestSuite) testSessionJoin(t *testing.T) {
// First, authenticate to get a token pair
authenticateRes := ts.authenticate(t, TEST_USERNAME, TEST_PASSWORD)
accessToken := authenticateRes.AccessToken
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
selectedProfile := Unwrap(UUIDToID(user.UUID))
serverID := "0000000000000000000000000000000000000000"
{
// Successful join
payload := sessionJoinRequest{
AccessToken: accessToken,
SelectedProfile: selectedProfile,
ServerID: serverID,
}
rec := ts.PostJSON(t, ts.Server, "/session/minecraft/join", payload, nil, nil)
assert.Equal(t, http.StatusNoContent, rec.Code)
// User ServerID should be set
result = ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
assert.Equal(t, serverID, *UnmakeNullString(&user.ServerID))
}
{
// Successful joinserver.jsp
sessionID := "token:" + accessToken + ":" + selectedProfile
rec := ts.Get(t, ts.Server, "/game/joinserver.jsp?user="+TEST_USERNAME+"&sessionId="+sessionID+"&serverId="+serverID, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "OK", rec.Body.String())
}
{
// Join should fail if we send an invalid access token
// Start with an invalid ServerID
assert.Nil(t, ts.App.DB.First(&user, "username = ?", TEST_USERNAME).Error)
user.ServerID = MakeNullString(nil)
assert.Nil(t, ts.App.DB.Save(&user).Error)
payload := sessionJoinRequest{
AccessToken: "invalid",
SelectedProfile: selectedProfile,
ServerID: serverID,
}
rec := ts.PostJSON(t, ts.Server, "/session/minecraft/join", payload, nil, nil)
assert.Equal(t, http.StatusForbidden, rec.Code)
var response ErrorResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, "ForbiddenOperationException", *response.Error)
assert.Equal(t, "Invalid token.", *response.ErrorMessage)
// User ServerID should be invalid
assert.Nil(t, ts.App.DB.First(&user, "username = ?", TEST_USERNAME).Error)
assert.False(t, user.ServerID.Valid)
}
{
// "Bad login" with invalid access token
// Start with an invalid ServerID
assert.Nil(t, ts.App.DB.First(&user, "username = ?", TEST_USERNAME).Error)
user.ServerID = MakeNullString(nil)
assert.Nil(t, ts.App.DB.Save(&user).Error)
accessToken := "invalid"
sessionID := "token:" + accessToken + ":" + selectedProfile
rec := ts.Get(t, ts.Server, "/game/joinserver.jsp?user="+TEST_USERNAME+"&sessionId="+sessionID+"&serverId="+serverID, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "Bad login", rec.Body.String())
// User ServerID should be invalid
assert.Nil(t, ts.App.DB.First(&user, "username = ?", TEST_USERNAME).Error)
assert.False(t, user.ServerID.Valid)
}
}
func (ts *TestSuite) testSessionHasJoined(t *testing.T) {
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
serverID := "0000000000000000000000000000000000000000"
// Start with a valid ServerID
assert.Nil(t, ts.App.DB.First(&user, "username = ?", TEST_USERNAME).Error)
user.ServerID = MakeNullString(&serverID)
assert.Nil(t, ts.App.DB.Save(&user).Error)
{
// Successful hasJoined
url := "/session/minecraft/hasJoined?username=" + user.PlayerName + "&serverId=" + serverID + "&ip=" + "127.0.0.1"
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
var response SessionProfileResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, Unwrap(UUIDToID(user.UUID)), response.ID)
assert.Equal(t, user.PlayerName, response.Name)
}
{
// Successful checkserver.jsp
url := "/game/checkserver.jsp?user=" + user.PlayerName + "&serverId=" + serverID
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "YES", rec.Body.String())
}
{
// hasJoined should fail if we send an invalid server ID
// Start with a valid ServerID
assert.Nil(t, ts.App.DB.First(&user, "username = ?", TEST_USERNAME).Error)
user.ServerID = MakeNullString(&serverID)
assert.Nil(t, ts.App.DB.Save(&user).Error)
url := "/session/minecraft/hasJoined?username=" + user.PlayerName + "&serverId=" + "invalid" + "&ip=" + "127.0.0.1"
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusForbidden, rec.Code)
}
{
// Unsuccessful checkserver.jsp
invalidServerID := "INVALID-SERVER-ID"
url := "/game/checkserver.jsp?user=" + user.PlayerName + "&serverId=" + invalidServerID
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
assert.Equal(t, "NO", rec.Body.String())
}
}
func (ts *TestSuite) testSessionProfile(t *testing.T) {
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
{
// Successfully get profile
url := "/session/minecraft/profile/" + Unwrap(UUIDToID(user.UUID))
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
var response SessionProfileResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, Unwrap(UUIDToID(user.UUID)), response.ID)
assert.Equal(t, user.PlayerName, response.Name)
}
{
// Successfully get profile with dashes in UUID
url := "/session/minecraft/profile/" + user.UUID
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
var response SessionProfileResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, Unwrap(UUIDToID(user.UUID)), response.ID)
assert.Equal(t, user.PlayerName, response.Name)
}
{
// If the UUID doesn't exist, we should get a StatusNoContent
url := "/session/minecraft/profile/" + "00000000000000000000000000000000"
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusNoContent, rec.Code)
}
{
// If the UUID is invalid, we should get a StatusBadRequest with an error message
url := "/session/minecraft/profile/" + "invalid"
rec := ts.Get(t, ts.Server, url, nil, nil)
assert.Equal(t, http.StatusBadRequest, rec.Code)
var response ErrorResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, "Not a valid UUID: "+"invalid", *response.ErrorMessage)
}
}
func (ts *TestSuite) testSessionBlockedServers(t *testing.T) {
rec := ts.Get(t, ts.Server, "/blockedservers", nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
}