diff --git a/config/application-test.properties b/config/application-test.properties index 92845458..08317543 100644 --- a/config/application-test.properties +++ b/config/application-test.properties @@ -52,6 +52,6 @@ phoenix.response-enc-switch=false phoenix.aes-key=Wt4EJu6Rrq5udd/42bNpCQ== #====custom config,begin with phoenix==== #调试配置 -dispose.check-protocol-timeout=true +dispose.check-protocol-timeout=false dispose.check-request-token=true dispose.check-admin-permission=true diff --git a/src/main/java/com/dispose/controller/AuthController.java b/src/main/java/com/dispose/controller/AuthController.java index 004c552d..b85ea861 100644 --- a/src/main/java/com/dispose/controller/AuthController.java +++ b/src/main/java/com/dispose/controller/AuthController.java @@ -76,6 +76,9 @@ public class AuthController { .expireTime(System.currentTimeMillis() + ConstValue.GlobalConfigure.TOKEN_EXPIRED_TIME_MS) .build(); + rspInfo.setStatus(ErrorCode.ERR_OK.getCode()); + rspInfo.setMessage(ErrorCode.ERR_OK.getMsg()); + return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } diff --git a/src/test/java/com/dispose/Global/InitTestEnvironment.java b/src/test/java/com/dispose/Global/InitTestEnvironment.java index d3950f56..00a7cd4b 100644 --- a/src/test/java/com/dispose/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/Global/InitTestEnvironment.java @@ -3,8 +3,10 @@ package com.dispose.Global; import com.dispose.common.ConstValue; import com.dispose.common.ErrorCode; import com.dispose.common.GlobalVar; +import com.dispose.pojo.dto.ProtocolRespDTO; import com.dispose.pojo.po.MReturnType; import com.dispose.service.UserAccountService; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.json.JSONException; @@ -82,8 +84,44 @@ public class InitTestEnvironment { return logToken; } + /** + * Verify resp string. + * + * @param resp the resp + * @return the string + */ + public String verifyResp(String resp) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + ProtocolRespDTO rspInfo = objectMapper.readValue(resp, ProtocolRespDTO.class); + + // 校验版本 + if (rspInfo.getVer() < ConstValue.Protocol.VERSION) { + Assert.fail(); + } + + // 校验时间错 + if (GlobalVar.IS_CHECK_REQUEST_TIMEOUT + && Math.abs(System.currentTimeMillis() - rspInfo.getTimeStamp()) >= ConstValue.Protocol.REQUEST_TIMEOUT_MS) { + Assert.fail(); + } + + return rspInfo.getMsgContent(); + } catch (Exception ex) { + Assert.fail(); + } + + Assert.fail(); + return ""; + } + /** * Check response body. + * + * @param data the data + * @param reqTime the req time + * @return the string + * @throws JSONException the json exception */ public String verifyRep(String data, Long reqTime) throws JSONException { Long verValue = 2L; diff --git a/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java b/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java index e9dac582..a1e4b6f6 100644 --- a/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java +++ b/src/test/java/com/dispose/controller/AuthControllerExceptionSmokeTest.java @@ -4,10 +4,13 @@ import com.dispose.Global.InitTestEnvironment; 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.vo.auth.LoginReq; +import com.dispose.pojo.vo.auth.LoginRsp; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; @@ -45,7 +48,37 @@ public class AuthControllerExceptionSmokeTest extends InitTestEnvironment { private ObjectMapper objectMapper; @Test - public void t1_VerExceptionTest() throws Exception { + public void t1_NormalAuthTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"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_OK.getHttpCode())) + .andReturn() + .getResponse() + .getContentAsString(); + + LoginRsp logRsp = 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.assertEquals(Long.valueOf(logRsp.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + } + + @Test + public void t2_VerExceptionTest() throws Exception { String reqData = "{\"ver\":\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":" + "\"{\\\"password\\\":\\\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\\\"," + "\\\"userName\\\":\\\"admin\\\"}\"}"; @@ -53,7 +86,7 @@ public class AuthControllerExceptionSmokeTest extends InitTestEnvironment { mockMvc.perform(MockMvcRequestBuilders .post("/auth/login") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(reqData))) + .content(reqData)) .andDo(print()).andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode())) .andReturn() @@ -67,9 +100,9 @@ public class AuthControllerExceptionSmokeTest extends InitTestEnvironment { mockMvc.perform(MockMvcRequestBuilders .post("/auth/login") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(reqData))) + .content(reqData)) .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode())) + .andExpect(jsonPath("$.code").value(ErrorCode.ERR_VERSION.getHttpCode())) .andReturn() .getResponse() .getContentAsString();