33import cloudgene .mapred .util .CloudgeneClientRestAssured ;
44import io .micronaut .test .extensions .junit5 .annotation .MicronautTest ;
55import io .restassured .RestAssured ;
6+ import io .restassured .http .ContentType ;
67import io .restassured .http .Header ;
78import jakarta .inject .Inject ;
89import org .junit .jupiter .api .Test ;
910
11+ import java .util .Map ;
12+
1013import static org .hamcrest .Matchers .hasSize ;
1114import static org .hamcrest .core .IsEqual .equalTo ;
1215
@@ -18,40 +21,51 @@ public class BannerTest {
1821
1922 @ Test
2023 public void testBannerCreation () {
21- // Must be logged in .
24+ // Banner listing endpoint is public (no authentication required) .
2225 RestAssured
2326 .when ()
24- .get ("/api/v2/admin/banner" )
27+ .get ("/api/v2/banner" )
28+ .then ()
29+ .statusCode (200 )
30+ .body (equalTo ("[]" )); // We haven't added any elements yet.
31+
32+ // Must be logged in to POST.
33+ RestAssured
34+ .given ()
35+ .contentType (ContentType .JSON )
36+ .body (Map .of (
37+ "type" , "warning" ,
38+ "message" , "Test, test's \" ; 1, 2, 3!" ))
39+ .when ()
40+ .post ("/api/v2/admin/banner" )
2541 .then ()
2642 .statusCode (401 ); // Requires authentication.
2743
28- // Non-admin user denied.
44+ // Non-admin POST denied.
2945 Header publicToken = client .loginAsPublicUser ();
3046 RestAssured
3147 .given ()
3248 .header (publicToken )
49+ .contentType (ContentType .JSON )
50+ .body (Map .of (
51+ "type" , "warning" ,
52+ "message" , "Test, test's \" ; 1, 2, 3!" ))
3353 .when ()
34- .get ("/api/v2/admin/banner" )
54+ .post ("/api/v2/admin/banner" )
3555 .then ()
3656 .statusCode (403 ); // Requires higher permissions.
3757
38- // Admin user allowed, but we haven't added anything yet .
58+ // Admin user allowed.
3959 Header adminToken = client .login ("admin" , "admin1978" );
40- RestAssured
41- .given ()
42- .header (adminToken )
43- .when ()
44- .get ("/api/v2/admin/banner" )
45- .then ()
46- .statusCode (200 )
47- .body (equalTo ("[]" )); // Top-level element is an empty list.
4860
4961 // Add a warning
5062 RestAssured
5163 .given ()
5264 .header (adminToken )
53- .formParam ("type" , "warning" )
54- .formParam ("message" , "Test, test's \" ; 1, 2, 3!" )
65+ .contentType (ContentType .JSON )
66+ .body (Map .of (
67+ "type" , "warning" ,
68+ "message" , "Test, test's \" ; 1, 2, 3!" ))
5569 .when ()
5670 .post ("/api/v2/admin/banner" )
5771 .then ()
@@ -60,12 +74,10 @@ public void testBannerCreation() {
6074 .body ("message" , equalTo ("Test, test's \" ; 1, 2, 3!" ))
6175 .body ("id" , equalTo (1 ));
6276
63- // Now there is one element
77+ // Now there is one element (publicly visible)
6478 RestAssured
65- .given ()
66- .header (adminToken )
6779 .when ()
68- .get ("/api/v2/admin/ banner" )
80+ .get ("/api/v2/banner" )
6981 .then ()
7082 .statusCode (200 )
7183 .body ("" , hasSize (1 ))
@@ -77,8 +89,10 @@ public void testBannerCreation() {
7789 RestAssured
7890 .given ()
7991 .header (adminToken )
80- .formParam ("type" , "danger" )
81- .formParam ("message" , "\" or \" \" =\" " )
92+ .contentType (ContentType .JSON )
93+ .body (Map .of (
94+ "type" , "danger" ,
95+ "message" , "\" or \" \" =\" " ))
8296 .when ()
8397 .post ("/api/v2/admin/banner" )
8498 .then ()
@@ -89,10 +103,8 @@ public void testBannerCreation() {
89103
90104 // Now there are two elements, in order of addition.
91105 RestAssured
92- .given ()
93- .header (adminToken )
94106 .when ()
95- .get ("/api/v2/admin/ banner" )
107+ .get ("/api/v2/banner" )
96108 .then ()
97109 .statusCode (200 )
98110 .body ("" , hasSize (2 ))
@@ -115,10 +127,8 @@ public void testBannerCreation() {
115127
116128 // Only second element remains.
117129 RestAssured
118- .given ()
119- .header (adminToken )
120130 .when ()
121- .get ("/api/v2/admin/ banner" )
131+ .get ("/api/v2/banner" )
122132 .then ()
123133 .statusCode (200 )
124134 .body ("" , hasSize (1 ))
@@ -139,8 +149,10 @@ public void testBannerCreation() {
139149 RestAssured
140150 .given ()
141151 .header (adminToken )
142- .formParam ("type" , "warning" )
143- .formParam ("message" , "!@#$%^&*" )
152+ .contentType (ContentType .JSON )
153+ .body (Map .of (
154+ "type" , "warning" ,
155+ "message" , "!@#$%^&*" ))
144156 .when ()
145157 .post ("/api/v2/admin/banner" )
146158 .then ()
@@ -151,10 +163,8 @@ public void testBannerCreation() {
151163
152164 // Two elements again.
153165 RestAssured
154- .given ()
155- .header (adminToken )
156166 .when ()
157- .get ("/api/v2/admin/ banner" )
167+ .get ("/api/v2/banner" )
158168 .then ()
159169 .statusCode (200 )
160170 .body ("" , hasSize (2 ))
@@ -170,19 +180,46 @@ public void testBannerCreation() {
170180 RestAssured
171181 .given ()
172182 .header (adminToken )
173- .formParam ("id1" , 2 )
174- .formParam ("id2" , 3 )
183+ .contentType (ContentType .JSON )
184+ .body (Map .of (
185+ "id1" , 2 ,
186+ "id2" , 3 ))
175187 .when ()
176188 .post ("/api/v2/admin/banner/swap" )
177189 .then ()
178190 .statusCode (204 );
179191
180192 // The elements have swapped.
193+ RestAssured
194+ .when ()
195+ .get ("/api/v2/banner" )
196+ .then ()
197+ .statusCode (200 )
198+ .body ("" , hasSize (2 ))
199+ .body ("[0].type" , equalTo ("warning" ))
200+ .body ("[0].message" , equalTo ("!@#$%^&*" ))
201+ .body ("[0].id" , equalTo (3 ))
202+ .and ()
203+ .body ("[1].type" , equalTo ("danger" ))
204+ .body ("[1].message" , equalTo ("\" or \" \" =\" " ))
205+ .body ("[1].id" , equalTo (2 ));
206+
207+ // Let's update one element.
181208 RestAssured
182209 .given ()
183210 .header (adminToken )
211+ .contentType (ContentType .JSON )
212+ .body (Map .of (
213+ "message" , "fresh" ))
214+ .when ()
215+ .put ("/api/v2/admin/banner/2" )
216+ .then ()
217+ .statusCode (204 );
218+
219+ // The element is updated.
220+ RestAssured
184221 .when ()
185- .get ("/api/v2/admin/ banner" )
222+ .get ("/api/v2/banner" )
186223 .then ()
187224 .statusCode (200 )
188225 .body ("" , hasSize (2 ))
@@ -191,7 +228,7 @@ public void testBannerCreation() {
191228 .body ("[0].id" , equalTo (3 ))
192229 .and ()
193230 .body ("[1].type" , equalTo ("danger" ))
194- .body ("[1].message" , equalTo ("\" or \" \" = \" " ))
231+ .body ("[1].message" , equalTo ("fresh" )) // updated value here
195232 .body ("[1].id" , equalTo (2 ));
196233 }
197234}
0 commit comments