def test_save_with_random_token_failures(self): from django.core.exceptions import ObjectDoesNotExist t = Token() self.assertRaises(ObjectDoesNotExist, t.save_with_random_token) self.assertEqual('', t.token) t = Token(user=self.user) self.assertRaises(ObjectDoesNotExist, t.save_with_random_token) self.assertEqual('', t.token) t = Token(experiment=self.experiment) self.assertRaises(ObjectDoesNotExist, t.save_with_random_token) self.assertEqual('', t.token)
def test_default_expiry(self): the_time = old_datetime(2011, 9, 20, 1, 52, 31, 570376) FrozenTime.freeze_time(the_time) expected_expiry = the_time.date() + \ datetime.timedelta(settings.TOKEN_EXPIRY_DAYS) t = Token() self.assertEqual(expected_expiry, t.expiry_date)
def test_save_with_random_token_gives_up(self): from django.db import IntegrityError t = Token(user=self.user, experiment=self.experiment) t.save = _raise_integrity_error self.assertRaises(IntegrityError, t.save_with_random_token) # should have tried to assign at least one token self.assertTrue(len(t.token) > 0)
def test_get_session_expiry_expired_token(self): now = old_datetime(2011, 8, 20, 13) FrozenTime.freeze_time(now) yesterday = (now - datetime.timedelta(1)).date() t = Token() t.expiry_date = yesterday self.assertEqual(now.year, t.get_session_expiry().year) self.assertEqual(now.month, t.get_session_expiry().month) self.assertEqual(now.day, t.get_session_expiry().day) self.assertEqual(now.hour, t.get_session_expiry().hour)
def test_get_session_expiry(self): three_am = old_datetime(2011, 8, 20, 3) now = three_am FrozenTime.freeze_time(now) tomorrow_4am = old_datetime(2011, 8, 21, 4) t = Token() expected_expiry = tomorrow_4am self.assertEqual(expected_expiry.year, t.get_session_expiry().year) self.assertEqual(expected_expiry.month, t.get_session_expiry().month) self.assertEqual(expected_expiry.day, t.get_session_expiry().day) self.assertEqual(expected_expiry.hour, t.get_session_expiry().hour)
def test_get_session_expiry_near_expiry(self): now = old_datetime(2011, 8, 20, 3) FrozenTime.freeze_time(now) t = Token() t.expiry_date = now.date() expected_expiry = old_datetime(2011, 8, 20, 23, 59, 59) actual_expiry = t.get_session_expiry() print actual_expiry self.assertEqual(expected_expiry.year, actual_expiry.year) self.assertEqual(expected_expiry.month, actual_expiry.month) self.assertEqual(expected_expiry.day, actual_expiry.day) self.assertEqual(expected_expiry.hour, actual_expiry.hour)
def test_is_expired(self): now = old_datetime(2011, 8, 20, 0, 0, 0, 1) FrozenTime.freeze_time(now) today = now.date() yesterday = today - datetime.timedelta(1) tomorrow = today + datetime.timedelta(1) t = Token() t.expiry_date = yesterday self.assertTrue(t.is_expired()) t.expiry_date = today self.assertFalse(t.is_expired()) t.expiry_date = tomorrow self.assertFalse(t.is_expired())
def test_save_with_random_token(self): t = Token(user=self.user, experiment=self.experiment) t.save_with_random_token() self.assertTrue(len(t.token) > 0)
def create_token(request, experiment_id): experiment = Experiment.objects.get(id=experiment_id) token = Token(experiment=experiment, user=request.user) token.save_with_random_token() logger.info('created token: %s' % token) return HttpResponse('{"success": true}', content_type='application/json')