-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: tests on operation customizer for roles
- Loading branch information
Showing
3 changed files
with
91 additions
and
1 deletion.
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
74 changes: 74 additions & 0 deletions
74
src/test/java/fr/insee/queen/api/configuration/springdoc/DisplayRolesOnSwaggerUITest.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 @@ | ||
package fr.insee.queen.api.configuration.springdoc; | ||
|
||
import fr.insee.queen.api.configuration.swagger.role.DisplayRolesOnSwaggerUI; | ||
import fr.insee.queen.api.configuration.swagger.role.RoleUIMapper; | ||
import io.swagger.v3.oas.models.Operation; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.web.method.HandlerMethod; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DisplayRolesOnSwaggerUITest { | ||
|
||
private DummyController controller; | ||
private Operation operation; | ||
private DisplayRolesOnSwaggerUI operationCustomizer; | ||
@BeforeEach | ||
void init() { | ||
controller = new DummyController(); | ||
operation = new Operation(); | ||
operationCustomizer = new DisplayRolesOnSwaggerUI(); | ||
} | ||
|
||
@Test | ||
@DisplayName("on generate operation, when preauthorize annotation is not set do nothing") | ||
void testOperation01() throws NoSuchMethodException { | ||
Method method = controller.getClass().getMethod("testMethodNoPreauthorize"); | ||
HandlerMethod handlerMethod = new HandlerMethod(controller, method); | ||
Operation resultOperation = operationCustomizer.customize(operation, handlerMethod); | ||
|
||
assertThat(resultOperation) | ||
.isEqualTo(operation); | ||
} | ||
|
||
@Test | ||
@DisplayName("on generate operation, when preauthorize annotation is set return roles in operation description") | ||
void testOperation02() throws NoSuchMethodException { | ||
Method method = controller.getClass().getMethod("testMethodHasAnyRole"); | ||
HandlerMethod handlerMethod = new HandlerMethod(controller, method); | ||
Operation resultOperation = operationCustomizer.customize(operation, handlerMethod); | ||
|
||
assertThat(resultOperation.getDescription()) | ||
.isEqualTo(DisplayRolesOnSwaggerUI.AUTHORIZED_ROLES + RoleUIMapper.AUTHENTICATED + " / "); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("on generate operation, when description is set return it before roles") | ||
void testOperation03() throws NoSuchMethodException { | ||
String description = "description"; | ||
operation.setDescription(description); | ||
Method method = controller.getClass().getMethod("testMethodHasAnyRole"); | ||
HandlerMethod handlerMethod = new HandlerMethod(controller, method); | ||
Operation resultOperation = operationCustomizer.customize(operation, handlerMethod); | ||
|
||
assertThat(resultOperation.getDescription()) | ||
.startsWith(description +"\n"); | ||
} | ||
|
||
@Test | ||
@DisplayName("on generate operation, when preauthorize annotation with multiples roles is set return roles in operation description") | ||
void testOperation04() throws NoSuchMethodException { | ||
Method method = controller.getClass().getMethod("testMethodAdminOrInterviewer"); | ||
HandlerMethod handlerMethod = new HandlerMethod(controller, method); | ||
Operation resultOperation = operationCustomizer.customize(operation, handlerMethod); | ||
|
||
assertThat(resultOperation.getDescription()) | ||
.isEqualTo(DisplayRolesOnSwaggerUI.AUTHORIZED_ROLES + RoleUIMapper.ADMIN + " / " + RoleUIMapper.INTERVIEWER + " / "); | ||
|
||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/fr/insee/queen/api/configuration/springdoc/DummyController.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,15 @@ | ||
package fr.insee.queen.api.configuration.springdoc; | ||
|
||
import fr.insee.queen.api.configuration.auth.AuthorityRole; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
|
||
public class DummyController { | ||
|
||
@PreAuthorize(AuthorityRole.HAS_ANY_ROLE) | ||
public void testMethodHasAnyRole() {} | ||
|
||
@PreAuthorize(AuthorityRole.HAS_ADMIN_PRIVILEGES + "||" + AuthorityRole.HAS_ROLE_INTERVIEWER) | ||
public void testMethodAdminOrInterviewer() {} | ||
|
||
public void testMethodNoPreauthorize() {} | ||
} |