def test_email_stage_template_validation(self) -> None: self.set_tenant(0) user = self.users[0] campaign = Campaign.objects.create(name='some campaign', owner=user) step = Step.objects.create(campaign=campaign, start=datetime.time(9, 45), end=datetime.time(18, 30)) t_client = TenantClient(self.get_current_tenant()) t_client.handler = ForceAuthClientHandler(enforce_csrf_checks=False) t_client.handler._force_user = user self.assertTrue(t_client.login(username=user.username, password='******'), 'Test user was not logged in') url = reverse.reverse('api:campaigns-steps-email-list', args=[campaign.pk, step.pk, ]) with modify_settings(ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url}): response = t_client.post(url, json.dumps(dict( subject='Hello good fellow', html_content='Some invalid email template to {{First name}}!', )), content_type='application/json', ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, str(response.content)) error_data = response.data self.assertTrue("Could not parse" in error_data['html_content'][0])
def test_bulk_delete_participation(self) -> None: self.set_tenant(0) user = self.users[0] first_contact = Contact.objects.create(email='*****@*****.**', first_name='First', last_name='Smith') second_contact = Contact.objects.create(email='*****@*****.**', first_name='Second', last_name='Smith') campaign = Campaign.objects.create(name='cool campaign', owner=user) Participation.objects.bulk_create([ Participation(campaign=campaign, contact=first_contact), Participation(campaign=campaign, contact=second_contact), ]) t_client = TenantClient(self.get_current_tenant()) t_client.handler = ForceAuthClientHandler(enforce_csrf_checks=False) t_client.handler._force_user = user self.assertTrue(t_client.login(username=user.username, password='******'), 'Test user was not logged in') url = reverse.reverse('api:campaigns-contacts-list', args=[campaign.pk, ]) with modify_settings(ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url}): response = t_client.delete(urljoin(url, '?contact__in=%s' % str(first_contact.id)), content_type='application/json', ) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, str(response.content)) participation = Participation.objects.get(campaign=campaign) self.assertEqual(second_contact, participation.contact)
def test_bulk_post_participation(self) -> None: self.set_tenant(0) user = self.users[0] first_contact = Contact.objects.create(email='*****@*****.**', first_name='First', last_name='Smith') second_contact = Contact.objects.create(email='*****@*****.**', first_name='Second', last_name='Smith') campaign = Campaign.objects.create(name='cool campaign', owner=user) t_client = TenantClient(self.get_current_tenant()) t_client.handler = ForceAuthClientHandler(enforce_csrf_checks=False) t_client.handler._force_user = user self.assertTrue(t_client.login(username=user.username, password='******'), 'Test user was not logged in') url = reverse.reverse('api:campaigns-contacts-list', args=[campaign.pk, ]) with modify_settings(ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url}): response = t_client.post(url, json.dumps([ dict(contact=first_contact.id), dict(contact=second_contact.id), ]), content_type='application/json', ) self.assertEqual(response.status_code, status.HTTP_201_CREATED, str(response.content)) self.assertEqual(2, Participation.objects.filter( campaign=campaign, contact_id__in=(first_contact.id, second_contact.id,), ).count()) contacts = campaign.contacts.all() self.assertListEqual([first_contact, second_contact, ], list(contacts))
def test_contact_by_campaign_filtering(self) -> None: self.set_tenant(0) user = self.users[0] first_contact = Contact.objects.create(email='*****@*****.**', first_name='First', last_name='Smith') second_contact = Contact.objects.create(email='*****@*****.**', first_name='Second', last_name='Smith') Contact.objects.create(email='*****@*****.**', first_name='Third', last_name='Smith') fourth_contact = Contact.objects.create(email='*****@*****.**', first_name='Fourth', last_name='Smith') first_campaign = Campaign.objects.create(name='first campaign for filtering test', owner=user) second_campaign = Campaign.objects.create(name='second campaign for filtering test', owner=user) Participation.objects.bulk_create([ Participation(campaign=first_campaign, contact=first_contact), Participation(campaign=second_campaign, contact=second_contact), Participation(campaign=first_campaign, contact=fourth_contact), Participation(campaign=second_campaign, contact=fourth_contact), ]) t_client = TenantClient(self.get_current_tenant()) t_client.handler = ForceAuthClientHandler(enforce_csrf_checks=False) t_client.handler._force_user = user self.assertTrue(t_client.login(username=user.username, password='******'), 'Test user was not logged in') url = reverse.reverse('api:contacts-list') with modify_settings(ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url}): query = '?campaigns__in=%s' % ','.join(map(str, [first_campaign.id, second_campaign.id])) response = t_client.get(urljoin(url, query)) self.assertEqual(response.status_code, status.HTTP_200_OK, str(response.content)) contacts_data = response.data self.assertEqual(3, len(contacts_data)) self.assertSetEqual({first_contact.id, second_contact.id, fourth_contact.id}, {c['id'] for c in contacts_data}) with modify_settings(ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url}): query = '?campaigns=' + ','.join(map(str, [first_campaign.id, second_campaign.id])) response = t_client.get(urljoin(url, query)) self.assertEqual(response.status_code, status.HTTP_200_OK, str(response.content)) contacts_data = response.data self.assertEqual(1, len(contacts_data)) self.assertEqual(fourth_contact.id, contacts_data[0]['id']) with modify_settings(ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url}): query = '?campaigns=%s' % second_campaign.id response = t_client.get(urljoin(url, query)) self.assertEqual(response.status_code, status.HTTP_200_OK, str(response.content)) contacts_data = response.data self.assertEqual(1, len(contacts_data)) self.assertEqual(second_contact.id, contacts_data[0]['id'])
def test_unread_only_filtering(self): self.set_tenant(0) now = timezone.now() action = NoticeType.objects.first() Notification.objects.bulk_create([ Notification(user=self.user, created=now, action=action), Notification(user=self.user, created=now, action=action, read_datetime=now, extra_context=dict(mark=True)), Notification(user=self.user, created=now, action=action), ]) t_client = TenantClient(self.get_current_tenant()) t_client.handler = ForceAuthClientHandler(enforce_csrf_checks=False) t_client.handler._force_user = self.user self.assertTrue( t_client.login(username=self.user.username, password='******'), 'Test user was not logged in') url = reverse.reverse('api:notifications-list') with modify_settings( ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url }): query = '?unread_only=true' response = t_client.get(urljoin(url, query)) self.assertEqual(response.status_code, status.HTTP_200_OK, str(response.content)) notifications_data = response.data self.assertEqual(2, len(notifications_data)) notifications = Notification.objects.filter( extra_context__isnull=True).all() self.assertSetEqual({n.id for n in notifications}, {n['id'] for n in notifications_data})
def test_bulk_delete_with_filtering(self) -> None: self.set_tenant(0) first_contact = Contact.objects.create(email='*****@*****.**', first_name='First', last_name='Smith') second_contact = Contact.objects.create(email='*****@*****.**', first_name='Second', last_name='Smith') third_contact = Contact.objects.create(email='*****@*****.**', first_name='Third', last_name='Smith') t_client = TenantClient(self.get_current_tenant()) t_client.handler = ForceAuthClientHandler(enforce_csrf_checks=False) t_client.handler._force_user = self.user self.assertTrue( t_client.login(username=self.user.username, password='******'), 'Test user was not logged in') ids_for_delete = [ first_contact.id, third_contact.id, ] url = reverse.reverse('api:contacts-list') with modify_settings( ALLOWED_HOSTS={'append': self.get_current_tenant().domain_url }): response = t_client.delete( urljoin( url, '?id__in=%s' % ','.join(str(pk) for pk in ids_for_delete))) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, str(response.content)) self.assertFalse( Contact.objects.filter(id__in=ids_for_delete).exists()) self.assertTrue(Contact.objects.filter(id=second_contact.id).exists())