Skip to content

Commit

Permalink
Fixes #249
Browse files Browse the repository at this point in the history
  • Loading branch information
1-alex98 committed Sep 9, 2018
1 parent d9f737d commit fef9aac
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
95 changes: 95 additions & 0 deletions src/inttest/java/com/faforever/api/data/VotingElideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -49,6 +51,84 @@ public class VotingElideTest extends AbstractIntegrationTest {
" }\n" +
"}";

/*
{"data":
{"type":"votingSubject",
"attributes":{
"subjectKey":"bla",
"numberOfVotes":0,
"topicUrl":"test",
"beginOfVoteTime":"2018-09-09T11:00:00Z",
"endOfVoteTime":"2018-09-09T11:00:00Z",
"minGamesToVote":0,
"descriptionKey":"test",
"revealWinner":false},
"relationships":{
"votingQuestions":{
"data":[]
}
}
}
}
*/
private static final String CREATE_VOTING_SUBJECT_REVEAL_WINNER_FALSE = "{\"data\":\n" +
" {\"type\":\"votingSubject\",\n" +
" \"attributes\":{\n" +
" \"subjectKey\":\"bla\",\n" +
" \"numberOfVotes\":0,\n" +
" \"topicUrl\":\"test\",\n" +
" \"beginOfVoteTime\":\"2018-09-09T11:00:00Z\",\n" +
" \"endOfVoteTime\":\"2018-09-09T11:00:00Z\",\n" +
" \"minGamesToVote\":0,\n" +
" \"descriptionKey\":\"test\",\n" +
" \"revealWinner\":false},\n" +
" \"relationships\":{\n" +
" \"votingQuestions\":{\n" +
" \"data\":[]\n" +
" }\n" +
" }\n" +
" }\n" +
" }";

/*
{"data":
{"type":"votingSubject",
"attributes":{
"subjectKey":"bla",
"numberOfVotes":0,
"topicUrl":"test",
"beginOfVoteTime":"2018-09-09T11:00:00Z",
"endOfVoteTime":"2018-09-09T11:00:00Z",
"minGamesToVote":0,
"descriptionKey":"test",
"revealWinner":true},
"relationships":{
"votingQuestions":{
"data":[]
}
}
}
}
*/
private static final String CREATE_VOTING_SUBJECT_REVEAL_WINNER_TRUE = "{\"data\":\n" +
" {\"type\":\"votingSubject\",\n" +
" \"attributes\":{\n" +
" \"subjectKey\":\"bla\",\n" +
" \"numberOfVotes\":0,\n" +
" \"topicUrl\":\"test\",\n" +
" \"beginOfVoteTime\":\"2018-09-09T11:00:00Z\",\n" +
" \"endOfVoteTime\":\"{end-time}\",\n" +
" \"minGamesToVote\":0,\n" +
" \"descriptionKey\":\"test\",\n" +
" \"revealWinner\":true},\n" +
" \"relationships\":{\n" +
" \"votingQuestions\":{\n" +
" \"data\":[]\n" +
" }\n" +
" }\n" +
" }\n" +
" }";

/*
{
"data": {
Expand Down Expand Up @@ -222,4 +302,19 @@ public void postVoteOnEndedSubject() throws Exception {
mockMvc.perform(post("/voting/vote").contentType(MediaType.APPLICATION_JSON).content(POST_VOTE_SUBJECT2).with(getOAuthToken(OAuthScope._VOTE)))
.andExpect(status().is(422));
}

@Test
@WithUserDetails(AUTH_MODERATOR)
public void postVotingSubject() throws Exception {
mockMvc.perform(post("/data/votingSubject").contentType(MediaType.APPLICATION_JSON).content(CREATE_VOTING_SUBJECT_REVEAL_WINNER_FALSE))
.andExpect(status().is(201));
}

@Test
@WithUserDetails(AUTH_MODERATOR)
public void postVotingSubjectWithRevealWinnerTrueButVoteNotEnded() throws Exception {
String votingSubject = CREATE_VOTING_SUBJECT_REVEAL_WINNER_TRUE.replaceAll("\\{end-time}", OffsetDateTime.now().plusYears(1).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
mockMvc.perform(post("/data/votingSubject").contentType(MediaType.APPLICATION_JSON).content(votingSubject))
.andExpect(status().is(400));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Integer getOrdinal() {

@Transient
@ComputedAttribute
public int getNumberOfAnswers() {
public Integer getNumberOfAnswers() {
return numberOfAnswers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import javax.validation.ConstraintValidatorContext;
import java.time.OffsetDateTime;

/**
* Avoids that a VotingSubject's result is revealed before the vote on the subject ended
*/
public class VotingSubjectRevealWinnerValidator implements ConstraintValidator<VotingSubjectRevealWinnerCheck, VotingSubject> {

@Override
Expand All @@ -15,6 +18,6 @@ public void initialize(VotingSubjectRevealWinnerCheck constraintAnnotation) {

@Override
public boolean isValid(VotingSubject votingSubject, ConstraintValidatorContext context) {
return votingSubject.getRevealWinner() && votingSubject.getEndOfVoteTime().isBefore(OffsetDateTime.now());
return votingSubject.getRevealWinner() != Boolean.TRUE || votingSubject.getEndOfVoteTime().isBefore(OffsetDateTime.now());
}
}

0 comments on commit fef9aac

Please sign in to comment.