Skip to content

Commit 2cf13a4

Browse files
Adapt tests for resources deleted twice (State: GONE)
1 parent 45ed686 commit 2cf13a4

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

src/test/java/edu/kit/datamanager/repo/test/integration/DataResourceControllerTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,22 +825,37 @@ public void testDeleteResourceWithoutPermission() throws Exception {
825825

826826
@Test
827827
public void testDeleteResourceAsAdmin() throws Exception {
828+
ObjectMapper mapper = createObjectMapper();
828829
String etag = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
829830
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn().getResponse().getHeader("ETag");
830831

831832
this.mockMvc.perform(delete("/api/v1/dataresources/" + sampleResource.getId()).header("If-Match", etag).header(HttpHeaders.AUTHORIZATION,
832833
"Bearer " + adminToken).contentType("application/json")).andExpect(status().isNoContent());
834+
// Test access on revoked resource
835+
//from now on, the resource should be in state REVOKED...HTTP GET should fail for users
836+
this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
837+
"Bearer " + userToken)).andDo(print()).andExpect(status().isNotFound());
838+
839+
//from now on, the resource should be in state REVOKED...HTTP GET should still work for admins
840+
MvcResult result = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
841+
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn();
842+
DataResource resource = mapper.readValue(result.getResponse().getContentAsString(), DataResource.class);
843+
Assert.assertEquals(DataResource.State.REVOKED, resource.getState());
833844

834845
//try a second time...this should work
835846
etag = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
836847
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn().getResponse().getHeader("ETag");
837848

838849
this.mockMvc.perform(delete("/api/v1/dataresources/" + sampleResource.getId()).header("If-Match", etag).header(HttpHeaders.AUTHORIZATION,
839850
"Bearer " + adminToken).contentType("application/json")).andExpect(status().isNoContent());
840-
//from now on, the resource should be in state GONE...HTTP GET should fail
851+
//from now on, the resource should be in state GONE...HTTP GET should fail for users
841852
this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
842-
"Bearer " + adminToken)).andDo(print()).andExpect(status().isNotFound());
843-
853+
"Bearer " + userToken)).andDo(print()).andExpect(status().isNotFound());
854+
//from now on, the resource should be in state GONE...HTTP GET should still work for admins
855+
result = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
856+
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn();
857+
resource = mapper.readValue(result.getResponse().getContentAsString(), DataResource.class);
858+
Assert.assertEquals(DataResource.State.GONE, resource.getState());
844859
}
845860

846861
@Test

src/test/java/edu/kit/datamanager/repo/test/integration/DataResourceControllerTestWithoutVersioning.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -787,22 +787,37 @@ public void testDeleteResourceWithoutPermission() throws Exception {
787787

788788
@Test
789789
public void testDeleteResourceAsAdmin() throws Exception {
790-
String etag = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
791-
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn().getResponse().getHeader("ETag");
792-
793-
this.mockMvc.perform(delete("/api/v1/dataresources/" + sampleResource.getId()).header("If-Match", etag).header(HttpHeaders.AUTHORIZATION,
794-
"Bearer " + adminToken).contentType("application/json")).andExpect(status().isNoContent());
795-
796-
//try a second time...this should work
797-
etag = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
798-
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn().getResponse().getHeader("ETag");
799-
800-
this.mockMvc.perform(delete("/api/v1/dataresources/" + sampleResource.getId()).header("If-Match", etag).header(HttpHeaders.AUTHORIZATION,
801-
"Bearer " + adminToken).contentType("application/json")).andExpect(status().isNoContent());
802-
//from now on, the resource should be in state GONE...HTTP GET should fail
803-
this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
804-
"Bearer " + adminToken)).andDo(print()).andExpect(status().isNotFound());
805-
790+
ObjectMapper mapper = createObjectMapper();
791+
String etag = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
792+
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn().getResponse().getHeader("ETag");
793+
794+
this.mockMvc.perform(delete("/api/v1/dataresources/" + sampleResource.getId()).header("If-Match", etag).header(HttpHeaders.AUTHORIZATION,
795+
"Bearer " + adminToken).contentType("application/json")).andExpect(status().isNoContent());
796+
// Test access on revoked resource
797+
//from now on, the resource should be in state REVOKED...HTTP GET should fail for users
798+
this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
799+
"Bearer " + userToken)).andDo(print()).andExpect(status().isNotFound());
800+
801+
//from now on, the resource should be in state REVOKED...HTTP GET should still work for admins
802+
MvcResult result = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
803+
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn();
804+
DataResource resource = mapper.readValue(result.getResponse().getContentAsString(), DataResource.class);
805+
Assert.assertEquals(DataResource.State.REVOKED, resource.getState());
806+
807+
//try a second time...this should work
808+
etag = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
809+
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn().getResponse().getHeader("ETag");
810+
811+
this.mockMvc.perform(delete("/api/v1/dataresources/" + sampleResource.getId()).header("If-Match", etag).header(HttpHeaders.AUTHORIZATION,
812+
"Bearer " + adminToken).contentType("application/json")).andExpect(status().isNoContent());
813+
//from now on, the resource should be in state GONE...HTTP GET should fail for users
814+
this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
815+
"Bearer " + userToken)).andDo(print()).andExpect(status().isNotFound());
816+
//from now on, the resource should be in state GONE...HTTP GET should still work for admins
817+
result = this.mockMvc.perform(get("/api/v1/dataresources/" + sampleResource.getId()).header(HttpHeaders.AUTHORIZATION,
818+
"Bearer " + adminToken)).andDo(print()).andExpect(status().isOk()).andReturn();
819+
resource = mapper.readValue(result.getResponse().getContentAsString(), DataResource.class);
820+
Assert.assertEquals(DataResource.State.GONE, resource.getState());
806821
}
807822

808823
@Test

0 commit comments

Comments
 (0)