AccountTest.java 1.97 KB
package com.xkl.upsoft;

import com.xkl.Constants;
import com.xkl.EncodeTools;
import com.xkl.HttpTools;
import org.json.JSONObject;
import org.junit.Test;

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

/**
 * 上传软件登录、退出、修改密码测试。
 * zhaoyue
 * 2017-1-14
 */

public class AccountTest {
    private static final String URL_LOGIN = Constants.URL_PREFIX + "/upsoft/account";

    public static String loginAndGetToken(String user, String pass) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("account", user);
        params.put("password", EncodeTools.encode("MD5", pass));
        params.put("t", HttpTools.getNow());
        String response = HttpTools.requestByMap(URL_LOGIN, "POST", params);
        System.out.println("login success:" + response);
        JSONObject jsonObject = new JSONObject(response);
        JSONObject content = jsonObject.getJSONObject("content");
        return content.getString("userId") + "_" + content.getString("token");
    }

    @Test
    public void testLogin() {
        System.out.println(loginAndGetToken(Data.ADMIN_ACCOUNT, Data.ADMIN_PWD));
    }

    @Test
    public void testLogout() {
        String token = loginAndGetToken(Data.ADMIN_ACCOUNT, Data.ADMIN_PWD);
        Map<String, String> params = new HashMap<String, String>();
        params.put("t", HttpTools.getNow());
        String response = HttpTools.requestByMapWithToken(URL_LOGIN, "DELETE", params, token);
        System.out.println("logout success:" + response);
    }

    @Test
    public void testModPwd() {
        String token = loginAndGetToken(Data.ADMIN_ACCOUNT, Data.ADMIN_PWD);
        Map<String, String> params = new HashMap<String, String>();
        String newPassWord = "pass1";
        params.put("newpwd", EncodeTools.encode("MD5", newPassWord));
        String response = HttpTools.requestByMapWithToken(URL_LOGIN, "PUT", params, token);
        System.out.println("modify password success:" + response);
    }
}