|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package smoketest.web.secure; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 24 | +import org.springframework.http.HttpMethod; |
| 25 | +import org.springframework.http.HttpStatus; |
| 26 | +import org.springframework.http.ResponseEntity; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Abstract base class for tests to ensure that the error page is accessible only to |
| 32 | + * authorized users. |
| 33 | + * |
| 34 | + * @author Madhura Bhave |
| 35 | + */ |
| 36 | +abstract class AbstractUnauthenticatedErrorPageTests { |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private TestRestTemplate testRestTemplate; |
| 40 | + |
| 41 | + private final String pathPrefix; |
| 42 | + |
| 43 | + protected AbstractUnauthenticatedErrorPageTests(String pathPrefix) { |
| 44 | + this.pathPrefix = pathPrefix; |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testBadCredentials() { |
| 49 | + final ResponseEntity<JsonNode> response = this.testRestTemplate.withBasicAuth("username", "wrongpassword") |
| 50 | + .exchange(this.pathPrefix + "/test", HttpMethod.GET, null, JsonNode.class); |
| 51 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); |
| 52 | + JsonNode jsonResponse = response.getBody(); |
| 53 | + assertThat(jsonResponse.get("error").asText()).isEqualTo("Unauthorized"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testNoCredentials() { |
| 58 | + final ResponseEntity<JsonNode> response = this.testRestTemplate.exchange(this.pathPrefix + "/test", |
| 59 | + HttpMethod.GET, null, JsonNode.class); |
| 60 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); |
| 61 | + JsonNode jsonResponse = response.getBody(); |
| 62 | + assertThat(jsonResponse.get("error").asText()).isEqualTo("Unauthorized"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testPublicNotFoundPage() { |
| 67 | + final ResponseEntity<JsonNode> response = this.testRestTemplate.exchange(this.pathPrefix + "/public/notfound", |
| 68 | + HttpMethod.GET, null, JsonNode.class); |
| 69 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); |
| 70 | + JsonNode jsonResponse = response.getBody(); |
| 71 | + assertThat(jsonResponse.get("error").asText()).isEqualTo("Not Found"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testPublicNotFoundPageWithCorrectCredentials() { |
| 76 | + final ResponseEntity<JsonNode> response = this.testRestTemplate.withBasicAuth("username", "password") |
| 77 | + .exchange(this.pathPrefix + "/public/notfound", HttpMethod.GET, null, JsonNode.class); |
| 78 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); |
| 79 | + JsonNode jsonResponse = response.getBody(); |
| 80 | + assertThat(jsonResponse.get("error").asText()).isEqualTo("Not Found"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testPublicNotFoundPageWithBadCredentials() { |
| 85 | + final ResponseEntity<JsonNode> response = this.testRestTemplate.withBasicAuth("username", "wrong") |
| 86 | + .exchange(this.pathPrefix + "/public/notfound", HttpMethod.GET, null, JsonNode.class); |
| 87 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); |
| 88 | + JsonNode jsonResponse = response.getBody(); |
| 89 | + assertThat(jsonResponse.get("error").asText()).isEqualTo("Unauthorized"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testCorrectCredentialsWithControllerException() { |
| 94 | + final ResponseEntity<JsonNode> response = this.testRestTemplate.withBasicAuth("username", "password") |
| 95 | + .exchange(this.pathPrefix + "/fail", HttpMethod.GET, null, JsonNode.class); |
| 96 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); |
| 97 | + JsonNode jsonResponse = response.getBody(); |
| 98 | + assertThat(jsonResponse.get("error").asText()).isEqualTo("Internal Server Error"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testCorrectCredentials() { |
| 103 | + final ResponseEntity<String> response = this.testRestTemplate.withBasicAuth("username", "password") |
| 104 | + .exchange(this.pathPrefix + "/test", HttpMethod.GET, null, String.class); |
| 105 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 106 | + assertThat(response.getBody()).isEqualTo("test"); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments