Update user expiry field, fix tests

This commit is contained in:
Christian Cueni 2020-02-26 10:31:35 +01:00
parent 3d63a7933d
commit 4e2c47d00a
4 changed files with 52 additions and 12 deletions

View File

@ -32,5 +32,9 @@ class Command(BaseCommand):
product = hep_client.myskillbox_product_for_customer(admin_token, hep_user.hep_id)
if product and License.objects.filter(licensee=hep_user, order_id=product['order_id']).count() == 0:
License.objects.create_license_for_role(hep_user, product['activated'], product['raw'],
license = License.objects.create_license_for_role(hep_user, product['activated'], product['raw'],
product['edition'], product['order_id'])
if license.is_valid():
hep_user.license_expiry_date = license.expire_date
hep_user.save()

View File

@ -133,6 +133,16 @@ class LicenseManager(models.Manager):
def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id):
return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id)
def get_active_licenses_for_user(self, user):
return self.filter(licensee=user, expire_date__gte=timezone.now())\
.filter(expire_date__lt=timezone.now()).order_by('-expire_date')
def get_active_license_for_user(self, user):
licenses = self.filter(licensee=user, expire_date__gte=timezone.now()).order_by('-expire_date')
if len(licenses) == 0:
return None
license = licenses[0]
# update license on user
user.license_expiry_date = license.expire_date
user.save()
return license

View File

@ -69,7 +69,7 @@ class LoginTests(TestCase):
self.user.save()
now = timezone.now()
expiry_date = now + timedelta(365)
expiry_date = now + timedelta(100)
LicenseFactory(expire_date=expiry_date, licensee=self.user, for_role=self.teacher_role).save()
result = self.make_login_mutation(TOKEN)
@ -77,6 +77,30 @@ class LoginTests(TestCase):
self.assertTrue(result.get('data').get('login').get('success'))
self.assertTrue(self.user.is_authenticated)
reloaded_user = User.objects.get(pk=self.user.id)
self.assertEqual(reloaded_user.license_expiry_date, expiry_date.date())
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
def test_user_can_login_with_local_user_and_valid_local_licenses(self, me_mock):
self.user.hep_id = ME_DATA['id']
self.user.save()
now = timezone.now()
expiry_date1 = now + timedelta(100)
expiry_date2 = now + timedelta(110)
LicenseFactory(expire_date=expiry_date1, licensee=self.user, for_role=self.teacher_role).save()
LicenseFactory(expire_date=expiry_date2, licensee=self.user, for_role=self.teacher_role).save()
result = self.make_login_mutation(TOKEN)
self.assertTrue(result.get('data').get('login').get('success'))
self.assertTrue(self.user.is_authenticated)
reloaded_user = User.objects.get(pk=self.user.id)
self.assertEqual(reloaded_user.license_expiry_date, expiry_date2.date())
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
def test_user_can_login_with_updated_email(self, me_mock):
@ -101,14 +125,17 @@ class LoginTests(TestCase):
self.assertTrue(self.user.is_authenticated)
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
def test_user_can_login_with_local_user_and_valid_local_license(self, me_mock):
def test_user_can_login_with_local_user_and_valid_local_licenses(self, me_mock):
self.user.hep_id = ME_DATA['id']
self.user.save()
now = timezone.now()
expiry_date = now + timedelta(365)
LicenseFactory(expire_date=expiry_date, licensee=self.user, for_role=self.teacher_role).save()
expiry_date1 = now + timedelta(365)
expiry_date2 = now + timedelta(366)
LicenseFactory(expire_date=expiry_date1, licensee=self.user, for_role=self.teacher_role).save()
LicenseFactory(expire_date=expiry_date2, licensee=self.user, for_role=self.teacher_role).save()
result = self.make_login_mutation(TOKEN)

View File

@ -38,10 +38,9 @@ def handle_user_and_verify_products(user_data):
except HepClientException:
return user, UNKNOWN_ERROR
try:
license = License.objects.get(licensee=user)
# Todo how handle invalid license? Cron Job? How to select correct license? Save all licenses? History?
except License.DoesNotExist:
license = License.objects.get_active_license_for_user(user)
if not license:
license, error_msg = check_and_create_licenses(hep_client, user)
if error_msg: