|
| 1 | +package com.baeldung.springbootsecurity; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.context.embedded.LocalServerPort; |
| 7 | +import org.springframework.boot.test.context.SpringBootTest; |
| 8 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 9 | +import org.springframework.http.*; |
| 10 | +import org.springframework.test.context.ActiveProfiles; |
| 11 | +import org.springframework.test.context.junit4.SpringRunner; |
| 12 | +import org.springframework.util.LinkedMultiValueMap; |
| 13 | +import org.springframework.util.MultiValueMap; |
| 14 | + |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.regex.Matcher; |
| 17 | +import java.util.regex.Pattern; |
| 18 | + |
| 19 | +import static org.junit.Assert.*; |
| 20 | +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
| 21 | + |
| 22 | +@RunWith(SpringRunner.class) |
| 23 | +@SpringBootTest(webEnvironment = RANDOM_PORT) |
| 24 | +@ActiveProfiles("form") |
| 25 | +public class FormConfigurationIntegrationTest { |
| 26 | + |
| 27 | + @Autowired TestRestTemplate restTemplate; |
| 28 | + @LocalServerPort int port; |
| 29 | + |
| 30 | + @Test |
| 31 | + public void whenLoginPageIsRequested_ThenSuccess() { |
| 32 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 33 | + httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); |
| 34 | + ResponseEntity<String> responseEntity = restTemplate.exchange("/login", HttpMethod.GET, new HttpEntity<Void>(httpHeaders), String.class); |
| 35 | + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); |
| 36 | + assertTrue(responseEntity |
| 37 | + .getBody() |
| 38 | + .contains("_csrf")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenTryingToLoginWithCorrectCredentials_ThenAuthenticateWithSuccess() { |
| 43 | + HttpHeaders httpHeaders = getHeaders(); |
| 44 | + httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); |
| 45 | + httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| 46 | + MultiValueMap<String, String> form = getFormSubmitCorrectCredentials(); |
| 47 | + ResponseEntity<String> responseEntity = this.restTemplate.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, httpHeaders), String.class); |
| 48 | + assertEquals(responseEntity.getStatusCode(), HttpStatus.FOUND); |
| 49 | + assertTrue(responseEntity |
| 50 | + .getHeaders() |
| 51 | + .getLocation() |
| 52 | + .toString() |
| 53 | + .endsWith(this.port + "/")); |
| 54 | + assertNotNull(responseEntity |
| 55 | + .getHeaders() |
| 56 | + .get("Set-Cookie")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void whenTryingToLoginWithInorrectCredentials_ThenAuthenticationFailed() { |
| 61 | + HttpHeaders httpHeaders = getHeaders(); |
| 62 | + httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); |
| 63 | + httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| 64 | + MultiValueMap<String, String> form = getFormSubmitIncorrectCredentials(); |
| 65 | + ResponseEntity<String> responseEntity = this.restTemplate.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, httpHeaders), String.class); |
| 66 | + assertEquals(responseEntity.getStatusCode(), HttpStatus.FOUND); |
| 67 | + assertTrue(responseEntity |
| 68 | + .getHeaders() |
| 69 | + .getLocation() |
| 70 | + .toString() |
| 71 | + .endsWith(this.port + "/login?error")); |
| 72 | + assertNull(responseEntity |
| 73 | + .getHeaders() |
| 74 | + .get("Set-Cookie")); |
| 75 | + } |
| 76 | + |
| 77 | + private MultiValueMap<String, String> getFormSubmitCorrectCredentials() { |
| 78 | + MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); |
| 79 | + form.set("username", "user"); |
| 80 | + form.set("password", "password"); |
| 81 | + return form; |
| 82 | + } |
| 83 | + |
| 84 | + private MultiValueMap<String, String> getFormSubmitIncorrectCredentials() { |
| 85 | + MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); |
| 86 | + form.set("username", "user"); |
| 87 | + form.set("password", "wrongpassword"); |
| 88 | + return form; |
| 89 | + } |
| 90 | + |
| 91 | + private HttpHeaders getHeaders() { |
| 92 | + HttpHeaders headers = new HttpHeaders(); |
| 93 | + ResponseEntity<String> page = this.restTemplate.getForEntity("/login", String.class); |
| 94 | + assertEquals(page.getStatusCode(), HttpStatus.OK); |
| 95 | + String cookie = page |
| 96 | + .getHeaders() |
| 97 | + .getFirst("Set-Cookie"); |
| 98 | + headers.set("Cookie", cookie); |
| 99 | + Pattern pattern = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*"); |
| 100 | + Matcher matcher = pattern.matcher(page.getBody()); |
| 101 | + assertTrue(matcher.matches()); |
| 102 | + headers.set("X-CSRF-TOKEN", matcher.group(1)); |
| 103 | + return headers; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments