yuusuke-roughの日記

Java,SpringBoot,趣味等

単体テストの見直し日記

はじめに

GWも後半となったが、認証と認可をいじって、AOPでログ出力を試している程度だ。

一日目は部屋と寝具含めた全体の掃除と社内勉強会、三日目は今後のお仕事についてヒアリングを受け、四日目は四月の疲れが思い出したようにどっときて、ほとんど眠っていた。

それにしても進捗は良くない。あまり楽観的に考えられないのも祟っている。

 

という事で、仕切り直し。

今回は以前書いたテストコードを修正していきます。

 

内容

①CustomAuthenticactionProviderの認証

現在はユーザー名とパスワードでの認証となっている。

@BeforeEachでAuthenticationの準備と認証するためのデータをDBに設定し、各パラメーターの組み合わせを調べる。

 

@BeforeEach
    void setUp() throws Exception{
        //ユーザー情報の取得のための設定。Mockに変更予定。
        customAuthenticationProvider.setUserDetailsService(userDetailsServiceImple);
        customAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        
        // OTPユーザーの情報
        List<Privilege> privilege = new ArrayList<Privilege>();
        privilege.add(new Privilege("READ"));
        
        List<Role> role = new ArrayList<Role>();
        role.add(new Role("ROLE_OTPUSER", privilege));
        Collection<Role> roles = new ArrayList<Role>(role) ;
        
        SiteUser otpUser = new SiteUser();
        otpUser.setUsername("OTPUser@test.jp");
        otpUser.setMail("OTPUser@test.jp");
        otpUser.setPassword(passwordEncoder().encode("Zaq1Zaq1"));
        otpUser.setSecret("OTPSecretCode");
        otpUser.setEnabled(true);
        otpUser.setRoles(roles);
        otpUser.setUsing2FA(true);
        siteUserRepository.save(otpUser);
        
        
        // 通常ユーザーの情報
        List<Privilege> privilege2 = new ArrayList<Privilege>();
        privilege2.add(new Privilege("READ"));
        
        List<Role> role2 = new ArrayList<Role>();
        role2.add(new Role("ROLE_USER", privilege2));
        Collection<Role> roles2 = new ArrayList<Role>(role2) ;

        SiteUser user = new SiteUser();
        user.setUsername("user@test.jp");
        user.setMail("user@test.jp");
        user.setPassword(passwordEncoder().encode("Zaq1Zaq1"));
        user.setSecret(null);
        user.setEnabled(true);
        user.setRoles(roles2);
        user.setUsing2FA(false);
        siteUserRepository.save(user);
    }
    
    @Test
    void OTPユーザーがユーザー名とパスワードを入力した際にROLE_OTPUSERを付与したAuthenticationオブジェクトを返す事() {
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("OTPUser@test.jp", "Zaq1Zaq1");
        GrantedAuthority OTPRole = new SimpleGrantedAuthority("ROLE_OTPUSER");
        Optional<? extends GrantedAuthority> result = customAuthenticationProvider.authenticate(auth).getAuthorities().stream().filter*1.findFirst();
        
        assertTrue(result.isPresent());
    }
    
    
    @ParameterizedTest
    @ValueSource(strings = {"@test.jp","t@t.jp", "test.jp", "", " ", "あ", "ア", "ァ", "a", "1", ".jp" })
    void OTPユーザーが誤ったユーザー名を入力した際に例外を投げる事(String username) {
        
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(username, "Zaq1Zaq1");
        
        assertThrows(BadCredentialsException.class, () -> customAuthenticationProvider.authenticate(auth));    
    }
    
    @ParameterizedTest
    @ValueSource(strings = {"zaq1zaq1","za", "1", "", " ", "あ", "ア", "ァ", "' OR 'a'='a", ":;]" })
    void OTPユーザーが誤ったパスワードを入力した際に例外を投げる事(String password) {
        
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("OTPUser@test.jp", password);
        
        assertThrows(BadCredentialsException.class, () -> customAuthenticationProvider.authenticate(auth));    
    }

...長くなるので以下略

 

Mockに変えたい...

ひとまず、以前書いていた前処理が頓珍漢の極みだった。

ついでにフィールドインジェクションもコンストラクタインジェクションに書き直した。

@CsvSourceを使用してmailとpasswordの組み合わせテストも実装したが、冗長すぎる気がする。

 

感想

感想というか近況。数学は時間取れず、AtCoderもなかなかできない日々が続き悲しい。

池上彰の現代史と神保町で買ったフロイト(というか精神分析入門書)を読んでいて、わりと入り込めないくらい精神的余裕の無さに疲れているなと辟易する。

 

*1:s) -> OTPRole.equals(s