Skip to content

Commit ca8e310

Browse files
committed
faf-java-api-428 Fix after rebase
1 parent c773104 commit ca8e310

File tree

8 files changed

+201
-294
lines changed

8 files changed

+201
-294
lines changed

src/inttest/java/com/faforever/api/clan/ClanControllerTest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
import org.junit.jupiter.api.Test;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.http.HttpHeaders;
11-
import org.springframework.security.core.context.SecurityContextHolder;
12-
import org.springframework.security.test.context.support.WithUserDetails;
13-
import org.springframework.test.annotation.DirtiesContext;
14-
import org.springframework.test.annotation.DirtiesContext.MethodMode;
1511
import org.springframework.test.context.jdbc.Sql;
1612
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
1713
import org.springframework.test.web.servlet.MvcResult;
@@ -20,16 +16,15 @@
2016

2117
import static org.hamcrest.Matchers.is;
2218
import static org.hamcrest.core.IsNull.nullValue;
23-
import static org.junit.Assert.assertFalse;
24-
import static org.junit.Assert.assertNotNull;
25-
import static org.junit.Assert.assertNull;
26-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
2723
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2824
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2925
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3026
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3127

32-
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
3328
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql")
3429
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql")
3530
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepClanData.sql")
@@ -115,7 +110,6 @@ public void createClanWithoutAuth() throws Exception {
115110
}
116111

117112
@Test
118-
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
119113
public void createClanWithExistingName() throws Exception {
120114
Player player = playerRepository.getById(USERID_USER);
121115

@@ -138,7 +132,6 @@ public void createClanWithExistingName() throws Exception {
138132
}
139133

140134
@Test
141-
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
142135
public void createClanWithExistingTag() throws Exception {
143136
Player player = playerRepository.getById(USERID_USER);
144137

@@ -161,7 +154,6 @@ public void createClanWithExistingTag() throws Exception {
161154
}
162155

163156
@Test
164-
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
165157
public void createSecondClan() throws Exception {
166158
Player player = playerRepository.getById(USERID_CLAN_MEMBER);
167159

src/main/java/com/faforever/api/clan/ClanService.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import com.fasterxml.jackson.databind.ObjectMapper;
1616
import lombok.RequiredArgsConstructor;
1717
import lombok.SneakyThrows;
18-
import org.springframework.security.jwt.Jwt;
19-
import org.springframework.security.core.Authentication;
2018
import org.springframework.stereotype.Service;
2119
import org.springframework.transaction.annotation.Transactional;
2220
import org.springframework.util.Assert;
@@ -69,15 +67,16 @@ public void preCreate(Clan clan) {
6967
@SneakyThrows
7068
@Transactional
7169
@Deprecated
72-
// use POST via Elide instead
73-
Clan create(String name, String tag, String description) {
70+
// use POST via Elide instead
71+
public Clan create(String name, String tag, String description) {
7472
Clan clan = new Clan();
7573
clan.setName(name);
7674
clan.setTag(tag);
7775
clan.setDescription(description);
7876
clan.setRequiresInvitation(true);
79-
clan.setFounder(playerService.getCurrentPlayer());
80-
clan.setLeader(playerService.getCurrentPlayer());
77+
Player currentPlayer = playerService.getCurrentPlayer();
78+
clan.setFounder(currentPlayer);
79+
clan.setLeader(currentPlayer);
8180

8281
// validation is done at preCreate() called by ClanListener
8382
clanRepository.save(clan);
@@ -86,7 +85,7 @@ Clan create(String name, String tag, String description) {
8685

8786
@SneakyThrows
8887
@Transactional
89-
String generatePlayerInvitationToken(int newMemberId, int clanId) {
88+
public String generatePlayerInvitationToken(int newMemberId, int clanId) {
9089
Player requester = playerService.getCurrentPlayer();
9190

9291
Clan clan = clanRepository.findById(clanId)
@@ -102,16 +101,14 @@ String generatePlayerInvitationToken(int newMemberId, int clanId) {
102101
.plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES)
103102
.toEpochMilli();
104103

105-
InvitationResult result = new InvitationResult(expire,
106-
ClanResult.of(clan),
107-
PlayerResult.of(newMember));
104+
InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
108105
return jwtService.sign(result);
109106
}
110107

111108
@SneakyThrows
112109
void acceptPlayerInvitationToken(String stringToken) {
113-
Jwt token = jwtService.decodeAndVerify(stringToken);
114-
InvitationResult invitation = objectMapper.readValue(token.getClaims(), InvitationResult.class);
110+
String decodedToken = jwtService.decodeAndVerify(stringToken);
111+
InvitationResult invitation = objectMapper.readValue(decodedToken, InvitationResult.class);
115112

116113
if (invitation.isExpired()) {
117114
throw ApiException.of(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE);
@@ -122,7 +119,7 @@ void acceptPlayerInvitationToken(String stringToken) {
122119
Clan clan = clanRepository.findById(clanId)
123120
.orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
124121

125-
Player newMember = playerService.getById(invitation.newMember().getId());
122+
Player newMember = playerService.getById(invitation.newMember().id());
126123

127124
if (!Objects.equals(player.getId(), newMember.getId())) {
128125
throw ApiException.of(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER);

src/main/java/com/faforever/api/config/IgnoreOctetStreamToObjectHttpMessageConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.springframework.http.MediaType;
77
import org.springframework.http.converter.AbstractHttpMessageConverter;
88
import org.springframework.lang.Nullable;
9-
import org.springframework.security.core.Authentication;
109
import org.springframework.util.StreamUtils;
1110
import org.springframework.web.multipart.MultipartFile;
1211

@@ -16,7 +15,7 @@
1615
* This class is used to support having both @RequestParam and @RequestPart with same multipart name in one request handler.
1716
* When multipart request contains simple request param octet-stream, this class is used to ignore parsing
1817
* of byte stream to {@link MapUploadMetadata}.
19-
* See {@link com.faforever.api.map.MapsController#uploadMap(MultipartFile, String, MapUploadMetadata, Authentication)}
18+
* See {@link com.faforever.api.map.MapsController#uploadMap(MultipartFile, String, MapUploadMetadata)}
2019
*/
2120
public class IgnoreOctetStreamToObjectHttpMessageConverter extends AbstractHttpMessageConverter<byte[]> {
2221

src/main/java/com/faforever/api/config/NoopMultipartFileToStringConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import com.faforever.api.map.MapUploadMetadata;
44
import org.springframework.core.convert.converter.Converter;
5-
import org.springframework.security.core.Authentication;
65
import org.springframework.web.multipart.MultipartFile;
76

87
/**
98
* This class is used to support having both @RequestParam and @RequestPart with same multipart name in one request handler.
109
* When multipart request contains json multipart, this class is used to ignore conversion
1110
* of MultipartFile to String.
12-
* See {@link com.faforever.api.map.MapsController#uploadMap(MultipartFile, String, MapUploadMetadata, Authentication)}
11+
* See {@link com.faforever.api.map.MapsController#uploadMap(MultipartFile, String, MapUploadMetadata)}
1312
*/
1413
public class NoopMultipartFileToStringConverter implements Converter<MultipartFile, String> {
1514

src/main/java/com/faforever/api/map/MapsController.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.faforever.api.map;
22

3-
import com.faforever.api.config.FafApiProperties;
43
import com.faforever.api.data.domain.Player;
54
import com.faforever.api.error.ApiException;
65
import com.faforever.api.error.ErrorCode;
@@ -14,9 +13,9 @@
1413
import lombok.AllArgsConstructor;
1514
import lombok.extern.slf4j.Slf4j;
1615
import org.springframework.security.access.prepost.PreAuthorize;
17-
import org.springframework.security.core.Authentication;
16+
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.PostMapping;
1818
import org.springframework.web.bind.annotation.RequestMapping;
19-
import org.springframework.web.bind.annotation.RequestMethod;
2019
import org.springframework.web.bind.annotation.RequestParam;
2120
import org.springframework.web.bind.annotation.RequestPart;
2221
import org.springframework.web.bind.annotation.RestController;
@@ -34,12 +33,11 @@
3433
@AllArgsConstructor
3534
public class MapsController {
3635
private final MapService mapService;
37-
private final FafApiProperties fafApiProperties;
3836
private final ObjectMapper objectMapper;
3937
private final PlayerService playerService;
4038

4139

42-
@RequestMapping(path = "/validate", method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE)
40+
@GetMapping(path = "/validate", produces = APPLICATION_JSON_UTF8_VALUE)
4341
public ModelAndView showValidationForm(Map<String, Object> model) {
4442
return new ModelAndView("validate_map_metadata.html");
4543
}
@@ -49,11 +47,7 @@ public ModelAndView showValidationForm(Map<String, Object> model) {
4947
@ApiResponse(code = 200, message = "Information about derived names to be used in the scenario.lua"),
5048
@ApiResponse(code = 422, message = "A list of reasons why the name is not valid.")
5149
})
52-
@RequestMapping(
53-
path = "/validateMapName",
54-
method = RequestMethod.POST,
55-
produces = APPLICATION_JSON_UTF8_VALUE
56-
)
50+
@PostMapping(path = "/validateMapName", produces = APPLICATION_JSON_UTF8_VALUE)
5751
public MapNameValidationResponse validateMapName(@RequestParam("mapName") String mapName) {
5852
return mapService.requestMapNameValidation(mapName);
5953
}
@@ -62,11 +56,7 @@ public MapNameValidationResponse validateMapName(@RequestParam("mapName") String
6256
@ApiResponses(value = {
6357
@ApiResponse(code = 200, message = "Valid without further information"),
6458
@ApiResponse(code = 422, message = "A list of errors in the scenario.lua")})
65-
@RequestMapping(
66-
path = "/validateScenarioLua",
67-
method = RequestMethod.POST,
68-
produces = APPLICATION_JSON_UTF8_VALUE
69-
)
59+
@PostMapping(path = "/validateScenarioLua", produces = APPLICATION_JSON_UTF8_VALUE)
7060
public void validateScenarioLua(@RequestParam(name = "scenarioLua") String scenarioLua) {
7161
mapService.validateScenarioLua(scenarioLua);
7262
}
@@ -76,12 +66,13 @@ public void validateScenarioLua(@RequestParam(name = "scenarioLua") String scena
7666
@ApiResponse(code = 200, message = "Success"),
7767
@ApiResponse(code = 401, message = "Unauthorized"),
7868
@ApiResponse(code = 500, message = "Failure")})
79-
@RequestMapping(path = "/upload", method = RequestMethod.POST, produces = APPLICATION_JSON_UTF8_VALUE)
69+
@PostMapping(path = "/upload", produces = APPLICATION_JSON_UTF8_VALUE)
8070
@PreAuthorize("hasScope('" + OAuthScope._UPLOAD_MAP + "')")
81-
public void uploadMap(@RequestParam("file") MultipartFile file,
82-
@Deprecated @RequestParam(value = "metadata", required = false) String metadataJsonString,
83-
@RequestPart(value = "metadata", required = false) MapUploadMetadata metadata,
84-
Authentication authentication) throws IOException {
71+
public void uploadMap(
72+
@RequestParam("file") MultipartFile file,
73+
@Deprecated @RequestParam(value = "metadata", required = false) String metadataJsonString,
74+
@RequestPart(value = "metadata", required = false) MapUploadMetadata metadata
75+
) throws IOException {
8576
if (metadataJsonString == null && metadata == null) {
8677
throw ApiException.of(ErrorCode.PARAMETER_MISSING, "metadata");
8778
}
@@ -102,7 +93,7 @@ public void uploadMap(@RequestParam("file") MultipartFile file,
10293
licenseId = metadata.licenseId();
10394
}
10495

105-
Player player = playerService.getPlayer(authentication);
96+
Player player = playerService.getCurrentPlayer();
10697
mapService.uploadMap(file.getInputStream(), file.getOriginalFilename(), player, ranked, licenseId);
10798
}
10899
}

src/main/java/com/faforever/api/mod/ModsController.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.faforever.api.mod;
22

3-
import com.faforever.api.config.FafApiProperties;
43
import com.faforever.api.player.PlayerService;
54
import com.faforever.api.security.OAuthScope;
65
import io.swagger.annotations.ApiOperation;
76
import io.swagger.annotations.ApiResponse;
87
import io.swagger.annotations.ApiResponses;
98
import lombok.RequiredArgsConstructor;
109
import org.springframework.security.access.prepost.PreAuthorize;
11-
import org.springframework.security.core.Authentication;
10+
import org.springframework.web.bind.annotation.PostMapping;
1211
import org.springframework.web.bind.annotation.RequestMapping;
13-
import org.springframework.web.bind.annotation.RequestMethod;
1412
import org.springframework.web.bind.annotation.RequestParam;
1513
import org.springframework.web.bind.annotation.RequestPart;
1614
import org.springframework.web.bind.annotation.RestController;
@@ -28,27 +26,26 @@ public class ModsController {
2826

2927
private final PlayerService playerService;
3028
private final ModService modService;
31-
private final FafApiProperties fafApiProperties;
3229

3330
@ApiOperation("Upload a mod")
3431
@ApiResponses(value = {
3532
@ApiResponse(code = 200, message = "Success"),
3633
@ApiResponse(code = 401, message = "Unauthorized"),
3734
@ApiResponse(code = 500, message = "Failure")})
38-
@RequestMapping(path = "/upload", method = RequestMethod.POST, produces = APPLICATION_JSON_UTF8_VALUE)
35+
@PostMapping(path = "/upload", produces = APPLICATION_JSON_UTF8_VALUE)
3936
@PreAuthorize("hasScope('" + OAuthScope._UPLOAD_MOD + "')")
4037
public void uploadMod(
4138
@RequestParam("file") MultipartFile file,
42-
@RequestPart(value = "metadata", required = false) ModUploadMetadata metadata, //TODO make required when implemented by client
43-
Authentication authentication) throws IOException {
39+
@RequestPart(value = "metadata", required = false) ModUploadMetadata metadata //TODO make required when implemented by client
40+
) throws IOException {
4441

4542
Path tempFile = java.nio.file.Files.createTempFile("mod", ".tmp");
4643
file.transferTo(tempFile.toFile());
4744

4845
modService.processUploadedMod(
4946
tempFile,
5047
file.getOriginalFilename(),
51-
playerService.getPlayer(authentication),
48+
playerService.getCurrentPlayer(),
5249
metadata != null ? metadata.licenseId() : null,
5350
metadata != null ? metadata.repositoryUrl() : null
5451
);

src/main/java/com/faforever/api/player/PlayerService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PlayerService {
1616

1717
private final PlayerRepository playerRepository;
1818

19-
public Player getPlayer() {
19+
public Player getCurrentPlayer() {
2020
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
2121

2222
if (authentication instanceof FafAuthenticationToken fafAuthenticationToken) {

0 commit comments

Comments
 (0)