Browse Source

添加登录单元测试

wangmh 7 years ago
parent
commit
4bfcfd9882

+ 9 - 0
sso-server/src/main/java/com/uas/sso/controller/LoginController.java

@@ -207,6 +207,7 @@ public class LoginController extends BaseController {
      * @return
      */
     @RequestMapping(value = "/proxy", method = RequestMethod.GET)
+    @Deprecated
     public ModelAndView loginProxyByToken() {
         WafRequestWrapper wr = new WafRequestWrapper(request);
         String returnUrl = wr.getParameter("returnURL");
@@ -246,6 +247,10 @@ public class LoginController extends BaseController {
                 app = StringUtils.isEmpty(app.getUserControl()) ? app : appService.findOne(app.getUserControl());
             }
 
+            if (StringUtils.isEmpty(returnUrl)) {
+                returnUrl = HOME_PAGE;
+            }
+
             if (app == null) {
                 app = appService.findOne(AccountConfig.ACCOUNT_CENTER);
             }
@@ -672,6 +677,10 @@ public class LoginController extends BaseController {
             request.getSession().setAttribute("baseUrl", baseUrl);
         }
 
+        if (StringUtils.isEmpty(returnUrl)) {
+            returnUrl = HOME_PAGE;
+        }
+
         // 校验token
         Assert.hasText(token, "请先获取验证码");
         Token existToken = tokenService.findOne(token);

+ 1 - 1
sso-server/src/test/java/com/uas/sso/service/ApplicationContextRegister.java → sso-server/src/test/java/com/uas/sso/ApplicationContextRegister.java

@@ -1,4 +1,4 @@
-package com.uas.sso.service;
+package com.uas.sso;
 
 import com.uas.sso.util.ContextUtils;
 import org.apache.log4j.Logger;

+ 6 - 15
sso-server/src/test/java/com/uas/sso/service/AppServiceTest.java → sso-server/src/test/java/com/uas/sso/BaseTest.java

@@ -1,28 +1,19 @@
-package com.uas.sso.service;
+package com.uas.sso;
 
+import com.uas.sso.ApplicationContextRegister;
 import com.uas.sso.SsoApplication;
-import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.context.annotation.Import;
-import org.springframework.context.annotation.ImportResource;
+import org.springframework.context.annotation.Profile;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
-import static org.junit.Assert.*;
-
 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest(classes = SsoApplication.class)
-//@ImportResource("classpath:spring/*.xml")
 @Import(ApplicationContextRegister.class)
-public class AppServiceTest {
-
-    @Autowired
-    AppService appService;
-
-    @Test
-    public void sample() {
-     //   System.out.println(1);
-    }
+@Profile("test")
+public class BaseTest {
 }

+ 32 - 0
sso-server/src/test/java/com/uas/sso/controller/BaseControllerTest.java

@@ -0,0 +1,32 @@
+package com.uas.sso.controller;
+
+import com.uas.sso.BaseTest;
+import org.junit.Before;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+/**
+ * @author wangmh
+ * @create 2018-08-09 16:11
+ * @desc
+ **/
+@AutoConfigureMockMvc
+@WebAppConfiguration
+public class BaseControllerTest extends BaseTest {
+
+    protected MockMvc mvc;
+
+    @Autowired
+    protected WebApplicationContext wac;
+
+
+    @Before()  //这个方法在每个方法执行之前都会执行一遍
+    public void setup() {
+        mvc = MockMvcBuilders.webAppContextSetup(wac).build();  //初始化MockMvc对象
+    }
+
+}

+ 139 - 0
sso-server/src/test/java/com/uas/sso/controller/LoginControllerTest.java

@@ -0,0 +1,139 @@
+package com.uas.sso.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.uas.sso.entity.Token;
+import com.uas.sso.service.TokenService;
+import com.uas.sso.util.StringUtil;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.ui.ModelMap;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * @author uas
+ * @date 2018/8/9.
+ */
+public class LoginControllerTest extends BaseControllerTest {
+
+    private final String PRE_URI = "/sso/login";
+
+    @Autowired
+    private TokenService tokenService;
+
+    @Test
+    public void login() throws Exception {
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+        params.add("username", "17770035301");
+        params.add("password", "a12345678");
+        params.add("spaceUU", "1000001");
+        MockHttpServletResponse response = mvc.perform(post(PRE_URI).params(params))
+                .andExpect(status().isOk())
+                .andDo(print())
+                .andReturn().getResponse();
+        checkLoginResponse(response);
+    }
+
+    private void checkLoginResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
+        String result = response.getContentAsString();
+        JSONObject jsonObject =  JSON.parseObject(result);
+        Assert.assertTrue((Boolean) jsonObject.get("success"));
+        Assert.assertNotNull("content is null", jsonObject.getJSONObject("content"));
+        Assert.assertNotNull("data is null", jsonObject.getJSONObject("content").getJSONObject("data"));
+        Assert.assertNotNull("loginUrls is null", jsonObject.getJSONObject("content").getJSONArray("loginUrls"));
+        Assert.assertNotNull("returnUrl is null", jsonObject.getJSONObject("content").getString("returnUrl"));
+        String uid = getCookieValue(response, "uid");
+        Assert.assertNotNull("cookie uid is null", uid);
+    }
+
+    private String getCookieValue(MockHttpServletResponse response, String k) {
+        List<String> list = response.getHeaders("Set-Cookie");
+        Assert.assertNotNull("header Set-Cookie is null", list);
+        for (String str : list) {
+            String[] splits = str.split(";");
+            for (String split : splits) {
+                String key = split.split("=")[0];
+                if (k.equals(key)) {
+                    return split.split("=")[1];
+                }
+            }
+        }
+        return null;
+    }
+
+    @Test
+    public void loginProxyByToken() throws Exception {
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+        ModelMap data = new ModelMap("userUU", 1000010030L).addAttribute("spaceUU", 1000001L);
+        Token token = new Token(data);
+        tokenService.save(token);
+        params.add("token", token.getId());
+        params.add("appId", "mall");
+        MockHttpServletResponse response = mvc.perform(post(PRE_URI + "/proxy").params(params))
+                .andExpect(status().isOk())
+                .andDo(print())
+                .andReturn().getResponse();
+        checkLoginResponse(response);
+    }
+
+    @Test
+    public void loginProxyByToken1() throws Exception {
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+        ModelMap data = new ModelMap("userUU", 1000010030L);
+        Token token = new Token(data);
+        tokenService.save(token);
+        params.add("token", token.getId());
+        params.add("spaceUU", "1000001");
+        params.add("appId", "mall");
+        params.add("returnUrl", "http://10.1.51.50:8081");
+        MockHttpServletResponse response = mvc.perform(post(PRE_URI + "/mobile/proxy").params(params))
+                .andExpect(status().isOk())
+                .andDo(print())
+                .andReturn().getResponse();
+        checkLoginResponse(response);
+    }
+
+    @Test
+    public void getAllAccount() throws Exception {
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+        params.add("mobile", "17770035301");
+        params.add("password", "a12345678");
+        MockHttpServletResponse response = mvc.perform(post(PRE_URI + "/mobile").params(params))
+                .andExpect(status().isOk())
+                .andDo(print())
+                .andReturn().getResponse();
+        System.out.println(response.getContentAsString());
+        String result = response.getContentAsString();
+//        JSONObject jsonObject =  JSON.parseObject(result);
+//        Assert.assertTrue((Boolean) jsonObject.get("success"));
+    }
+
+    @Test
+    public void loginBySms() throws Exception {
+        String code = StringUtil.getRandomNumber(6);
+        Token token = new Token(code, 10 * 60);
+        tokenService.save(token);
+        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+        params.add("code", code);
+        params.add("token", token.getId());
+        params.add("mobile", "17770035301");
+        params.add("spaceUU", "1000001");
+        params.add("spaceUU", "1000001");
+        MockHttpServletResponse response = mvc.perform(post(PRE_URI + "/sms").params(params))
+                .andExpect(status().isOk())
+                .andDo(print())
+                .andReturn().getResponse();
+        checkLoginResponse(response);
+    }
+}

+ 17 - 0
sso-server/src/test/java/com/uas/sso/controller/UserManagerControllerTest.java

@@ -0,0 +1,17 @@
+package com.uas.sso.controller;
+
+import com.uas.sso.BaseTest;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author uas
+ * @date 2018/8/9.
+ */
+public class UserManagerControllerTest extends BaseTest {
+
+    @Test
+    public void getToken() {
+    }
+}