-
Notifications
You must be signed in to change notification settings - Fork 135
/
TestControllerIasTest.java
91 lines (75 loc) · 3.24 KB
/
TestControllerIasTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* SPDX-FileCopyrightText: 2018-2023 SAP SE or an SAP affiliate company and Cloud Security Client Java contributors
* <p>
* SPDX-License-Identifier: Apache-2.0
*/
package sample.spring.security.junitjupiter;
import com.sap.cloud.security.test.api.SecurityTestContext;
import com.sap.cloud.security.test.extension.IasExtension;
import com.sap.cloud.security.token.Token;
import com.sap.cloud.security.token.TokenClaims;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static sample.spring.security.util.MockBearerTokenRequestPostProcessor.bearerToken;
@SpringBootTest
@AutoConfigureMockMvc
@ExtendWith(IasExtension.class)
class TestControllerIasTest {
@Autowired
private MockMvc mvc;
private String jwt;
@BeforeEach
void setup(SecurityTestContext securityTest) {
jwt = securityTest.getPreconfiguredJwtGenerator()
.withClaimsFromFile("/iasClaims.json")
.createToken().getTokenValue();
}
@Test
void sayHello() throws Exception {
String response = mvc.perform(get("/sayHello").with(bearerToken(jwt)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertTrue(response.contains("sb-clientId!t0815"));
assertTrue(response.contains("the-app-tid"));
}
@Test
void sayHello_compatibility() throws Exception {
mvc.perform(get("/comp/sayHello").with(bearerToken(jwt)))
.andExpect(status().is5xxServerError());
}
@Test
void readData_OK() throws Exception {
String response = mvc
.perform(get("/method").with(bearerToken(jwt)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertTrue(response.contains("You got the sensitive data for tenant 'the-app-tid'."));
}
@Test
void readData_FORBIDDEN(SecurityTestContext securityTest) throws Exception {
String jwtNoScopes = securityTest.getPreconfiguredJwtGenerator()
.createToken().getTokenValue();
mvc.perform(get("/method").with(bearerToken(jwtNoScopes)))
.andExpect(status().isForbidden());
}
/**
* Ensures that tokens with an issuer whose domain is not part of
* {@link com.sap.cloud.security.config.ServiceConstants.IAS#DOMAINS} in the credentials are still not trusted, even
* when java-security-test supplies {@link com.sap.cloud.security.token.validation.LocalhostIssuerValidator}, which
* trusts issuers from tokens targeting localhost.
*/
@Test
void acceptsOnlyLocalhostIssuers(SecurityTestContext securityTest) throws Exception {
Token jwt = securityTest.getPreconfiguredJwtGenerator().withClaimsFromFile("/iasClaims.json")
.withClaimValue(TokenClaims.IAS_ISSUER, "https://auth.google.com").createToken();
mvc.perform(get("/sayHello").with(bearerToken(jwt.getTokenValue()))).andExpect(status().isUnauthorized());
}
}