forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_teams_test.go
229 lines (203 loc) · 7.04 KB
/
setup_teams_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestSetupTeams(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
// Check that there are no teams to start.
recorder := getHttpResponse("/setup/teams")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "0 teams")
// Mock the URL to download team info from.
teamInfoBody := `{
"website": "http://www.team254.com",
"name": "NASA Ames Research Center",
"locality": "San Jose",
"rookie_year": 1999,
"region": "CA",
"team_number": 254,
"location": "San Jose, CA, USA",
"key": "frc254",
"country_name": "USA",
"nickname": "The Cheesy Poofs"
}`
teamInfoServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, teamInfoBody)
}))
defer teamInfoServer.Close()
tbaTeamBaseUrl = teamInfoServer.URL
teamAwardsBody := `[{
"event_key": "2014cmp",
"award_type": 1,
"name": "Championship Winners",
"recipient_list": [
{
"team_number": 254,
"awardee": null
},
{
"team_number": 2848,
"awardee": null
},
{
"team_number": 469,
"awardee": null
},
{
"team_number": 74,
"awardee": null
}
],
"year": 2014
}]`
teamAwardsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, teamAwardsBody)
}))
defer teamAwardsServer.Close()
tbaTeamAwardsBaseUrl = teamAwardsServer.URL
eventBody := `{ "name": "Championship" }`
eventServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, eventBody)
}))
defer eventServer.Close()
tbaEventBaseUrl = eventServer.URL
// Add some teams.
recorder = postHttpResponse("/setup/teams", "teamNumbers=254\r\nnotateam\r\n1114\r\n")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/teams")
assert.Contains(t, recorder.Body.String(), "2 teams")
assert.Contains(t, recorder.Body.String(), "The Cheesy Poofs")
assert.Contains(t, recorder.Body.String(), "1114")
// Add another team.
recorder = postHttpResponse("/setup/teams", "teamNumbers=33")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/teams")
assert.Contains(t, recorder.Body.String(), "3 teams")
assert.Contains(t, recorder.Body.String(), "33")
// Edit a team.
recorder = getHttpResponse("/setup/teams/254/edit")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "The Cheesy Poofs")
recorder = postHttpResponse("/setup/teams/254/edit", "nickname=Teh Chezy Pofs")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/teams")
assert.Contains(t, recorder.Body.String(), "Teh Chezy Pofs")
// Delete a team.
recorder = postHttpResponse("/setup/teams/1114/delete", "")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/teams")
assert.Contains(t, recorder.Body.String(), "2 teams")
// Test clearing all teams.
recorder = postHttpResponse("/setup/teams/clear", "")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/teams")
assert.Contains(t, recorder.Body.String(), "0 teams")
}
func TestSetupTeamsDisallowModification(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
db.CreateTeam(&Team{Id: 254, Nickname: "The Cheesy Poofs"})
db.CreateMatch(&Match{Type: "qualification"})
// Disallow adding teams.
recorder := postHttpResponse("/setup/teams", "teamNumbers=33")
assert.Contains(t, recorder.Body.String(), "can't modify")
assert.Contains(t, recorder.Body.String(), "1 teams")
assert.Contains(t, recorder.Body.String(), "The Cheesy Poofs")
// Disallow deleting team.
recorder = postHttpResponse("/setup/teams/254/delete", "")
assert.Contains(t, recorder.Body.String(), "can't modify")
assert.Contains(t, recorder.Body.String(), "1 teams")
assert.Contains(t, recorder.Body.String(), "The Cheesy Poofs")
// Disallow clearing all teams.
recorder = postHttpResponse("/setup/teams/clear", "")
assert.Contains(t, recorder.Body.String(), "can't modify")
assert.Contains(t, recorder.Body.String(), "1 teams")
assert.Contains(t, recorder.Body.String(), "The Cheesy Poofs")
// Allow editing a team.
recorder = postHttpResponse("/setup/teams/254/edit", "nickname=Teh Chezy Pofs")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/teams")
assert.NotContains(t, recorder.Body.String(), "can't modify")
assert.Contains(t, recorder.Body.String(), "1 teams")
assert.Contains(t, recorder.Body.String(), "Teh Chezy Pofs")
}
func TestSetupTeamsBadReqest(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
recorder := getHttpResponse("/setup/teams/254/edit")
assert.Equal(t, 400, recorder.Code)
assert.Contains(t, recorder.Body.String(), "No such team")
recorder = postHttpResponse("/setup/teams/254/edit", "")
assert.Equal(t, 400, recorder.Code)
assert.Contains(t, recorder.Body.String(), "No such team")
recorder = postHttpResponse("/setup/teams/254/delete", "")
assert.Equal(t, 400, recorder.Code)
assert.Contains(t, recorder.Body.String(), "No such team")
}
func TestSetupTeamsWpaKeys(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
eventSettings.NetworkSecurityEnabled = true
team1 := &Team{Id: 254, WpaKey: "aaaaaaaa"}
team2 := &Team{Id: 1114}
db.CreateTeam(team1)
db.CreateTeam(team2)
recorder := getHttpResponse("/setup/teams/generate_wpa_keys?all=false")
assert.Equal(t, 302, recorder.Code)
team1, _ = db.GetTeamById(254)
team2, _ = db.GetTeamById(1114)
assert.Equal(t, "aaaaaaaa", team1.WpaKey)
assert.Equal(t, 8, len(team2.WpaKey))
recorder = getHttpResponse("/setup/teams/generate_wpa_keys?all=true")
assert.Equal(t, 302, recorder.Code)
team1, _ = db.GetTeamById(254)
team3, _ := db.GetTeamById(1114)
assert.NotEqual(t, "aaaaaaaa", team1.WpaKey)
assert.Equal(t, 8, len(team1.WpaKey))
assert.NotEqual(t, team2.WpaKey, team3.WpaKey)
assert.Equal(t, 8, len(team3.WpaKey))
// Disallow invalid manual WPA keys.
recorder = postHttpResponse("/setup/teams/254/edit", "wpa_key=1234567")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "WPA key must be between 8 and 63 characters")
}
func TestSetupTeamsPublish(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
tbaBaseUrl = "fakeurl"
eventSettings.TbaPublishingEnabled = true
recorder := postHttpResponse("/setup/teams/publish", "")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Failed to publish teams")
}