def test_authorized_users_api_bundle_inactive_user(self): """ With the addition of Bundle partners, the API should still return the correct list of authorized users. This time, the list should be empty because the user hasn't been active in the last two weeks """ bundle_partner_1 = PartnerFactory(authorization_method=Partner.BUNDLE) self.editor1.wp_bundle_eligible = True # The user had last logged in three weeks ago self.editor1.user.last_login = timezone.now() - timedelta(weeks=3) self.editor1.user.save() self.editor1.save() self.editor1.update_bundle_authorization() # Verify we created the bundle auth as expected self.assertEqual(get_all_bundle_authorizations().count(), 1) factory = APIRequestFactory() request = factory.get("/api/v0/users/authorizations/partner/1") force_authenticate(request, user=self.editor1.user) response = TWLight.users.views.AuthorizedUsers.as_view()( request, bundle_partner_1.pk, 0) # Returned list should be empty because of the applied filter expected_json = [] self.assertEqual(response.data, expected_json)
def test_authorized_users_api_bundle(self): """ With the addition of Bundle partners, the API should still return the correct list of authorized users. """ bundle_partner_1 = PartnerFactory(authorization_method=Partner.BUNDLE) bundle_partner_2 = PartnerFactory(authorization_method=Partner.BUNDLE) self.editor1.wp_bundle_eligible = True self.editor1.user.last_login = timezone.now() self.editor1.user.save() self.editor1.save() self.editor1.update_bundle_authorization() # Verify we created the bundle auth as expected self.assertEqual(get_all_bundle_authorizations().count(), 1) factory = APIRequestFactory() request = factory.get("/api/v0/users/authorizations/partner/1") force_authenticate(request, user=self.editor1.user) response = TWLight.users.views.AuthorizedUsers.as_view()( request, bundle_partner_1.pk, 0) expected_json = [{"wp_username": self.editor1.wp_username}] self.assertEqual(response.data, expected_json)
def update_existing_bundle_authorizations(sender, instance, **kwargs): """ If this partner was just switched to Bundle from a non-Bundle authorization method, update any existing Bundle authorizations to include it, and vice-versa, including if it was marked not-available. Also delete any authorizations that previously existed to this partner. """ add_to_auths = False remove_from_auths = False try: previous_data = Partner.even_not_available.get(pk=instance.pk) # We must be creating this partner, we'll handle this case in a # post-save signal except Partner.DoesNotExist: return # New data for this partner for readability now_bundle = instance.authorization_method == Partner.BUNDLE now_available = instance.status == Partner.AVAILABLE # Previous data for this partner for readability previously_available = previous_data.status == Partner.AVAILABLE previously_bundle = previous_data.authorization_method == Partner.BUNDLE if now_available: if now_bundle: if not previously_available or not previously_bundle: add_to_auths = True else: if previously_bundle: remove_from_auths = True elif not now_available: if previously_available and previously_bundle: remove_from_auths = True # Let's avoid db queries if we don't need them if add_to_auths or remove_from_auths: authorizations_to_update = get_all_bundle_authorizations() if add_to_auths: # Before updating Bundle auths, let's delete any # previously existing authorizations for this partner all_partner_authorizations = Authorization.objects.filter( partners__pk=instance.pk ) for defunct_authorization in all_partner_authorizations: defunct_authorization.delete() for authorization in authorizations_to_update: authorization.partners.add(instance) elif remove_from_auths: for authorization in authorizations_to_update: authorization.partners.remove(instance)
def update_bundle_authorizations_on_bundle_partner_creation( sender, instance, created, **kwargs): """ This does the same thing that the pre-save signal update_existing_bundle_authorizations() does, except it handles new Bundle-partner creations. We can't do this in pre-save because the partner object doesn't exist yet. """ if (created and instance.status == Partner.AVAILABLE and instance.authorization_method == Partner.BUNDLE): authorizations_to_update = get_all_bundle_authorizations() if authorizations_to_update: for authorization in authorizations_to_update: authorization.partners.add(instance)