Repository navigation
Expand file tree
/
Copy pathTestControllerIasTest.java
More file actions
77 lines (63 loc) · 2.57 KB
/
Copy pathTestControllerIasTest.java
File metadata and controls
77 lines (63 loc) · 2.57 KB
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
/**
* 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 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.context.ActiveProfiles;
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
@ActiveProfiles("multixsuaa") // properties are provided with /resources/application-multixsuaa.yml
@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());
}
}