def test_employer_opt_out_siae_no_job_descriptions(self): siae = SiaeWithMembershipFactory() user = siae.members.first() recipient = user.siaemembership_set.get(siae=siae) form_name = "new_job_app_notification_form" self.client.login(username=user.email, password=DEFAULT_PASSWORD) # Recipient's notifications are empty for the moment. self.assertFalse(recipient.notifications) url = reverse("dashboard:edit_user_preferences") response = self.client.get(url) self.assertEqual(response.status_code, 200) # Recipients are subscribed to spontaneous notifications by default, # the form should reflect that. self.assertTrue( response.context[form_name].fields["spontaneous"].initial) data = {"spontaneous": False} response = self.client.post(url, data=data) self.assertEqual(response.status_code, 302) recipient.refresh_from_db() self.assertTrue(recipient.notifications) self.assertFalse( NewSpontaneousJobAppEmployersNotification.is_subscribed( recipient=recipient))
def test_unsubscribe(self): self.notification.unsubscribe(recipient=self.membership) self.assertTrue(self.membership.notifications) # Dict is not empty self.assertFalse( NewSpontaneousJobAppEmployersNotification.is_subscribed( recipient=self.membership)) key = self.notification.NAME self.assertTrue(self.membership.notifications.get(key)) # Key exists
def test_unsubscribe_and_subscribe(self): """ Make sure it's possible to toggle preferences. """ NewSpontaneousJobAppEmployersNotification.unsubscribe( recipient=self.membership) self.assertFalse( NewSpontaneousJobAppEmployersNotification.is_subscribed( recipient=self.membership)) NewSpontaneousJobAppEmployersNotification.subscribe( recipient=self.membership) self.assertTrue( NewSpontaneousJobAppEmployersNotification.is_subscribed( recipient=self.membership)) NewSpontaneousJobAppEmployersNotification.unsubscribe( recipient=self.membership) self.assertFalse( NewSpontaneousJobAppEmployersNotification.is_subscribed( recipient=self.membership))
def __init__(self, *args, **kwargs): self.recipient = kwargs.pop("recipient") self.siae = kwargs.pop("siae") super().__init__(*args, **kwargs) self.fields[ "spontaneous"].initial = NewSpontaneousJobAppEmployersNotification.is_subscribed( self.recipient) if self.siae.job_description_through.exists(): default_pks = self.siae.job_description_through.values_list( "pk", flat=True) self.subscribed_pks = NewQualifiedJobAppEmployersNotification.recipient_subscribed_pks( recipient=self.recipient, default_pks=default_pks) choices = [ (job_description.pk, job_description.display_name) for job_description in self.siae.job_description_through.all() ] self.fields["qualified"] = forms.MultipleChoiceField( label="Fiches de poste", required=False, widget=MultipleSwitchCheckboxWidget(), choices=choices, initial=self.subscribed_pks, )