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

[#23][TEST]Controller test #30

Merged
merged 6 commits into from
May 24, 2024
Merged

[#23][TEST]Controller test #30

merged 6 commits into from
May 24, 2024

Conversation

Caramel1004
Copy link
Owner

@Caramel1004 Caramel1004 commented May 24, 2024

๐Ÿชต ์ž‘์—… ๋ธŒ๋žœ์น˜

test / #23

โœ… ํ…Œ์ŠคํŠธ ๋ชฉ๋ก

  • ์†ก๊ธˆ ๋ฐ ์†ก๊ธˆ ๊ฒฌ์ ์„œ ๊ด€๋ จ(Remittance Controller Test)

    • EndPoint: /api/v2/transfer/quote
    • EndPoint: /api/v2/transfer/request
    • EndPoint: /api/v2/transfer/list
  • ํšŒ์› ๊ด€๋ จ(Member Controller Test)

    • EndPoint: /api/v2/user/signup

๐Ÿž ๋ฒ„๊ทธ ์ด์Šˆ

๐Ÿž ๋ฒ„๊ทธ ์ด์Šˆ ํ•ด๊ฒฐ
[#28]
Spring Context์—†์ด ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ๋•Œ๋ฌธ์— SecurityContext๊ฐ์ฒด๋ฅผ ๋ชจํ‚นํ•˜๊ณ 
์œ ์ €์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ธฐ์œ„ํ•ด Authentication ๊ฐ์ฒด์™€ GrantedAuthority ๊ฐ์ฒด๋ฅผ Mocking ํ•ฉ๋‹ˆ๋‹ค.
Mocking๋œ SecurityContext๊ฐ์ฒด์—์„œ getAuthentication() ํ˜ธ์ถœํ•˜๋ฉด ๋ชจํ‚น๋œ Authentication ๋ฆฌํ„ดํ•˜๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

@Mock
    private SecurityContext securityContextMock;

    @Mock
    private Authentication authenticationMock = new TestingAuthenticationToken(
  null, // principal
          null, // credentials
          "REG_NO"); // authority roles;

    @Mock
    private GrantedAuthority grantedAuthorityMock;

@BeforeEach
    @Test
    @DisplayName("Security Core ์„ค์ •")
    void setSecurityCoreAndTesting() {
        /* given:
         *  Servlet Filter ์—์„œ ์ธ์ฆ / ์ธ๊ฐ€ ์„ฑ๊ณตํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •
         * -> SecurityContext and Authentication Mock ๊ฐ์ฒด ์ •์˜
         *  */
        /* given: SecurityContext ๊ฐ์ฒด์—์„œ Authentication ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜๋ฉด Authentication Mock ๊ฐ์ฒด ๋ฆฌํ„ด */
        given(securityContextMock.getAuthentication()).willReturn(authenticationMock);

        /* given: Authentication ๊ฐ์ฒด์—์„œ ์œ ์ €์˜ ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜๋ฉด ์œ ์ €์•„์ด๋”” ๋ฆฌํ„ด */
        given(authenticationMock.getName()).willReturn("[email protected]");

        /* given: Mock the authorities */
        given(authenticationMock.getAuthorities()).willReturn((Collection) Collections.singleton(grantedAuthorityMock));
        given(grantedAuthorityMock.getAuthority()).willReturn("REG_NO");

        /* Set the SecurityContextHolder to use the mocked SecurityContext */
        SecurityContextHolder.setContext(securityContextMock);

        /* when: SecurityContext ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ -> ์œ ์ € ์•„์ด๋””์™€ ์œ ์ € ํƒ€์ž…  */
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();

        /* then: Authentication Mock ๊ฐ์ฒด์— ์œ ์ € ์ •๋ณด๊ฐ€ ์„ธํŒ…๋˜์—ˆ๋Š”์ง€ ํ™•์ธ */
        assertEquals(username, "[email protected]");
        assertEquals(1, authorities.size()); // ๋‹จ ํ•˜๋‚˜์˜ ์š”์†Œ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•จ
        assertEquals("REG_NO", authorities.iterator().next().getAuthority()); // ํšŒ์› ํƒ€์ž… ํ™•์ธ == ๊ฐœ์ธํšŒ์›
    }

[#29]
์›์ธ์€ builderํ˜•ํƒœ๋กœ ์„ ์–ธํ•œ dto๊ฐ์ฒด๋ฅผ json์œผ๋กœ ๋ณ€ํ™˜ ํ•˜์ง€ ๋ชปํ•˜์—ฌ ์ƒ๊ธฐ๋Š” ์˜ค๋ฅ˜ ์˜€์Šต๋‹ˆ๋‹ค.
์ด์œ ๋Š” ์•„์ง๊นŒ์ง€ ์ฐพ๋Š” ์ค‘ ์ด๊ณ  @DaTa๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  setํ•จ์ˆ˜๋กœ ๊ฒฌ์ ์„œ์•„์ด๋”” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์„ธํŒ… ํ›„ Gson๊ฐ์ฒด์— json๋ณ€ํ™˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜์˜€๋”๋‹ˆ ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โ™ป๏ธ [refactor] ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์„ธํŒ…์„ ์œ„ํ•ด @DaTa๋กœ ๋ณ€๊ฒฝ
โž• [add] Gson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
๐Ÿ”ฅ [remove] ํ™˜์œจ ๊ณ„์‚ฐ ํ…Œ์ŠคํŠธ ํŒŒ์ผ ์‚ญ์ œ
โœ… [test] Controller Test ์ฝ”๋“œ ์ถ”๊ฐ€
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment