Skip to content

Commit

Permalink
Basic tests for ban
Browse files Browse the repository at this point in the history
Fix Comments from PR #87 Review
  • Loading branch information
IDragonfire committed May 13, 2017
1 parent b579437 commit 5cd7717
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BasePermission extends UserCheck {

@Override
public boolean ok(User user) {
return user.getOpaqueUser() instanceof FafUserDetails &&
((FafUserDetails) user.getOpaqueUser()).hasPermission(permission);
return user.getOpaqueUser() instanceof FafUserDetails
&& ((FafUserDetails) user.getOpaqueUser()).hasPermission(permission);
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/faforever/api/data/domain/BanInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@UpdatePermission(expression = HasBanUpdate.EXPRESSION)
@Setter
public class BanInfo {
// TODO: Use AbstractEntity class
// TODO: Use AbstractEntity class #73
private int id;
private Player player;
private Player author;
Expand Down Expand Up @@ -94,16 +94,16 @@ public OffsetDateTime getUpdateTime() {
return updateTime;
}

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
// Cascading is needed for Create & Delete
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id")
public BanRevokeData getBanRevokeData() {
return banRevokeData;
}

@Transient
public BanDurationType getDuration() {
return (expiresAt == null) ? BanDurationType.PERMANENT : BanDurationType.TEMPORARY;
return expiresAt == null ? BanDurationType.PERMANENT : BanDurationType.TEMPORARY;
}

@Transient
Expand All @@ -114,8 +114,8 @@ public BanStatus getBanStatus() {
if (getDuration() == BanDurationType.PERMANENT) {
return BanStatus.BANNED;
}
return (expiresAt.isBefore(OffsetDateTime.now()))
? BanStatus.BANNED
: BanStatus.EXPIRED;
return expiresAt.isBefore(OffsetDateTime.now())
? BanStatus.BANNED
: BanStatus.EXPIRED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@CreatePermission(expression = HasBanUpdate.EXPRESSION)
@UpdatePermission(expression = HasBanUpdate.EXPRESSION)
public class BanRevokeData {
// TODO: Use AbstractEntity class
// TODO: Use AbstractEntity class #73
private int id;
private OffsetDateTime createTime;
private OffsetDateTime updateTime;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/faforever/api/data/domain/Login.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.IsLoginOwner;
import com.yahoo.elide.annotation.ComputedAttribute;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;
Expand All @@ -14,7 +15,7 @@
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -30,6 +31,10 @@ public abstract class Login {
private String userAgent;
private List<BanInfo> bans;

public Login() {
this.bans = new ArrayList<>(0);
}

@Id
@GeneratedValue
public int getId() {
Expand Down Expand Up @@ -62,13 +67,11 @@ public String getUserAgent() {
// Permission is managed by BanInfo class
@UpdatePermission(expression = "Prefab.Role.All")
public List<BanInfo> getBans() {
if (this.bans == null) {
this.bans = Collections.emptyList();
}
return this.bans;
}

@Transient
@ComputedAttribute
public List<BanInfo> getActiveBans() {
return getBans().stream().filter(ban -> ban.getBanStatus() == BanStatus.BANNED).collect(Collectors.toList());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/faforever/api/security/FafUserDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FafUserDetails extends org.springframework.security.core.userdetail
private final int id;

public FafUserDetails(User user) {
// TODO implement lobby_admin
// TODO implement lobby_admin #81
this(user.getId(), user.getLogin(), user.getPassword(), !user.isGlobalBanned(), singletonList(new SimpleGrantedAuthority("ROLE_USER")));
}

Expand All @@ -26,7 +26,7 @@ public FafUserDetails(int id, String username, String password, boolean accountN


public boolean hasPermission(String permission) {
// TODO: implement permission system
// TODO: implement permission system #81
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public OAuthClientDetails(OAuthClient oAuthClient) {
super(oAuthClient.getId(),
null,
oAuthClient.getDefaultScope().replace(' ', ','),
// FIXME read from database instead of hardcoding (but DB migration is required)
// FIXME read from database instead of hardcoding (but DB migration is required) #68
"authorization_code,refresh_token,implicit,password,client_credentials",
null);
setClientSecret(oAuthClient.getClientSecret());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.faforever.api.data;

import com.faforever.integration.TestDatabase;
import com.faforever.integration.factories.BanFactory;
import com.faforever.integration.utils.MockMvcHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;
import javax.servlet.Filter;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@Import(TestDatabase.class)
public class JsonApiBanIntegrationTest {

private MockMvc mvc;
private WebApplicationContext context;
private Filter springSecurityFilterChain;

private TestDatabase database;

@Inject
public void init(TestDatabase database, WebApplicationContext context, Filter springSecurityFilterChain) {
this.context = context;
this.springSecurityFilterChain = springSecurityFilterChain;
this.database = database;
}

@Before
public void setUp() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.addFilter(springSecurityFilterChain)
.build();
database.assertEmptyDatabase();
}

@After
public void tearDown() {
// TODO: This is needed, because Elide has some problems with @Transactional annotation #71
database.tearDown();
}

@Test
public void getBansWithoutToken() throws Exception {
BanFactory.builder().database(database).build();
assertEquals(1, database.getBanRepository().count());
assertEquals(2, database.getPlayerRepository().count());

MockMvcHelper.of(this.mvc).perform(
get("/data/banInfo"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data", hasSize(0)));

assertEquals(1, database.getBanRepository().count());
assertEquals(2, database.getPlayerRepository().count());
}

// TODO: implement more tests if permission system is ready #81
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void setUp() {

@After
public void tearDown() {
// TODO: This is needed, because Elide has some problems with @Transactional annotation
// TODO: This is needed, because Elide has some problems with @Transactional annotation #71
database.tearDown();
}

Expand Down
8 changes: 7 additions & 1 deletion src/test/java/com/faforever/integration/TestDatabase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faforever.integration;

import com.faforever.api.ban.BanRepository;
import com.faforever.api.clan.ClanMembershipRepository;
import com.faforever.api.clan.ClanRepository;
import com.faforever.api.client.OAuthClientRepository;
Expand All @@ -20,22 +21,26 @@ public class TestDatabase {
private ClanMembershipRepository clanMembershipRepository;
private PlayerRepository playerRepository;
private OAuthClientRepository oAuthClientRepository;
private BanRepository banRepository;


@Inject
public void init(ClanRepository clanRepository,
UserRepository userRepository,
PlayerRepository playerRepository,
OAuthClientRepository oAuthClientRepository,
ClanMembershipRepository clanMembershipRepository) {
ClanMembershipRepository clanMembershipRepository,
BanRepository banRepository) {
this.clanRepository = clanRepository;
this.userRepository = userRepository;
this.playerRepository = playerRepository;
this.oAuthClientRepository = oAuthClientRepository;
this.clanMembershipRepository = clanMembershipRepository;
this.banRepository = banRepository;
}

public void assertEmptyDatabase() {
assertEquals(0, banRepository.count());
assertEquals(0, clanRepository.count());
assertEquals(0, userRepository.count());
assertEquals(0, playerRepository.count());
Expand All @@ -44,6 +49,7 @@ public void assertEmptyDatabase() {
}

public void tearDown() {
banRepository.deleteAll();
clanMembershipRepository.deleteAll();
clanRepository.deleteAll();
userRepository.deleteAll();
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/faforever/integration/factories/BanFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.faforever.integration.factories;

import com.faforever.api.data.domain.BanInfo;
import com.faforever.api.data.domain.BanLevel;
import com.faforever.api.data.domain.Player;
import com.faforever.integration.TestDatabase;
import lombok.Builder;
import lombok.SneakyThrows;
import org.springframework.util.Assert;

import java.time.OffsetDateTime;

public class BanFactory {
public static final String PLAYER_NAME_BANNED = "JUnitBannedPlayer";
public static final String PLAYER_NAME_AUTHOR = "JUnitBanAuthor";
public static final String DEFAULT_REASON = "This is a cool ban reason";


@Builder
@SneakyThrows
public static BanInfo createBan(Player player, Player author, String reason,
BanLevel level, OffsetDateTime expiresAt, TestDatabase database) {
Assert.notNull(database, "'database' must not be null");
BanInfo ban = new BanInfo()
.setPlayer(player != null ? player : PlayerFactory.createPlayer(PLAYER_NAME_BANNED, database))
.setAuthor(author != null ? author : PlayerFactory.createPlayer(PLAYER_NAME_AUTHOR, database))
.setExpiresAt(expiresAt)
.setLevel(level)
.setReason(reason != null ? reason : DEFAULT_REASON);
return database.getBanRepository().save(ban);
}

}

0 comments on commit 5cd7717

Please sign in to comment.