-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Validate sort direction and remove unsupported options [TECH-1…
…622]
- Loading branch information
1 parent
20bb935
commit f90e199
Showing
7 changed files
with
143 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2004-2022, University of Oslo | ||
* Copyright (c) 2004-2023, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
|
@@ -25,20 +25,14 @@ | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.webapi.security.config; | ||
package org.hisp.dhis.webapi.common; | ||
|
||
import java.util.List; | ||
import java.beans.PropertyEditorSupport; | ||
import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria; | ||
import org.springframework.core.convert.converter.Converter; | ||
|
||
/** | ||
* String to Order Criterias converter for MVC requests | ||
* | ||
* @author Giuseppe Nespolino <[email protected]> | ||
*/ | ||
class StringToOrderCriteriaListConverter implements Converter<String, List<OrderCriteria>> { | ||
public class OrderCriteriaParamEditor extends PropertyEditorSupport { | ||
@Override | ||
public List<OrderCriteria> convert(String source) { | ||
return OrderCriteria.fromOrderString(source); | ||
public void setAsText(String source) { | ||
setValue(OrderCriteria.toOrderCriteria(source)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/OrderBindingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2004-2022, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.webapi.controller; | ||
|
||
import static org.hamcrest.core.StringContains.containsString; | ||
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
|
||
import java.util.List; | ||
import org.hisp.dhis.dxf2.webmessage.WebMessage; | ||
import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
class OrderBindingTest { | ||
private static final String ENDPOINT = "/ordering"; | ||
|
||
private MockMvc mockMvc; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
mockMvc = | ||
MockMvcBuilders.standaloneSetup(new OrderingController()) | ||
.setControllerAdvice(new CrudControllerAdvice()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void shouldReturnDefaultSortDirectionWhenNoSortDirectionIsPassedAsParameter() throws Exception { | ||
mockMvc | ||
.perform(get(ENDPOINT).param("order", "field")) | ||
.andExpect(content().string(containsString("OK"))) | ||
.andExpect(content().string(containsString("field"))) | ||
.andExpect(content().string(containsString("ASC"))); | ||
} | ||
|
||
@Test | ||
void shouldReturnAscSortDirectionWhenAscSortDirectionIsPassedAsParameter() throws Exception { | ||
mockMvc | ||
.perform(get(ENDPOINT).param("order", "field:asc")) | ||
.andExpect(content().string(containsString("OK"))) | ||
.andExpect(content().string(containsString("field"))) | ||
.andExpect(content().string(containsString("ASC"))); | ||
} | ||
|
||
@Test | ||
void shouldReturnDescSortDirectionWhenDescSortDirectionIsPassedAsParameter() throws Exception { | ||
mockMvc | ||
.perform(get(ENDPOINT).param("order", "field:desc")) | ||
.andExpect(content().string(containsString("OK"))) | ||
.andExpect(content().string(containsString("field"))) | ||
.andExpect(content().string(containsString("DESC"))); | ||
} | ||
|
||
@Test | ||
void shouldReturnABadRequestWhenInvalidSortDirectionIsPassedAsParameter() throws Exception { | ||
mockMvc | ||
.perform(get(ENDPOINT).param("order", "field:wrong")) | ||
.andExpect(content().string(containsString("Bad Request"))) | ||
.andExpect( | ||
content() | ||
.string( | ||
containsString( | ||
"'wrong' is not a valid sort direction. Valid values are: [ASC, DESC]"))); | ||
} | ||
|
||
@Test | ||
void shouldReturnABadRequestWhenInvalidSortDirectionIsPassedAsParameterInAListOfOrders() | ||
throws Exception { | ||
mockMvc | ||
.perform(get(ENDPOINT).param("order", "field1:wrong").param("order", "field2:asc")) | ||
.andExpect(content().string(containsString("Bad Request"))) | ||
.andExpect( | ||
content() | ||
.string( | ||
containsString( | ||
"'wrong' is not a valid sort direction. Valid values are: [ASC, DESC]"))); | ||
} | ||
|
||
@Controller | ||
private static class OrderingController extends CrudControllerAdvice { | ||
@GetMapping(value = ENDPOINT) | ||
public @ResponseBody WebMessage getOrder(@RequestParam List<OrderCriteria> order) { | ||
return ok(order.toString()); | ||
} | ||
} | ||
} |