Exemplo n.º 1
0
    def test_attrs(self):
        # start with the defaults
        at = AccessToken()
        self.assertEqual(at.token, '')
        self.assertEqual(at.is_active, True)
        self.assertEqual(at.expires_on, default_expiry())
        self.assertEqual(at.created_at, None)
        self.assertEqual(at.updated_at, None)
        self.assertEqual(at.created_by, None)

        # check the timestamps
        at = at.save()
        self.assertIsNotNone(at.created_at)
        self.assertEqual(at.updated_at, at.created_at)

        # check the timestamps are updated
        x = at.created_at
        at = at.save()
        # created_at is _not_ updated
        self.assertEqual(at.created_at, x)
        # but updated_at _is_
        self.assertTrue(at.updated_at > at.created_at)

        self.assertTrue(at.is_valid)
        self.assertFalse(at.has_expired)
Exemplo n.º 2
0
 def test_has_expired(self):
     at = AccessToken()
     at.expires_on = YESTERDAY
     self.assertTrue(at.has_expired)
     at.expires_on = TODAY
     self.assertFalse(at.has_expired)
     at.expires_on = TOMORROW
     self.assertFalse(at.has_expired)
Exemplo n.º 3
0
 def test_record(self):
     at = AccessToken(token="test_token").save()
     atu = at.record("*****@*****.**", "Hugo")
     self.assertEqual(atu, AccessTokenUse.objects.get())
     self.assertEqual(atu.user_email, "*****@*****.**")
     self.assertEqual(atu.user_name, "Hugo")
     self.assertIsNotNone(atu.timestamp, "Hugo")
     self.assertEqual(atu.client_ip, "unknown")
     self.assertEqual(atu.client_user_agent, "unknown")
Exemplo n.º 4
0
class GatewayFormTests(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.payload = {
            'token': "test",
            'email': "*****@*****.**",
            'name': "Hugo Rodger-Brown"
        }
        self.request = self.factory.post('/',
            data=self.payload,
            REMOTE_ADDR='127.0.0.1',
            HTTP_USER_AGENT='test_agent'
        )
        self.token = AccessToken(token="test").save()
        self.form = GatewayForm(self.request.POST)

    def test_post_valid_token(self):
        self.assertTrue(self.form.is_valid())
        self.assertEqual(self.form.token, self.token)

    def test_clean_inactive_token(self):
        self.token.is_active = False
        self.token.save(update_fields=['is_active'])
        self.assertFalse(self.form.is_valid())
        self.assertRaises(ValidationError, self.form.clean_token)

    def test_clean_expired_token(self):
        self.token.expires_on = YESTERDAY
        self.token.save(update_fields=['expires_on'])
        self.assertFalse(self.form.is_valid())
        self.assertRaises(ValidationError, self.form.clean_token)

    def test_no_matching_token(self):
        AccessToken.objects.all().delete()
        self.assertFalse(self.form.is_valid())
        self.assertRaises(ValidationError, self.form.clean_token)

    def test_save(self):
        self.request.session = {}
        self.assertTrue(self.form.is_valid())
        au = self.form.save(self.request)
        self.assertTrue(AccessTokenUse.objects.get(), au)
        self.assertEqual(au.user_email, self.payload['email'])
        self.assertEqual(au.user_name, self.payload['name'])
        self.assertEqual(au.token, self.token)
        self.assertEqual(au.client_ip, '127.0.0.1')
        self.assertEqual(au.client_user_agent, 'test_agent')
    def handle(self, *args, **options):

        has_expires = options.get('expires') is not None
        days = options.get('expires') or PERIMETER_DEFAULT_EXPIRY
        token = options.get('token') or AccessToken.random_token_value()
        expires_on = (now() + datetime.timedelta(days=days)).date()
        try:
            access_token = AccessToken.objects.create_access_token(
                token=token,
                expires_on=expires_on
            )
            self.stdout.write(
                'Created new access token: "{}" (expires {})'.format(
                    access_token.token,
                    access_token.expires_on
                )
            )
        except IntegrityError:
            access_token = AccessToken.objects.get(token=token)
            if has_expires:
                self.stdout.write("Extending existing token")
                access_token.expires_on = expires_on
                access_token.save()
                self.stdout.write("Token extended: {}".format(access_token))
            else:
                self.stdout.write("Token exists already - please use --expires option to extend.")
Exemplo n.º 6
0
 def test_str(self):
     # test unicode, str
     today = date.today()
     yesterday = today - timedelta(days=1)
     at = AccessToken(token="foobar", expires_on=today, is_active=True)
     self.assertEqual(
         unicode(at),
         u"foobar - valid until %s" % today
     )
     at.is_active = False
     self.assertEqual(
         unicode(at),
         u"foobar - inactive"
     )
     at.is_active = True
     at.expires_on = yesterday
     self.assertEqual(
         unicode(at),
         u"foobar - expired on %s" % yesterday
     )
 def handle(self, *args, **options):
     days = options.get('expires') or PERIMETER_DEFAULT_EXPIRY
     token = options.get('token') or AccessToken.random_token_value()
     expires_on = (now() + datetime.timedelta(days=days)).date()
     token = AccessToken.objects.create_access_token(
         token=token,
         expires_on=expires_on
     )
     print (
         'Created new access token: "%s" (expires %s)'
         % (token.token, token.expires_on)
     )
Exemplo n.º 8
0
 def setUp(self):
     self.factory = RequestFactory()
     self.payload = {
         'token': "test",
         'email': "*****@*****.**",
         'name': "Hugo Rodger-Brown"
     }
     self.request = self.factory.post('/',
         data=self.payload,
         REMOTE_ADDR='127.0.0.1',
         HTTP_USER_AGENT='test_agent'
     )
     self.token = AccessToken(token="test").save()
     self.form = GatewayForm(self.request.POST)
Exemplo n.º 9
0
 def test_generate_random_token(self):
     f = AccessToken._meta.get_field('token').max_length
     t1 = AccessToken.random_token_value()
     t2 = AccessToken.random_token_value()
     self.assertNotEqual(t1, t2)
     self.assertEqual(len(t1), f)
Exemplo n.º 10
0
 def test_cache_key(self):
     token = AccessToken(token="test")
     self.assertIsNotNone(token.cache_key)
     self.assertEqual(token.cache_key, AccessToken.get_cache_key("test"))