-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #253
- Loading branch information
Showing
16 changed files
with
312 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package com.faforever.api.data; | ||
|
||
import com.faforever.api.AbstractIntegrationTest; | ||
import com.faforever.api.email.EmailSender; | ||
import com.faforever.api.player.PlayerRepository; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.security.test.context.support.WithUserDetails; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase; | ||
|
@@ -46,9 +50,11 @@ public class BanInfoTest extends AbstractIntegrationTest { | |
} | ||
} | ||
*/ | ||
private static final String testPost = "{\"data\":{\"type\":\"banInfo\",\"attributes\":{\"level\":\"CHAT\",\"reason\":\"This test ban should be revoked\"},\"relationships\":{\"author\":{\"data\":{\"type\":\"player\",\"id\":\"1\"}},\"player\":{\"data\":{\"type\":\"player\",\"id\":\"3\"}}}}}"; | ||
private static final String testPost = "{\"data\":{\"type\":\"banInfo\",\"attributes\":{\"level\":\"CHAT\",\"reason\":\"This test ban should be revoked\"},\"relationships\":{\"author\":{\"data\":{\"type\":\"player\",\"id\":\"2\"}},\"player\":{\"data\":{\"type\":\"player\",\"id\":\"3\"}}}}}"; | ||
@Autowired | ||
PlayerRepository playerRepository; | ||
@MockBean | ||
private EmailSender emailSender; | ||
|
||
@Test | ||
@WithUserDetails(AUTH_USER) | ||
|
@@ -98,5 +104,11 @@ public void canCreateBanInfoAsModerator() throws Exception { | |
.andExpect(status().isCreated()); | ||
|
||
assertThat(playerRepository.getOne(3).getBans().size(), is(1)); | ||
Mockito.verify(emailSender).sendMail(ArgumentMatchers.eq("[email protected]"), | ||
ArgumentMatchers.eq("[email protected]"), | ||
ArgumentMatchers.eq("[email protected]"), | ||
ArgumentMatchers.eq("ban subject"), | ||
ArgumentMatchers.matches("Hello ADMIN,\\|Your account was banned\\|Reason - This test ban should be revoked\\|Banner - MODERATOR\\|Time - (.)*\\|Type - CHAT\\|Expires - never\\|Thank you for your fairness and acceptance[.]") | ||
); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/inttest/java/com/faforever/api/data/BanRevokeElideTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.faforever.api.data; | ||
|
||
import com.faforever.api.AbstractIntegrationTest; | ||
import com.faforever.api.data.domain.BanStatus; | ||
import com.faforever.api.email.EmailSender; | ||
import com.faforever.api.player.PlayerRepository; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.security.test.context.support.WithUserDetails; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultUser.sql") | ||
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepBanRevokeData.sql") | ||
@Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:sql/cleanBanRevokeData.sql") | ||
public class BanRevokeElideTest extends AbstractIntegrationTest { | ||
@MockBean | ||
private EmailSender emailSender; | ||
@Autowired | ||
private PlayerRepository playerRepository; | ||
/* | ||
{"data":{"type":"banRevokeData","attributes":{"reason":"unban"},"relationships":{"ban":{"data":{"type":"banInfo","id":"1"}},"author":{"data":{"type":"player","id":"2"}}}}} */ | ||
private static final String TEST_REVOKE="{\"data\":{\"type\":\"banRevokeData\",\"attributes\":{\"reason\":\"unban\"},\"relationships\":{\"ban\":{\"data\":{\"type\":\"banInfo\",\"id\":\"1\"}},\"author\":{\"data\":{\"type\":\"player\",\"id\":\"2\"}}}}}"; | ||
|
||
@Ignore(value = "Posting of ban revokes never worked, moderator use to change expire date instead see issue #259") | ||
@WithUserDetails(AUTH_MODERATOR) | ||
@Test | ||
public void testRevokeBanWithId1() throws Exception { | ||
assertThat(playerRepository.getOne(4).getBans().size(), is(1)); | ||
assertThat(playerRepository.getOne(4).getBans().iterator().next().getBanStatus(), is(BanStatus.BANNED)); | ||
|
||
mockMvc.perform(post("/data/banRevokeData") | ||
.content(TEST_REVOKE)) | ||
.andExpect(status().isCreated()); | ||
|
||
assertThat(playerRepository.getOne(4).getBans().iterator().next().getBanStatus(), is(BanStatus.DISABLED)); | ||
Mockito.verify(emailSender).sendMail("","","","",""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DELETE FROM ban_revoke; | ||
DELETE FROM ban; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,9 @@ INSERT INTO login (id, login, email, password) VALUES | |
(4, 'BANNED', '[email protected]', 'not relevant'); | ||
|
||
INSERT INTO ban (id, player_id, author_id, reason, expires_at, level) VALUES | ||
(1, 4, 1, 'Test permaban', DATE_ADD(NOW(), INTERVAL 1 DAY), 'GLOBAL'), | ||
(2, 2, 1, 'To be revoked ban', DATE_ADD(NOW(), INTERVAL 1 DAY), 'GLOBAL'); | ||
(1, 4, 2, 'Test permaban', DATE_ADD(NOW(), INTERVAL 1 DAY), 'GLOBAL'), | ||
(2, 2, 2, 'To be revoked ban', DATE_ADD(NOW(), INTERVAL 1 DAY), 'GLOBAL'); | ||
|
||
INSERT INTO ban_revoke (ban_id, reason, author_id) VALUES | ||
(2, 'Test revoke', 1); | ||
(2, 'Test revoke', 2); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
DELETE FROM ban_revoke; | ||
DELETE FROM ban; | ||
|
||
INSERT INTO login (id, login, email, password) VALUES | ||
(4, 'BANNED', '[email protected]', 'not relevant'); | ||
|
||
INSERT INTO ban (id, player_id, author_id, reason, expires_at, level) VALUES | ||
(1, 4, 2, 'Test permaban', DATE_ADD(NOW(), INTERVAL 1 DAY), 'GLOBAL'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/faforever/api/data/listeners/BanInfoPostCreateListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.faforever.api.data.listeners; | ||
|
||
import com.faforever.api.data.domain.BanDurationType; | ||
import com.faforever.api.data.domain.BanInfo; | ||
import com.faforever.api.data.domain.Player; | ||
import com.faforever.api.email.EmailService; | ||
import com.yahoo.elide.functions.LifeCycleHook; | ||
import com.yahoo.elide.security.RequestScope; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.inject.Inject; | ||
import javax.validation.constraints.NotNull; | ||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@Slf4j | ||
public class BanInfoPostCreateListener implements LifeCycleHook<BanInfo> { | ||
|
||
private final EmailService emailService; | ||
|
||
@Inject | ||
public BanInfoPostCreateListener(EmailService emailService) { | ||
this.emailService = emailService; | ||
} | ||
|
||
@Override | ||
public void execute(BanInfo elideEntity, RequestScope requestScope, Optional changes) { | ||
try { | ||
@NotNull Player player = elideEntity.getPlayer(); | ||
emailService.sendBanMail(player.getEmail(), | ||
player.getLogin(), | ||
elideEntity.getReason(), | ||
elideEntity.getAuthor().getLogin(), | ||
OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME), | ||
elideEntity.getLevel().name(), | ||
elideEntity.getDuration() == BanDurationType.PERMANENT ? "never" : elideEntity.getExpiresAt().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); | ||
} catch (Exception e) { | ||
log.error("Sending ban email failed", e); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/faforever/api/data/listeners/BanRevokePostCreateListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.faforever.api.data.listeners; | ||
|
||
import com.faforever.api.data.domain.BanInfo; | ||
import com.faforever.api.data.domain.BanRevokeData; | ||
import com.faforever.api.data.domain.Player; | ||
import com.faforever.api.email.EmailService; | ||
import com.yahoo.elide.functions.LifeCycleHook; | ||
import com.yahoo.elide.security.ChangeSpec; | ||
import com.yahoo.elide.security.RequestScope; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@Component | ||
public class BanRevokePostCreateListener implements LifeCycleHook<BanRevokeData> { | ||
private final EmailService emailService; | ||
|
||
public BanRevokePostCreateListener(EmailService emailService) { | ||
this.emailService = emailService; | ||
} | ||
|
||
@Override | ||
public void execute(BanRevokeData banRevoke, RequestScope requestScope, Optional<ChangeSpec> changes) { | ||
try { | ||
@NotNull BanInfo ban = banRevoke.getBan(); | ||
@NotNull Player player = ban.getPlayer(); | ||
emailService.sendBanRevokeMail(player.getEmail(), | ||
player.getLogin(), | ||
banRevoke.getAuthor().getLogin(), | ||
banRevoke.getReason(), | ||
ban.getReason(), | ||
ban.getAuthor().getLogin(), | ||
ban.getCreateTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); | ||
} catch (Exception e) { | ||
log.error("Failed to send ban revoke email", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.