Skip to content

Commit

Permalink
chore: add test for custom javalin oidc action adapter (#4380)
Browse files Browse the repository at this point in the history
* chore: add test for custom javlin oidc action adepter

* use unused instance
  • Loading branch information
connoratrug authored Oct 18, 2024
1 parent 5ae0013 commit 15ad576
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.molgenis.emx2.web.controllers;
package org.molgenis.emx2.web;

import io.javalin.http.*;
import org.pac4j.core.context.WebContext;
Expand All @@ -7,13 +7,13 @@
import org.pac4j.core.exception.http.WithLocationAction;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.javalin.JavalinHttpActionAdapter;
import org.pac4j.javalin.JavalinWebContext;

public class JavalinCustomHttpActionAdapter implements HttpActionAdapter {
public static final JavalinHttpActionAdapter INSTANCE = new JavalinHttpActionAdapter();
public static final JavalinCustomHttpActionAdapter INSTANCE =
new JavalinCustomHttpActionAdapter();

public JavalinCustomHttpActionAdapter() {}
private JavalinCustomHttpActionAdapter() {}

public Void adapt(HttpAction action, WebContext webContext) {
CommonHelper.assertNotNull("action", action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;
import org.molgenis.emx2.Database;
import org.molgenis.emx2.MolgenisException;
import org.molgenis.emx2.web.JavalinCustomHttpActionAdapter;
import org.molgenis.emx2.web.MolgenisSessionManager;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.session.SessionStore;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void handleLoginRequest(Context ctx) {
public void handleLoginCallback(Context ctx) {
final JavalinWebContext context = new JavalinWebContext(ctx);

HttpActionAdapter adapter = new JavalinCustomHttpActionAdapter();
HttpActionAdapter adapter = JavalinCustomHttpActionAdapter.INSTANCE;
final CallbackLogic callbackLogic =
FindBest.callbackLogic(null, securityConfig, DefaultCallbackLogic.INSTANCE);

Expand Down
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));
}
}

0 comments on commit 15ad576

Please sign in to comment.