コード例 #1
0
ファイル: views.py プロジェクト: willgraham29/balance-checker
def get_token(request):
    CLIENT_DATA = {}
    data = request.GET
    code = data.get('code')

    body = {
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'code': code,
        'grant_type': 'authorization_code',
        'redirect_uri': REDIRECT_URI,
    }

    res1 = requests.post('https://auth.truelayer-sandbox.com/connect/token', data=body)
    CLIENT_DATA['token'] = res1.json()
    # print(CLIENT_DATA['token'])

    access_token = CLIENT_DATA['token']['access_token']
    header = {'Authorization': f'Bearer {access_token}'}
    res2 = requests.get('https://api.truelayer-sandbox.com/data/v1/me', headers=header)
    CLIENT_DATA['credentials'] = res2.json()
    # print(CLIENT_DATA['credentials']['results'][0]['credentials_id'])

    token_data = Token(
        credentials_id = CLIENT_DATA['credentials']['results'][0]['credentials_id'],
        access_token=CLIENT_DATA['token']['access_token'],
        access_expires_in=timedelta(seconds = CLIENT_DATA['token']['expires_in']),
        refresh_token=CLIENT_DATA['token']['refresh_token']
        )
    token_data.user = request.user
    token_data.save()

    return access_token
コード例 #2
0
    def test_can_confirm_a_new_user(self):
        token = Token(user_id=self.user.id, type='VERIFY')
        token.save()

        c = Client()
        response = c.post(f'/v1/confirm/?id={token.id}')

        self.assertEquals(response.status_code, status.HTTP_200_OK)
コード例 #3
0
 def test_serialized_and_then_deserialized_data_is_valid(self):
     token = Token(email=TEST_EMAIL)
     token.save()
     serializer = TokenSerializer(token)
     content = JSONRenderer().render(serializer.data)
     stream = BytesIO(content)
     data = JSONParser().parse(stream)
     serializer = TokenSerializer(data=data)
     self.assertTrue(serializer.is_valid())
コード例 #4
0
 def test_serialized_and_then_deserialized_data_is_valid(self):
     token = Token(email=TEST_EMAIL)
     token.save()
     serializer = TokenSerializer(token)
     content = JSONRenderer().render(serializer.data)
     stream = BytesIO(content)
     data = JSONParser().parse(stream)
     serializer = TokenSerializer(data=data)
     self.assertTrue(serializer.is_valid())
コード例 #5
0
    def test_reset_expired_token(self):
        token = Token(user_id=self.user.id, type='VERIFY')
        token.save()

        c = Client()
        response = c.post('/v1/reset_confirm/'.format(token.id), json.dumps({
            'email': '*****@*****.**'
        }), content_type='application/json')

        self.assertEquals(response.status_code, status.HTTP_200_OK)
コード例 #6
0
    def test_reset_password(self):
        token = Token(user_id=self.user.id, type='RESET')
        token.save()

        c = Client()
        response = c.post(f'/v1/change_password/{token.id}/', json.dumps({
            'email': '*****@*****.**',
            'password': '******'
        }), content_type='application/json')

        self.assertEquals(response.status_code, status.HTTP_200_OK)
コード例 #7
0
    def test_fails_if_confirm_token_expired(self):
        timestamp = datetime.datetime.now() - datetime.timedelta(days=2)
        token = Token(user_id=self.user.id, type='VERIFY')
        token.save()

        # Overwrite token expiration time
        cursor = connection.cursor()
        cursor.execute("UPDATE accounts_token SET expires='{}' WHERE id='{}'; ".format(str(timestamp), token.id))

        c = Client()
        response = c.post(f'/v1/confirm/?id={token.id}')

        self.assertEquals(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
コード例 #8
0
    def test_password_reset_fails_with_expired_token(self):
        timestamp = datetime.datetime.now() - datetime.timedelta(days=2)

        token = Token(user_id=self.user.id, type='RESET')
        token.save()

        # Overwrite token expiration time
        cursor = connection.cursor()
        cursor.execute("UPDATE accounts_token SET expires='{}' WHERE id='{}'; ".format(str(timestamp), token.id))

        c = Client()
        response = c.post('/v1/change_password/{}/'.format(token.id), json.dumps({
            'email': '*****@*****.**',
            'password': '******'
        }), content_type='application/json')

        self.assertEquals(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
コード例 #9
0
 def create(self, request, *args, **kwargs):
     serializer = serializers.UsersInscriptionSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     user = User.objects.get(email=serializer['email'].value)
     token_gen = random_token(99)
     token = Token(user=user, token=token_gen)
     token.save()
     #TODO: Integrar Celerys.
     send_email(serializer['email'].value, serializer['first_name'].value,
                serializer['last_name'].value, token_gen)
     headers = self.get_success_headers(serializer.data)
     return Response(
         {
             "completado": "Verifique su cuenta en el correo electronico.",
             "user": serializer.data,
             "token": token_gen
         },
         status=status.HTTP_201_CREATED,
         headers=headers)
コード例 #10
0
 def test_email_is_primary_key(self):
     ''' 登录验证模型的PrimaryKey是Email字段
     '''
     token_object = Token(email='*****@*****.**')
     self.assertEqual(token_object.pk, '*****@*****.**')