def post(self, data): password = data.get("password", "") customer: Customer = Manager(Customer).get(email=self.kwargs["email"]) if customer.verify_password(password): token = Token(customer=customer.pk, token=str(uuid4())) token.save() return self.response({"token": token.token}) else: return self.response({"error": "Invalid password"})
def check_user_token(user): """ Retrieve, update or create a user's token @param user: the user to get a token for @return: a valid token associated with the user """ token = Token.objects(user=user).first() if token: return token else: token = Token(user=user) token.save() return token
def test_login_required_view_with_token(self): path = "/website" customer = Customer(name="test", password="******", email="test", subscription=None, plan=None) customer.save() token_str = "token" token = Token(token=token_str, customer=customer.pk) token.save() view = router.router(path, token_str) self.assertIsInstance(view, WebsiteView) self.assertEqual(view.customer.pk, customer.pk)