1+ /*
2+ *
3+ * *
4+ * * * Copyright 2018 King's College London
5+ * * *
6+ * * * Licensed under the Apache License, Version 2.0 (the "License");
7+ * * * you may not use this file except in compliance with the License.
8+ * * * You may obtain a copy of the License at
9+ * * *
10+ * * * http://www.apache.org/licenses/LICENSE-2.0
11+ * * *
12+ * * * Unless required by applicable law or agreed to in writing, software
13+ * * * distributed under the License is distributed on an "AS IS" BASIS,
14+ * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * * * See the License for the specific language governing permissions and
16+ * * * limitations under the License.
17+ * * *
18+ * *
19+ *
20+ */
21+
22+ package org .radarbase .appserver .auth ;
23+
24+ import static org .junit .jupiter .api .Assertions .assertEquals ;
25+
26+ import org .junit .jupiter .api .BeforeAll ;
27+ import org .junit .jupiter .api .MethodOrderer .OrderAnnotation ;
28+ import org .junit .jupiter .api .Order ;
29+ import org .junit .jupiter .api .Test ;
30+ import org .junit .jupiter .api .TestMethodOrder ;
31+ import org .junit .jupiter .api .extension .ExtendWith ;
32+ import org .radarbase .appserver .auth .common .MPOAuthHelper ;
33+ import org .radarbase .appserver .auth .common .OAuthHelper ;
34+ import org .radarbase .appserver .dto .ProjectDto ;
35+ import org .springframework .boot .test .context .SpringBootTest ;
36+ import org .springframework .boot .test .web .client .TestRestTemplate ;
37+ import org .springframework .boot .test .web .server .LocalServerPort ;
38+ import org .springframework .http .HttpEntity ;
39+ import org .springframework .http .HttpHeaders ;
40+ import org .springframework .http .HttpMethod ;
41+ import org .springframework .http .HttpStatus ;
42+ import org .springframework .http .ResponseEntity ;
43+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
44+ import org .springframework .web .client .ResourceAccessException ;
45+
46+ @ ExtendWith (SpringExtension .class )
47+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
48+ @ TestMethodOrder (OrderAnnotation .class )
49+ @ SuppressWarnings ("PMD.DataflowAnomalyAnalysis" )
50+ public class ProjectEndpointAuthTest {
51+
52+ public static final String PROJECT_PATH = "/projects" ;
53+ private static final HttpHeaders HEADERS = new HttpHeaders ();
54+ private static HttpHeaders AUTH_HEADER ;
55+ private final transient TestRestTemplate restTemplate = new TestRestTemplate ();
56+ @ LocalServerPort private transient int port ;
57+
58+ @ BeforeAll
59+ static void init () {
60+ OAuthHelper oAuthHelper = new MPOAuthHelper ();
61+ AUTH_HEADER = new HttpHeaders ();
62+ AUTH_HEADER .setBearerAuth (oAuthHelper .getAccessToken ());
63+ }
64+
65+ public static String createURLWithPort (int port , String uri ) {
66+ return "http://localhost:" + port + uri ;
67+ }
68+
69+ @ Test
70+ public void unauthorisedCreateProject () {
71+
72+ ProjectDto projectDto = new ProjectDto (null , "radar" , null , null );
73+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(projectDto , HEADERS );
74+
75+ ResponseEntity <ProjectDto > responseEntity = null ;
76+ try {
77+ responseEntity =
78+ restTemplate .exchange (
79+ createURLWithPort (port , PROJECT_PATH ),
80+ HttpMethod .POST ,
81+ projectEntity ,
82+ ProjectDto .class );
83+ } catch (ResourceAccessException e ) {
84+ assertEquals (responseEntity , null );
85+ }
86+ }
87+
88+ @ Test
89+ public void unauthorisedViewProjects () {
90+
91+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(null , HEADERS );
92+
93+ ResponseEntity <ProjectDto > responseEntity =
94+ restTemplate .exchange (
95+ createURLWithPort (port , PROJECT_PATH ), HttpMethod .GET , projectEntity , ProjectDto .class );
96+ assertEquals (HttpStatus .UNAUTHORIZED , responseEntity .getStatusCode ());
97+ }
98+
99+ @ Test
100+ public void unauthorisedViewSingleProject () {
101+
102+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(null , HEADERS );
103+
104+ ResponseEntity <ProjectDto > responseEntity =
105+ restTemplate .exchange (
106+ createURLWithPort (port , "/projects/radar" ),
107+ HttpMethod .GET ,
108+ projectEntity ,
109+ ProjectDto .class );
110+ assertEquals (HttpStatus .UNAUTHORIZED , responseEntity .getStatusCode ());
111+ }
112+
113+ @ Test
114+ public void forbiddenViewProjects () {
115+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(null , AUTH_HEADER );
116+
117+ ResponseEntity <ProjectDto > responseEntity =
118+ restTemplate .exchange (
119+ createURLWithPort (port , PROJECT_PATH ), HttpMethod .GET , projectEntity , ProjectDto .class );
120+
121+ // Only Admins can view the list of all projects
122+ assertEquals (HttpStatus .FORBIDDEN , responseEntity .getStatusCode ());
123+ }
124+
125+ @ Test
126+ @ Order (1 )
127+ public void createSingleProjectWithAuth () {
128+ ProjectDto projectDto = new ProjectDto (null , "radar" , null , null );
129+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(projectDto , AUTH_HEADER );
130+
131+ ResponseEntity <ProjectDto > responseEntity =
132+ restTemplate .exchange (
133+ createURLWithPort (port , PROJECT_PATH ),
134+ HttpMethod .POST ,
135+ projectEntity ,
136+ ProjectDto .class );
137+
138+ if (responseEntity .getStatusCode ().equals (HttpStatus .EXPECTATION_FAILED )) {
139+ // The auth was successful but expectation failed if the project already exits.
140+ // Since this is just an auth test we can return.
141+ return ;
142+ }
143+ assertEquals (HttpStatus .CREATED , responseEntity .getStatusCode ());
144+ }
145+
146+ @ Test
147+ @ Order (2 )
148+ public void getSingleProjectWithAuth () {
149+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(null , AUTH_HEADER );
150+
151+ ResponseEntity <ProjectDto > responseEntity =
152+ restTemplate .exchange (
153+ createURLWithPort (port , "/projects/radar" ),
154+ HttpMethod .GET ,
155+ projectEntity ,
156+ ProjectDto .class );
157+
158+ assertEquals (HttpStatus .OK , responseEntity .getStatusCode ());
159+ }
160+
161+ @ Test
162+ @ Order (3 )
163+ public void getForbiddenProjectWithAuth () {
164+ HttpEntity <ProjectDto > projectEntity = new HttpEntity <>(null , AUTH_HEADER );
165+
166+ ResponseEntity <ProjectDto > responseEntity =
167+ restTemplate .exchange (
168+ createURLWithPort (port , "/projects/test" ),
169+ HttpMethod .GET ,
170+ projectEntity ,
171+ ProjectDto .class );
172+
173+ // Access denied as the user has only access to the project that it is part of.
174+ assertEquals (HttpStatus .FORBIDDEN , responseEntity .getStatusCode ());
175+ }
176+ }
0 commit comments