Skip to content

Commit

Permalink
Fix tests for invalid attributes for json path
Browse files Browse the repository at this point in the history
[#119211635] https://www.pivotaltracker.com/story/show/119211635

Signed-off-by: Leslie Chang <lechang@pivotal.io>
Signed-off-by: Jeanie Jung <jeaniejung@berkeley.edu>
Signed-off-by: Leslie Chang <lechang@pivotal.io>
  • Loading branch information
jeaniejung authored and Leslie Chang committed Jul 2, 2016
1 parent 39e88e9 commit ee2a4e2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
Expand Up @@ -300,10 +300,8 @@ public void testListGroupsWithInvalidFilterFails() {
}

@Test
public void testListGroupsWithInvalidAttributesFails() {
expectedEx.expect(ScimException.class);
expectedEx.expectMessage("Invalid attributes");
endpoints.listGroups("id,displayName..", "displayName co \"admin\"", "created", "ascending", 1, 100);
public void testListGroupsWithInvalidAttributes() {
validateSearchResults(endpoints.listGroups("id,displayNameee", "displayName co \"admin\"", "created", "ascending", 1, 100), 1);
}

@Test
Expand Down Expand Up @@ -339,10 +337,8 @@ public void legacyTestListGroupsWithInvalidFilterFails() {
}

@Test
public void legacyTestListGroupsWithInvalidAttributesFails() {
expectedEx.expect(ScimException.class);
expectedEx.expectMessage("Invalid attributes");
endpoints.listGroups("id,displayName..", "displayName co 'admin'", "created", "ascending", 1, 100);
public void legacyTestListGroupsWithInvalidAttributes() {
validateSearchResults(endpoints.listGroups("id,displayNameee", "displayName co 'admin'", "created", "ascending", 1, 100), 1);
}

@Test
Expand Down
Expand Up @@ -68,14 +68,7 @@
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.servlet.View;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -603,6 +596,15 @@ public void testFindAllAttributes() {
assertEquals("Joel", joelMap.get("givenName"));
}

@Test
public void testFindNonExistingAttributes() {
String nonExistingAttribute = "blabla";
List<Map<String, Object>> resources = (List<Map<String, Object>>) endpoints.findUsers(nonExistingAttribute, "id pr", null, "ascending", 1, 100).getResources();
for (Map<String, Object> resource : resources) {
assertNull(resource.get(nonExistingAttribute));
}
}

@Test
public void testFindUsersGroupsSyncedByDefault() throws Exception {
ScimGroupMembershipManager mockgroupMembershipManager = mock(ScimGroupMembershipManager.class);
Expand Down
Expand Up @@ -534,33 +534,23 @@ public void testGetGroupsInvalidFilter() throws Exception {

@Test
public void testGetGroupsInvalidAttributes() throws Exception {
String filterNarrow = "displayName eq \"clients.read\" or displayName eq \"clients.write\"";
String nonexistentAttribute = "displayBlaBla";

MockHttpServletRequestBuilder get = get("/Groups")
.header("Authorization", "Bearer " + scimReadToken)
.contentType(MediaType.APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("attributes", "displayBlaBla,hello")
.param("filter", filterNarrow);
.header("Authorization", "Bearer " + scimReadToken)
.contentType(MediaType.APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("attributes", nonexistentAttribute);

MvcResult mvcResult = getMockMvc().perform(get)
.andExpect(status().isOk())
.andReturn();
.andExpect(status().isOk())
.andReturn();

String body = mvcResult.getResponse().getContentAsString();
SearchResults<ScimGroup> searchResults = JsonUtils.readValue(body, SearchResults.class);
Map<String, Object> attMap = (Map<String, Object>) searchResults.getResources().get(0);
assertNull(attMap.get("displayBlaBla"));
assertThat("Search results: " + body, searchResults.getResources(), hasSize(2));

get = get("/Groups")
.header("Authorization", "Bearer " + scimReadUserToken)
.contentType(MediaType.APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("attributes", "displayBlaBla");

getMockMvc().perform(get)
.andExpect(status().isOk())
.andReturn();
List<Map> attList = (List) JsonUtils.readValue(body, Map.class).get("resources");
for (Map<String, Object> attMap : attList) {
assertNull(attMap.get(nonexistentAttribute));
}
}

@Test
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
Expand All @@ -56,13 +57,12 @@
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.security.oauth2.common.util.OAuth2Utils.CLIENT_ID;
import static org.springframework.security.oauth2.common.util.OAuth2Utils.REDIRECT_URI;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand Down Expand Up @@ -559,6 +559,29 @@ public void testGetUser() throws Exception {
getUser(scimReadWriteToken, HttpStatus.OK.value());
}

@Test
public void testGetUserWithInvalidAttributes() throws Exception {

String nonexistentAttribute = "displayBlaBla";

MockHttpServletRequestBuilder get = get("/Users")
.header("Authorization", "Bearer " + scimReadWriteToken)
.contentType(MediaType.APPLICATION_JSON)
.param("attributes", nonexistentAttribute)
.accept(APPLICATION_JSON);

MvcResult mvcResult = getMockMvc().perform(get)
.andExpect(status().isOk())
.andReturn();

String body = mvcResult.getResponse().getContentAsString();

List<Map> attList = (List) JsonUtils.readValue(body, Map.class).get("resources");
for (Map<String, Object> attMap : attList) {
assertNull(attMap.get(nonexistentAttribute));
}
}

@Test
public void testGetUserWithScimCreateToken() throws Exception {
getUser(scimCreateToken,HttpStatus.FORBIDDEN.value());
Expand Down

0 comments on commit ee2a4e2

Please sign in to comment.