forked from SAP/cloud-security-services-integration-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestControllerTest.java
More file actions
96 lines (80 loc) · 3.2 KB
/
TestControllerTest.java
File metadata and controls
96 lines (80 loc) · 3.2 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 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;
import com.sap.cloud.security.config.Service;
import com.sap.cloud.security.test.JwtGenerator;
import com.sap.cloud.security.test.SecurityTestRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.junit.Assert.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;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
// Test properties are provided with /resources/application.yml
public class TestControllerTest {
@Autowired
private MockMvc mvc;
private String jwtXsuaa;
private String jwtIas;
@ClassRule
public static SecurityTestRule ruleXsuaa = SecurityTestRule.getInstance(Service.XSUAA);
@ClassRule
public static SecurityTestRule ruleIas = SecurityTestRule.getInstance(Service.IAS);
@Before
public void setUp() {
jwtXsuaa = ruleXsuaa.getPreconfiguredJwtGenerator()
.withLocalScopes("Read")
.createToken().getTokenValue();
jwtIas = ruleIas.getPreconfiguredJwtGenerator()
.withClaimsFromFile("/iasClaims.json")
.createToken().getTokenValue();
}
@Test
public void sayHello() throws Exception {
String response = mvc
.perform(get("/sayHello").with(bearerToken(jwtXsuaa)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertTrue(response.contains("sb-clientId!t0815"));
assertTrue(response.contains("xsapp!t0815.Read"));
response = mvc
.perform(get("/sayHello").with(bearerToken(jwtIas)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertTrue(response.contains(SecurityTestRule.DEFAULT_CLIENT_ID));
assertTrue(response.contains(JwtGenerator.DEFAULT_APP_TID));
}
@Test
public void readData_OK() throws Exception {
String response = mvc
.perform(get("/method").with(bearerToken(jwtXsuaa)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertTrue(response.contains("You got the sensitive data for tenant 'the-zone-id'."));
response = mvc
.perform(get("/method").with(bearerToken(jwtIas)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertTrue(response.contains("You got the sensitive data for tenant 'the-app-tid'."));
}
@Test
public void readData_FORBIDDEN() throws Exception {
String jwtNoScopes = ruleXsuaa.getPreconfiguredJwtGenerator()
.createToken().getTokenValue();
mvc.perform(get("/method").with(bearerToken(jwtNoScopes)))
.andExpect(status().isForbidden());
}
}