Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #249 #250

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}