Skip to content

Commit

Permalink
Merge pull request #632 from apache/add-404-its
Browse files Browse the repository at this point in the history
Add tests for 404 when updating SCIM resources
  • Loading branch information
bdemers authored Sep 13, 2024
2 parents c81c577 + fb090a2 commit 111c1c3
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ public void updateGroupWithPatch() {
);
}

@Test
@DisplayName("Update Invalid Group")
void updateInvalidGroup() {

String id = randomName("group-invalidGroup-id");
String groupName = randomName("group-invalidGroup");

String updatedBody = "{" +
"\"schemas\": [\"urn:ietf:params:scim:schemas:core:2.0:Group\"]," +
"\"displayName\": \"" + groupName + "\"," +
"\"members\": []}";

put("/Groups/" + id, updatedBody)
.statusCode(404)
.body(
"schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"),
"status", is("404"),
"detail", is("Resource " + id + " not found.")
);
}

@Test
@DisplayName("Delete Invalid Group")
void deleteInvalidGroup() {
String id = randomName("group-invalidGroup-id");

delete("/Groups/" + id)
.statusCode(404)
.body(
"schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"),
"status", is("404"),
"detail", is("Resource " + id + " not found.")
);
}

String createUser(String name, String email) {
String body = "{" +
"\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"]," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,37 @@ public void deactivateWithPatch() {
"active", is(false)
);
}

@Test
@DisplayName("Update Invalid User")
void updateInvalidUser() {
String id = randomName("user-invalidGroup-id");

String updatedBody = "{" +
"\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"]," +
"\"active\":true" +
"}";

put("/Users/" + id, updatedBody)
.statusCode(404)
.body(
"schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"),
"status", is("404"),
"detail", is("Resource " + id + " not found.")
);
}

@Test
@DisplayName("Delete Invalid Users")
void deleteInvalidUser() {
String id = randomName("user-invalidGroup-id");

delete("/Users/" + id)
.statusCode(404)
.body(
"schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"),
"status", is("404"),
"detail", is("Resource " + id + " not found.")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@
package org.apache.directory.scim.example.jersey.service;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.directory.scim.core.repository.ETag;
import org.apache.directory.scim.core.repository.PatchHandler;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.core.repository.Repository;
import org.apache.directory.scim.core.schema.SchemaRegistry;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.spec.exception.ResourceException;
import org.apache.directory.scim.spec.exception.ResourceNotFoundException;
import org.apache.directory.scim.spec.filter.Filter;
import org.apache.directory.scim.spec.filter.FilterExpressions;
import org.apache.directory.scim.spec.filter.FilterResponse;
import org.apache.directory.scim.spec.filter.Filter;
import org.apache.directory.scim.spec.filter.PageRequest;
import org.apache.directory.scim.spec.filter.SortRequest;
import org.apache.directory.scim.spec.filter.attribute.AttributeReference;
Expand All @@ -46,10 +50,6 @@
import java.util.UUID;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
import org.apache.directory.scim.core.schema.SchemaRegistry;

@Named
@ApplicationScoped
public class InMemoryGroupService implements Repository<ScimGroup> {
Expand Down Expand Up @@ -106,12 +106,20 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti

@Override
public ScimGroup update(String id, Set<ETag> etags, ScimGroup resource, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!groups.containsKey(id)) {
throw new ResourceNotFoundException(id);
}

groups.put(id, resource);
return resource;
}

@Override
public ScimGroup patch(String id, Set<ETag> etags, List<PatchOperation> patchOperations, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!groups.containsKey(id)) {
throw new ResourceNotFoundException(id);
}

ScimGroup resource = patchHandler.apply(get(id), patchOperations);
groups.put(id, resource);
return resource;
Expand All @@ -123,8 +131,10 @@ public ScimGroup get(String id) {
}

@Override
public void delete(String id) {
groups.remove(id);
public void delete(String id) throws ResourceException {
if (groups.remove(id) == null) {
throw new ResourceNotFoundException(id);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.directory.scim.example.jersey.extensions.LuckyNumberExtension;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.spec.exception.ResourceException;
import org.apache.directory.scim.spec.exception.ResourceNotFoundException;
import org.apache.directory.scim.spec.extension.EnterpriseExtension;
import org.apache.directory.scim.spec.filter.Filter;
import org.apache.directory.scim.spec.filter.FilterExpressions;
Expand Down Expand Up @@ -142,12 +143,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException

@Override
public ScimUser update(String id, Set<ETag> etags, ScimUser resource, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!users.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
users.put(id, resource);
return resource;
}

@Override
public ScimUser patch(String id, Set<ETag> etags, List<PatchOperation> patchOperations, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!users.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
ScimUser resource = patchHandler.apply(get(id), patchOperations);
users.put(id, resource);
return resource;
Expand All @@ -166,8 +173,10 @@ public ScimUser get(String id) {
* @see Repository#delete(java.lang.String)
*/
@Override
public void delete(String id) {
users.remove(id);
public void delete(String id) throws ResourceException {
if (users.remove(id) == null) {
throw new ResourceNotFoundException(id);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.core.repository.Repository;
import org.apache.directory.scim.spec.exception.ResourceException;
import org.apache.directory.scim.spec.exception.ResourceNotFoundException;
import org.apache.directory.scim.spec.filter.FilterExpressions;
import org.apache.directory.scim.spec.filter.FilterResponse;
import org.apache.directory.scim.spec.filter.Filter;
Expand Down Expand Up @@ -106,12 +107,18 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti

@Override
public ScimGroup update(String id, Set<ETag> etags, ScimGroup resource, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!groups.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
groups.put(id, resource);
return resource;
}

@Override
public ScimGroup patch(String id, Set<ETag> etags, List<PatchOperation> patchOperations, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!groups.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
ScimGroup resource = patchHandler.apply(get(id), patchOperations);
groups.put(id, resource);
return resource;
Expand All @@ -123,8 +130,10 @@ public ScimGroup get(String id) {
}

@Override
public void delete(String id) {
groups.remove(id);
public void delete(String id) throws ResourceException {
if (groups.remove(id) == null) {
throw new ResourceNotFoundException(id);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.directory.scim.example.memory.extensions.LuckyNumberExtension;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.spec.exception.ResourceException;
import org.apache.directory.scim.spec.exception.ResourceNotFoundException;
import org.apache.directory.scim.spec.extension.EnterpriseExtension;
import org.apache.directory.scim.spec.filter.Filter;
import org.apache.directory.scim.spec.filter.FilterExpressions;
Expand Down Expand Up @@ -142,12 +143,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException

@Override
public ScimUser update(String id, Set<ETag> etags, ScimUser resource, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!users.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
users.put(id, resource);
return resource;
}

@Override
public ScimUser patch(String id, Set<ETag> etags, List<PatchOperation> patchOperations, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!users.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
ScimUser resource = patchHandler.apply(get(id), patchOperations);
users.put(id, resource);
return resource;
Expand All @@ -165,8 +172,10 @@ public ScimUser get(String id) {
* @see Repository#delete(java.lang.String)
*/
@Override
public void delete(String id) {
users.remove(id);
public void delete(String id) throws ResourceException {
if (users.remove(id) == null) {
throw new ResourceNotFoundException(id);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.directory.scim.core.schema.SchemaRegistry;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.spec.exception.ResourceException;
import org.apache.directory.scim.spec.exception.ResourceNotFoundException;
import org.apache.directory.scim.spec.filter.Filter;
import org.apache.directory.scim.spec.filter.FilterExpressions;
import org.apache.directory.scim.spec.filter.FilterResponse;
Expand Down Expand Up @@ -105,12 +106,18 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti

@Override
public ScimGroup update(String id, Set<ETag> etags, ScimGroup resource, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!groups.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
groups.put(id, resource);
return resource;
}

@Override
public ScimGroup patch(String id, Set<ETag> etags, List<PatchOperation> patchOperations, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!groups.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
ScimGroup resource = patchHandler.apply(get(id), patchOperations);
groups.put(id, resource);
return resource;
Expand All @@ -122,8 +129,10 @@ public ScimGroup get(String id) {
}

@Override
public void delete(String id) {
groups.remove(id);
public void delete(String id) throws ResourceException {
if (groups.remove(id) == null) {
throw new ResourceNotFoundException(id);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.directory.scim.example.quarkus.extensions.LuckyNumberExtension;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.spec.exception.ResourceException;
import org.apache.directory.scim.spec.exception.ResourceNotFoundException;
import org.apache.directory.scim.spec.extension.EnterpriseExtension;
import org.apache.directory.scim.spec.filter.Filter;
import org.apache.directory.scim.spec.filter.FilterExpressions;
Expand Down Expand Up @@ -142,12 +143,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException

@Override
public ScimUser update(String id, Set<ETag> etags, ScimUser resource, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!users.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
users.put(id, resource);
return resource;
}

@Override
public ScimUser patch(String id, Set<ETag> etags, List<PatchOperation> patchOperations, Set<AttributeReference> includedAttributeReferences, Set<AttributeReference> excludedAttributeReferences) throws ResourceException {
if (!users.containsKey(id)) {
throw new ResourceNotFoundException(id);
}
ScimUser resource = patchHandler.apply(get(id), patchOperations);
users.put(id, resource);
return resource;
Expand All @@ -165,8 +172,10 @@ public ScimUser get(String id) {
* @see Repository#delete(java.lang.String)
*/
@Override
public void delete(String id) {
users.remove(id);
public void delete(String id) throws ResourceException {
if (users.remove(id) == null) {
throw new ResourceNotFoundException(id);
}
}

/**
Expand Down
Loading

0 comments on commit 111c1c3

Please sign in to comment.