def add(request): #print "add" data = None uname = None pword = None user = None errCode = "errCode" count = 0 err_code = SUCCESS if request.method == "POST": if not('application/json' in request.META['CONTENT_TYPE']): return client(request) data = json.loads(request.body) print 'Raw Data: "%s"' % data uname = data['user'] pword = data['password'] usermodel = UsersModel() add_result = usermodel.add(uname, pword) #print add_result err_code = add_result[0] count = add_result[1] if err_code == SUCCESS: jsonresponse = json.dumps({errCode:err_code, 'count': count}) return HttpResponse(jsonresponse, content_type="application/json"); else: jsonresponse = json.dumps({errCode:err_code}) return HttpResponse(jsonresponse, content_type="application/json"); else: print "OMG" return client(request)
def login(request): #print "login" data = None input_name = None input_pw = None existed_user = None jsonreponse = None errCode = "errCode" err_code = SUCCESS count = 0 if request.method == "POST": print request print "isPost" if not('application/json' in request.META['CONTENT_TYPE']): return client(request) data = json.loads(request.body) #print 'Raw Data: %s"' % data input_name = data['user'] input_pw = data['password'] #print input_name #print input_pw usermodel = UsersModel() add_result = usermodel.login(input_name, input_pw) #print "finish usermodel login" err_code = add_result[0] count = add_result[1] #print err_code #print count if err_code == SUCCESS: print "success" jsonresponse = json.dumps({errCode:err_code, 'count': count}) return HttpResponse(jsonresponse, content_type="application/json"); else: print "not success" jsonresponse = json.dumps({errCode:err_code}) return HttpResponse(jsonresponse, content_type="application/json"); return client(request)
def test_success_add(self): newuser = UsersModel() self.assertEqual(newuser.add("user", "password"), (SUCCESS,1))
def test_username_password_mismatch_login(self): earlieruser = Users(username= "******", password = "******") earlieruser.save() newuser = UsersModel() self.assertEqual(newuser.login("user", "wordpass"), (ERR_BAD_CREDENTIALS,0))
def test_success_login(self): earlieruser = Users(username= "******", password = "******") earlieruser.save() newuser = UsersModel() self.assertEqual(newuser.login("user", "password"), (SUCCESS,2))
def test_long_add_password_sting(self): usermodel = UsersModel() longstring = "a"*129 self.assertEqual(usermodel.add("username", longstring), (ERR_BAD_PASSWORD,0))
def test_repetivtive_add_user(self): earlieruser = Users(username= "******", password = "******") earlieruser.save() newuser = UsersModel() self.assertEqual(newuser.add("user", "password"), (ERR_USER_EXISTS,0))
def test_empty_add_password_sting(self): usermodel = UsersModel() self.assertEqual(usermodel.add("username", ""), (ERR_BAD_PASSWORD,0))
def test_long_add_user_sting(self): usermodel = UsersModel() longstring = "a"*129 self.assertEqual(usermodel.add(longstring, "password"), (ERR_BAD_USERNAME,0))
def test_empty_add_user_sting(self): usermodel = UsersModel() self.assertEqual(usermodel.add("","password"), (ERR_BAD_USERNAME,0))
def test_long_login_user_sting(self): usermodel = UsersModel() longstring = "d"*129 print usermodel self.assertEqual(usermodel.login(longstring, "password"), (ERR_BAD_USERNAME,0))
def test_empty_login_user_sting(self): print "I'm here" usermodel = UsersModel() #print usermodel self.assertEqual(usermodel.login("","password"), (ERR_BAD_USERNAME,0))