Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
form_data = {
'username': 'UnIqUeFoO',
'email': 'some@some.com',
'email2': 'some@some.com',
'password': 'pass'}
response = self.client.post(
reverse('spirit:user:auth:register'), form_data)
expected_url = reverse('spirit:user:auth:login')
self.assertRedirects(response, expected_url, status_code=302)
self.assertTrue(
UserProfile.objects.filter(
nickname='UnIqUeFoO',
user__username='uniquefoo'
).exists())
self.assertFalse(
UserProfile.objects.filter(nickname='uniquefoo').exists())
def test_last_seen(self):
"""
Should update user last_seen when threshold is reached
"""
req = RequestFactory().get('/')
req.user = self.user
self.assertTrue(req.user.is_authenticated)
delta = datetime.timedelta(
seconds=settings.ST_USER_LAST_SEEN_THRESHOLD_MINUTES * 60 + 1)
self.assertEqual(
UserProfile.objects
.filter(pk=req.user.st.pk)
.update(last_seen=timezone.now() - delta), 1)
# Some DBs don't save microseconds, so get the real value
last_seen = UserProfile.objects.get(pk=req.user.st.pk).last_seen
req.user.st.last_seen = last_seen
self.assertIsNone(
middleware.LastSeenMiddleware().process_request(req))
self.assertGreater(
UserProfile.objects.get(pk=req.user.st.pk).last_seen,
last_seen)
def test_migration_profiles(self):
"""Should create profile for existing users"""
foo = utils.create_user(username='foo')
bar = utils.create_user(username='bar')
UserProfile.objects.filter(user=foo).delete()
UserProfile.objects.filter(user=bar).delete()
foo = User.objects.get(pk=foo.pk)
bar = User.objects.get(pk=bar.pk)
with self.assertRaises(ObjectDoesNotExist):
self.assertIsNone(foo.st)
with self.assertRaises(ObjectDoesNotExist):
self.assertIsNone(bar.st)
data_migration_profiles.migrate_profiles(apps, None)
self.assertTrue(User.objects.get(pk=foo.pk).st.is_verified)
self.assertTrue(User.objects.get(pk=bar.pk).st.is_verified)
def test_profile_creation_on_register_case_insensitive_user(self):
form_data = {
'username': 'UnIqUeFoO',
'email': 'some@some.com',
'email2': 'some@some.com',
'password': 'pass'}
response = self.client.post(
reverse('spirit:user:auth:register'), form_data)
expected_url = reverse('spirit:user:auth:login')
self.assertRedirects(response, expected_url, status_code=302)
self.assertTrue(
UserProfile.objects.filter(
nickname='UnIqUeFoO',
user__username='uniquefoo'
).exists())
self.assertFalse(
UserProfile.objects.filter(nickname='uniquefoo').exists())
def increase_received_likes_count(self):
UserProfile.objects.filter(pk=self.pk).update(received_likes_count=F('received_likes_count') + 1)
def increase_given_likes_count(self):
UserProfile.objects.filter(pk=self.pk).update(given_likes_count=F('given_likes_count') + 1)
def update_post_hash(self, post_hash):
# Let the DB do the hash
# comparison for atomicity
return bool(
UserProfile.objects
.filter(pk=self.pk)
.exclude(
last_post_hash=post_hash,
last_post_on__gte=timezone.now() - timedelta(
minutes=settings.ST_DOUBLE_POST_THRESHOLD_MINUTES))
.update(
last_post_hash=post_hash,
last_post_on=timezone.now()))
def increase_topic_count(self):
UserProfile.objects.filter(pk=self.pk).update(topic_count=F('topic_count') + 1)
def process_request(self, request):
if not request.user.is_authenticated:
return
threshold = settings.ST_USER_LAST_SEEN_THRESHOLD_MINUTES * 60
delta = timezone.now() - request.user.st.last_seen
if delta.total_seconds() < threshold:
return
(UserProfile.objects
.filter(pk=request.user.st.pk)
.update(last_seen=timezone.now()))