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

โ“ Spring security testing #19

Open
david-parkk opened this issue Feb 23, 2024 · 1 comment
Open

โ“ Spring security testing #19

david-parkk opened this issue Feb 23, 2024 · 1 comment
Assignees
Labels
๊ณต๋ถ€ ์ •๋ฆฌ โœ๏ธ ๊ฐœ์ธ์ ์œผ๋กœ ์ฐพ์•„๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ

Comments

@david-parkk
Copy link
Member

david-parkk commented Feb 23, 2024

๐Ÿ‘ Spring security ํ™˜๊ฒฝ์—์„œ Test ํ•˜๊ธฐ

๋ชฉํ‘œ

  • spring security ์— ๊ตฌํ˜„ํ•œ ๋ณด์•ˆ ์‚ฌํ•ญ ์‹ค์ œ๋กœ ์ ์šฉ๋˜๋Š”์ง€ Test๋กœ ํ™•์ธํ•˜๊ธฐ

ํ…Œ์ŠคํŒ… ํ™˜๊ฒฝ

  • @WebMVCTest์™€ @Mockmvc๋ฅผ ์ด์šฉํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ

Test 1 ํ—ˆ์šฉ๋˜์ง€ ์•Š์€ URL ์ ‘์†์‹œ 403 โญ•

    @Test
    @DisplayName("select ํ™”๋ฉด์€ ๋กœ๊ทธ์ธ ํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ ‘๊ทผ๊ฐ€๋Šฅํ•ด์„  ์•ˆ๋œ๋‹ค.")
    public void loginSelectTest1() throws Exception {
        mockMvc.perform(get("http://localhost:8080/login/select"))
                .andExpect(status().is3xxRedirection())
                .andDo(print());
    }

์ธ์ฆ์ด ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ 403์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— 302(Redirction code)๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

Test 2 ์ธ์ฆ๋œ User๋Š” URL ์ ‘์†์‹œ 200 (๋˜๋Š” View ๋ฐ˜ํ™˜) โŒ

    @Test
    @WithMockUser
    @DisplayName("select ํ™”๋ฉด์€ ์ธ์ฆ๋œ ์œ ์ €๊ฐ€ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.")
    public void loginSelectTest2() throws Exception {
        mockMvc.perform(get("http://localhost:8080/login/select")
                        .accept(MediaType.parseMediaType("application/html;charset=UTF-8")))
                .andExpect(view().name("login_signup_select"))
                ;
    }

Test 3 ์ธ์ฆ๋œ User๊ฐ€ ๋‹ค์‹œ ํšŒ์›๊ฐ€์ž…ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์—์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ (๊ตฌํ˜„ ์•ˆํ•จ)

  • ์ด๋ฏธ ๋ด‰์‚ฌ์ž๋กœ ํšŒ์›๊ฐ€์ž…๋œ ์‚ฌ์šฉ์ž๊ฐ€ select๋กœ ๋„˜์–ด๊ฐ€์„œ ์žฅ์• ์ธ์œผ๋กœ ํšŒ์›๊ฐ€์ž…ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ
@david-parkk david-parkk added the ๋ฒ„๊ทธ ์ˆ˜์ • ๐Ÿ› ๋ฒ„๊ทธ ์ˆ˜์ • label Feb 23, 2024
@david-parkk david-parkk self-assigned this Feb 23, 2024
@david-parkk
Copy link
Member Author

๐Ÿ’ฃ trouble shooting: test 2

TemplateInputException ์œผ๋กœ template์„ resolve ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
๊ฐ€์žฅ ์œ ๋ ฅํ•œ ๊ฐ€์„ค์€ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ๋˜๋Š” MockMvc๊ฐ€ mock์ด๊ธฐ ๋•Œ๋ฌธ์— ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์•„ view๋ฅผ rendering ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
https://stackoverflow.com/questions/66641634/spring-controller-modelandview-test-mockmvc-empty-response

@david-parkk david-parkk added ๊ณต๋ถ€ ์ •๋ฆฌ โœ๏ธ ๊ฐœ์ธ์ ์œผ๋กœ ์ฐพ์•„๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ and removed ๋ฒ„๊ทธ ์ˆ˜์ • ๐Ÿ› ๋ฒ„๊ทธ ์ˆ˜์ • labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
๊ณต๋ถ€ ์ •๋ฆฌ โœ๏ธ ๊ฐœ์ธ์ ์œผ๋กœ ์ฐพ์•„๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ
Projects
None yet
Development

No branches or pull requests

1 participant