forked from unmojang/drasl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_test.go
173 lines (137 loc) · 5.18 KB
/
account_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
package main
import (
"bytes"
"encoding/json"
// "fmt"
"github.com/stretchr/testify/assert"
// "net"
"net/http"
"net/http/httptest"
// "net/url"
"testing"
)
func TestAccount(t *testing.T) {
{
ts := &TestSuite{}
config := testConfig()
ts.Setup(config)
defer ts.Teardown()
ts.CreateTestUser(ts.Server, TEST_USERNAME)
t.Run("Test /users/profiles/minecraft/:playerName", ts.testAccountPlayerNameToID)
t.Run("Test /profiles/minecraft", ts.testAccountPlayerNamesToIDs)
}
{
ts := &TestSuite{}
auxConfig := testConfig()
ts.SetupAux(auxConfig)
config := testConfig()
config.FallbackAPIServers = []FallbackAPIServer{ts.ToFallbackAPIServer(ts.AuxApp, "Aux")}
ts.Setup(config)
defer ts.Teardown()
ts.CreateTestUser(ts.AuxServer, TEST_USERNAME)
t.Run("Test /users/profiles/minecraft/:playerName, fallback API server", ts.testAccountPlayerNameToIDFallback)
t.Run("Test /profile/minecraft, fallback API server", ts.testAccountPlayerNamesToIDsFallback)
}
}
func (ts *TestSuite) testAccountPlayerNameToID(t *testing.T) {
rec := ts.Get(t, ts.Server, "/users/profiles/minecraft/"+TEST_USERNAME, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
var response playerNameToUUIDResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
// Check that the player name is correct
assert.Equal(t, response.Name, TEST_USERNAME)
// Get the real UUID
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
// Check that the UUID is correct
uuid, err := IDToUUID(response.ID)
assert.Nil(t, err)
assert.Equal(t, uuid, user.UUID)
// Any case variations of the username should return the same user
rec = ts.Get(t, ts.Server, "/users/profiles/minecraft/"+TEST_USERNAME_UPPERCASE, nil, nil)
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
uuid, err = IDToUUID(response.ID)
assert.Equal(t, user.UUID, uuid)
}
func (ts *TestSuite) testAccountPlayerNamesToIDs(t *testing.T) {
payload := []string{TEST_USERNAME, "nonexistent"}
body, err := json.Marshal(payload)
assert.Nil(t, err)
req := httptest.NewRequest(http.MethodPost, "/profiles/minecraft", bytes.NewBuffer(body))
rec := httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
var response []playerNameToUUIDResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
// Get the real UUID
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
id, err := UUIDToID(user.UUID)
assert.Nil(t, err)
// There should only be one user, the nonexistent user should not be present
assert.Equal(t, []playerNameToUUIDResponse{{Name: TEST_USERNAME, ID: id}}, response)
}
func (ts *TestSuite) testAccountPlayerNameToIDFallback(t *testing.T) {
{
req := httptest.NewRequest(http.MethodGet, "/users/profiles/minecraft/"+TEST_USERNAME, nil)
rec := httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
var response playerNameToUUIDResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
// Check that the player name is correct
assert.Equal(t, response.Name, TEST_USERNAME)
// Get the real UUID
var user User
result := ts.AuxApp.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
// Check that the UUID is correct
uuid, err := IDToUUID(response.ID)
assert.Nil(t, err)
assert.Equal(t, uuid, user.UUID)
// Test that fallback requests are correctly cached: change the aux
// user's player name and make sure the main server finds the old
// profile in the cache
user.PlayerName = "testcache"
ts.AuxApp.DB.Save(&user)
req = httptest.NewRequest(http.MethodGet, "/users/profiles/minecraft/"+TEST_USERNAME, nil)
rec = httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
uuid, err = IDToUUID(response.ID)
assert.Equal(t, uuid, user.UUID)
}
// Test a non-existent user
{
req := httptest.NewRequest(http.MethodGet, "/users/profiles/minecraft/"+"nonexistent", nil)
rec := httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)
assert.Equal(t, http.StatusNotFound, rec.Code)
}
}
func (ts *TestSuite) testAccountPlayerNamesToIDsFallback(t *testing.T) {
payload := []string{TEST_USERNAME, "nonexistent"}
body, err := json.Marshal(payload)
assert.Nil(t, err)
req := httptest.NewRequest(http.MethodPost, "/profiles/minecraft", bytes.NewBuffer(body))
rec := httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
var response []playerNameToUUIDResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
// Get the real UUID
var user User
result := ts.AuxApp.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
// There should only be one user, the nonexistent user should not be present
id, err := UUIDToID(user.UUID)
assert.Nil(t, err)
assert.Equal(t, []playerNameToUUIDResponse{{Name: TEST_USERNAME, ID: id}}, response)
}
func (ts *TestSuite) testAccountVerifySecurityLocation(t *testing.T) {
rec := ts.Get(t, ts.Server, "/user/security/location", nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
}