-
Notifications
You must be signed in to change notification settings - Fork 14
/
account_test.go
150 lines (119 loc) · 4.63 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
package main
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"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.makeTestAccountPlayerNamesToIDs("/profiles/minecraft"))
t.Run("Test /users/security/location", ts.testAccountVerifySecurityLocation)
}
{
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.Nil(t, err)
assert.Equal(t, user.UUID, uuid)
}
func (ts *TestSuite) testAccountPlayerNameToIDFallback(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.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"
assert.Nil(t, ts.AuxApp.DB.Save(&user).Error)
rec = ts.Get(t, ts.Server, "/users/profiles/minecraft/"+TEST_USERNAME, nil, nil)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
uuid, err = IDToUUID(response.ID)
assert.Nil(t, err)
assert.Equal(t, uuid, user.UUID)
// Change the aux user's player name back
user.PlayerName = TEST_USERNAME
assert.Nil(t, ts.AuxApp.DB.Save(&user).Error)
}
// Test a non-existent user
{
rec := ts.Get(t, ts.Server, "/users/profiles/minecraft/", nil, nil)
assert.Equal(t, http.StatusNotFound, rec.Code)
}
}
func (ts *TestSuite) testAccountPlayerNamesToIDsFallback(t *testing.T) {
{
payload := []string{TEST_USERNAME, "nonexistent"}
rec := ts.PostJSON(t, ts.Server, "/profiles/minecraft", payload, nil, nil)
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)
}
{
payload := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}
rec := ts.PostJSON(t, ts.Server, "/profiles/minecraft", payload, nil, nil)
assert.Equal(t, http.StatusBadRequest, rec.Code)
var response ErrorResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, "CONSTRAINT_VIOLATION", *response.Error)
}
}
func (ts *TestSuite) testAccountVerifySecurityLocation(t *testing.T) {
rec := ts.Get(t, ts.Server, "/user/security/location", nil, nil)
assert.Equal(t, http.StatusNoContent, rec.Code)
}