From 964c63d360acde02db0b3b256681453984a20177 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Fri, 8 May 2020 14:44:21 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=B0=86=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E7=94=B1=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=94=B9=E4=B8=BA=E5=8C=85=E8=A3=85=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E6=B5=8B=E8=AF=95=E5=AD=97=E6=AE=B5=E4=B8=BA?= =?UTF-8?q?null=E6=83=85=E5=86=B5=202.=20=E5=8D=8F=E8=AE=AE=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=AA=8C=E8=AF=81=E5=85=B3=E9=94=AE=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E4=B8=BA=E7=A9=BA=203.=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=AE=A4=E8=AF=81=E5=86=92=E7=83=9F=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dispose/pojo/dto/ProtocolDTO.java | 4 +- .../com/dispose/pojo/dto/ProtocolReqDTO.java | 5 + .../AuthControllerExceptionSmokeTest.java | 294 +++++++++++++++++- 3 files changed, 286 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/dispose/pojo/dto/ProtocolDTO.java b/src/main/java/com/dispose/pojo/dto/ProtocolDTO.java index c804da5c..bdee5d64 100644 --- a/src/main/java/com/dispose/pojo/dto/ProtocolDTO.java +++ b/src/main/java/com/dispose/pojo/dto/ProtocolDTO.java @@ -22,7 +22,7 @@ public abstract class ProtocolDTO { * The Ver. */ @ApiModelProperty(value = "协议版本号", required = true, example = "1") - private int ver; + private Integer ver; /** * The Crypto type. @@ -33,7 +33,7 @@ public abstract class ProtocolDTO { "2:采用AES加密后的base64编码格式\n", required = true, allowableValues = "0, 1, 2", example = "0") - private int cryptoType; + private Integer cryptoType; /** * The Time stamp. diff --git a/src/main/java/com/dispose/pojo/dto/ProtocolReqDTO.java b/src/main/java/com/dispose/pojo/dto/ProtocolReqDTO.java index 9e7dbdf2..bc2d4ffd 100644 --- a/src/main/java/com/dispose/pojo/dto/ProtocolReqDTO.java +++ b/src/main/java/com/dispose/pojo/dto/ProtocolReqDTO.java @@ -89,6 +89,11 @@ public class ProtocolReqDTO extends ProtocolDTO { * @return the error code */ public ErrorCode verifyRequest() { + + if(this.getVer() == null || this.getCryptoType() == null || this.getTimeStamp() == null) { + return ErrorCode.ERR_PARAMEXCEPTION; + } + // 校验版本 if (this.getVer() < ConstValue.Protocol.VERSION) { return ErrorCode.ERR_VERSION; diff --git a/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java b/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java index a1e4b6f6..50f15c91 100644 --- a/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java +++ b/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java @@ -5,6 +5,7 @@ import com.dispose.common.ConstValue; import com.dispose.common.ErrorCode; import com.dispose.pojo.dto.ProtocolReqDTO; import com.dispose.pojo.dto.ProtocolRespDTO; +import com.dispose.pojo.po.ReturnStatus; import com.dispose.pojo.vo.auth.LoginReq; import com.dispose.pojo.vo.auth.LoginRsp; import com.fasterxml.jackson.core.JsonProcessingException; @@ -29,6 +30,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +/** + * The type Auth controller exception smoke test. + */ @AutoConfigureMockMvc @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -47,8 +51,13 @@ public class AuthControllerExceptionSmokeTest extends InitTestEnvironment { @Resource private ObjectMapper objectMapper; + /** + * A 1 normal auth test. + * + * @throws Exception the exception + */ @Test - public void t1_NormalAuthTest() throws Exception { + public void a1_normalAuthTest() throws Exception { String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + "\"msgContent\":\"{\\\"password\\\"" + ":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + @@ -64,26 +73,31 @@ public class AuthControllerExceptionSmokeTest extends InitTestEnvironment { .getResponse() .getContentAsString(); - LoginRsp logRsp = objectMapper.readValue(verifyResp(ret), LoginRsp.class) ; + LoginRsp rspInfo = objectMapper.readValue(verifyResp(ret), LoginRsp.class) ; - Assert.assertNotNull(logRsp); - Assert.assertNotNull(logRsp.getUserName()); - Assert.assertNotNull(logRsp.getLogTime()); - Assert.assertNotNull(logRsp.getToken()); - Assert.assertNotNull(logRsp.getExpireTime()); - Assert.assertNotNull(logRsp.getMessage()); - Assert.assertNotNull(logRsp.getStatus()); + Assert.assertNotNull(rspInfo); + Assert.assertNotNull(rspInfo.getUserName()); + Assert.assertNotNull(rspInfo.getLogTime()); + Assert.assertNotNull(rspInfo.getToken()); + Assert.assertNotNull(rspInfo.getExpireTime()); + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); - Assert.assertEquals(Long.valueOf(logRsp.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); } + /** + * A 2 ver json exception test. + * + * @throws Exception the exception + */ @Test - public void t2_VerExceptionTest() throws Exception { + public void a2_verJsonExceptionTest() throws Exception { String reqData = "{\"ver\":\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":" + "\"{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + "\\\"userName\\\":\\\"admin\\\"}\"}"; - mockMvc.perform(MockMvcRequestBuilders + String ret = mockMvc.perform(MockMvcRequestBuilders .post("/auth/login") .contentType(MediaType.APPLICATION_JSON) .content(reqData)) @@ -93,18 +107,268 @@ public class AuthControllerExceptionSmokeTest extends InitTestEnvironment { .getResponse() .getContentAsString(); - reqData = "{\"ver\":null,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"" + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + /** + * A 3 ver null exception test. + * + * @throws Exception the exception + */ + @Test + public void a3_verNullExceptionTest() throws Exception { + String reqData = "{\"ver\":null,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"" + "{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + "\\\"userName\\\":\\\"admin\\\"}\"}"; - mockMvc.perform(MockMvcRequestBuilders + String ret = mockMvc.perform(MockMvcRequestBuilders .post("/auth/login") .contentType(MediaType.APPLICATION_JSON) .content(reqData)) .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(ErrorCode.ERR_VERSION.getHttpCode())) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode())) .andReturn() .getResponse() .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + /** + * A 4 crypto json exception test. + * + * @throws Exception the exception + */ + @Test + public void a4_cryptoJsonExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":\"timeStamp\":1587604296988,\"msgContent\":\"" + + "{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + + "\\\"userName\\\":\\\"admin\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + /** + * A 5 crypto null exception test. + * + * @throws Exception the exception + */ + @Test + public void a5_cryptoNullExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":null,\"timeStamp\":1587604296988,\"msgContent\":\"" + + "{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + + "\\\"userName\\\":\\\"admin\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + /** + * A 6 username not exist exception test. + * + * @throws Exception the exception + */ + @Test + public void a6_usernameNotExistExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"" + + "{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + + "\\\"userName\\\":\\\"123456\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_USERNOTFOUND.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_USERNOTFOUND.getCode())); + } + + /** + * A 7 username empty exception test. + * + * @throws Exception the exception + */ + @Test + public void a7_usernameEmptyExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"" + + "{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + + "\\\"userName\\\":\\\"\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_USERNOTFOUND.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_USERNOTFOUND.getCode())); + } + + /** + * A 8 username null exception test. + * + * @throws Exception the exception + */ + @Test + public void a8_usernameNullExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"" + + "{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + + "\\\"userName\\\":null}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_USERNOTFOUND.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_USERNOTFOUND.getCode())); + } + + /** + * A 9 password empty exception test. + * + * @throws Exception the exception + */ + @Test + public void a9_passwordEmptyExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"password\\\":\\\"\\\",\\\"userName\\\":\\\"admin\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PASSWORD.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PASSWORD.getCode())); + } + + /** + * B 1 password null exception test. + * + * @throws Exception the exception + */ + @Test + public void b1_passwordNullExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"password\\\":null,\\\"userName\\\":\\\"admin\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PASSWORD.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PASSWORD.getCode())); + } + + /** + * B 2 password error exception test. + * + * @throws Exception the exception + */ + @Test + public void b2_passwordErrorExceptionTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"password\\\":\\\"123456\\\",\\\"userName\\\":\\\"admin\\\"}\"}"; + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PASSWORD.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + ReturnStatus rspInfo = objectMapper.readValue(verifyResp(ret), ReturnStatus.class) ; + + Assert.assertNotNull(rspInfo.getMessage()); + Assert.assertNotNull(rspInfo.getStatus()); + + Assert.assertEquals(Long.valueOf(rspInfo.getStatus()), Long.valueOf(ErrorCode.ERR_PASSWORD.getCode())); } }