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

feat : auth 유효 검증 api 구현 #108

Merged
merged 3 commits into from
Sep 8, 2024
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false 조건을
로비 가없을때,
로비에서 게임시작전 플레이어(이름)가 없을때,
게임이 있을때 플레이어(이름)가 없는경우

라고 생각하면되는거지?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅇㅇ 맞음

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러번 번복해서 죄송한데, game exists 라는 네이밍이 개인적으로는 어색한 것 같아요. 사실 로비가 있는것도 게임이 생성된 것이니 괜찮은 것 같기도 하지만 딱 한번에 와닿지는 않는 느낌?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네이밍이라고 하면 api말하시는건가요? 아니면 controller의 isGameExist말하시는 건가요? 저도 하면서 exists보다는 auth의 유효성 검사니까 valid가 더 맞는거 같던데...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Comment on lines +91 to +94
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

역시 개발자는 코드로 이야기하나보네요 ㅋㅋㅋ 이건 이해가 확실히 되네요.

}
}
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
Loading