|
|
@@ -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);
|
|
|
+ }
|
|
|
+}
|