Exemplo n.º 1
0
    def test_filter_has_expired(self):
        self._create_generic_token_with_metadata(user=create_user('testuser1'),
                                                 app='testapp',
                                                 token='test-token1',
                                                 expiration_datetime=datetime(
                                                     2015, 1, 1, 14, 30))
        self._create_generic_token_with_metadata(user=create_user('testuser2'),
                                                 app='testapp',
                                                 token='test-token2',
                                                 expiration_datetime=None)
        expired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser3'),
            app='testapp',
            token='test-token3',
            expiration_datetime=datetime(2015, 1, 1, 13, 30))

        with mock.patch(
                'django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                lambda: datetime(2015, 1, 1, 14)):
            self.assertEquals(
                GenericTokenWithMetadata.objects.filter_has_expired().count(),
                1)
            self.assertEquals(
                GenericTokenWithMetadata.objects.filter_has_expired().first(),
                expired_generic_token_with_metadata)
Exemplo n.º 2
0
 def test_generate_handle_not_unique(self):
     self._create_generic_token_with_metadata(user=create_user('testuser1'), app='testapp1', token='taken')
     tokens = iter(['taken', 'free'])
     with mock.patch('django_cradmin.apps.cradmin_generic_token_with_metadata.models.generate_token',
                     lambda: next(tokens)):
         unique_user_token = GenericTokenWithMetadata.objects.generate(
             content_object=create_user('testuser2'), app='testapp2',
             expiration_datetime=get_expiration_datetime_for_app('testapp'))
     self.assertEqual(unique_user_token.token, 'free')
Exemplo n.º 3
0
    def test_pop_expired(self):
        self._create_generic_token_with_metadata(
            user=create_user('testuser'),
            app='testapp', token='test-token',
            expiration_datetime=datetime(2015, 1, 1, 13, 30))

        with mock.patch('django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                        lambda: datetime(2015, 1, 1, 14)):
            with self.assertRaises(GenericTokenWithMetadata.DoesNotExist):
                GenericTokenWithMetadata.objects.pop(app='testapp', token='test-token')
Exemplo n.º 4
0
    def test_pop_not_expired(self):
        testuser = create_user('testuser')
        self._create_generic_token_with_metadata(
            user=testuser, token='test-token',
            expiration_datetime=datetime(2015, 1, 1, 14, 30))

        with mock.patch('django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                        lambda: datetime(2015, 1, 1, 14)):
            self.assertEquals(GenericTokenWithMetadata.objects.pop(app='testapp', token='test-token').content_object,
                              testuser)
Exemplo n.º 5
0
    def test_is_expired(self):
        unexpired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser1'), token='test-token1',
            expiration_datetime=datetime(2015, 1, 1, 14, 30))
        expired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser2'), token='test-token2',
            expiration_datetime=datetime(2015, 1, 1, 13, 30))

        with mock.patch('django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                        lambda: datetime(2015, 1, 1, 14)):
            self.assertFalse(unexpired_generic_token_with_metadata.is_expired())
            self.assertTrue(expired_generic_token_with_metadata.is_expired())
Exemplo n.º 6
0
 def test_generate_handle_not_unique(self):
     self._create_generic_token_with_metadata(user=create_user('testuser1'),
                                              app='testapp1',
                                              token='taken')
     tokens = iter(['taken', 'free'])
     with mock.patch(
             'django_cradmin.apps.cradmin_generic_token_with_metadata.models.generate_token',
             lambda: next(tokens)):
         unique_user_token = GenericTokenWithMetadata.objects.generate(
             content_object=create_user('testuser2'),
             app='testapp2',
             expiration_datetime=get_expiration_datetime_for_app('testapp'))
     self.assertEqual(unique_user_token.token, 'free')
Exemplo n.º 7
0
    def test_pop_expired(self):
        self._create_generic_token_with_metadata(user=create_user('testuser'),
                                                 app='testapp',
                                                 token='test-token',
                                                 expiration_datetime=datetime(
                                                     2015, 1, 1, 13, 30))

        with mock.patch(
                'django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                lambda: datetime(2015, 1, 1, 14)):
            with self.assertRaises(GenericTokenWithMetadata.DoesNotExist):
                GenericTokenWithMetadata.objects.pop(app='testapp',
                                                     token='test-token')
Exemplo n.º 8
0
    def test_filter_not_expired_none(self):
        expired_none_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser1'), token='test-token1',
            expiration_datetime=None)
        self._create_generic_token_with_metadata(
            user=create_user('testuser2'), token='test-token2',
            expiration_datetime=datetime(2015, 1, 1, 13, 30))

        with mock.patch('django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                        lambda: datetime(2015, 1, 1, 14)):
            self.assertEquals(GenericTokenWithMetadata.objects.filter_not_expired().count(), 1)
            self.assertEquals(GenericTokenWithMetadata.objects.filter_not_expired().first(),
                              expired_none_generic_token_with_metadata)
Exemplo n.º 9
0
    def test_pop_not_expired(self):
        testuser = create_user('testuser')
        self._create_generic_token_with_metadata(user=testuser,
                                                 token='test-token',
                                                 expiration_datetime=datetime(
                                                     2015, 1, 1, 14, 30))

        with mock.patch(
                'django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                lambda: datetime(2015, 1, 1, 14)):
            self.assertEquals(
                GenericTokenWithMetadata.objects.pop(
                    app='testapp', token='test-token').content_object,
                testuser)
Exemplo n.º 10
0
    def test_delete_expired_management_command(self):
        unexpired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser1'), token='test-token1',
            expiration_datetime=datetime(2015, 1, 1, 14, 30))
        self._create_generic_token_with_metadata(
            user=create_user('testuser2'), token='test-token2',
            expiration_datetime=datetime(2015, 1, 1, 13, 30))

        self.assertEquals(GenericTokenWithMetadata.objects.count(), 2)
        with mock.patch('django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                        lambda: datetime(2015, 1, 1, 14)):
            call_command('cradmin_generic_token_with_metadata_delete_expired')
        self.assertEquals(GenericTokenWithMetadata.objects.count(), 1)
        self.assertEquals(GenericTokenWithMetadata.objects.first(),
                          unexpired_generic_token_with_metadata)
Exemplo n.º 11
0
    def test_is_expired(self):
        unexpired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser1'),
            token='test-token1',
            expiration_datetime=datetime(2015, 1, 1, 14, 30))
        expired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser2'),
            token='test-token2',
            expiration_datetime=datetime(2015, 1, 1, 13, 30))

        with mock.patch(
                'django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                lambda: datetime(2015, 1, 1, 14)):
            self.assertFalse(
                unexpired_generic_token_with_metadata.is_expired())
            self.assertTrue(expired_generic_token_with_metadata.is_expired())
Exemplo n.º 12
0
    def test_delete_expired_management_command(self):
        unexpired_generic_token_with_metadata = self._create_generic_token_with_metadata(
            user=create_user('testuser1'),
            token='test-token1',
            expiration_datetime=datetime(2015, 1, 1, 14, 30))
        self._create_generic_token_with_metadata(user=create_user('testuser2'),
                                                 token='test-token2',
                                                 expiration_datetime=datetime(
                                                     2015, 1, 1, 13, 30))

        self.assertEquals(GenericTokenWithMetadata.objects.count(), 2)
        with mock.patch(
                'django_cradmin.apps.cradmin_generic_token_with_metadata.models._get_current_datetime',
                lambda: datetime(2015, 1, 1, 14)):
            call_command('cradmin_generic_token_with_metadata_delete_expired')
        self.assertEquals(GenericTokenWithMetadata.objects.count(), 1)
        self.assertEquals(GenericTokenWithMetadata.objects.first(),
                          unexpired_generic_token_with_metadata)