Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fix test for user assets

Rich Turner0cda2deca79a
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
index f98137422..8c51a88cd 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
@@ -74,32 +74,19 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
return new Asset<?>[0];
}
+ AssetQuery assetQuery = new AssetQuery()
+ .select(AssetQuery.Select.selectExcludePathAndParentInfo());
+
if (!isRestrictedUser()) {
- List<Asset<?>> result = assetStorageService.findAll(
- new AssetQuery()
- .select(selectExcludePathAndAttributes())
- .parents(new ParentPredicate(true))
- .tenant(new TenantPredicate(getAuthenticatedRealm()))
- );
- return result.toArray(new Asset<?>[0]);
+ assetQuery
+ .tenant(new TenantPredicate(getAuthenticatedRealm()))
+ .recursive(true);
+ } else {
+ assetQuery
+ .userIds(getUserId());
}
- List<Asset<?>> assets = assetStorageService.findAll(
- new AssetQuery()
- .select(selectExcludePathAndAttributes())
- .userIds(getUserId())
- );
-
- // Filter assets that might have been moved into a different realm and can no longer be accessed by user
- // TODO: Should we forbid moving assets between realms?
- Iterator<Asset<?>> it = assets.iterator();
- while (it.hasNext()) {
- Asset<?> asset = it.next();
- if (!asset.getRealm().equals(getAuthenticatedRealm())) {
- LOG.warning("User '" + getUsername() + "' linked to asset in other realm, skipping: " + asset);
- it.remove();
- }
- }
+ List<Asset<?>> assets = assetStorageService.findAll(assetQuery);
// Compress response (the request attribute enables the interceptor)
request.setAttribute(HttpHeaders.CONTENT_ENCODING, "gzip");
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
index 7d6e812b4..5923be22f 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
@@ -237,15 +237,17 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
/* ############################################## READ ####################################### */
- when: "the home assets of the authenticated user are retrieved"
+ when: "the assets of the authenticated user are retrieved"
def assets = assetResource.getCurrentUserAssets(null)
- then: "result should match"
- assets.length == 1
- assets[0].id == managerTestSetup.smartOfficeId
- // Assets should not be completely loaded
- assets[0].path == null
- assets[0].attributes.size() == 6
+ then: "result should match (all assets in the master realm as user is not restricted)"
+ assets.length == 5
+ assets.find {it.id == managerTestSetup.smartOfficeId}.attributes.size() == 8
+ // Assets should not be completely loaded (no path or parent info)
+ assets.find {it.id == managerTestSetup.smartOfficeId}.path == null
+ assets.find {it.id == managerTestSetup.smartOfficeId}.parentId == null
+ assets.find {it.id == managerTestSetup.smartOfficeId}.parentName == null
+ assets.find {it.id == managerTestSetup.smartOfficeId}.parentType == null
when: "the root assets of the authenticated realm are retrieved"
assets = assetResource.queryAssets(null,
@@ -404,12 +406,14 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
when: "the home assets of the authenticated user are retrieved"
def assets = assetResource.getCurrentUserAssets(null)
- then: "result should match"
- assets.length == 1
- assets[0].id == managerTestSetup.smartBuildingId
- // Assets should not be completely loaded
- assets[0].path == null
- assets[0].attributes.size() == 6
+ then: "result should match (all assets in the building realm as user is not restricted)"
+ assets.length == 13
+ assets.find {it.id == managerTestSetup.smartBuildingId}.attributes.size() == 8
+ // Assets should not be completely loaded (no path or parent info)
+ assets.find {it.id == managerTestSetup.smartBuildingId}.path == null
+ assets.find {it.id == managerTestSetup.smartBuildingId}.parentId == null
+ assets.find {it.id == managerTestSetup.smartBuildingId}.parentName == null
+ assets.find {it.id == managerTestSetup.smartBuildingId}.parentType == null
when: "the root assets of a foreign realm are retrieved"
assets = assetResource.queryAssets(null,
@@ -556,7 +560,7 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
apartment1.type == BuildingAsset.DESCRIPTOR.getName()
apartment1.parentId == managerTestSetup.smartBuildingId
apartment1.path == null
- apartment1.attributes.size() == 6
+ apartment1.attributes.size() == 12
Asset apartment1Livingroom = assets[1]
apartment1Livingroom.id == managerTestSetup.apartment1LivingroomId