|
| 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.security.OAuthScope; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.test.context.jdbc.Sql; |
| 9 | +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
| 10 | + |
| 11 | +import static com.faforever.api.data.JsonApiMediaType.JSON_API_MEDIA_TYPE; |
| 12 | +import static org.hamcrest.Matchers.hasSize; |
| 13 | +import static org.hamcrest.Matchers.is; |
| 14 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 15 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; |
| 16 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 18 | + |
| 19 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql") |
| 20 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql") |
| 21 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepMatchmakerQueueMapPoolData.sql") |
| 22 | +public class MatchmakerQueueMapPoolElideTest extends AbstractIntegrationTest { |
| 23 | + |
| 24 | + private static <T> String genUpdateRequestForField(String fieldName, T fieldValue) { |
| 25 | + if (fieldValue instanceof String) { |
| 26 | + fieldValue = (T) ("\"" + fieldValue + "\""); |
| 27 | + } |
| 28 | + |
| 29 | + return """ |
| 30 | + { |
| 31 | + "data": { |
| 32 | + "type": "matchmakerQueueMapPool", |
| 33 | + "id": "101", |
| 34 | + "attributes": { |
| 35 | + "%s": %s |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + """.formatted(fieldName, fieldValue); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void canReadMatchmakerQueueMapPoolWithoutScopeAndRole() throws Exception { |
| 44 | + mockMvc.perform(get("/data/matchmakerQueueMapPool") |
| 45 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES))) |
| 46 | + .andExpect(status().isOk()) |
| 47 | + .andExpect(jsonPath("$.data", hasSize(5))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void canReadMatchmakerQueueMapPoolParamsWithoutScopeAndRole() throws Exception { |
| 52 | + mockMvc.perform(get("/data/matchmakerQueueMapPool/101") |
| 53 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES))) |
| 54 | + .andExpect(status().isOk()) |
| 55 | + .andExpect(jsonPath("$.data.id", is("101"))) |
| 56 | + .andExpect(jsonPath("$.data.type", is("matchmakerQueueMapPool"))) |
| 57 | + .andExpect(jsonPath("$.data.attributes.maxRating", is(800.0))) |
| 58 | + .andExpect(jsonPath("$.data.attributes.minRating", is(300.0))) |
| 59 | + .andExpect(jsonPath("$.data.attributes.vetoTokensPerPlayer", is(1))) |
| 60 | + .andExpect(jsonPath("$.data.attributes.minimumMapsAfterVeto", is(1.5))) |
| 61 | + .andExpect(jsonPath("$.data.attributes.maxTokensPerMap", is(1))) |
| 62 | + .andExpect(jsonPath("$.data.relationships.mapPool.data.id", is("2"))) |
| 63 | + .andExpect(jsonPath("$.data.relationships.matchmakerQueue.data.id", is("1"))); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void cannotUpdateMatchmakerQueueMapPoolMinRatingWithoutScopeAndRole() throws Exception { |
| 68 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 69 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)) |
| 70 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 71 | + .content(genUpdateRequestForField("minRating", 1000))) |
| 72 | + .andExpect(status().isForbidden()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void cannotUpdateMatchmakerQueueMapPoolMaxRatingWithoutScopeAndRole() throws Exception { |
| 77 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 78 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)) |
| 79 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 80 | + .content(genUpdateRequestForField("maxRating", 1000))) |
| 81 | + .andExpect(status().isForbidden()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void cannotUpdateMatchmakerQueueMapPoolVetoTokensPerPlayerWithoutScopeAndRole() throws Exception { |
| 86 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 87 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)) |
| 88 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 89 | + .content(genUpdateRequestForField("vetoTokensPerPlayer", 1))) |
| 90 | + .andExpect(status().isForbidden()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void cannotUpdateMatchmakerQueueMapPoolMaxTokensPerMapWithoutScopeAndRole() throws Exception { |
| 95 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 96 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)) |
| 97 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 98 | + .content(genUpdateRequestForField("maxTokensPerMap", 2))) |
| 99 | + .andExpect(status().isForbidden()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void cannotUpdateMatchmakerQueueMapPoolMinimumMapsAfterVetoWithoutScopeAndRole() throws Exception { |
| 104 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 105 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, NO_AUTHORITIES)) |
| 106 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 107 | + .content(genUpdateRequestForField("minimumMapsAfterVeto", 3.1))) |
| 108 | + .andExpect(status().isForbidden()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void canUpdateMatchmakerQueueMapPoolMinRatingWithScopeAndRole() throws Exception { |
| 113 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 114 | + .with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)) |
| 115 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 116 | + .content(genUpdateRequestForField("minRating", 1000))) |
| 117 | + .andExpect(status().isNoContent()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void canUpdateMatchmakerQueueMapPoolMaxRatingWithScopeAndRole() throws Exception { |
| 122 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 123 | + .with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)) |
| 124 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 125 | + .content(genUpdateRequestForField("maxRating", 1000))) |
| 126 | + .andExpect(status().isNoContent()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void canUpdateMatchmakerQueueMapPoolVetoTokensPerPlayerWithScopeAndRole() throws Exception { |
| 131 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 132 | + .with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)) |
| 133 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 134 | + .content(genUpdateRequestForField("vetoTokensPerPlayer", 1))) |
| 135 | + .andExpect(status().isNoContent()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void canUpdateMatchmakerQueueMapPoolMaxTokensPerMapWithScopeAndRole() throws Exception { |
| 140 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 141 | + .with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)) |
| 142 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 143 | + .content(genUpdateRequestForField("maxTokensPerMap", 2))) |
| 144 | + .andExpect(status().isNoContent()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void canUpdateMatchmakerQueueMapPoolMinimumMapsAfterVetoWithScopeAndRole() throws Exception { |
| 149 | + mockMvc.perform(patch("/data/matchmakerQueueMapPool/101") |
| 150 | + .with(getOAuthTokenWithActiveUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)) |
| 151 | + .header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE) |
| 152 | + .content(genUpdateRequestForField("minimumMapsAfterVeto", 3.1))) |
| 153 | + .andExpect(status().isNoContent()); |
| 154 | + } |
| 155 | +} |
0 commit comments