Skip to content

Commit

Permalink
test: Add MockMvc
Browse files Browse the repository at this point in the history
  • Loading branch information
nya-elimu committed Jul 14, 2022
1 parent fe8c4fa commit 439468c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To run the application in debug mode, replace `mvn` with `mvnDebug` in the comma

```
mvn test
open target/site/jacoco/index.html
open target/site/jacoco/index.html
```


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ai/elimu/web/SignOnControllerWeb3.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SignOnControllerWeb3 {
@Autowired
private ContributorDao contributorDao;

@RequestMapping("/sign-on/web3")
@RequestMapping(value="/sign-on/web3", method=RequestMethod.GET)
public String handleGetRequest(HttpServletRequest request) throws IOException {
logger.info("handleGetRequest");

Expand Down
84 changes: 84 additions & 0 deletions src/test/java/ai/elimu/web/SignOnControllerWeb3Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ai.elimu.web;

import static org.junit.Assert.*;
import org.junit.Before;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml"
})
public class SignOnControllerWeb3Test {

@Autowired
private SignOnControllerWeb3 signOnControllerWeb3;

private MockMvc mockMvc;

@Before
public void setup() {
assertNotNull(signOnControllerWeb3);
mockMvc = MockMvcBuilders.standaloneSetup(signOnControllerWeb3).build();
assertNotNull(mockMvc);
}

@Test
public void testHandleGetRequest() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.get("/sign-on/web3");
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(HttpStatus.OK.value(), mvcResult.getResponse().getStatus());
}

@Test
public void testHandleAuthorization_missingParameters() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/sign-on/web3")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(HttpStatus.BAD_REQUEST.value(), mvcResult.getResponse().getStatus());
}

@Test
public void testHandleAuthorization_emptyParameters() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/sign-on/web3")
.param("address", "")
.param("signature", "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus());

requestBuilder = MockMvcRequestBuilders
.post("/sign-on/web3")
.param("address", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe")
.param("signature", "")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus());
}

@Test
public void testHandleAuthorization_invalidSignature() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/sign-on/web3")
.param("address", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe")
.param("signature", "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus());
}
}

0 comments on commit 439468c

Please sign in to comment.