|
| 1 | +package com.faforever.api.data; |
| 2 | + |
| 3 | +import com.faforever.api.AbstractIntegrationTest; |
| 4 | +import com.faforever.api.data.domain.GroupPermission; |
| 5 | +import com.faforever.api.map.MapRepository; |
| 6 | +import com.faforever.api.security.OAuthScope; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.HttpHeaders; |
| 10 | +import org.springframework.test.context.jdbc.Sql; |
| 11 | +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
| 12 | + |
| 13 | +import static com.faforever.api.data.JsonApiMediaType.JSON_API_MEDIA_TYPE; |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | +import static org.hamcrest.Matchers.hasSize; |
| 16 | +import static org.hamcrest.Matchers.is; |
| 17 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | + |
| 22 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql") |
| 23 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql") |
| 24 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepMapVersion.sql") |
| 25 | +public class MapElideTest extends AbstractIntegrationTest { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + MapRepository mapRepository; |
| 29 | + |
| 30 | + private static final String MAP_RECOMMENDED_FALSE_ID_1 = """ |
| 31 | + { |
| 32 | + "data": { |
| 33 | + "type": "map", |
| 34 | + "id": "1", |
| 35 | + "attributes": { |
| 36 | + "recommended": false |
| 37 | + } |
| 38 | + } |
| 39 | + }"""; |
| 40 | + |
| 41 | + private static final String MAP_RECOMMENDED_TRUE_ID_1 = """ |
| 42 | + { |
| 43 | + "data": { |
| 44 | + "type": "map", |
| 45 | + "id": "1", |
| 46 | + "attributes": { |
| 47 | + "recommended": true |
| 48 | + } |
| 49 | + } |
| 50 | + }"""; |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + public void canReadMapWithoutScopeAndRole() throws Exception { |
| 55 | + mockMvc.perform(get("/data/map") |
| 56 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, NO_AUTHORITIES))) |
| 57 | + .andExpect(status().isOk()) |
| 58 | + .andExpect(jsonPath("$.data", hasSize(1))); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void canUpdateMapRecommendationToTrueWithScopeAndRole() throws Exception { |
| 63 | + mockMvc.perform( |
| 64 | + patch("/data/map/1") |
| 65 | + .with(getOAuthTokenWithTestUser(OAuthScope._MANAGE_VAULT, GroupPermission.ROLE_ADMIN_MAP)) |
| 66 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 67 | + // update recommended to true requires the vault permission, thus we test it here for the success case |
| 68 | + .content(MAP_RECOMMENDED_TRUE_ID_1)) |
| 69 | + .andExpect(status().isNoContent()); |
| 70 | + |
| 71 | + assertThat(mapRepository.getOne(1).getRecommended(), is(true)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void cannotUpdateMapRecommendationToTrueWithoutScope() throws Exception { |
| 76 | + mockMvc.perform( |
| 77 | + patch("/data/map/1") |
| 78 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_ADMIN_MAP)) |
| 79 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 80 | + // update recommended to true requires the vault permission, thus we test it here for the failing case |
| 81 | + .content(MAP_RECOMMENDED_TRUE_ID_1)) |
| 82 | + .andExpect(status().isForbidden()); |
| 83 | + |
| 84 | + assertThat(mapRepository.getOne(1).getRecommended(), is(false)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void cannotUpdateMapRecommendationToTrueWithoutRole() throws Exception { |
| 89 | + mockMvc.perform( |
| 90 | + patch("/data/map/1") |
| 91 | + .with(getOAuthTokenWithTestUser(OAuthScope._MANAGE_VAULT, NO_AUTHORITIES)) |
| 92 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 93 | + // update recommended to true requires the vault permission, thus we test it here for the failing case |
| 94 | + .content(MAP_RECOMMENDED_TRUE_ID_1)) |
| 95 | + .andExpect(status().isForbidden()); |
| 96 | + |
| 97 | + assertThat(mapRepository.getOne(1).getRecommended(), is(false)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void canUpdateMapRecommendationToFalseWithScopeAndRole() throws Exception { |
| 102 | + mockMvc.perform( |
| 103 | + patch("/data/map/1") |
| 104 | + .with(getOAuthTokenWithTestUser(OAuthScope._MANAGE_VAULT, GroupPermission.ROLE_ADMIN_MAP)) |
| 105 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 106 | + // update recommended to false requires the vault permission, thus we test it here for the success case |
| 107 | + .content(MAP_RECOMMENDED_FALSE_ID_1)) |
| 108 | + .andExpect(status().isNoContent()); |
| 109 | + |
| 110 | + assertThat(mapRepository.getOne(1).getRecommended(), is(false)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void cannotUpdateMapRecommendationToFalseWithoutScope() throws Exception { |
| 115 | + mockMvc.perform( |
| 116 | + patch("/data/map/1") |
| 117 | + .with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_ADMIN_MAP)) |
| 118 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 119 | + // update recommended to false requires the vault permission, thus we test it here for the failing case |
| 120 | + .content(MAP_RECOMMENDED_FALSE_ID_1)) |
| 121 | + .andExpect(status().isForbidden()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void cannotUpdateMapRecommendationToFalseWithoutRole() throws Exception { |
| 126 | + mockMvc.perform( |
| 127 | + patch("/data/map/1") |
| 128 | + .with(getOAuthTokenWithTestUser(OAuthScope._MANAGE_VAULT, NO_AUTHORITIES)) |
| 129 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 130 | + // update recommended to false requires the vault permission, thus we test it here for the failing case |
| 131 | + .content(MAP_RECOMMENDED_FALSE_ID_1)) |
| 132 | + .andExpect(status().isForbidden()); |
| 133 | + } |
| 134 | +} |
| 135 | + |
0 commit comments