AccountTest.java 4.43 KB
package com.xkl.uspih;

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;

/**
 * USPIH login and logout
 * zhaoyue
 * 2017-1-14
 */

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

    /**
     * @param user user account
     * @param pass user password
     * @return
     */
    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)); // encode password with MD5 algorithm
        params.put("t", HttpTools.getNow());
        String response = HttpTools.requestByMap(URL_LOGIN, "POST", params);
        try {
            System.out.println("login success:" + response);
            JSONObject jsonObject = new JSONObject(response);
            JSONObject content = jsonObject.getJSONObject("content");
            return content.getString("userId") + "_" + content.getString("token");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * @param user   user account
     * @param pass   user password
     * @param newpwd new user password
     * @return
     */
    public static String loginModPwdAndGetToken(String user, String pass, String newpwd) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("account", user);
        params.put("password", EncodeTools.encode("MD5", pass)); // encode password with MD5 algorithm
        params.put("newpwd", EncodeTools.encode("MD5", newpwd)); // encode password with MD5 algorithm
        params.put("t", HttpTools.getNow());
        String response = HttpTools.requestByMap(URL_LOGIN, "PUT", params);
        try {
            System.out.println("login success:" + response);
            JSONObject jsonObject = new JSONObject(response);
            JSONObject content = jsonObject.getJSONObject("content");
            return content.getString("userId") + "_" + content.getString("token");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * login success test
     */
    @Test
    public void testLoginSuccess() {
        String response = loginAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD);
        Assert.assertTrue(response.contains("_"));
        System.out.println(response);
    }

    /**
     * login fail test
     */
    @Test
    public void testLoginFail() {
        String response = loginAndGetToken(Constants.ADMIN_ACCOUNT + "ppppp", Constants.ADMIN_PWD);
        Assert.assertTrue(response == null);
        System.out.println(response);
    }

    /**
     * login and modify password success test
     */
    @Test
    public void testLoginModPwdSuccess() {
        String response = loginModPwdAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD, Constants.ADMIN_PWD);
        Assert.assertTrue(response.contains("_"));
        System.out.println(response);
    }

    /**
     * login and modify password fail test
     */
    @Test
    public void testLoginModPwdFail() {
        String response = loginModPwdAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD + "pppppp", Constants.ADMIN_PWD);
        Assert.assertTrue(response == null);
        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);
        System.out.println("logout success:" + response);
        Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.CODE_SUCC));
    }

    @Test
    public void testLogoutFail() {
        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 + "PPPPPP");
        System.out.println("logout success:" + response);
        Assert.assertTrue(response == null);
    }
}