Test login and register, and fix the bugs in them

This commit is contained in:
fanfuxiaoran 2013-11-27 11:02:18 +08:00
parent 05c38c1436
commit 2abd605102
10 changed files with 2939 additions and 24 deletions

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes({ "accessToken", "username","testPlanTaskList" })
@SessionAttributes({ "accessToken", "username", "testPlanTaskList" })
public class AuthorizeActionController extends BaseControllerService {
private Logger logger = Logger.getLogger(AuthorizeActionController.class);
@ -29,6 +29,8 @@ public class AuthorizeActionController extends BaseControllerService {
public @ResponseBody
boolean login(UserModel user, ModelMap model) {
System.out.println("enter authorize");
if(isInvalidate(user))
return false;
AuthorizeResponseModel authorizeResponseModel = authorize(user);
boolean result = extractAuthorizeResponseModel(authorizeResponseModel,
user, model);
@ -41,7 +43,6 @@ public class AuthorizeActionController extends BaseControllerService {
Map<String, String> params = this.makeParamsMap("userName",
user.getUsername());
params.put("password", user.getPassword());
System.out.println(this.getMasterIP() + url);
HttpResponse httpResponse = this.getHttpRequester().sendGet(
this.getMasterIP() + url, params, null);
if (httpResponse == null) {
@ -77,7 +78,7 @@ public class AuthorizeActionController extends BaseControllerService {
authorizeResponseModel.getAccessToken());
model.addAttribute("username", user.getUsername());
List<TestPlanTaskModel> testPlanTaskModels = new ArrayList<TestPlanTaskModel>();
model.addAttribute("testPlanTaskList",testPlanTaskModels);
model.addAttribute("testPlanTaskList", testPlanTaskModels);
return true;
}
@ -88,10 +89,13 @@ public class AuthorizeActionController extends BaseControllerService {
public boolean register(UserModel user) {
System.out.println("enter register!!");
String url = "user/register";
if(isInvalidate(user))
return false;
if(!user.getPassword().equals(user.getRetype_pwd()))
return false;
Map<String, String> params = this.makeParamsMap("userName",
user.getUsername());
params.put("password", user.getPassword());
logger.info("master Ip:" + this.getMasterIP() + url);
System.out.println("master Ip:" + this.getMasterIP() + url);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
@ -116,6 +120,13 @@ public class AuthorizeActionController extends BaseControllerService {
}
public boolean isInvalidate(UserModel user){
if (user.getPassword() == null || user.getUsername() == null
|| user.getPassword().equals("")
|| user.getUsername().equals(""))
return true;
else return false;
}
public boolean extractRegisterModel(
RegisterResponseModel registerResponseModel) {
if (registerResponseModel == null) {
@ -123,7 +134,6 @@ public class AuthorizeActionController extends BaseControllerService {
} else if (!registerResponseModel.isSuccess()) {
return false;
}
return true;
}

View File

@ -42,7 +42,6 @@ public abstract class BaseControllerService {
prop.load(inputStream);
inputStream.close();
this.masterIP = prop.getProperty("masterAddress") + "/";
System.out.println(masterIP);
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e) {

View File

@ -4,6 +4,18 @@ public class UserModel {
private String username;
private String password;
private String retype_pwd;
public UserModel(){
}
public UserModel(String name,String password){
this.username=name;
this.password=password;
}
public UserModel(String name,String password,String retype_pwd){
this.username=name;
this.password=password;
this.retype_pwd=retype_pwd;
}
public String getUsername() {
return username;
}

View File

@ -2,23 +2,22 @@ $(document).ready(function() {
loadProperties();
});
function loadProperties(){
function loadProperties() {
jQuery.i18n.properties({// 加载资浏览器语言对应的资源文件
name:'i18n', // 资源文件名称
path:'i18n/', // 资源文件路径
mode:'map', // 用 Map 的方式使用资源文件中的值
} );
}
name : 'i18n', // 资源文件名称
path : 'i18n/', // 资源文件路径
mode : 'map', // 用 Map 的方式使用资源文件中的值
});
}
function checkName(name) {
if (name == '') {
document.getElementById('alertUserName').style.display='block';
document.getElementById('alertUserName').style.display = 'block';
} else {
document.getElementById('alertUserName').style.display='none';
document.getElementById('alertUserName').style.display = 'none';
}
}
@ -27,11 +26,11 @@ function checkPass(password) {
if (password == "") {
document.getElementById('alertPassWord').style.display='block';
document.getElementById('alertPassWord').style.display = 'block';
} else {
document.getElementById('alertPassWord').style.display='none';
document.getElementById('alertPassWord').style.display = 'none';
}
}
function login(url) {
@ -57,10 +56,17 @@ function login(url) {
function register() {
var username = $('#username').val();
var password = $('#password').val();
var re_password = $("#re_password").val();
var rePassword = $("#re_password").val();
if (rePassword != passward) {
$('#username').val("");
$('#password').val("");
alert("password is wrong! retype again");
}
$.post('register', {
username : username,
password : password
password : password,
rePassword:rePassword
}, function(data) {
if (data) {
$('#registerMsg').hide();
@ -72,5 +78,5 @@ function register() {
// $('#registerMsg').html($.i18n.prop('msgRegister'));
$('#registerMsg').show();
}
},"json");
}, "json");
}

View File

@ -0,0 +1,38 @@
package org.bench4q.web.api.test;
import org.bench4q.web.api.AuthorizeActionController;
import org.bench4q.web.entity.master.User;
import org.bench4q.web.model.UserModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/resources/bench4qweb-servlet.xml"})
public class LoginTest {
@Autowired
private AuthorizeActionController authorizeActionController;
@Test
public void login(){
UserModel userRight=new UserModel("www","www");
ModelMap model=new ModelMap();
UserModel userPError=new UserModel("www","123");
UserModel userNError=new UserModel("123", "www");
UserModel userErrorModel=new UserModel(null, "123");
UserModel userNameEmpty=new UserModel("", "890");
Assert.isTrue(authorizeActionController.login(userRight, model));
Assert.state(userRight.getUsername().equals(model.get("username")));
Assert.isTrue(!authorizeActionController.login(userErrorModel, model));
Assert.isTrue(!authorizeActionController.login(userNError, model));
Assert.isTrue(!authorizeActionController.login(userPError, model));
Assert.isTrue(!userNError.getUsername().equals(model.get("username")));
Assert.isTrue(!authorizeActionController.login(userNameEmpty, model));
}
}

View File

@ -0,0 +1,46 @@
package org.bench4q.web.api.test;
import javassist.expr.NewArray;
import org.bench4q.web.api.AuthorizeActionController;
import org.bench4q.web.model.UserModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/resources/bench4qweb-servlet.xml" })
public class RegisterTest {
@Autowired
AuthorizeActionController authorizeActionController;
@Test
public void register() {
UserModel userRight = new UserModel("ihih", "345", "345");
Assert.isTrue(authorizeActionController.register(userRight));
UserModel userExist = new UserModel("www", "www", "www");
Assert.isTrue(!authorizeActionController.register(userExist));
UserModel userNameExit = new UserModel("www", "123", "123");
Assert.isTrue(!authorizeActionController.register(userNameExit));
UserModel userNameEmpty = new UserModel("", "123", "123");
Assert.isTrue(!authorizeActionController.register(userNameEmpty));
UserModel userPassEmpty = new UserModel("wxxx", "", "");
Assert.isTrue(!authorizeActionController.register(userPassEmpty));
UserModel userPassError = new UserModel("pol", "123", "234");
Assert.isTrue(!authorizeActionController.register(userPassError));
UserModel userNameNull = new UserModel(null, "123", "123");
Assert.isTrue(!authorizeActionController.register(userNameNull));
UserModel userPassNull = new UserModel("mnb", null, "345");
Assert.isTrue(!authorizeActionController.register(userPassNull));
UserModel userTPassNull = new UserModel("eeff", "34", null);
Assert.isTrue(!authorizeActionController.register(userTPassNull));
}
}