Skip to content

Commit b33aa67

Browse files
Added functional test for authentication (#117)
1 parent d5139aa commit b33aa67

File tree

7 files changed

+191
-3
lines changed

7 files changed

+191
-3
lines changed

pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@
9393
<groupId>org.springframework.security</groupId>
9494
<artifactId>spring-security-ldap</artifactId>
9595
</dependency>
96+
97+
<dependency>
98+
<groupId>org.springframework.security</groupId>
99+
<artifactId>spring-security-test</artifactId>
100+
<scope>test</scope>
101+
</dependency>
96102

97103
<dependency>
98104
<groupId>org.springframework.session</groupId>
@@ -310,7 +316,7 @@
310316
</configuration>
311317
</execution>
312318
</executions>
313-
</plugin>
319+
</plugin>
314320

315321
<plugin>
316322
<groupId>org.apache.maven.plugins</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.ericsson.ei.subscriptions.authentication;
2+
3+
import com.ericsson.ei.controller.AuthControllerImpl;
4+
import com.ericsson.ei.utils.FunctionalTestBase;
5+
import com.ericsson.ei.utils.TestLDAPInitializer;
6+
import cucumber.api.java.en.Given;
7+
import cucumber.api.java.en.Then;
8+
import cucumber.api.java.en.When;
9+
import org.apache.commons.io.FileUtils;
10+
import org.apache.tomcat.util.codec.binary.Base64;
11+
import org.apache.tomcat.util.codec.binary.StringUtils;
12+
import org.json.JSONObject;
13+
import org.junit.Ignore;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
18+
import org.springframework.http.HttpHeaders;
19+
import org.springframework.http.MediaType;
20+
import org.springframework.test.context.ContextConfiguration;
21+
import org.springframework.test.web.servlet.MockMvc;
22+
import org.springframework.test.web.servlet.MvcResult;
23+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
24+
25+
import java.io.File;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30+
31+
@Ignore
32+
@AutoConfigureMockMvc
33+
@ContextConfiguration(initializers = TestLDAPInitializer.class)
34+
public class AuthenticationSteps extends FunctionalTestBase {
35+
36+
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationSteps.class);
37+
private static final String SUBSCRIPTION = "src/functionaltests/resources/subscription_single.json";
38+
39+
@Autowired
40+
private MockMvc mockMvc;
41+
42+
@Autowired
43+
private AuthControllerImpl authController;
44+
45+
private MvcResult mvcResult;
46+
47+
private String requestBody;
48+
49+
@Given("^LDAP is activated$")
50+
public void ldap_is_activated() throws Throwable {
51+
String responseBody = new JSONObject().put("security", true).toString();
52+
mockMvc.perform(MockMvcRequestBuilders.get("/auth")
53+
.accept(MediaType.APPLICATION_JSON_VALUE))
54+
.andExpect(status().isOk())
55+
.andExpect(content().string(responseBody))
56+
.andReturn();
57+
requestBody = FileUtils.readFileToString(new File(SUBSCRIPTION), "UTF-8");
58+
}
59+
60+
@When("^make a POST request to the subscription REST API \"(/\\w+)\" without credentials$")
61+
public void make_a_post_request_to_the_subscription_rest_api_without_credentials(String endpoint) throws Throwable {
62+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(endpoint)
63+
.accept(MediaType.APPLICATION_JSON)
64+
.content(requestBody)
65+
.contentType(MediaType.APPLICATION_JSON))
66+
.andReturn();
67+
}
68+
69+
@Then("^get response code of (\\d+) and subscription with name \"(\\w+)\" is not created$")
70+
public void get_response_code_of_and_subscription_with_name_is_not_created(int statusCode, String subscriptionName) throws Throwable {
71+
assertEquals(statusCode, mvcResult.getResponse().getStatus());
72+
mockMvc.perform(MockMvcRequestBuilders.get("/subscriptions/" + subscriptionName)
73+
.accept(MediaType.APPLICATION_JSON_VALUE))
74+
.andExpect(status().isBadRequest())
75+
.andExpect(content().string("[]"))
76+
.andReturn();
77+
}
78+
///Scenario:1 ends ===============================================================================
79+
80+
@When("^make a POST request to the subscription REST API \"(/\\w+)\" with username \"(\\w+)\" and password \"(\\w+)\"")
81+
public void make_a_post_request_to_the_subscription_rest_api_with_username_and_password(String endpoint, String username, String password) throws Throwable {
82+
String auth = username + ":" + password;
83+
String encodedAuth = StringUtils.newStringUtf8(Base64.encodeBase64(auth.getBytes()));
84+
85+
mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(endpoint)
86+
.header(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth)
87+
.accept(MediaType.APPLICATION_JSON)
88+
.content(requestBody)
89+
.contentType(MediaType.APPLICATION_JSON))
90+
.andReturn();
91+
}
92+
93+
@Then("^get response code of (\\d+) and subscription with name \"(\\w+)\" is created$")
94+
public void get_response_code_of_and_subscription_with_name_is_created(int statusCode, String subscriptionName) throws Throwable {
95+
assertEquals(statusCode, mvcResult.getResponse().getStatus());
96+
mockMvc.perform(MockMvcRequestBuilders.get("/subscriptions/" + subscriptionName)
97+
.accept(MediaType.APPLICATION_JSON_VALUE))
98+
.andExpect(status().isOk())
99+
.andReturn();
100+
}
101+
///Scenario:2 ends ===============================================================================
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.ericsson.ei.subscriptions.authentication;
2+
3+
import cucumber.api.CucumberOptions;
4+
import cucumber.api.junit.Cucumber;
5+
import org.junit.runner.RunWith;
6+
7+
@RunWith(Cucumber.class)
8+
@CucumberOptions(features = "src/functionaltests/resources/features/authentication.feature", glue = {
9+
"com.ericsson.ei.subscriptions.authentication" }, plugin = { "pretty",
10+
"html:target/cucumber-reports/TestSubscriptionCRUDRunner" }, monochrome = false)
11+
public class TestAuthenticationRunner {
12+
13+
14+
15+
}

src/functionaltests/java/com/ericsson/ei/utils/TestConfigs.java

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.File;
88
import java.io.IOException;
99

10+
import org.apache.tomcat.util.codec.binary.Base64;
11+
import org.apache.tomcat.util.codec.binary.StringUtils;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214
import org.springframework.amqp.core.BindingBuilder;
@@ -76,6 +78,17 @@ MongoClient mongoClient() throws IOException {
7678
return null;
7779
}
7880

81+
@Bean
82+
void setAuthorization() {
83+
String password = StringUtils.newStringUtf8(Base64.encodeBase64("password".getBytes()));
84+
System.setProperty("ldap.enabled", "true");
85+
System.setProperty("ldap.url", "ldap://ldap.forumsys.com:389/dc=example,dc=com");
86+
System.setProperty("ldap.base.dn", "dc=example,dc=com");
87+
System.setProperty("ldap.username", "cn=read-only-admin,dc=example,dc=com");
88+
System.setProperty("ldap.password", password);
89+
System.setProperty("ldap.user.filter", "uid={0}");
90+
}
91+
7992
public void createExchange(final String exchangeName, final String queueName) {
8093
final CachingConnectionFactory ccf = new CachingConnectionFactory(cf);
8194
RabbitAdmin admin = new RabbitAdmin(ccf);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ericsson.ei.utils;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.context.ApplicationContextInitializer;
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
8+
public class TestLDAPInitializer extends TestConfigs
9+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
10+
11+
private static final Logger LOGGER = LoggerFactory.getLogger(TestLDAPInitializer.class);
12+
13+
@Override
14+
public void initialize(ConfigurableApplicationContext ac) {
15+
try {
16+
setAuthorization();
17+
} catch (Exception e) {
18+
LOGGER.error(e.getMessage(), e);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#Keywords Summary :
3+
#Feature: List of scenarios.
4+
#Scenario: Business rule through list of steps with arguments.
5+
#Given: Some precondition step
6+
#When: Some key actions
7+
#Then: To observe outcomes or validation
8+
#And,But: To enumerate more Given,When,Then steps
9+
#Scenario Outline: List of steps for data-driven as an Examples and <placeholder>
10+
#Examples: Container for s table
11+
#Background: List of steps run before each of the scenarios
12+
#""" (Doc Strings)
13+
#| (Data Tables)
14+
#@ (Tags/Labels):To group Scenarios
15+
#<> (placeholder)
16+
#""
17+
## (Comments)
18+
#Sample Feature Definition Template
19+
@tag
20+
Feature: Test Authentication
21+
22+
@tag1
23+
Scenario: Call an REST API without credentials
24+
Given LDAP is activated
25+
When make a POST request to the subscription REST API "/subscriptions" without credentials
26+
Then get response code of 401 and subscription with name "Subscription_Test" is not created
27+
28+
@tag2
29+
Scenario: Call an REST API with credentials
30+
Given LDAP is activated
31+
When make a POST request to the subscription REST API "/subscriptions" with username "gauss" and password "password"
32+
Then get response code of 200 and subscription with name "Subscription_Test" is created

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import org.springframework.beans.factory.annotation.Value;
2828
import org.springframework.http.HttpStatus;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.security.core.Authentication;
31-
import org.springframework.security.core.context.SecurityContextHolder;
3230
import org.springframework.stereotype.Component;
3331
import org.springframework.web.bind.annotation.CrossOrigin;
3432
import org.springframework.web.bind.annotation.PathVariable;

0 commit comments

Comments
 (0)