async def method_post(self, request: Request, body: dict, session, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestAuthUserDto(body) try: db_user = user_queries.get_user(session, login=request_model.login) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: check_hash(request_model.password, db_user.password) except CheckPasswordHashException: raise SanicPasswordHashException('Wrong password') payload = { 'user_id': db_user.id, } token = create_token(payload) response = AuthResponseObject(token) response_model = ResponseAuthUserDto(response) return await self.make_response_json( body=response_model.dump(), status=200, )
def test_read_valid_token(token_data): request_token = create_token(token_data) response_token = read_token(request_token) response_token.pop('exp') # вытаскиваем ключ exp (expiration date) assert response_token == token_data
def user_register(request): """注册""" if request.method == "POST": print("开始注册") register_form = RegisterForm(request.POST) if 1: email = request.POST.get("email", "") username = request.POST.get("username", "") password = request.POST.get("password", "") code = request.POST.get("code") try: user_profile = UserProfile.objects.get( Q(username=username) | Q(email=email)) except Exception as e: user_profile = None if user_profile: if user_profile.is_staff: return ajax_fail_data(message="用戶已存在") code = EmailVerifyRecord.objects.get(code=code, email=email, send_type="register") if not code: return ajax_fail_data(message="请输入正确验证码") token = create_token(email) user_profile = UserProfile() user_profile.username = username user_profile.is_staff = True user_profile.email = email user_profile.password = make_password(password) user_profile.token = token user_profile.save() return ajax_ok_data() else: print("f**k") data = register_form return ajax_ok_data(data)
def test_read_expired_token_new(token_data): request_token = create_token(token_data, lifetime=-5) with pytest.raises(ReadTokenException): read_token(request_token)
def test_read_expired_token(token_data, patch_datetime_utcnow): request_token = create_token(token_data) with pytest.raises(ReadTokenException): read_token(request_token)