def test_authenticate_bad(self): self.assertEquals( Person.authenticate( 'snhorne', self.person_one.account, 'wrongpassword', ), None)
def test_authenticate_bad(self): self.assertEquals( Person.authenticate( 'snhorne', self.person_one.account, 'wrongpassword', ), None )
def test_get_user_from_session_with_no_session_key(self): """ Try to load person from session when ther is no session key """ request = MockRequest() request.account = self.person_one.account self.assertEquals( Person.load_from_request(request), None, )
def test_get_user_from_session(self): """ Loads the user with id = request.session[Person.SESSION_KEY] """ request = MockRequest() request.account = self.person_one.account self.person_one.login(request) self.assertEquals( Person.load_from_request(request), self.person_one, )
def test_get_user_from_session_with_invalid_session_key(self): """ Try to load person from session when the session key doesn't match any person's id. """ request = MockRequest() request.account = self.person_one.account request.session[Person.SESSION_KEY] = 100 self.assertEquals( Person.load_from_request(request), None, )
def test_login_logout(self): request = MockRequest() self.person_one.login(request) # Person will be saved to request self.assertEquals( request.person, self.person_one, ) # Person's id will be saved to session self.assertEquals( request.session[Person.SESSION_KEY], self.person_one.id, ) # On logout these are cleared. Person.logout(request) self.assertEquals( request.person, None, ) self.assertFalse(Person.SESSION_KEY in request.session, )
def account_manage(request): if request.method == 'POST': username = request.POST.get('username', False) password = request.POST.get('password', False) name = request.POST.get('name', False) email = request.POST.get('email', False) social_id = request.POST.get('social_id', False) gender = request.POST.get('gender', False) birthday = request.POST.get('birthday', False) address = request.POST.get('address', False) phone = request.POST.get('phone', False) if not username or not password or not name or not email or not social_id or not gender or not birthday or not address or not phone: return HttpResponse(status=400) user = User.objects.create_user(username=username, password=password, email=email) user.save() # TODO set is_active to False, and send email to user person = Person(user=user, name=name, social_id=social_id, gender=gender, birthday=datetime.strptime(birthday, "%Y-%m-%d").date(), address=address, phone=phone) person.save() return HttpResponse(status=200) elif request.method == 'GET': if request.user.is_authenticated() == False: return HttpResponseForbidden() user = User.objects.get(username=request.user.username) response_data = model_to_dict(user.person, exclude=['id']) print(response_data) return HttpResponse(json.dumps(response_data,default=date_handler), status=200, content_type='application/json')
def test_login_logout(self): request = MockRequest() self.person_one.login(request) # Person will be saved to request self.assertEquals( request.person, self.person_one, ) # Person's id will be saved to session self.assertEquals( request.session[Person.SESSION_KEY], self.person_one.id, ) # On logout these are cleared. Person.logout(request) self.assertEquals( request.person, None, ) self.assertFalse( Person.SESSION_KEY in request.session, )