Commit ac2b5bcc by Calen Pennington Committed by GitHub

Merge pull request #13717 from open-craft/smarnach/anonymous-id

Allow to store multiple anonymous ids per (user, course)
parents 84c42ae1 f5d556e7
......@@ -121,7 +121,6 @@ class AnonymousUserId(models.Model):
user = models.ForeignKey(User, db_index=True)
anonymous_user_id = models.CharField(unique=True, max_length=32)
course_id = CourseKeyField(db_index=True, max_length=255, blank=True)
unique_together = (user, course_id)
def anonymous_id_for_user(user, course_id, save=True):
......@@ -159,22 +158,11 @@ def anonymous_id_for_user(user, course_id, save=True):
return digest
try:
anonymous_user_id, __ = AnonymousUserId.objects.get_or_create(
defaults={'anonymous_user_id': digest},
AnonymousUserId.objects.get_or_create(
user=user,
course_id=course_id
course_id=course_id,
anonymous_user_id=digest,
)
if anonymous_user_id.anonymous_user_id != digest:
log.error(
u"Stored anonymous user id %(anonymous_user_id)r for "
u"user %(user)r in course %(course_id)r doesn't match "
u"computed id %(digest)r", {
"anonymous_user_id": anonymous_user_id.anonymous_user_id,
"user": user,
"course_id": course_id,
"digest": digest,
}
)
except IntegrityError:
# Another thread has already created this entry, so
# continue
......
......@@ -12,7 +12,7 @@ import ddt
from django.conf import settings
from django.contrib.auth.models import User, AnonymousUser
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test import TestCase, override_settings
from django.test.client import Client
from edx_oauth2_provider.tests.factories import ClientFactory
import httpretty
......@@ -945,7 +945,7 @@ class AnonymousLookupTable(ModuleStoreTestCase):
def setUp(self):
super(AnonymousLookupTable, self).setUp()
self.course = CourseFactory.create()
self.user = UserFactory()
self.user = UserFactory.create()
CourseModeFactory.create(
course_id=self.course.id,
mode_slug='honor',
......@@ -974,6 +974,18 @@ class AnonymousLookupTable(ModuleStoreTestCase):
self.assertEqual(self.user, real_user)
self.assertEqual(anonymous_id, anonymous_id_for_user(self.user, course2.id, save=False))
def test_secret_key_changes(self):
"""Test that a new anonymous id is returned when the secret key changes."""
CourseEnrollment.enroll(self.user, self.course.id)
anonymous_id = anonymous_id_for_user(self.user, self.course.id)
with override_settings(SECRET_KEY='some_new_and_totally_secret_key'):
# Recreate user object to clear cached anonymous id.
self.user = User.objects.get(pk=self.user.id)
new_anonymous_id = anonymous_id_for_user(self.user, self.course.id)
self.assertNotEqual(anonymous_id, new_anonymous_id)
self.assertEqual(self.user, user_by_anonymous_id(anonymous_id))
self.assertEqual(self.user, user_by_anonymous_id(new_anonymous_id))
@attr(shard=3)
@httpretty.activate
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment