summaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidator.kt16
-rw-r--r--backend/src/test/kotlin/org/luxons/sevenwonders/SevenWondersTest.kt4
-rw-r--r--backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt4
3 files changed, 8 insertions, 16 deletions
diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidator.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidator.kt
index 138791fb..5f704357 100644
--- a/backend/src/main/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidator.kt
+++ b/backend/src/main/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidator.kt
@@ -3,7 +3,6 @@ package org.luxons.sevenwonders.validation
import org.luxons.sevenwonders.repositories.LobbyRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
-import java.util.regex.Matcher
import java.util.regex.Pattern
@Component
@@ -23,7 +22,7 @@ class DestinationAccessValidator @Autowired constructor(private val lobbyReposit
if (!gameMatcher.matches()) {
return false // no game reference is always OK
}
- val gameId = extractId(gameMatcher)
+ val gameId = gameMatcher.group("id").toLong()
return !isUserInLobby(username, gameId)
}
@@ -32,24 +31,17 @@ class DestinationAccessValidator @Autowired constructor(private val lobbyReposit
if (!lobbyMatcher.matches()) {
return false // no lobby reference is always OK
}
- val lobbyId = extractId(lobbyMatcher)
+ val lobbyId = lobbyMatcher.group("id").toLong()
return !isUserInLobby(username, lobbyId)
}
- private fun isUserInLobby(username: String, lobbyId: Int): Boolean {
- val lobby = lobbyRepository.find(lobbyId.toLong())
- return lobby.containsUser(username)
- }
+ private fun isUserInLobby(username: String, lobbyId: Long): Boolean =
+ lobbyRepository.find(lobbyId).containsUser(username)
companion object {
private val lobbyDestination = Pattern.compile(".*?/lobby/(?<id>\\d+?)(/.*)?")
private val gameDestination = Pattern.compile(".*?/game/(?<id>\\d+?)(/.*)?")
-
- private fun extractId(matcher: Matcher): Int {
- val id = matcher.group("id")
- return Integer.parseInt(id)
- }
}
}
diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/SevenWondersTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/SevenWondersTest.kt
index e8ffbe42..b98d9b91 100644
--- a/backend/src/test/kotlin/org/luxons/sevenwonders/SevenWondersTest.kt
+++ b/backend/src/test/kotlin/org/luxons/sevenwonders/SevenWondersTest.kt
@@ -100,7 +100,7 @@ class SevenWondersTest {
var receivedLobbies = games.next()
assertNotNull(receivedLobbies)
- assertEquals(0, receivedLobbies.size.toLong())
+ assertEquals(0, receivedLobbies.size)
val ownerSession = newPlayer("GameOwner")
val gameName = "Test Game"
@@ -108,7 +108,7 @@ class SevenWondersTest {
receivedLobbies = games.next()
assertNotNull(receivedLobbies)
- assertEquals(1, receivedLobbies.size.toLong())
+ assertEquals(1, receivedLobbies.size)
val receivedLobby = receivedLobbies[0]
assertEquals(createdLobby.id, receivedLobby.id)
assertEquals(createdLobby.name, receivedLobby.name)
diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt
index 1d8b0f4d..aae431f4 100644
--- a/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt
+++ b/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt
@@ -118,8 +118,8 @@ class LobbyTest {
}
private fun addPlayers(nbPlayers: Int) {
- for (i in 0 until nbPlayers) {
- val player = Player("testuser$i", "Test User $i")
+ repeat(nbPlayers) {
+ val player = Player("testuser$it", "Test User $it")
lobby.addPlayer(player)
}
}
bgstack15