-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Security Tests for ManagementAPI @PreAuthorize methods
- Loading branch information
vasilchev
committed
Jan 6, 2025
1 parent
7e7fb0c
commit 727d997
Showing
20 changed files
with
2,720 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...est/java/org/eclipse/hawkbit/repository/jpa/AbstractRepositoryManagementSecurityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright (c) 2022 Bosch.IO GmbH and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.hawkbit.repository.jpa; | ||
|
||
import java.util.List; | ||
|
||
import io.qameta.allure.Description; | ||
import org.eclipse.hawkbit.im.authentication.SpPermission; | ||
import org.eclipse.hawkbit.repository.RepositoryManagement; | ||
import org.eclipse.hawkbit.repository.test.util.WithUser; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public abstract class AbstractRepositoryManagementSecurityTest<T, C, U> extends AbstractJpaIntegrationTest { | ||
|
||
/** | ||
* @return the repository management to test with | ||
*/ | ||
protected abstract RepositoryManagement<T, C, U> getRepositoryManagement(); | ||
|
||
/** | ||
* @return the object to create | ||
*/ | ||
protected abstract C getCreateObject(); | ||
|
||
/** | ||
* @return the object to update | ||
*/ | ||
protected abstract U getUpdateObject(); | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
void createCollectionPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().create(List.of(getCreateObject())), List.of(SpPermission.CREATE_REPOSITORY, SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
void createPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().create(getCreateObject()), List.of(SpPermission.CREATE_REPOSITORY, SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
void updatePermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().update(getUpdateObject()), List.of(SpPermission.UPDATE_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
void deletePermissionCheck() { | ||
assertPermissions(() -> { | ||
getRepositoryManagement().delete(1L); | ||
return null; | ||
}, List.of(SpPermission.DELETE_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void countPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().count(), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void deleteCollectionRepositoryManagement() { | ||
assertPermissions(() -> { | ||
getRepositoryManagement().delete(List.of(1L)); | ||
return null; | ||
}, List.of(SpPermission.DELETE_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void getPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().get(1L), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void getCollectionPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().get(List.of(1L)), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void existsCollectionPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().exists(1L), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void findAllPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().findAll(Pageable.ofSize(1)), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests RepositoryManagement PreAuthorized method with correct and insufficient permissions.") | ||
public void findByRsqlPermissionCheck() { | ||
assertPermissions(() -> getRepositoryManagement().findByRsql(Pageable.ofSize(1), "(name==*)"), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
...st/java/org/eclipse/hawkbit/repository/jpa/management/ArtifactManagementSecurityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) 2022 Bosch.IO GmbH and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.hawkbit.repository.jpa.management; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.List; | ||
|
||
import io.qameta.allure.Description; | ||
import io.qameta.allure.Feature; | ||
import io.qameta.allure.Story; | ||
import org.eclipse.hawkbit.im.authentication.SpPermission; | ||
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest; | ||
import org.eclipse.hawkbit.repository.model.ArtifactUpload; | ||
import org.eclipse.hawkbit.repository.test.util.WithUser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Feature("SecurityTests - ArtifactManagement") | ||
@Story("SecurityTests ArtifactManagement") | ||
class ArtifactManagementSecurityTest extends AbstractJpaIntegrationTest { | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#count() method") | ||
@WithUser(principal = "user", authorities = { SpPermission.READ_REPOSITORY }) | ||
void countPermissionCheck() { | ||
assertPermissions(() -> artifactManagement.count(), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#create() method") | ||
void createPermissionCheck() { | ||
ArtifactUpload artifactUpload = new ArtifactUpload(new ByteArrayInputStream("RandomString".getBytes()), 1L, "filename", false, 1024); | ||
assertPermissions(() -> artifactManagement.create(artifactUpload), List.of(SpPermission.CREATE_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#delete() method") | ||
void deletePermissionCheck() { | ||
assertPermissions(() -> { | ||
artifactManagement.delete(1); | ||
return null; | ||
}, List.of(SpPermission.DELETE_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#get() method") | ||
void getPermissionCheck() { | ||
assertPermissions(() -> artifactManagement.get(1L), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#getByFilenameAndSoftwareModule() method") | ||
void getByFilenameAndSoftwareModulePermissionCheck() { | ||
assertPermissions(() -> artifactManagement.getByFilenameAndSoftwareModule("filename", 1L), | ||
List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#findFirstBySHA1() method") | ||
void findFirstBySHA1PermissionCheck() { | ||
assertPermissions(() -> artifactManagement.findFirstBySHA1("sha1"), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#getByFilename() method") | ||
void getByFilenamePermissionCheck() { | ||
assertPermissions(() -> artifactManagement.getByFilename("filename"), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#findBySoftwareModule() method") | ||
void findBySoftwareModulePermissionCheck() { | ||
assertPermissions(() -> artifactManagement.findBySoftwareModule(PAGE, 1L), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#countBySoftwareModule() method") | ||
void countBySoftwareModulePermissionCheck() { | ||
assertPermissions(() -> artifactManagement.countBySoftwareModule(1L), List.of(SpPermission.READ_REPOSITORY)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ArtifactManagement#loadArtifactBinary() method") | ||
void loadArtifactBinaryPermissionCheck() { | ||
assertPermissions(() -> artifactManagement.loadArtifactBinary("sha1", 1L, false), List.of(SpPermission.DOWNLOAD_REPOSITORY_ARTIFACT)); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...ava/org/eclipse/hawkbit/repository/jpa/management/ConfirmationManagementSecurityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright (c) 2022 Bosch.IO GmbH and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.hawkbit.repository.jpa.management; | ||
|
||
import java.util.List; | ||
|
||
import io.qameta.allure.Description; | ||
import io.qameta.allure.Feature; | ||
import io.qameta.allure.Story; | ||
import org.eclipse.hawkbit.im.authentication.SpPermission; | ||
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Feature("SecurityTests - ConfirmationManagement") | ||
@Story("SecurityTests ConfirmationManagement") | ||
class ConfirmationManagementSecurityTest extends AbstractJpaIntegrationTest { | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#findActiveActionsWaitingConfirmation() method") | ||
void findActiveActionsWaitingConfirmationPermissionsCheck() { | ||
assertPermissions(() -> confirmationManagement.findActiveActionsWaitingConfirmation("controllerId"), List.of(SpPermission.READ_TARGET)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#activateAutoConfirmation() method") | ||
void activateAutoConfirmationPermissionsCheck() { | ||
assertPermissions(() -> confirmationManagement.activateAutoConfirmation("controllerId", "initiator", "remark"), | ||
List.of(SpPermission.READ_REPOSITORY, SpPermission.UPDATE_TARGET)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#getStatus() method") | ||
void getStatusPermissionsCheck() { | ||
assertPermissions(() -> confirmationManagement.getStatus("controllerId"), List.of(SpPermission.READ_TARGET)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#autoConfirmActiveActions() method") | ||
void autoConfirmActiveActionsPermissionsCheck() { | ||
assertPermissions(() -> confirmationManagement.autoConfirmActiveActions("controllerId"), | ||
List.of(SpPermission.READ_REPOSITORY, SpPermission.UPDATE_TARGET)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#confirmAction() method") | ||
void confirmActionPermissionsCheck() { | ||
assertPermissions(() -> confirmationManagement.confirmAction(1L, null, null), | ||
List.of(SpPermission.READ_REPOSITORY, SpPermission.UPDATE_TARGET)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#denyAction() method") | ||
void denyActionPermissionsCheck() { | ||
assertPermissions(() -> confirmationManagement.denyAction(1L, null, null), | ||
List.of(SpPermission.READ_REPOSITORY, SpPermission.UPDATE_TARGET)); | ||
} | ||
|
||
@Test | ||
@Description("Tests ConfirmationManagement#deactivateAutoConfirmation() method") | ||
void deactivateAutoConfirmationPermissionsCheck() { | ||
assertPermissions(() -> { | ||
confirmationManagement.deactivateAutoConfirmation("controllerId"); | ||
return null; | ||
}, List.of(SpPermission.READ_REPOSITORY, SpPermission.UPDATE_TARGET)); | ||
} | ||
|
||
} |
Oops, something went wrong.