Skip to content

Commit 2af57d4

Browse files
committed
Added S3 tests
1 parent 33b042a commit 2af57d4

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

server/src/test/java/access/AbstractTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public abstract class AbstractTest {
163163
protected final Map<String, Long> seedIdentifiers = new HashMap<>();
164164

165165
@RegisterExtension
166-
static CustomWireMockExtension mockServer = new CustomWireMockExtension(8081);
166+
protected static CustomWireMockExtension mockServer = new CustomWireMockExtension(8081);
167167

168168
@LocalServerPort
169169
protected int port;

server/src/test/java/access/api/ApplicationS3ControllerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import access.AbstractTest;
44
import access.AccessCookieFilter;
55
import access.model.Application;
6-
import access.model.Connection;
76
import access.model.Organization;
87
import com.fasterxml.jackson.core.type.TypeReference;
98
import io.restassured.http.ContentType;

server/src/test/java/access/api/ManageControllerTest.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import access.model.EntityType;
66
import access.model.Environment;
77
import access.security.InstitutionAdmin;
8+
import com.fasterxml.jackson.core.JsonProcessingException;
89
import com.fasterxml.jackson.core.type.TypeReference;
910
import com.nimbusds.jose.util.IOUtils;
1011
import io.restassured.common.mapper.TypeRef;
@@ -124,6 +125,31 @@ void policyByServiceProvider() {
124125
assertEquals(1, policies.size());
125126
}
126127

128+
@SneakyThrows
129+
@Test
130+
void policyByServiceProviderNotAllowed() {
131+
Map<String, Object> identityProvider = super.stubForIdentityProviderByEntityId("http://mock-idp");
132+
Map<String, Object> attributes = Map.of(
133+
"sub", INSTITUTION_ADMIN,
134+
InstitutionAdmin.IDENTITY_PROVIDER, identityProvider);
135+
AccessCookieFilter accessCookieFilter = mockLoginFlow(attributes);
136+
137+
String serviceProviderEntityId = "nope";
138+
//The IdP is fetched to check the allowed entities
139+
this.stubForGetProvider(EntityType.saml20_idp, "7", Environment.PROD);
140+
141+
given()
142+
.when()
143+
.filter(accessCookieFilter.cookieFilter())
144+
.header(csrfHeader(accessCookieFilter))
145+
.accept(ContentType.JSON)
146+
.contentType(ContentType.JSON)
147+
.queryParam("entityId", serviceProviderEntityId)
148+
.get("/api/v1/manage/policies")
149+
.then()
150+
.statusCode(HttpStatus.FORBIDDEN.value());
151+
}
152+
127153
@SneakyThrows
128154
@Test
129155
void uniqueEntityId() {
@@ -182,6 +208,25 @@ void arpInfo() {
182208
assertTrue(allAttributesPresent);
183209
}
184210

211+
@Test
212+
void allowedAttributes() throws JsonProcessingException {
213+
List<Map<String, Object>> allowedAttributes = localManage.allowedAttributes();
214+
String body = objectMapper.writeValueAsString(allowedAttributes);
215+
stubFor(get("/manage/api/internal/protected/allowed-attributes")
216+
.willReturn(aResponse().withHeader("Content-Type", "application/json")
217+
.withBody(body)
218+
.withStatus(200)));
219+
220+
allowedAttributes = given()
221+
.when()
222+
.accept(ContentType.JSON)
223+
.contentType(ContentType.JSON)
224+
.get("/api/v1/manage/allowed-attributes")
225+
.as(new TypeRef<>() {
226+
});
227+
assertEquals(9, allowedAttributes.size());
228+
}
229+
185230
@Test
186231
void createPolicy() throws Exception {
187232
AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/me", ADMIN_SUB);
@@ -214,13 +259,13 @@ void uniquePolicyName() throws Exception {
214259
.withBody("[]")
215260
.withStatus(200)));
216261

217-
List<Map<String, Object>> policies =given()
262+
List<Map<String, Object>> policies = given()
218263
.when()
219264
.filter(accessCookieFilter.cookieFilter())
220265
.header(accessCookieFilter.csrfToken().getHeaderName(), accessCookieFilter.csrfToken().getToken())
221266
.accept(ContentType.JSON)
222267
.contentType(ContentType.JSON)
223-
.body(Map.of("name","policyName"))
268+
.body(Map.of("name", "policyName"))
224269
.post("/api/v1/manage/unique-policy-name")
225270
.as(new TypeRef<>() {
226271
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package access.api;
2+
3+
import access.AbstractTest;
4+
import org.apache.commons.io.IOUtils;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.core.io.ClassPathResource;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.util.Base64;
12+
13+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
class S3StorageTest extends AbstractTest {
17+
18+
@Autowired
19+
private S3Storage s3Storage;
20+
21+
22+
@Test
23+
void uploadFile() throws IOException {
24+
stubFor(head(urlPathMatching("/s3-images"))
25+
.willReturn(aResponse()
26+
.withStatus(404)
27+
.withHeader("Content-Type", "application/xml")
28+
.withBody("<Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist</Message></Error>")));
29+
30+
stubFor(put(urlPathMatching("/s3-images"))
31+
.willReturn(aResponse()
32+
.withStatus(200)));
33+
34+
stubFor(put(urlPathMatching("/s3-images/.*"))
35+
.willReturn(aResponse()
36+
.withStatus(201)));
37+
38+
InputStream inputStream = new ClassPathResource("/s3/squirl.jpg").getInputStream();
39+
byte[] byteArray = IOUtils.toByteArray(inputStream);
40+
String base64Encoded = Base64.getEncoder().encodeToString(byteArray);
41+
42+
String url = s3Storage.uploadFile(base64Encoded);
43+
assertTrue(url.startsWith("http://localhost:8081/s3-images/"));
44+
}
45+
}

0 commit comments

Comments
 (0)