Skip to content

Commit

Permalink
Add support for GET /mfa-providers and /mfa-provider/{id}
Browse files Browse the repository at this point in the history
[#151224730] https://www.pivotaltracker.com/story/show/151224730

Signed-off-by: Shash Reddy <sreddy@pivotal.io>
  • Loading branch information
Bharath Sekar authored and Pivotal committed Oct 11, 2017
1 parent dd00f85 commit 8366bbe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 118 deletions.
Expand Up @@ -9,10 +9,13 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

Expand Down
Expand Up @@ -7,7 +7,6 @@
import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.doNothing;
Expand Down
@@ -1,6 +1,5 @@
package org.cloudfoundry.identity.uaa.mock.mfa_provider;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.cloudfoundry.identity.uaa.mfa_provider.GoogleMfaProviderConfig;
import org.cloudfoundry.identity.uaa.mfa_provider.JdbcMfaProviderProvisioning;
Expand All @@ -12,20 +11,19 @@
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneSwitchingFilter;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -41,44 +39,6 @@ public void setup() throws Exception{
"clients.read clients.write clients.secret clients.admin uaa.admin");
}

@Test
public void testCreateGoogleMfaProvider() throws Exception {
MfaProvider mfaProvider = constructGoogleProvider();
((GoogleMfaProviderConfig)mfaProvider.getConfig())
.setAlgorithm(GoogleMfaProviderConfig.Algorithm.SHA512)
.setDigits(25)
.setDuration(10);
MvcResult mfaResponse = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaProvider))).andReturn();
assertEquals(HttpStatus.CREATED.value(), mfaResponse.getResponse().getStatus());
MfaProvider<GoogleMfaProviderConfig> mfaProviderCreated = JsonUtils.readValue(mfaResponse.getResponse().getContentAsString(), MfaProvider.class);
assertEquals(IdentityZoneHolder.get().getName(), mfaProviderCreated.getConfig().getIssuer());
assertEquals(IdentityZoneHolder.get().getId(), mfaProviderCreated.getIdentityZoneId());

}

@Test
public void testCreateGoogleMfaProvider_UnauthorizedResponse() throws Exception{
MfaProvider mfaProvider = constructGoogleProvider();
((GoogleMfaProviderConfig)mfaProvider.getConfig())
.setAlgorithm(GoogleMfaProviderConfig.Algorithm.SHA512)
.setDigits(25)
.setDuration(10);
String unauthorizedToken = testClient.getClientCredentialsOAuthAccessToken("admin", "adminsecret",
"clients.read clients.write clients.secret clients.admin");
MvcResult mfaResponse = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + unauthorizedToken)
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaProvider))).andReturn();
assertEquals(HttpStatus.FORBIDDEN.value(), mfaResponse.getResponse().getStatus());
JsonNode json = JsonUtils.readTree(mfaResponse.getResponse().getContentAsString());
assertNotNull("response was not json",json);
}

@Test
public void testCreateGoogleMfaProviderConfigDefaults() throws Exception {
MfaProvider mfaProvider = constructGoogleProvider();
Expand All @@ -88,10 +48,10 @@ public void testCreateGoogleMfaProviderConfigDefaults() throws Exception {
.header("Authorization", "Bearer " + adminToken)
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaProvider))).andReturn();
assertEquals(HttpStatus.CREATED.value(), mfaResponse.getResponse().getStatus());
Assert.assertEquals(HttpStatus.CREATED.value(), mfaResponse.getResponse().getStatus());
MfaProvider<GoogleMfaProviderConfig> mfaProviderCreated = JsonUtils.readValue(mfaResponse.getResponse().getContentAsString(), MfaProvider.class);
assertEquals(IdentityZoneHolder.get().getName(), mfaProviderCreated.getConfig().getIssuer());
assertEquals(IdentityZoneHolder.get().getId(), mfaProviderCreated.getIdentityZoneId());
Assert.assertEquals(IdentityZoneHolder.get().getName(), mfaProviderCreated.getConfig().getIssuer());
Assert.assertEquals(IdentityZoneHolder.get().getId(), mfaProviderCreated.getIdentityZoneId());

}

Expand All @@ -100,44 +60,12 @@ public void testCreateGoogleMfaProviderInvalidType() throws Exception {
MfaProvider mfaProvider = constructGoogleProvider();
ObjectNode mfaAsJSON = (ObjectNode) JsonUtils.readTree(JsonUtils.writeValueAsString(mfaProvider));
mfaAsJSON.put("type", "not-google-authenticator");
MockHttpServletResponse response = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaAsJSON))).andReturn().getResponse();
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY.value(), response.getStatus());
assertThat(response.getContentAsString(), Matchers.containsString("Provider type is required. Must be one of " + MfaProvider.MfaProviderType.getStringValues()));
}

@Test
public void testCreateGoogleMfaProviderConfig() throws Exception {
MfaProvider mfaProvider = constructGoogleProvider();
((GoogleMfaProviderConfig) mfaProvider.getConfig()).setDigits(-1);
ObjectNode mfaAsJSON = (ObjectNode) JsonUtils.readTree(JsonUtils.writeValueAsString(mfaProvider));
MockHttpServletResponse response = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaAsJSON))).andReturn().getResponse();
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY.value(), response.getStatus());
assertThat(response.getContentAsString(), Matchers.containsString("Invalid Config for MFA Provider. Digits must be greater than 0"));
}

@Test
public void testCreateGoogleMfaProviderConfigAlgorithm() throws Exception {
MfaProvider mfaProvider = constructGoogleProvider();
ObjectNode mfaAsJSON = (ObjectNode) JsonUtils.readTree(JsonUtils.writeValueAsString(mfaProvider));
JsonNode configNode = mfaAsJSON.get("config");
((ObjectNode)configNode).put("algorithm", "SHA100");
mfaAsJSON.set("config", configNode);

MockHttpServletResponse response = getMockMvc().perform(
ResultActions authorization = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaAsJSON))).andReturn().getResponse();
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY.value(), response.getStatus());
assertThat(response.getContentAsString(), Matchers.containsString("Invalid Config for MFA Provider. Algorithm must be one of " + GoogleMfaProviderConfig.Algorithm.getStringaValues()));
.content(JsonUtils.writeValueAsString(mfaAsJSON)));
Assert.assertEquals(HttpStatus.UNPROCESSABLE_ENTITY.value(), authorization.andReturn().getResponse().getStatus());
}


Expand Down Expand Up @@ -166,43 +94,6 @@ public void testRetrieveMfaProviderById() throws Exception {
Assert.assertEquals(JsonUtils.writeValueAsString(createdProvider), result.getResponse().getContentAsString());
}

@Test
public void testCreateMfaForOtherZone() throws Exception{
IdentityZone identityZone = MockMvcUtils.utils().createZoneUsingWebRequest(getMockMvc(), adminToken);

MfaProvider mfaProvider = constructGoogleProvider();
MvcResult mfaResponse = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.header(IdentityZoneSwitchingFilter.HEADER, identityZone.getId())
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaProvider))).andReturn();
Assert.assertEquals(HttpStatus.CREATED.value(), mfaResponse.getResponse().getStatus());
}

@Test
public void testGetMfaInOtherZone() throws Exception{
IdentityZone identityZone = MockMvcUtils.utils().createZoneUsingWebRequest(getMockMvc(), adminToken);

MfaProvider mfaProvider = constructGoogleProvider();
MvcResult createResult = getMockMvc().perform(
post("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.header(IdentityZoneSwitchingFilter.HEADER, identityZone.getId())
.contentType(APPLICATION_JSON)
.content(JsonUtils.writeValueAsString(mfaProvider))).andReturn();
mfaProvider = JsonUtils.readValue(createResult.getResponse().getContentAsString(), MfaProvider.class);


MvcResult mfaListResult = getMockMvc().perform(
get("/mfa-providers")
.header("Authorization", "Bearer " + adminToken)
.header(IdentityZoneSwitchingFilter.HEADER, identityZone.getId())).andReturn();
List<Map> mfaProviders = JsonUtils.readValue(mfaListResult.getResponse().getContentAsString(), List.class);
List providerIds = mfaProviders.stream().map(provider -> provider.get("id")).collect(Collectors.toList());
assertTrue(providerIds.contains(mfaProvider.getId()));
}

@Test
public void testRetrieveMfaProviderByIdInvalid() throws Exception {
MvcResult authorization = getMockMvc().perform(
Expand Down

0 comments on commit 8366bbe

Please sign in to comment.