-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test for custom javalin oidc action adapter (#4380)
* chore: add test for custom javlin oidc action adepter * use unused instance
- Loading branch information
1 parent
5ae0013
commit 15ad576
Showing
3 changed files
with
95 additions
and
5 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
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
89 changes: 89 additions & 0 deletions
89
...s-emx2-webapi/src/test/java/org/molgenis/emx2/web/JavalinCustomHttpActionAdapterTest.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,89 @@ | ||
package org.molgenis.emx2.web; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import io.javalin.http.BadRequestResponse; | ||
import io.javalin.http.Context; | ||
import io.javalin.http.ForbiddenResponse; | ||
import io.javalin.http.UnauthorizedResponse; | ||
import org.junit.jupiter.api.Test; | ||
import org.pac4j.core.context.WebContext; | ||
import org.pac4j.core.exception.http.FoundAction; | ||
import org.pac4j.core.exception.http.HttpAction; | ||
import org.pac4j.core.exception.http.OkAction; | ||
import org.pac4j.javalin.JavalinWebContext; | ||
|
||
class JavalinCustomHttpActionAdapterTest { | ||
|
||
@Test | ||
void checksContextForJavalin() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(HttpAction.class); | ||
WebContext ctx = mock(WebContext.class); | ||
assertThrows(RuntimeException.class, () -> adapter.adapt(action, ctx)); | ||
} | ||
|
||
@Test | ||
void throwsUnauthorizedIfActionHas401Code() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(HttpAction.class); | ||
when(action.getCode()).thenReturn(401); | ||
WebContext ctx = mock(JavalinWebContext.class); | ||
assertThrows(UnauthorizedResponse.class, () -> adapter.adapt(action, ctx)); | ||
} | ||
|
||
@Test | ||
void throwsForbiddenIfActionHas403Code() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(HttpAction.class); | ||
when(action.getCode()).thenReturn(403); | ||
WebContext ctx = mock(JavalinWebContext.class); | ||
assertThrows(ForbiddenResponse.class, () -> adapter.adapt(action, ctx)); | ||
} | ||
|
||
@Test | ||
void throwsBadRequestIfActionHas400Code() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(HttpAction.class); | ||
when(action.getCode()).thenReturn(400); | ||
WebContext ctx = mock(JavalinWebContext.class); | ||
assertThrows(BadRequestResponse.class, () -> adapter.adapt(action, ctx)); | ||
} | ||
|
||
@Test | ||
void shouldReturnAfterSettingContext() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(OkAction.class); | ||
when(action.getCode()).thenReturn(200); | ||
JavalinWebContext ctx = mock(JavalinWebContext.class); | ||
Context context = mock(Context.class); | ||
|
||
when(ctx.getJavalinCtx()).thenReturn(context); | ||
assertEquals(null, adapter.adapt(action, ctx)); | ||
} | ||
|
||
@Test | ||
void shouldReturnInCaseOfLocationAction() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(FoundAction.class); | ||
when(action.getCode()).thenReturn(300); | ||
JavalinWebContext ctx = mock(JavalinWebContext.class); | ||
Context context = mock(Context.class); | ||
|
||
when(ctx.getJavalinCtx()).thenReturn(context); | ||
assertEquals(null, adapter.adapt(action, ctx)); | ||
} | ||
|
||
@Test | ||
void shouldReturnInCaseOfUnmatchedAction() { | ||
JavalinCustomHttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE; | ||
HttpAction action = mock(HttpAction.class); | ||
when(action.getCode()).thenReturn(418); | ||
JavalinWebContext ctx = mock(JavalinWebContext.class); | ||
Context context = mock(Context.class); | ||
|
||
when(ctx.getJavalinCtx()).thenReturn(context); | ||
assertEquals(null, adapter.adapt(action, ctx)); | ||
} | ||
} |