AccountTest.java
3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.xkl.uspih;
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;
/**
* 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);
System.out.println("login success:" + response);
JSONObject jsonObject = new JSONObject(response);
JSONObject content = jsonObject.getJSONObject("content");
return content.getString("userId") + "_" + content.getString("token");
}
/**
* @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);
System.out.println("login success:" + response);
JSONObject jsonObject = new JSONObject(response);
JSONObject content = jsonObject.getJSONObject("content");
return content.getString("userId") + "_" + content.getString("token");
}
/**
* login success test
*/
@Test
public void testLoginSuccess() {
System.out.println(loginAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD));
}
/**
* login fail test
*/
@Test
public void testLoginFail() {
System.out.println(loginAndGetToken(Constants.ADMIN_ACCOUNT + "ppppp", Constants.ADMIN_PWD));
}
/**
* login and modify password success test
*/
@Test
public void testLoginModPwdSuccess() {
System.out.println(loginModPwdAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD, Constants.ADMIN_PWD));
}
/**
* login and modify password fail test
*/
@Test
public void testLoginModPwdFail() {
System.out.println(loginModPwdAndGetToken(Constants.ADMIN_ACCOUNT, Constants.ADMIN_PWD + "pppppp", Constants.ADMIN_PWD));
}
@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);
}
@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);
}
}