Skip to content

Commit

Permalink
feat : auth 유효 검증 api 구현 (#108)
Browse files Browse the repository at this point in the history
* feat : auth 유효 검증 api 구현

* refactor : game 조회 얼리 리턴 이후로 변경

* feat : exist 네이밍 valid로 변경
  • Loading branch information
waterricecake committed Oct 14, 2024
1 parent ec3a223 commit 83dd611
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GameStatusResponse findStatus(final String code) {
private StatusType checkStatusChanged(final Game game) {
game.setStatsSnapshot();
final StatusType statusType = game.getStatusType(Clock.systemDefaultZone().millis());
if (game.isDeleted()){
if (game.isDeleted()) {
gameRepository.delete(game);
return StatusType.WAIT;
}
Expand Down Expand Up @@ -81,4 +81,16 @@ public GameResultResponse findResult(final String code) {
}
return GameResultResponse.from(game);
}

public boolean isValid(final String code, final String name) {
Optional<Lobby> lobby = lobbyRepository.findById(code);
if (!lobby.isPresent()) {
return false;
}
Optional<Game> game = gameRepository.findById(code);
if (!game.isPresent()) {
return lobby.get().isParticipantExist(name);
}
return game.get().isPlayerExist(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mafia.mafiatogether.game.application.dto.response;

public record GameExistResponse(
boolean valid
) {
}
4 changes: 4 additions & 0 deletions src/main/java/mafia/mafiatogether/game/domain/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ public boolean isStatusChanged() {
public boolean isDeleted() {
return status.getType().equals(StatusType.DELETED);
}

public boolean isPlayerExist(final String name) {
return players.contains(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ public void killTarget(final String target) {
final Player targetPlayer = findByName(target);
targetPlayer.kill();
}

public boolean contains(final String name) {
return players.stream().anyMatch(player -> player.getName().equals(name));
}
}
13 changes: 12 additions & 1 deletion src/main/java/mafia/mafiatogether/game/ui/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.config.PlayerInfo;
import mafia.mafiatogether.game.application.GameService;
import mafia.mafiatogether.config.PlayerInfoDto;
import mafia.mafiatogether.game.application.GameService;
import mafia.mafiatogether.game.application.dto.response.GameExistResponse;
import mafia.mafiatogether.game.application.dto.response.GameInfoResponse;
import mafia.mafiatogether.game.application.dto.response.GameResultResponse;
import mafia.mafiatogether.game.application.dto.response.GameStatusResponse;
Expand Down Expand Up @@ -48,4 +49,14 @@ public ResponseEntity<GameInfoResponse> findGameInfo(
) {
return ResponseEntity.ok(gameService.findGameInfo(playerInfoDto.code(), playerInfoDto.name()));
}


@GetMapping("/valid")
public ResponseEntity<GameExistResponse> isValid(
@PlayerInfo final PlayerInfoDto playerInfoDto
) {
return ResponseEntity.ok(
new GameExistResponse(gameService.isValid(playerInfoDto.code(), playerInfoDto.name()))
);
}
}
4 changes: 4 additions & 0 deletions src/main/java/mafia/mafiatogether/lobby/domain/Lobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public void validateToStart() {
public void updateLastUpdateTime() {
this.lastUpdateTime = Clock.systemDefaultZone().millis();
}

public boolean isParticipantExist(final String name) {
return participants.contains(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ private void validateParticipant(Participant participant, int total) {
throw new GameException(ExceptionCode.INVALID_NAMES);
}
}

public boolean contains(String name) {
return participants.stream().anyMatch(participant -> participant.getName().equals(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ChatControllerTest extends ControllerTest {

@BeforeEach
void setTest() {
setRoom();
setLobby();
setGame();

Chat chat = new Chat(CODE, new ArrayList<>());
Expand Down
96 changes: 89 additions & 7 deletions src/test/java/mafia/mafiatogether/game/ui/GameControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package mafia.mafiatogether.game.ui;

import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.hamcrest.Matchers.equalTo;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import java.util.Base64;
import java.util.Map;
import mafia.mafiatogether.config.exception.ErrorResponse;
import mafia.mafiatogether.config.exception.ExceptionCode;
import mafia.mafiatogether.game.application.dto.response.GameInfoResponse;
import mafia.mafiatogether.game.application.dto.response.GameStatusResponse;
import mafia.mafiatogether.game.application.dto.response.PlayerResponse;
import mafia.mafiatogether.game.domain.Game;
import mafia.mafiatogether.game.domain.Player;
import mafia.mafiatogether.game.domain.status.StatusType;
import mafia.mafiatogether.global.ControllerTest;
import mafia.mafiatogether.game.application.dto.response.PlayerResponse;
import mafia.mafiatogether.job.domain.jobtype.JobType;
import mafia.mafiatogether.game.application.dto.response.GameInfoResponse;
import mafia.mafiatogether.game.application.dto.response.GameStatusResponse;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -25,8 +26,8 @@
class GameControllerTest extends ControllerTest {

@BeforeEach
void setTest(){
setRoom();
void setTest() {
setLobby();
}

@Test
Expand Down Expand Up @@ -99,7 +100,7 @@ void setTest(){
}

@Test
void 대기방에서_방장이_방의_정보를_찾는다(){
void 대기방에서_방장이_방의_정보를_찾는다() {
// given
final String basic = Base64.getEncoder().encodeToString((CODE + ":" + PLAYER1_NAME).getBytes());

Expand All @@ -115,7 +116,7 @@ void setTest(){

/// then
assertSoftly(
softly ->{
softly -> {
softly.assertThat(response.isMaster()).isTrue();
softly.assertThat(response.players()).hasSize(4);
}
Expand Down Expand Up @@ -238,4 +239,85 @@ private long countMafiaResponse(final String basic) {
.filter(response -> response.job() != null && response.job().equals(JobType.MAFIA))
.count();
}

@Test
void 게임이_진행되고_있을때_참가한_유저는_유효한지_검증한다() {
// given
setGame();
final String basic = Base64.getEncoder().encodeToString((CODE + ":" + PLAYER1_NAME).getBytes());

// when & then
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.header("Authorization", "Basic " + basic)
.when().get("/games/valid")
.then().log().all()
.statusCode(HttpStatus.OK.value())
.body("valid", equalTo(true));
}

@Test
void 로비만_존재하고_게임이_진행되지_않을때_참가한_유저가_유효한지_검증한다() {
// given
final String basic = Base64.getEncoder().encodeToString((CODE + ":" + PLAYER1_NAME).getBytes());

// when & then
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.header("Authorization", "Basic " + basic)
.when().get("/games/valid")
.then().log().all()
.statusCode(HttpStatus.OK.value())
.body("valid", equalTo(true));
}

@Test
void 유저가_요청한_방이_없는지_검증한다() {
// given
final String code = "123456789fail";
final String basic = Base64.getEncoder().encodeToString((code + ":" + PLAYER1_NAME).getBytes());

// when & then
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.header("Authorization", "Basic " + basic)
.when().get("/games/valid")
.then().log().all()
.statusCode(HttpStatus.OK.value())
.body("valid", equalTo(false));
}

@Test
void 유저가_요청한_방에_존재하는지_검증한다() {
// given
final String invalidPlayer = "invalidPlayer";
final String basic = Base64.getEncoder().encodeToString((CODE + ":" + invalidPlayer).getBytes());

// when & then
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.header("Authorization", "Basic " + basic)
.when().get("/games/valid")
.then().log().all()
.statusCode(HttpStatus.OK.value())
.body("valid", equalTo(false));
}


@Test
void 유저가_요청한_게임에_존재하는지_검증한다() {
// given
setGame();
final String invalidPlayer = "invalidPlayer";
final String basic = Base64.getEncoder().encodeToString((CODE + ":" + invalidPlayer).getBytes());

// when & then
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.header("Authorization", "Basic " + basic)
.when().get("/games/valid")
.then().log().all()
.statusCode(HttpStatus.OK.value())
.body("valid", equalTo(false));
}
}
3 changes: 2 additions & 1 deletion src/test/java/mafia/mafiatogether/global/ControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void setPort() {
RestAssured.port = port;
}

protected void setRoom() {
protected void setLobby() {
final Lobby lobby = Lobby.create(CODE, LobbyInfo.of(5, 2, 1, 1));
lobby.joinPlayer(PLAYER1_NAME);
lobby.joinPlayer(PLAYER2_NAME);
Expand Down Expand Up @@ -89,6 +89,7 @@ private String findPlayerNameByJobType(PlayerCollection players, JobType jobType
.findFirst().get()
.getName();
}

@AfterEach
void clearTest() {
voteRepository.deleteById(CODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Map;
import mafia.mafiatogether.config.exception.ExceptionCode;
import mafia.mafiatogether.global.ControllerTest;
import mafia.mafiatogether.job.domain.PlayerJobRepository;
import mafia.mafiatogether.job.domain.JobTargetRepository;
import mafia.mafiatogether.job.domain.PlayerJobRepository;
import mafia.mafiatogether.job.domain.jobtype.JobType;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -29,7 +29,7 @@ class JobControllerTest extends ControllerTest {

@BeforeEach
void setTest() {
setRoom();
setLobby();
setGame();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VoteControllerTest extends ControllerTest {

@BeforeEach
void setTest(){
setRoom();
setLobby();
setGame();
}

Expand Down

0 comments on commit 83dd611

Please sign in to comment.