|
| 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 | +} |
0 commit comments