Skip to content

Commit

Permalink
feat: 로컬용 FakePhoneAuthenticator 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Nov 6, 2023
1 parent b1b38d5 commit 517cdf0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ jobs:
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} .
docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ services:
spring:
depends_on:
- db
build: .
image: hjaehyun25/busan-reservation:latest
ports:
- '8080:8080'
restart: on-failure
environment:
SPRING_PROFILES_ACTIVE: default
SPRING_DATASOURCE_URL: jdbc:mysql://busan_db:3306/busan
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: password
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/example/busan/auth/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.busan.auth.dto.AuthenticatePhoneRequest;
import com.example.busan.auth.dto.Authentication;
import com.example.busan.auth.dto.FindEmailRequest;
import com.example.busan.auth.dto.FindEmailResponse;
import com.example.busan.auth.dto.LoginRequest;
import com.example.busan.auth.service.AuthService;
import com.example.busan.auth.service.PhoneAuthenticator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
package com.example.busan.auth.config;

import com.example.busan.auth.domain.AuthorizationCodeGenerator;
import com.example.busan.auth.infrastructure.InMemoryPhoneAuthenticator;
import com.example.busan.auth.service.PhoneAuthenticator;
import net.nurigo.sdk.message.service.DefaultMessageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class PhoneAuthenticatorConfig {

private final AuthorizationCodeGenerator codeGenerator;
private final String key;
private final String secret;
private final String sender;

public PhoneAuthenticatorConfig(@Value("${phone.key}") final String key,
@Value("${phone.secret}") final String secret) {
public PhoneAuthenticatorConfig(final AuthorizationCodeGenerator codeGenerator,
@Value("${phone.key}") final String key,
@Value("${phone.secret}") final String secret,
@Value("${phone.sender}") final String sender) {
this.codeGenerator = codeGenerator;
this.key = key;
this.secret = secret;
this.sender = sender;
}

@Profile("prod")
@Bean
public PhoneAuthenticator phoneAuthenticator() {
return new InMemoryPhoneAuthenticator(messageService(), codeGenerator, sender);
}

@Profile("default")
@Bean
public PhoneAuthenticator fakePhoneAuthenticator() {
return new PhoneAuthenticator() {
@Override
public void sendCode(final String phone) {
}

@Override
public void authenticate(final String code) {
}

@Override
public void validateAuthenticated(final String phone) {
}
};
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.busan.auth;
package com.example.busan.auth.dto;

public record FindEmailResponse(String email) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class InMemoryPhoneAuthenticator implements PhoneAuthenticator {

private static final Logger log = LoggerFactory.getLogger(InMemoryPhoneAuthenticator.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.busan.auth.service;

import com.example.busan.auth.FindEmailResponse;
import com.example.busan.auth.domain.PasswordEncoder;
import com.example.busan.auth.dto.Authentication;
import com.example.busan.auth.dto.FindEmailResponse;
import com.example.busan.auth.dto.LoginRequest;
import com.example.busan.member.domain.Member;
import com.example.busan.member.domain.MemberRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.busan.auth.dto.AuthenticatePhoneRequest;
import com.example.busan.auth.dto.Authentication;
import com.example.busan.auth.dto.FindEmailRequest;
import com.example.busan.auth.dto.FindEmailResponse;
import com.example.busan.auth.dto.LoginRequest;
import com.example.busan.auth.service.AuthService;
import com.example.busan.auth.service.PhoneAuthenticator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.busan.auth.domain.PasswordEncoder;
import com.example.busan.auth.dto.Authentication;
import com.example.busan.auth.dto.FindEmailRequest;
import com.example.busan.auth.dto.FindEmailResponse;
import com.example.busan.auth.dto.LoginRequest;
import com.example.busan.auth.service.AuthService;
import com.example.busan.member.domain.Member;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@
import net.nurigo.sdk.message.service.DefaultMessageService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;
import static org.mockito.Mockito.mock;

@SpringBootTest
class InMemoryPhoneAuthenticatorTest {

@Autowired
private InMemoryPhoneAuthenticator phoneAuthenticator;
@MockBean
private DefaultMessageService messageService;
@MockBean
private AuthorizationCodeGenerator codeGenerator;
private final DefaultMessageService messageService = mock(DefaultMessageService.class);
private final AuthorizationCodeGenerator codeGenerator = mock(AuthorizationCodeGenerator.class);
private final InMemoryPhoneAuthenticator phoneAuthenticator = new InMemoryPhoneAuthenticator(messageService, codeGenerator, "01012341234");

@Test
@DisplayName("휴대폰 인증 코드를 전송할 수 있다")
Expand All @@ -32,10 +28,8 @@ void sendCode() {
given(codeGenerator.generate())
.willReturn("123455");

final String phone = "01012345";

//when
phoneAuthenticator.sendCode(phone);
phoneAuthenticator.sendCode("01012345678");

//then
verify(messageService, times(1)).sendOne(any());
Expand Down

0 comments on commit 517cdf0

Please sign in to comment.