Add permissions for each account

This commit is contained in:
Rebwon
2021-09-07 18:20:47 +09:00
committed by MaengSol
parent 6f21844d23
commit 8448c62343
8 changed files with 102 additions and 33 deletions

View File

@@ -20,11 +20,13 @@ public final class Account {
private LocalDateTime lastModifiedAt;
private LocalDateTime withdrawalAt;
private boolean withdraw = false;
private Role role;
private Account(String email, String nickname, String password) {
this.email = email;
this.nickname = nickname;
this.password = password;
this.role = Role.DEFAULT;
}
public static Account of(String email, String nickname, String password) {

View File

@@ -0,0 +1,14 @@
package com.yam.app.account.domain;
import java.util.Arrays;
public enum Role {
DEFAULT, LOCALIZED, ADMIN;
public static Role findRole(String role) {
return Arrays.stream(Role.values())
.filter(r -> r.name().equals(role))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
}

View File

@@ -0,0 +1,35 @@
package com.yam.app.account.infrastructure;
import com.yam.app.account.domain.Role;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
@MappedTypes(RoleTypeHandler.class)
public final class RoleTypeHandler implements TypeHandler<Role> {
@Override
public void setParameter(PreparedStatement ps, int i,
Role parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter.name());
}
@Override
public Role getResult(ResultSet rs, String columnName) throws SQLException {
return Role.findRole(rs.getString(columnName));
}
@Override
public Role getResult(ResultSet rs, int columnIndex) throws SQLException {
return Role.findRole(rs.getString(columnIndex));
}
@Override
public Role getResult(CallableStatement cs, int columnIndex) throws SQLException {
return Role.findRole(cs.getString(columnIndex));
}
}

View File

@@ -40,7 +40,7 @@ public final class AccountCommandApi {
*/
@GetMapping("/api/accounts/authorize")
public ResponseEntity<Void> registerConfirm(
@ModelAttribute ConfirmRegisterAccountRequest request) throws Exception {
@ModelAttribute @Valid ConfirmRegisterAccountRequest request) throws Exception {
try {
accountFacade.registerConfirm(request);
} catch (Exception e) {

View File

@@ -10,16 +10,19 @@
email_verified = #{emailVerified},
nickname = #{nickname},
password = #{password},
withdraw = #{withdraw}
withdraw = #{withdraw},
joined_at = #{joinedAt},
role = #{role},
last_modified_at = NOW()
WHERE id = #{id}
</update>
<insert id="save" parameterType="com.yam.app.account.domain.Account">
INSERT
INTO ACCOUNT(email, email_check_token, email_check_token_generated_at, email_verified,
nickname, password, withdraw)
nickname, password, withdraw, role)
VALUES (#{email}, #{emailCheckToken}, #{emailCheckTokenGeneratedAt}, #{emailVerified},
#{nickname}, #{password}, #{withdraw})
#{nickname}, #{password}, #{withdraw}, #{role})
</insert>
</mapper>

View File

@@ -7,6 +7,7 @@ create table account
email_verified boolean not null,
joined_at timestamp,
last_modified_at timestamp,
role varchar(255) not null,
nickname varchar(255) not null,
password varchar(255) not null,
withdraw boolean not null,
@@ -20,5 +21,6 @@ alter table account
add constraint UK_s2a5omeaik0sruawqpvs18qfk unique (nickname);
insert into account(email, email_check_token, email_check_token_generated_at, email_verified,
joined_at, last_modified_at, nickname, password, withdraw)
values ('jiwonDev@gmail.com', 'emailchecktoken', now(), true, now(), now(), 'jiwon', 'password!', false);
joined_at, last_modified_at, nickname, password, withdraw, role)
values ('jiwonDev@gmail.com', 'emailchecktoken', now(), true, now(), now(), 'jiwon', 'password!',
false, 'DEFAULT');

View File

@@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import com.yam.app.account.domain.Account;
import com.yam.app.account.domain.AccountReader;
import com.yam.app.account.domain.AccountRepository;
import com.yam.app.account.domain.Role;
import java.util.Optional;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
@@ -53,7 +54,7 @@ final class MybatisAccountRepositoryTest {
Account account = accountRepository.save(
Account.of("rebwon@gmail.com", "rebwon", "password!"));
assertThat(account.getId()).isEqualTo(2);
assertThat(account.getRole()).isEqualTo(Role.DEFAULT);
}
@Test

View File

@@ -37,21 +37,25 @@ final class AccountCommandApiTests {
@DisplayName("이메일 검증 HTTP API")
class RegisterConfirmApi {
private static final String EMAIL_AUTHORIZE_API = "/api/accounts/authorize";
private static final String TOKEN = "token";
private static final String EMAIL = "email";
@ParameterizedTest
@NullAndEmptySource
@DisplayName("HTTP 파라메타가 비었거나 null인 검증요청을 보낸 경우 400 HTTP Code 리턴한다.")
void http_param_is_empty_or_null(String args) throws Exception {
// Act
// Arrange
var request = new ConfirmRegisterAccountRequest();
request.setToken(args);
request.setEmail(args);
doThrow(IllegalStateException.class).when(accountFacade).registerConfirm(request);
final var actions = mockMvc.perform(get("/api/accounts/authorize")
// Act
final var actions = mockMvc.perform(get(EMAIL_AUTHORIZE_API)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.param("token", args)
.param("email", args)
.param(TOKEN, args)
.param(EMAIL, args)
);
// Assert
@@ -60,20 +64,23 @@ final class AccountCommandApiTests {
.andExpect(status().isBadRequest());
}
@Test
@ParameterizedTest
@AutoSource
@DisplayName("HTTP 파라메타가 유효하지 않은 값으로 검증요청을 보낸 경우 400 HTTP Code 리턴한다.")
void http_param_is_not_valid() throws Exception {
// Act
void http_param_is_not_valid(String arg) throws Exception {
// Arrange
var request = new ConfirmRegisterAccountRequest();
request.setToken("QWEIUHQWDU");
request.setEmail("QWEIOWQJE@naver.com");
request.setToken(arg);
request.setEmail(arg);
// Act
doThrow(IllegalStateException.class).when(accountFacade).registerConfirm(request);
final var actions = mockMvc.perform(get("/api/accounts/authorize")
final var actions = mockMvc.perform(get(EMAIL_AUTHORIZE_API)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.param("token", "QWEIUHQWDU")
.param("email", "QWEIOWQJE@naver.com")
.param(TOKEN, arg)
.param(EMAIL, arg)
);
// Assert
@@ -85,16 +92,17 @@ final class AccountCommandApiTests {
@Test
@DisplayName("토큰과 이메일 정보로 검증요청을 보낸 경우 303 HTTP Code 리턴한다.")
void valid_success() throws Exception {
// Act
// Arrange
var request = new ConfirmRegisterAccountRequest();
request.setToken("emailTOken");
request.setEmail("jiwonDev@gmail.com");
final var actions = mockMvc.perform(get("/api/accounts/authorize")
// Act
final var actions = mockMvc.perform(get(EMAIL_AUTHORIZE_API)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.param("token", "emailTOken")
.param("email", "jiwonDev@gmail.com")
.param(TOKEN, "emailTOken")
.param(EMAIL, "jiwonDev@gmail.com")
);
// Assert
@@ -108,6 +116,8 @@ final class AccountCommandApiTests {
@DisplayName("회원가입 등록 HTTP API")
class RegisterApi {
private static final String REGISTER_API = "/api/accounts";
@Test
@DisplayName("회원가입에 적절한 파라미터가 입력되고 회원가입이 성공한다.")
void register_success() throws Exception {
@@ -121,7 +131,7 @@ final class AccountCommandApiTests {
when(accountFacade.register(request)).thenReturn(
new AccountResponse(1L, "msolo021015@gmail.com", "rebwon"));
final var actions = mockMvc.perform(post("/api/accounts")
final var actions = mockMvc.perform(post(REGISTER_API)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))
@@ -136,17 +146,19 @@ final class AccountCommandApiTests {
.andExpect(jsonPath("$.nickname").isString());
}
@Test
@ParameterizedTest
@AutoSource
@DisplayName("Accept와 Content-Type을 지정하지 않아, HttpMediaTypeNotSupportedException 발생.")
void register_account_api_not_use_accept_header_and_content_type() throws Exception {
void register_account_api_not_use_accept_header_and_content_type(String arg)
throws Exception {
// Arrange
var request = new RegisterAccountRequest();
request.setEmail("msolo021015@gmail.com");
request.setNickname("rebwon");
request.setPassword("password!");
request.setEmail(arg);
request.setNickname(arg);
request.setPassword(arg);
// Act
final var actions = mockMvc.perform(post("/api/accounts")
final var actions = mockMvc.perform(post(REGISTER_API)
.content(objectMapper.writeValueAsString(request))
);
@@ -166,7 +178,7 @@ final class AccountCommandApiTests {
request.setPassword(arg);
// Act
final var actions = mockMvc.perform(post("/api/accounts")
final var actions = mockMvc.perform(post(REGISTER_API)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))
@@ -189,7 +201,7 @@ final class AccountCommandApiTests {
request.setPassword(arg);
// Act
final var actions = mockMvc.perform(post("/api/accounts")
final var actions = mockMvc.perform(post(REGISTER_API)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))