Skip to content

Commit

Permalink
fixes #178 MySQL returns BigInteger on datediff()
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Nov 24, 2017
1 parent 61d34af commit a5b69ad
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/inttest/java/com/faforever/api/user/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,24 @@ public void changeUsernameSuccess() throws Exception {

assertThat(userRepository.findOne(1).getLogin(), is(NEW_USER));
}

@Test
@WithUserDetails(AUTH_MODERATOR)
public void changeUsernameTooEarly() throws Exception {
assertThat(userRepository.findOne(2).getLogin(), is(AUTH_MODERATOR));

MultiValueMap<String, String> params = new HttpHeaders();
params.add("newUsername", NEW_USER);

MvcResult result = mockMvc.perform(
post("/users/changeUsername")
.with(getOAuthToken(OAuthScope._WRITE_ACCOUNT_DATA))
.params(params))
.andExpect(status().is4xxClientError())
.andReturn();

assertApiError(result, ErrorCode.USERNAME_CHANGE_TOO_EARLY);

assertThat(userRepository.findOne(2).getLogin(), is(AUTH_MODERATOR));
}
}
3 changes: 3 additions & 0 deletions src/inttest/resources/sql/prepDefaultUser.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ VALUES (1, 'USER', 'user@faforever.com', '92b7b421992ef490f3b75898ec0e511f1a5c02
INSERT INTO lobby_admin (user_id, `group`) VALUES
(2, 1),
(3, 2);

INSERT INTO name_history (change_time, user_id, previous_name) VALUES
(NOW(), 2, 'OLD_MODERATOR');
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigInteger;
import java.util.Optional;

public interface NameRecordRepository extends JpaRepository<NameRecord, Integer> {
@Query(value = "SELECT datediff(now(), change_time) FROM name_history WHERE user_id = :userId AND datediff(now(), change_time) <= :maximumDaysAgo ORDER BY change_time DESC LIMIT 1", nativeQuery = true)
Optional<Integer> getDaysSinceLastNewRecord(@Param("userId") Integer userId, @Param("maximumDaysAgo") Integer maximumDaysAgo);
Optional<BigInteger> getDaysSinceLastNewRecord(@Param("userId") Integer userId, @Param("maximumDaysAgo") Integer maximumDaysAgo);

@Query(value = "SELECT user_id FROM name_history WHERE previous_name = :name AND now() > date_add(change_time, INTERVAL :months MONTH) ORDER BY change_time DESC LIMIT 1", nativeQuery = true)
Optional<Integer> getLastUsernameOwnerWithinMonths(@Param("name") String name, @Param("months") Integer months);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/faforever/api/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void changeLogin(String newLogin, User user) {
int minDaysBetweenChange = properties.getUser().getMinimumDaysBetweenUsernameChange();
nameRecordRepository.getDaysSinceLastNewRecord(user.getId(), minDaysBetweenChange)
.ifPresent(daysSinceLastRecord -> {
throw new ApiException(new Error(ErrorCode.USERNAME_CHANGE_TOO_EARLY, minDaysBetweenChange - daysSinceLastRecord));
throw new ApiException(new Error(ErrorCode.USERNAME_CHANGE_TOO_EARLY, minDaysBetweenChange - daysSinceLastRecord.intValue()));
});

int usernameReservationTimeInMonths = properties.getUser().getUsernameReservationTimeInMonths();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/faforever/api/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -217,7 +218,7 @@ public void changeLoginWithInvalidUsername() {
@Test
public void changeLoginTooEarly() {
expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.USERNAME_CHANGE_TOO_EARLY));
when(nameRecordRepository.getDaysSinceLastNewRecord(anyInt(), anyInt())).thenReturn(Optional.of(5));
when(nameRecordRepository.getDaysSinceLastNewRecord(anyInt(), anyInt())).thenReturn(Optional.of(BigInteger.valueOf(5)));

User user = createUser(TEST_USERID, TEST_USERNAME, TEST_CURRENT_PASSWORD, TEST_EMAIL);
instance.changeLogin(TEST_USERNAME_CHANED, user);
Expand Down

0 comments on commit a5b69ad

Please sign in to comment.