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

import com.xkl.Constants;
import com.xkl.EncodeTools;
import com.xkl.HttpTools;
import com.xkl.RtnCodeTools;
import org.json.JSONObject;
import org.junit.Assert;
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() {
        String response = loginAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD);
        Assert.assertTrue(response.contains("_"));
        System.out.println(response);
    }

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

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