|
| 1 | +package ai.elimu.web; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import org.junit.Before; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | +import org.springframework.test.web.servlet.MvcResult; |
| 15 | +import org.springframework.test.web.servlet.RequestBuilder; |
| 16 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 17 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 18 | + |
| 19 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 20 | +@ContextConfiguration(locations={ |
| 21 | + "file:src/main/webapp/WEB-INF/spring/applicationContext.xml", |
| 22 | + "file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml" |
| 23 | +}) |
| 24 | +public class SignOnControllerWeb3Test { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private SignOnControllerWeb3 signOnControllerWeb3; |
| 28 | + |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() { |
| 33 | + assertNotNull(signOnControllerWeb3); |
| 34 | + mockMvc = MockMvcBuilders.standaloneSetup(signOnControllerWeb3).build(); |
| 35 | + assertNotNull(mockMvc); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testHandleGetRequest() throws Exception { |
| 40 | + RequestBuilder requestBuilder = MockMvcRequestBuilders |
| 41 | + .get("/sign-on/web3"); |
| 42 | + MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); |
| 43 | + assertEquals(HttpStatus.OK.value(), mvcResult.getResponse().getStatus()); |
| 44 | + assertEquals("sign-on-web3", mvcResult.getModelAndView().getViewName()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testHandleAuthorization_missingParameters() throws Exception { |
| 49 | + RequestBuilder requestBuilder = MockMvcRequestBuilders |
| 50 | + .post("/sign-on/web3") |
| 51 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); |
| 52 | + MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); |
| 53 | + assertEquals(HttpStatus.BAD_REQUEST.value(), mvcResult.getResponse().getStatus()); |
| 54 | + assertNull(mvcResult.getModelAndView()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testHandleAuthorization_emptyParameters() throws Exception { |
| 59 | + RequestBuilder requestBuilder = MockMvcRequestBuilders |
| 60 | + .post("/sign-on/web3") |
| 61 | + .param("address", "") |
| 62 | + .param("signature", "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400") |
| 63 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); |
| 64 | + MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); |
| 65 | + assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus()); |
| 66 | + assertEquals("redirect:/sign-on/web3?error=Missing address", mvcResult.getModelAndView().getViewName()); |
| 67 | + |
| 68 | + requestBuilder = MockMvcRequestBuilders |
| 69 | + .post("/sign-on/web3") |
| 70 | + .param("address", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe") |
| 71 | + .param("signature", "") |
| 72 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); |
| 73 | + mvcResult = mockMvc.perform(requestBuilder).andReturn(); |
| 74 | + assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus()); |
| 75 | + assertEquals("redirect:/sign-on/web3?error=Missing signature", mvcResult.getModelAndView().getViewName()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testHandleAuthorization_invalidSignature() throws Exception { |
| 80 | + RequestBuilder requestBuilder = MockMvcRequestBuilders |
| 81 | + .post("/sign-on/web3") |
| 82 | + .param("address", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe") |
| 83 | + .param("signature", "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400") |
| 84 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); |
| 85 | + MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); |
| 86 | + assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus()); |
| 87 | + assertEquals("redirect:/sign-on/web3?error=Invalid signature", mvcResult.getModelAndView().getViewName()); |
| 88 | + } |
| 89 | +} |
0 commit comments