-
Notifications
You must be signed in to change notification settings - Fork 2k
/
PasswordFlowLiveTest.java
73 lines (54 loc) · 3.83 KB
/
PasswordFlowLiveTest.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
package com.baeldung.live;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.http.MediaType;
//Before running this live test make sure both authorization server and first resource server are running
public class PasswordFlowLiveTest {
@Test
public void givenUser_whenUseFooClient_thenOkForFooResourceOnly() {
final String accessToken = obtainAccessToken("fooClientIdPassword", "john", "123");
final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8082/spring-security-oauth-resource/foos/1");
assertEquals(200, fooResponse.getStatusCode());
assertNotNull(fooResponse.jsonPath().get("name"));
final Response barResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8082/spring-security-oauth-resource/bars/1");
assertEquals(403, barResponse.getStatusCode());
}
@Test
public void givenUser_whenUseBarClient_thenOkForBarResourceReadOnly() {
final String accessToken = obtainAccessToken("barClientIdPassword", "john", "123");
final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8082/spring-security-oauth-resource/foos/1");
assertEquals(403, fooResponse.getStatusCode());
final Response barReadResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8082/spring-security-oauth-resource/bars/1");
assertEquals(200, barReadResponse.getStatusCode());
assertNotNull(barReadResponse.jsonPath().get("name"));
final Response barWritResponse = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken).body("{\"id\":1,\"name\":\"MyBar\"}").post("http://localhost:8082/spring-security-oauth-resource/bars");
assertEquals(403, barWritResponse.getStatusCode());
}
@Test
public void givenAdmin_whenUseBarClient_thenOkForBarResourceReadWrite() {
final String accessToken = obtainAccessToken("barClientIdPassword", "tom", "111");
final Response fooResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8082/spring-security-oauth-resource/foos/1");
assertEquals(403, fooResponse.getStatusCode());
final Response barResponse = RestAssured.given().header("Authorization", "Bearer " + accessToken).get("http://localhost:8082/spring-security-oauth-resource/bars/1");
assertEquals(200, barResponse.getStatusCode());
assertNotNull(barResponse.jsonPath().get("name"));
final Response barWritResponse = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken).body("{\"id\":1,\"name\":\"MyBar\"}").post("http://localhost:8082/spring-security-oauth-resource/bars");
assertEquals(201, barWritResponse.getStatusCode());
assertEquals("MyBar", barWritResponse.jsonPath().get("name"));
}
//
private String obtainAccessToken(String clientId, String username, String password) {
final Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("client_id", clientId);
params.put("username", username);
params.put("password", password);
final Response response = RestAssured.given().auth().preemptive().basic(clientId, "secret").and().with().params(params).when().post("http://localhost:8081/spring-security-oauth-server/oauth/token");
return response.jsonPath().getString("access_token");
}
}