Skip to content

Commit 15b053c

Browse files
rozagerardomaibin
authored andcommitted
[BAEL-2735] REST-Assured Authentication article (eugenp#6753)
* Added restassured test using basic auth, form auth and digest. * Added Rest Assured Authentication - OAuth Live Test * Add Authentication with Rest Assured for autoconfigured Form Login * Add OAuth 1 Rest Assured scenario
1 parent 0120406 commit 15b053c

File tree

8 files changed

+360
-0
lines changed

8 files changed

+360
-0
lines changed

testing-modules/rest-assured/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<artifactId>commons-collections</artifactId>
163163
<version>${commons-collections.version}</version>
164164
</dependency>
165+
166+
<dependency>
167+
<groupId>org.springframework.boot</groupId>
168+
<artifactId>spring-boot-starter-security</artifactId>
169+
</dependency>
165170

166171
<!-- Rest Assured Dependencies-->
167172
<dependency>
@@ -179,6 +184,12 @@
179184
<artifactId>json-schema-validator</artifactId>
180185
<scope>test</scope>
181186
</dependency>
187+
<dependency>
188+
<groupId>com.github.scribejava</groupId>
189+
<artifactId>scribejava-apis</artifactId>
190+
<version>${scribejava.version}</version>
191+
<scope>test</scope>
192+
</dependency>
182193
</dependencies>
183194

184195
<properties>
@@ -211,6 +222,8 @@
211222

212223
<rest-assured.version>3.0.1</rest-assured.version>
213224
<rest-assured-json-schema-validator.version>3.0.1</rest-assured-json-schema-validator.version>
225+
226+
<scribejava.version>2.5.3</scribejava.version>
214227
</properties>
215228

216229
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.http.HttpStatus;
8+
9+
/**
10+
* For this Live Test we need:
11+
* * a running instance of the service located in the spring-security-rest-basic-auth module.
12+
* @see <a href="https://github.com/eugenp/tutorials/tree/master/spring-security-rest-basic-auth">spring-security-rest-basic-auth module</a>
13+
*
14+
*/
15+
public class BasicAuthenticationLiveTest {
16+
17+
private static final String USER = "user1";
18+
private static final String PASSWORD = "user1Pass";
19+
private static final String SVC_URL = "http://localhost:8080/spring-security-rest-basic-auth/api/foos/1";
20+
21+
@Test
22+
public void givenNoAuthentication_whenRequestSecuredResource_thenUnauthorizedResponse() {
23+
get(SVC_URL).then()
24+
.assertThat()
25+
.statusCode(HttpStatus.UNAUTHORIZED.value());
26+
}
27+
28+
@Test
29+
public void givenBasicAuthentication_whenRequestSecuredResource_thenResourceRetrieved() {
30+
given().auth()
31+
.basic(USER, PASSWORD)
32+
.when()
33+
.get(SVC_URL)
34+
.then()
35+
.assertThat()
36+
.statusCode(HttpStatus.OK.value());
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.containsString;
6+
import static org.hamcrest.Matchers.is;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.http.HttpStatus;
10+
11+
/**
12+
* For this Live Test we need:
13+
* * a running instance of the service located in the spring-boot-admin/spring-boot-admin-server module.
14+
* @see <a href="https://github.com/eugenp/tutorials/tree/master/spring-boot-admin/spring-boot-admin-server">spring-boot-admin/spring-boot-admin-server module</a>
15+
*
16+
*/
17+
public class BasicPreemtiveAuthenticationLiveTest {
18+
19+
private static final String USER = "admin";
20+
private static final String PASSWORD = "admin";
21+
private static final String SVC_URL = "http://localhost:8080/api/applications/";
22+
23+
@Test
24+
public void givenNoAuthentication_whenRequestSecuredResource_thenUnauthorizedResponse() {
25+
get(SVC_URL).then()
26+
.assertThat()
27+
.statusCode(HttpStatus.OK.value())
28+
.content(containsString("<form"), containsString("action=\"login\""));
29+
}
30+
31+
@Test
32+
public void givenNonPreemtiveBasicAuthentication_whenRequestSecuredResource_thenLoginPageRetrieved() {
33+
given().auth()
34+
.basic(USER, PASSWORD)
35+
.when()
36+
.get(SVC_URL)
37+
.then()
38+
.assertThat()
39+
.statusCode(HttpStatus.OK.value())
40+
.content(containsString("<form"), containsString("action=\"login\""));
41+
}
42+
43+
@Test
44+
public void givenPreemtiveBasicAuthentication_whenRequestSecuredResource_thenResourceRetrieved() {
45+
given().auth()
46+
.preemptive()
47+
.basic(USER, PASSWORD)
48+
.when()
49+
.get(SVC_URL)
50+
.then()
51+
.assertThat()
52+
.statusCode(HttpStatus.OK.value())
53+
.body("size()", is(1));
54+
}
55+
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.containsString;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.http.HttpStatus;
9+
10+
/**
11+
* For this Live Test we need:
12+
* * a running instance of the service located in the spring-security-mvc-digest-auth module.
13+
* @see <a href="https://github.com/eugenp/tutorials/tree/master/spring-security-mvc-digest-auth">spring-security-mvc-digest-auth module</a>
14+
*
15+
*/
16+
public class DigestAuthenticationLiveTest {
17+
18+
private static final String USER = "user1";
19+
private static final String PASSWORD = "user1Pass";
20+
private static final String SVC_URL = "http://localhost:8080/spring-security-mvc-digest-auth/homepage.html";
21+
22+
@Test
23+
public void givenNoAuthentication_whenRequestSecuredResource_thenUnauthorizedResponse() {
24+
get(SVC_URL).then()
25+
.assertThat()
26+
.statusCode(HttpStatus.UNAUTHORIZED.value());
27+
}
28+
29+
@Test
30+
public void givenFormAuthentication_whenRequestSecuredResource_thenResourceRetrieved() {
31+
given().auth()
32+
.digest(USER, PASSWORD)
33+
.when()
34+
.get(SVC_URL)
35+
.then()
36+
.assertThat()
37+
.statusCode(HttpStatus.OK.value())
38+
.content(containsString("This is the body of the sample view"));
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.containsString;
6+
import static org.hamcrest.Matchers.isEmptyString;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.http.HttpStatus;
10+
11+
import io.restassured.authentication.FormAuthConfig;
12+
13+
/**
14+
* For this Live Test we need:
15+
* * a running instance of the service located in the spring-security-mvc-login module.
16+
* @see <a href="https://github.com/eugenp/tutorials/tree/master/spring-security-mvc-login">spring-security-mvc-login module</a>
17+
*
18+
*/
19+
public class FormAuthenticationLiveTest {
20+
21+
private static final String USER = "user1";
22+
private static final String PASSWORD = "user1Pass";
23+
private static final String SVC_URL = "http://localhost:8080/spring-security-mvc-login/secured";
24+
25+
@Test
26+
public void givenNoAuthentication_whenRequestSecuredResource_thenLoginFormResponse() {
27+
get(SVC_URL).then()
28+
.assertThat()
29+
.statusCode(HttpStatus.OK.value())
30+
.content(containsString("<form"), containsString("action=\"perform_login\""));
31+
}
32+
33+
@Test
34+
public void givenParsingFormAuthentication_whenRequestSecuredResource_thenLoginFormResponse() {
35+
// Form can't be parsed correctly because the app is in servlet container, thus the form's 'action' attribute doesn't include the correct URI
36+
given().auth()
37+
.form(USER, PASSWORD)
38+
.when()
39+
.get(SVC_URL)
40+
.then()
41+
.assertThat()
42+
.statusCode(HttpStatus.OK.value())
43+
.content(containsString("<form"), containsString("action=\"perform_login\""));
44+
}
45+
46+
@Test
47+
public void givenFormAuthentication_whenRequestSecuredResource_thenResourceRetrieved() {
48+
given().auth()
49+
.form(USER, PASSWORD, new FormAuthConfig("/spring-security-mvc-login/perform_login", "username", "password"))
50+
.when()
51+
.get(SVC_URL)
52+
.then()
53+
.assertThat()
54+
.statusCode(HttpStatus.OK.value())
55+
.content(isEmptyString());
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.containsString;
6+
import static org.hamcrest.Matchers.is;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.http.HttpStatus;
10+
11+
/**
12+
* For this Live Test we need:
13+
* * a running instance of the service located in the spring-boot-admin/spring-boot-admin-server module.
14+
* @see <a href="https://github.com/eugenp/tutorials/tree/master/spring-boot-admin/spring-boot-admin-server">spring-boot-admin/spring-boot-admin-server module</a>
15+
*
16+
*/
17+
public class FormAutoconfAuthenticationLiveTest {
18+
19+
private static final String USER = "admin";
20+
private static final String PASSWORD = "admin";
21+
private static final String SVC_URL = "http://localhost:8080/ger1/api/applications/";
22+
23+
@Test
24+
public void givenNoAuthentication_whenRequestSecuredResource_thenUnauthorizedResponse() {
25+
get(SVC_URL).then()
26+
.assertThat()
27+
.statusCode(HttpStatus.OK.value())
28+
.content(containsString("<form"), containsString("action=\"login\""));
29+
}
30+
31+
@Test
32+
public void givenParsingFormAuthentication_whenRequestSecuredResource_thenResourceRetrieved() {
33+
given().auth()
34+
.form(USER, PASSWORD)
35+
.when()
36+
.get(SVC_URL)
37+
.then()
38+
.assertThat()
39+
.statusCode(HttpStatus.OK.value())
40+
.body("size()", is(1));
41+
}
42+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.hasKey;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.http.HttpStatus;
9+
10+
/**
11+
* For this Live Test we need:
12+
* * a running instance of the authorization server located in the spring-security-oauth repo - oauth-authorization-server module.
13+
* @see <a href="https://github.com/Baeldung/spring-security-oauth/tree/master/oauth-authorization-server">spring-security-oauth/oauth-authorization-server module</a>
14+
*
15+
* * a running instance of the service located in the spring-security-oauth repo - oauth-resource-server-1 module.
16+
* @see <a href="https://github.com/Baeldung/spring-security-oauth/tree/master/oauth-resource-server-1">spring-security-oauth/oauth-resource-server-1 module</a>
17+
*
18+
*/
19+
public class OAuth2AuthenticationLiveTest {
20+
21+
private static final String USER = "john";
22+
private static final String PASSWORD = "123";
23+
private static final String CLIENT_ID = "fooClientIdPassword";
24+
private static final String SECRET = "secret";
25+
private static final String AUTH_SVC_TOKEN_URL = "http://localhost:8081/spring-security-oauth-server/oauth/token";
26+
private static final String RESOURCE_SVC_URL = "http://localhost:8082/spring-security-oauth-resource/foos/1";
27+
28+
@Test
29+
public void givenNoAuthentication_whenRequestSecuredResource_thenUnauthorizedResponse() {
30+
get(RESOURCE_SVC_URL).then()
31+
.assertThat()
32+
.statusCode(HttpStatus.UNAUTHORIZED.value());
33+
}
34+
35+
@Test
36+
public void givenAccessTokenAuthentication_whenRequestSecuredResource_thenResourceRetrieved() {
37+
String accessToken = given().auth()
38+
.basic(CLIENT_ID, SECRET)
39+
.formParam("grant_type", "password")
40+
.formParam("username", USER)
41+
.formParam("password", PASSWORD)
42+
.formParam("scope", "read foo")
43+
.when()
44+
.post(AUTH_SVC_TOKEN_URL)
45+
.then()
46+
.assertThat()
47+
.statusCode(HttpStatus.OK.value())
48+
.extract()
49+
.path("access_token");
50+
51+
given().auth()
52+
.oauth2(accessToken)
53+
.when()
54+
.get(RESOURCE_SVC_URL)
55+
.then()
56+
.assertThat()
57+
.statusCode(HttpStatus.OK.value())
58+
.body("$", hasKey("id"))
59+
.body("$", hasKey("name"));
60+
}
61+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.restassured.authentication;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.hasKey;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.http.HttpStatus;
9+
10+
import io.restassured.http.ContentType;
11+
12+
/**
13+
* For this Live Test we need to obtain a valid Access Token and Token Secret:
14+
* * start spring-mvc-simple application in debug mode
15+
* @see <a href="https://github.com/eugenp/tutorials/tree/master/spring-mvc-simple">spring-mvc-simple module</a>
16+
* * calling localhost:8080/spring-mvc-simple/twitter/authorization/ using the browser
17+
* * debug the callback function where we can obtain the fields
18+
*/
19+
public class OAuthAuthenticationLiveTest {
20+
21+
// We can obtain these two from the spring-mvc-simple / TwitterController class
22+
private static final String OAUTH_API_KEY = "PSRszoHhRDVhyo2RIkThEbWko";
23+
private static final String OAUTH_API_SECRET = "prpJbz03DcGRN46sb4ucdSYtVxG8unUKhcnu3an5ItXbEOuenL";
24+
private static final String TWITTER_ENDPOINT = "https://api.twitter.com/1.1/account/settings.json";
25+
/* We can obtain the following by:
26+
* - starting the spring-mvc-simple application
27+
* - calling localhost:8080/spring-mvc-simple/twitter/authorization/
28+
* - debugging the callback function */
29+
private static final String ACCESS_TOKEN = "...";
30+
private static final String TOKEN_SECRET = "...";
31+
32+
@Test
33+
public void givenNoAuthentication_whenRequestSecuredResource_thenUnauthorizedResponse() {
34+
get(TWITTER_ENDPOINT).then()
35+
.assertThat()
36+
.statusCode(HttpStatus.BAD_REQUEST.value());
37+
}
38+
39+
@Test
40+
public void givenAccessTokenAuthentication_whenRequestSecuredResource_thenResourceIsRequested() {
41+
given().accept(ContentType.JSON)
42+
.auth()
43+
.oauth(OAUTH_API_KEY, OAUTH_API_SECRET, ACCESS_TOKEN, TOKEN_SECRET)
44+
.when()
45+
.get(TWITTER_ENDPOINT)
46+
.then()
47+
.assertThat()
48+
.statusCode(HttpStatus.OK.value())
49+
.body("$", hasKey("geo_enabled"))
50+
.body("$", hasKey("language"));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)