def test_authenticate_wrong_email(self): """ authenticate method must raise exception on wrong email """ self.create_user() with self.assertRaises(Exception): User.authenticate('wrong_email', 'password')
def test_authenticate(self): """ authenticate method must return user and jwt and store jwt in redis """ expected_user = self.create_user() user, encoded_jwt = User.authenticate('email', 'password') expected_jwt = list(User.redis_connection.database.keys())[0] self.assertEqual(expected_user.email, user.email, 'Wrong user returned') self.assertEqual(expected_jwt, encoded_jwt.encode('utf-8'), 'Wrong jwt returned')
def test_other_user_auth(self): self._create_user() _, jwt_header = User.authenticate(self.EMAIL, self.PASSWORD) post_user = self._create_user(email='alternative_email') post, name, text = self._create_post_name_text(post_user) post.status = Post.ACTIVE post.save() self.update_post_wrong_user(post.pk, jwt_header) self.delete_post_wrong_user(post.pk, jwt_header)
def test_post_live_cycle(self): self._create_user() _, jwt_header = User.authenticate(self.EMAIL, self.PASSWORD) post_pk = self.create_post_success(jwt_header).pk self.get_post_success(post_pk) self.update_post_success(post_pk, jwt_header) self.get_post_success(post_pk) self.delete_post_success(post_pk, jwt_header) self.get_post_error(post_pk)
def post(self, request, *args, **kwargs): try: body = self.process_body(request, [ 'email', 'password', ]) user, encoded_jwt = User.authenticate(body['email'], body['password']) return self.respond_success_json({ 'jwt': encoded_jwt, 'user': user.serialize() }) except Exception as error: return self.respond_error_json(error, status=403)