Exemplo n.º 1
0
    def test(self):
        user1 = User.objects.create_user(username='******', password='******')
        user2 = User.objects.create_user(username='******', password='******')

        token1 = token_generator.make(user1)
        token2 = token_generator.make(user2)

        self.assertTrue(token_generator.check(user1, token1))
        self.assertTrue(token_generator.check(user2, token2))

        self.assertFalse(token_generator.check(user1, token2))
        self.assertFalse(token_generator.check(user2, token1))
Exemplo n.º 2
0
    def test_long_base_36_check(self):
        user1 = User.objects.create_user(username='******', password='******')
        token1 = token_generator.make(user1)
        pk, ts_b36, hash = token1.split('-')
        token1 = "{0}-{1}{2}{3}-{4}".format(pk, ts_b36, ts_b36, ts_b36, hash)

        self.assertFalse(token_generator.check(user1, token1))
Exemplo n.º 3
0
    def test_password_change(self):
        user1 = User.objects.create_user(username='******', password='******')
        token1 = token_generator.make(user1)

        self.assertTrue(token_generator.check(user1, token1))
        user1.set_password('new')
        self.assertFalse(token_generator.check(user1, token1))
Exemplo n.º 4
0
    def test_timeout(self):
        user1 = User.objects.create_user(username='******', password='******')
        token1 = token_generator.make(user1)

        self.assertTrue(token_generator.check(user1, token1))
        time.sleep(3)
        self.assertFalse(token_generator.check(user1, token1))
Exemplo n.º 5
0
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')

        if username and password:
            user = authenticate(username=username, password=password)

            if user:
                login(request, user)
                token = token_generator.make(user)
                data = self.get(request)
                data['token'] = token
                return data

        return self.render(request, {'message': 'Invalid credentials'},
                           status=codes.unauthorized)
Exemplo n.º 6
0
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')

        if username and password:
            user = authenticate(username=username, password=password)

            if user:
                login(request, user)
                token = token_generator.make(user)
                data = self.get(request)
                data['token'] = token
                return data

        return self.render(request, {'message': 'Invalid credentials'},
                           status=codes.unauthorized)
Exemplo n.º 7
0
    def test_resource(self):
        user = User.objects.create_user(username='******', password='******')

        resp = self.client.get(reverse('serrano:root'),
                               HTTP_ACCEPT='application/json')
        self.assertEqual(resp.status_code, 401)

        self.assertTrue(self.client.login(username='******', password='******'))
        resp = self.client.get(reverse('serrano:root'),
                               HTTP_ACCEPT='application/json')
        self.assertEqual(resp.status_code, 200)

        self.client.logout()
        resp = self.client.get(reverse('serrano:root'),
                               HTTP_ACCEPT='application/json')
        self.assertEqual(resp.status_code, 401)

        token = token_generator.make(user)
        resp = self.client.get(reverse('serrano:root'), {'token': token},
                               HTTP_ACCEPT='application/json')
        self.assertEqual(resp.status_code, 200)
Exemplo n.º 8
0
 def test(self):
     user = User.objects.create_user(username='******', password='******')
     token = token_generator.make(user)
     self.assertEqual(user, authenticate(token=token))
Exemplo n.º 9
0
    def test_unsplitable_token_check(self):
        user1 = User.objects.create_user(username='******', password='******')
        token1 = token_generator.make(user1)
        token1 = token1.replace('-', '')

        self.assertFalse(token_generator.check(user1, token1))