|
| 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.error.ErrorCode; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.test.context.jdbc.Sql; |
| 8 | +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
| 9 | +import org.springframework.test.web.servlet.MvcResult; |
| 10 | + |
| 11 | +import static org.hamcrest.Matchers.hasSize; |
| 12 | +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; |
| 13 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 14 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 15 | + |
| 16 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql") |
| 17 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql") |
| 18 | +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepModData.sql") |
| 19 | +class ElidePageSizeTest extends AbstractIntegrationTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void normalUserValidPageSize() throws Exception { |
| 23 | + mockMvc.perform( |
| 24 | + get("/data/mod") |
| 25 | + .queryParam("page[size]", "100") |
| 26 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_USER)) |
| 27 | + ) |
| 28 | + .andExpect(status().isOk()) |
| 29 | + .andExpect(jsonPath("$.data").isArray()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void normalUserInvalidPageSize() throws Exception { |
| 34 | + final MvcResult result = mockMvc.perform( |
| 35 | + get("/data/mod") |
| 36 | + .queryParam("page[limit]", "101") |
| 37 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_USER)) |
| 38 | + ) |
| 39 | + .andExpect(status().isUnprocessableEntity()).andReturn(); |
| 40 | + |
| 41 | + assertApiError(result, ErrorCode.QUERY_INVALID_PAGE_SIZE); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void scraperUserValidPageSize() throws Exception { |
| 46 | + mockMvc.perform( |
| 47 | + get("/data/mod") |
| 48 | + .queryParam("page[size]", "9000") |
| 49 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_SCRAPER)) |
| 50 | + ) |
| 51 | + .andExpect(status().isOk()) |
| 52 | + .andExpect(jsonPath("$.data").isArray()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void scraperUserInvalidPageSize() throws Exception { |
| 57 | + final MvcResult result = mockMvc.perform( |
| 58 | + get("/data/mod") |
| 59 | + .queryParam("page[limit]", "10001") |
| 60 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_SCRAPER)) |
| 61 | + ) |
| 62 | + .andExpect(status().isUnprocessableEntity()).andReturn(); |
| 63 | + |
| 64 | + assertApiError(result, ErrorCode.QUERY_INVALID_PAGE_SIZE); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void nanPageSize() throws Exception { |
| 69 | + mockMvc.perform( |
| 70 | + get("/data/mod") |
| 71 | + .queryParam("page[limit]", "invalid-not-a-number") |
| 72 | + .with(getOAuthTokenWithActiveUser(NO_SCOPE, GroupPermission.ROLE_SCRAPER)) |
| 73 | + ) |
| 74 | + .andExpect(status().isBadRequest()) |
| 75 | + .andExpect(jsonPath("$.errors[*]", hasSize(1))); |
| 76 | + } |
| 77 | +} |
0 commit comments