-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathAvatarAssignmentElideTest.java
More file actions
219 lines (201 loc) · 9.95 KB
/
Copy pathAvatarAssignmentElideTest.java
File metadata and controls
219 lines (201 loc) · 9.95 KB
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
package com.faforever.api.data;
import com.faforever.api.AbstractIntegrationTest;
import com.faforever.api.data.domain.GroupPermission;
import com.faforever.api.security.OAuthScope;
import com.faforever.commons.api.dto.Avatar;
import com.faforever.commons.api.dto.AvatarAssignment;
import com.faforever.commons.api.dto.Player;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import java.time.OffsetDateTime;
import static com.faforever.api.data.JsonApiMediaType.JSON_API_MEDIA_TYPE;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql")
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql")
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepAvatarData.sql")
public class AvatarAssignmentElideTest extends AbstractIntegrationTest {
@Test
public void getUnusedAvatar() throws Exception {
mockMvc.perform(get("/data/avatar/3")
.with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id", is("3")))
.andExpect(jsonPath("$.data.type", is("avatar")))
.andExpect(jsonPath("$.data.attributes.tooltip", is("Donator Avatar")))
.andExpect(jsonPath("$.data.attributes.url", is("http://localhost/faf/avatars/donator.png")))
.andExpect(jsonPath("$.data.attributes.description", is("Only for donators")))
.andExpect(jsonPath("$.data.relationships.assignments.data", hasSize(0)));
}
@Test
public void getAvatarWithPlayer() throws Exception {
mockMvc.perform(get("/data/avatar/2")
.with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id", is("2")))
.andExpect(jsonPath("$.data.type", is("avatar")))
.andExpect(jsonPath("$.data.attributes.tooltip", is("Avatar No. 2")))
.andExpect(jsonPath("$.data.attributes.url", is("http://localhost/faf/avatars/avatar2.png")))
.andExpect(jsonPath("$.data.relationships.assignments.data", hasSize(1)));
}
@Test
public void getAvatarURLEncoded() throws Exception {
mockMvc.perform(get("/data/avatar/4")
.with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id", is("4")))
.andExpect(jsonPath("$.data.type", is("avatar")))
.andExpect(jsonPath("$.data.attributes.tooltip", is("Space Avatar")))
.andExpect(jsonPath("$.data.attributes.url", is("http://localhost/faf/avatars/avatar%20space.png")));
}
@Test
public void canAssignAvatarWithScopeAndRole() throws Exception {
final Avatar avatar = (Avatar) new Avatar().setId("1");
final Player player = (Player) new Player().setId("1");
final AvatarAssignment avatarAssignment = new AvatarAssignment()
.setAvatar(avatar)
.setSelected(false);
mockMvc.perform(
post("/data/player/{playerId}/avatarAssignments", player.getId())
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_AVATAR))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.data.relationships.player.data.id", is(player.getId())))
.andExpect(jsonPath("$.data.relationships.avatar.data.id", is(avatar.getId())));
}
@Test
public void cannotAssignAvatarWithoutScope() throws Exception {
final Avatar avatar = (Avatar) new Avatar().setId("1");
final Player player = (Player) new Player().setId("1");
final AvatarAssignment avatarAssignment = new AvatarAssignment()
.setAvatar(avatar)
.setSelected(false);
mockMvc.perform(
post("/data/player/{playerId}/avatarAssignments", player.getId())
.with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_WRITE_AVATAR))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isForbidden());
}
@Test
public void cannotAssignAvatarWithoutRole() throws Exception {
final Avatar avatar = (Avatar) new Avatar().setId("1");
final Player player = (Player) new Player().setId("1");
final AvatarAssignment avatarAssignment = new AvatarAssignment()
.setAvatar(avatar)
.setSelected(false);
mockMvc.perform(
post("/data/player/{playerId}/avatarAssignments", player.getId())
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isForbidden());
}
@Test
public void canDeleteAvatarAssignmentWithScopeAndRole() throws Exception {
mockMvc.perform(
delete("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_AVATAR))
).andExpect(status().isNoContent());
mockMvc.perform(
get("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_AVATAR))
).andExpect(status().isNotFound());
}
@Test
public void cannotDeleteAvatarAssignmentWithoutScope() throws Exception {
mockMvc.perform(
delete("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_WRITE_AVATAR))
).andExpect(status().isForbidden());
}
@Test
public void cannotDeleteAvatarAssignmentWithoutRole() throws Exception {
mockMvc.perform(
delete("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES))
).andExpect(status().isForbidden());
}
@Test
public void canUpdateAvatarAssignmentExpirationWithScopeAndRole() throws Exception {
final OffsetDateTime now = OffsetDateTime.now();
final AvatarAssignment avatarAssignment = (AvatarAssignment) new AvatarAssignment()
.setExpiresAt(now)
.setId("1");
mockMvc.perform(
patch("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_AVATAR))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isNoContent());
mockMvc.perform(
get("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.attributes.expiresAt", is(OFFSET_DATE_TIME_FORMATTER.format(now))));
}
@Test
public void cannotUpdateAvatarAssignmentExpirationWithoutScope() throws Exception {
final OffsetDateTime now = OffsetDateTime.now();
final AvatarAssignment avatarAssignment = (AvatarAssignment) new AvatarAssignment()
.setExpiresAt(now)
.setId("1");
mockMvc.perform(
patch("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_WRITE_AVATAR))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isForbidden());
}
@Test
public void cannotUpdateAvatarAssignmentExpirationWithoutRole() throws Exception {
final OffsetDateTime now = OffsetDateTime.now();
final AvatarAssignment avatarAssignment = (AvatarAssignment) new AvatarAssignment()
.setExpiresAt(now)
.setId("1");
mockMvc.perform(
patch("/data/avatarAssignment/{assignmentId}", 1)
.with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isForbidden());
}
@Test
public void ownerCanUpdateAvatarAssignmentSelection() throws Exception {
final AvatarAssignment avatarAssignment = (AvatarAssignment) new AvatarAssignment()
.setSelected(true)
.setId("2");
mockMvc.perform(
patch("/data/avatarAssignment/{assignmentId}", 2)
.with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isNoContent());
mockMvc.perform(
get("/data/avatarAssignment/{assignmentId}", 2)
.with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.attributes.selected", is(true)));
}
@Test
public void nonOwnerCannotUpdateAvatarAssignmentSelection() throws Exception {
final AvatarAssignment avatarAssignment = (AvatarAssignment) new AvatarAssignment()
.setSelected(false)
.setId("2");
mockMvc.perform(
patch("/data/avatarAssignment/{assignmentId}", 2)
.with(getOAuthTokenForUserId(USERID_USER, OAuthScope._PUBLIC_PROFILE))
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
.content(createJsonApiContent(avatarAssignment)))
.andExpect(status().isForbidden());
}
}