UserInfoTest.java 3.13 KB
package com.xkl;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static com.xkl.TokenTest.loginAndGetToken;

/**
 * Created by win7 on 2016/12/25.
 * 用户注册、修改密码、获取个人信息
 */
public class UserInfoTest {
    private static final String URL_USER = Constants.URL_PREFIX + "/userInfo";

    /**
     * 用户注册
     */
    @Test
    public void testRegister(){
       register("15211112222","pass152");
    }

    /**
     * 用户修改密码
     */
    @Test
    public void testModPass(){
        register("15211112222","pass152");

        Map<String,String> params = new HashMap<String, String>();
        params.put("password",EncodeTools.encode("MD5","pass152_new"));
        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        //获取token
        String token = loginAndGetToken("15211112222","pass152");
        System.out.println(token);
        String response = HttpTools.requestByMapWithToken(URL_USER,"PUT",params,token);
        System.out.println(response);
    }

    /**
     * 个人信息查询接口
     */
    @Test
    public void testGetUserInfo(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        //获取token
        String token = loginAndGetToken("user1","pass1");
        System.out.println(token);
        String response = HttpTools.requestByMapWithToken(URL_USER,"GET",params,token);
        System.out.println(response);
    }

    public void register(String user,String pass){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username",user);
        params.put("password",EncodeTools.encode("MD5",pass));
        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_USER,"POST",params);
        System.out.println(response);
    }

    /**
     * 错误的注册:不带sign
     */
    @Test
    public void testRegisterNoSign(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username","15211112222");
        params.put("password",EncodeTools.encode("MD5","pass152"));
        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign","nosign");
        String response = HttpTools.requestByMap(URL_USER,"POST",params);
        System.out.println(response);
    }

    /**
     * 错误注册:客户端与服务端时间差距过大
     */
    @Test
    public void testRegisterWrongTime(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username","15211112222");
        params.put("password",EncodeTools.encode("MD5","pass152"));
        params.put("t",HttpTools.getNow()+100000);
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_USER,"POST",params);
        System.out.println(response);
    }
}