11/**
2- * Copyright 2020 Dhatim
2+ * Copyright 2023 Dhatim
33 * <p>
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
55 * use this file except in compliance with the License. You may obtain a copy of
1919import io .dropwizard .testing .junit5 .DropwizardAppExtension ;
2020import io .dropwizard .testing .junit5 .DropwizardExtensionsSupport ;
2121import io .jsonwebtoken .lang .Strings ;
22- import org .junit .Assert ;
22+ import org .junit .jupiter . api . Assertions ;
2323import org .junit .jupiter .api .Test ;
2424import org .junit .jupiter .api .extension .ExtendWith ;
2525
26- import javax .ws .rs .client .Client ;
2726import javax .ws .rs .client .Entity ;
2827import javax .ws .rs .client .WebTarget ;
2928import javax .ws .rs .core .MediaType ;
@@ -47,37 +46,38 @@ public class JwtCookieAuthenticationTest {
4746 private WebTarget getTarget () {
4847 return EXT .client ().target ("http://localhost:" + EXT .getLocalPort () + "/application" ).path ("principal" );
4948 }
49+
5050 @ Test
5151 public void testUnauthorized () {
5252 //calls to APIs with the @Auth annotation without prior authentication should result in HTTP 401
5353 Response response = getTarget ().request (MediaType .APPLICATION_JSON ).get ();
54- Assert .assertEquals (401 , response .getStatus ());
54+ Assertions .assertEquals (401 , response .getStatus ());
5555 }
5656
5757 @ Test
5858 public void testCookieSetting () throws IOException {
5959 String principalName = UUID .randomUUID ().toString ();
6060 //a POST will set the principal
6161 Response response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (new DefaultJwtCookiePrincipal (principalName )));
62- Assert .assertEquals (200 , response .getStatus ());
62+ Assertions .assertEquals (200 , response .getStatus ());
6363 DefaultJwtCookiePrincipal principal = getPrincipal (response );
64- Assert .assertEquals (principalName , principal .getName ());
64+ Assertions .assertEquals (principalName , principal .getName ());
6565
6666 //check that a session cookie has been set
6767 NewCookie cookie1 = response .getCookies ().get (COOKIE_NAME );
68- Assert .assertNotNull (cookie1 );
69- Assert .assertTrue (Strings .hasText (cookie1 .getValue ()));
70- Assert .assertTrue (cookie1 .isHttpOnly ());
68+ Assertions .assertNotNull (cookie1 );
69+ Assertions .assertTrue (Strings .hasText (cookie1 .getValue ()));
70+ Assertions .assertTrue (cookie1 .isHttpOnly ());
7171
7272 //a GET with this cookie should return the Principal and refresh the cookie
7373 response = getTarget ().request (MediaType .APPLICATION_JSON ).cookie (cookie1 ).get ();
74- Assert .assertEquals (200 , response .getStatus ());
74+ Assertions .assertEquals (200 , response .getStatus ());
7575 principal = getPrincipal (response );
76- Assert .assertEquals (principalName , principal .getName ());
76+ Assertions .assertEquals (principalName , principal .getName ());
7777 NewCookie cookie2 = response .getCookies ().get (COOKIE_NAME );
78- Assert .assertNotNull (cookie2 );
79- Assert .assertTrue (Strings .hasText (cookie1 .getValue ()));
80- Assert .assertNotSame (cookie1 .getValue (), cookie2 .getValue ());
78+ Assertions .assertNotNull (cookie2 );
79+ Assertions .assertTrue (Strings .hasText (cookie1 .getValue ()));
80+ Assertions .assertNotSame (cookie1 .getValue (), cookie2 .getValue ());
8181 }
8282
8383 @ Test
@@ -88,9 +88,9 @@ public void testDontRefreshSession() throws IOException {
8888 NewCookie cookie = response .getCookies ().get (COOKIE_NAME );
8989
9090 response = getTarget ().path ("idempotent" ).request (MediaType .APPLICATION_JSON ).cookie (cookie ).get ();
91- Assert .assertEquals (200 , response .getStatus ());
92- Assert .assertEquals (principalName , getPrincipal (response ).getName ());
93- Assert .assertNull (response .getCookies ().get (COOKIE_NAME ));
91+ Assertions .assertEquals (200 , response .getStatus ());
92+ Assertions .assertEquals (principalName , getPrincipal (response ).getName ());
93+ Assertions .assertNull (response .getCookies ().get (COOKIE_NAME ));
9494 }
9595
9696 @ Test
@@ -101,8 +101,8 @@ public void testPublicEndpoint() {
101101
102102 //request made to public methods should not refresh the cookie
103103 response = getTarget ().path ("public" ).request (MediaType .APPLICATION_JSON ).cookie (cookie ).get ();
104- Assert .assertEquals (200 , response .getStatus ());
105- Assert .assertNull (response .getCookies ().get (COOKIE_NAME ));
104+ Assertions .assertEquals (200 , response .getStatus ());
105+ Assertions .assertNull (response .getCookies ().get (COOKIE_NAME ));
106106 }
107107
108108 @ Test
@@ -111,55 +111,55 @@ public void testRememberMe() {
111111 DefaultJwtCookiePrincipal principal = new DefaultJwtCookiePrincipal (UUID .randomUUID ().toString ());
112112 Response response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (principal ));
113113 NewCookie cookie = response .getCookies ().get (COOKIE_NAME );
114- Assert .assertNotNull (cookie );
115- Assert .assertEquals (-1 , cookie .getMaxAge ());
114+ Assertions .assertNotNull (cookie );
115+ Assertions .assertEquals (-1 , cookie .getMaxAge ());
116116
117117 //a long term principal should set a persistent cookie
118118 principal .setPersistent (true );
119119 response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (principal ));
120120 cookie = response .getCookies ().get (COOKIE_NAME );
121121 //default maxAge is 604800s (7 days)
122- Assert .assertNotNull (cookie );
123- Assert .assertEquals (604800 , cookie .getMaxAge ());
122+ Assertions .assertNotNull (cookie );
123+ Assertions .assertEquals (604800 , cookie .getMaxAge ());
124124 }
125125
126126 @ Test
127127 public void testRoles () {
128128 WebTarget restrictedTarget = getTarget ().path ("restricted" );
129129 //try to access the resource without cookie (-> 401 UNAUTHORIZED)
130130 Response response = restrictedTarget .request ().get ();
131- Assert .assertEquals (401 , response .getStatus ());
131+ Assertions .assertEquals (401 , response .getStatus ());
132132
133133 //set a principal without the admin role (-> 403 FORBIDDEN)
134134 DefaultJwtCookiePrincipal principal = new DefaultJwtCookiePrincipal (UUID .randomUUID ().toString ());
135135 response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (principal ));
136136 NewCookie cookie = response .getCookies ().get (COOKIE_NAME );
137- Assert .assertNotNull (cookie );
137+ Assertions .assertNotNull (cookie );
138138 response = restrictedTarget .request ().cookie (cookie ).get ();
139- Assert .assertEquals (403 , response .getStatus ());
139+ Assertions .assertEquals (403 , response .getStatus ());
140140
141141 //set a principal with the admin role (-> 200 OK)
142142 principal .setRoles (Collections .singleton ("admin" ));
143143 response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (principal ));
144144 cookie = response .getCookies ().get (COOKIE_NAME );
145- Assert .assertNotNull (cookie );
145+ Assertions .assertNotNull (cookie );
146146 response = restrictedTarget .request ().cookie (cookie ).get ();
147- Assert .assertEquals (200 , response .getStatus ());
147+ Assertions .assertEquals (200 , response .getStatus ());
148148 }
149149
150150 @ Test
151151 public void testDeleteCookie () {
152152 Response response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (new DefaultJwtCookiePrincipal (UUID .randomUUID ().toString ())));
153153 NewCookie cookie = response .getCookies ().get (COOKIE_NAME );
154- Assert .assertNotNull (cookie );
154+ Assertions .assertNotNull (cookie );
155155
156156 //removing the principal should produce a cookie with empty contenant and a past expiration date
157157 response = getTarget ().path ("unset" ).request ().cookie (cookie ).get ();
158- Assert .assertEquals (204 , response .getStatus ());
158+ Assertions .assertEquals (204 , response .getStatus ());
159159 cookie = response .getCookies ().get (COOKIE_NAME );
160- Assert .assertNotNull (cookie );
161- Assert .assertEquals ("" , cookie .getValue ());
162- Assert .assertEquals (Date .from (Instant .EPOCH ), cookie .getExpiry ());
160+ Assertions .assertNotNull (cookie );
161+ Assertions .assertEquals ("" , cookie .getValue ());
162+ Assertions .assertEquals (Date .from (Instant .EPOCH ), cookie .getExpiry ());
163163 }
164164
165165 @ Test
@@ -168,11 +168,11 @@ public void testGetCurrentPrincipal() throws IOException {
168168 String principalName = UUID .randomUUID ().toString ();
169169 Response response = getTarget ().request (MediaType .APPLICATION_JSON ).post (Entity .json (new DefaultJwtCookiePrincipal (principalName )));
170170 NewCookie cookie = response .getCookies ().get (COOKIE_NAME );
171- Assert .assertNotNull (cookie );
171+ Assertions .assertNotNull (cookie );
172172
173173 response = getTarget ().path ("current" ).request (MediaType .APPLICATION_JSON ).cookie (cookie ).get ();
174- Assert .assertEquals (200 , response .getStatus ());
175- Assert .assertEquals (principalName , getPrincipal (response ).getName ());
174+ Assertions .assertEquals (200 , response .getStatus ());
175+ Assertions .assertEquals (principalName , getPrincipal (response ).getName ());
176176 }
177177
178178 private DefaultJwtCookiePrincipal getPrincipal (Response response ) throws IOException {
0 commit comments