def add(request): data = json.loads(request.raw_post_data) username = data['user'] password = data['password'] response_data = {} if User.objects.filter(name=username).count() > 0 : #User exists, return ERR_USER_EXISTS response_data['errCode'] = -2 return HttpResponse(json.dumps(response_data), mimetype="application/json") elif username == '' or len(username) > 128 : #empty username or mroe than 128 ascii characters, return ERR_BAD_USERNAME response_data['errCode'] = -3 return HttpResponse(json.dumps(response_data), mimetype="application/json") elif (len(password) > 128) : #password more than 128 characters, return ERR_BAD_PASSWORD response_data['errCode'] = -4 return HttpResponse(json.dumps(response_data), mimetype="application/json") else : #all is good, save the new User new_user = User(name=username, password=password,num_logins=1) new_user.save() #return a SUCCESS and number of logins, should be 1 response_data['errCode'] = 1 response_data['count'] = new_user.num_logins return HttpResponse(json.dumps(response_data), mimetype="application/json")
def test_Clearing_Database(self): user = User(name='admin', password='******', num_logins=1) user.save() count = User.objects.count() self.assertEqual(count, 1) response = self.client.post('/TESTAPI/resetFixture', '', content_type="application/json") self.assertEqual(response.status_code, 200) count = User.objects.count() self.assertEqual(count, 0)