SpringとJPAベースのWebアプリケーション開発#15メンバー認証メール検証テストと再パッケージ

14203 ワード

SpringとJPAベースのWebアプリケーション開発#15メンバー認証メール検証テストと再パッケージ
これらはインフラストラクチャ、Spring、JPAベースのWebアプリケーション開発のコースに基づいて作成されています.

要約は、学習コース、要約ソースのタグ付け、ブログまたはドキュメント形式で公開できるようにする原則の下で公開されます.出典は前述の通り、インフラストラクチャ、Spring、JPAベースのWebアプリケーション開発.
私が勉強しているソースコードはhttps://github.com/n00nietzsche/jakestudy_webappにアップロードされます.私は伝言ごとに講義のどの部分を記録します.

メンバー認証メールのテストと再パッケージ


テスト


入力値
  • が無効です
  • errorpropertyがモデル内にあるかどうか
  • account/checked-emailという名前で表示
  • 入力値
  • 有効
  • 型番にエラーはありません
  • モデルに番号付きユーザーがいるかどうか
  • モデルにニックネームがあるかどうか
  • 表示名
  • 改造する

  • コードの位置は適当ですか?
  • AccountControlコードの変更

    accountService.completeSignUp(account);
    会員加入後、emailVerifiedtrueに変更した部分とjointedAtを現在の視点に変更した部分をサービス部門に渡す.注目事項の分離.
    スタックオーバーフローでいい文章を読みました.
    The controller responsibility is to get the parameter requests, and then call one or more service methods and combine the results in a response that is then sent back to the client.
    https://stackoverflow.com/questions/23118789/why-we-shouldnt-make-a-spring-mvc-controller-transactional

    AccountServiceコードの変更

        @Transactional
        public void completeSignUp(Account account) {
            account.completeSignUp();
        }
    completeSignUp()のメソッドを追加し,会員加入後のプログラムに実行コードを記述した.その詳細はドメインに関連するコンテンツでもドメイン内部に置かれている.

    勘定科目コードの変更

        public void completeSignUp() {
            this.setEmailVerified(true);
            this.setJoinedAt(LocalDateTime.now());
        }
    .completeSignUp()が呼び出されると、Eメール認証情報はtrueに変更され、会員登録日は現在の時刻に変更されます.

    AccountControllerTestコードの変更

        @DisplayName("인증 메일 확인 - 입력값 오류")
        @Test
        void checkEmailTokenWithWrongInput() throws Exception {
            mockMvc.perform(get("/check-email-token")
                    .param("token", "it's wrong token")
                    .param("email", "[email protected]"))
                    .andExpect(status().isOk())
                    .andExpect(model().attributeExists("error"))
                    .andExpect(view().name("account/checked-email"));
        }
    
        @DisplayName("인증 메일 확인 - 입력값 정상")
        @Test
        void checkEmailTokenWithCorrectInput() throws Exception {
            // 회원가입 이후
            if(!accountRepository.existsByEmail(testEmail)){
                signUpSubmitWithCorrectInput();
            }
    
            Account newAccount = accountRepository.findByEmail(testEmail);
    
            mockMvc.perform(get("/check-email-token")
                    .param("token", newAccount.getEmailCheckToken())
                    .param("email", newAccount.getEmail()))
                    .andExpect(status().isOk())
                    .andExpect(model().attributeDoesNotExist("error"))
                    .andExpect(model().attributeExists("nickname"))
                    .andExpect(model().attributeExists("numberOfUser"))
                    .andExpect(view().name("account/checked-email"));
    
            // 가입일자가 생겨야 함
            // @Transactional 애노테이션 없으면 detached 된 상태로 JPA 객체를 불러오는 것에 주의해야 함
            // 일단 detached 상태로 JPA 객체를 불러오면 컨트롤러 단과 같은 곳에서 올바르게 객체를 수정해도
            // JPA 객체 수정이 반영되지 않는다.
            // 그러므로 up-to-date 상태의 JPA 객체를 가지고 있고 싶으면 @Transactional 이 필요하다.
            // 그리고 또한 관심사의 분리로 컨트롤러 단에 @Transactional 붙이는 건 좋지 않다.
            // https://stackoverflow.com/questions/23118789/why-we-shouldnt-make-a-spring-mvc-controller-transactional
            assertNotNull(newAccount.getJoinedAt());
        }
  • Eメール認証の無効な値は
  • です.
  • 回のコストを入力後、電子メール認証で正しい値を入力すると、
  • になります.
    2つのケースについて作成した.
    次に,@Transactionalの重要性を学び,@Transactionalのない場所にJPAオブジェクトをマウントすると,そのオブジェクトはアプリケーション内部で変更されたときに同期しないことが分かった.detachedのオブジェクトは、最初のロード後に更新されないことを常に認識しておきましょう.