def test_closed_to_free(self): case = CaseFactory(pk=639, status=Case.STATUS.closed) message = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=message) case.refresh_from_db() self.assertEqual(case.status, Case.STATUS.free)
def test_with_case_count(self): user = UserFactory() CaseFactory.create_batch(size=25, client=user) self.assertEqual( User.objects.with_case_count().get(pk=user.pk).case_count, 25) self.assertEqual( User.objects.with_case_count().get(pk=UserFactory().pk).case_count, 0)
def test_with_case_count_assigned(self): user = UserFactory() self.assertEqual(User.objects.with_case_count_assigned().get(pk=user.pk).case_assigned, 0) for obj in CaseFactory.create_batch(size=5): assign_perm('cases.can_view', user, obj) self.assertEqual(User.objects.with_case_count_assigned().get(pk=user.pk).case_assigned, 5) for obj in CaseFactory.create_batch(size=5): assign_perm('cases.can_view', user, obj) self.assertEqual(User.objects.with_case_count_assigned().get(pk=user.pk).case_assigned, 10)
def test_closed_to_assigned(self): staff = UserFactory(is_staff=True) case = CaseFactory(pk=639, status=Case.STATUS.closed) assign_perm('cases.can_send_to_client', staff, case) msg = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=msg) case.refresh_from_db() self.assertEqual(case.status, Case.STATUS.assigned)
class CaseCloseViewTestCase(TestCase): def setUp(self): self.user = UserFactory() self.object = CaseFactory() self.client.login(username=self.user.username, password='******') def test_close_case(self): assign_perm('cases.can_close_case', self.user, self.object) resp = self.client.post(self.object.get_close_url(), {'notify': True}) self.assertEqual(resp.context['target'].status, Case.STATUS.closed) def test_close_case_not_permitted(self): resp = self.client.get(self.object.get_close_url()) self.assertEqual(resp.status_code, 403)
def test_with_case_count_assigned(self): user = UserFactory() self.assertEqual( User.objects.with_case_count_assigned().get( pk=user.pk).case_assigned, 0) for obj in CaseFactory.create_batch(size=5): assign_perm('cases.can_view', user, obj) self.assertEqual( User.objects.with_case_count_assigned().get( pk=user.pk).case_assigned, 5) for obj in CaseFactory.create_batch(size=5): assign_perm('cases.can_view', user, obj) self.assertEqual( User.objects.with_case_count_assigned().get( pk=user.pk).case_assigned, 10)
class CaseCloseViewTestCase(TestCase): def setUp(self): self.user = UserFactory() self.object = CaseFactory() self.client.login(username=self.user.username, password='******') def test_close_case(self): assign_perm('cases.can_close_case', self.user, self.object) resp = self.client.post(self.object.get_close_url(), {'notify': True}) self.assertEqual( resp.context['target'].status, Case.STATUS.closed) def test_close_case_not_permitted(self): resp = self.client.get(self.object.get_close_url()) self.assertEqual(resp.status_code, 403)
def test_get_by_email(self): with self.assertRaises(Case.DoesNotExist): Case.get_by_email('*****@*****.**') Case.get_by_email('*****@*****.**') obj = CaseFactory(pk=22) obj2 = Case.get_by_email('*****@*****.**') self.assertEqual(obj, obj2)
class CaseAdminTestCase(TestCase): def setUp(self): self.site = AdminSite() def assertIsValid(self, model_admin, model): # See django/tests/modeladmin/tests.py#L602 admin_obj = model_admin(model, self.site) if django.VERSION > (1, 9): errors = admin_obj.check() else: errors = admin_obj.check(model) expected = [] self.assertEqual(errors, expected) def test_is_valid(self): self.assertIsValid(CaseAdmin, Case) def test_record_count(self): case = CaseFactory() LetterFactory.create_batch(size=25, case=case) admin_obj = CaseAdmin(Case, AdminSite()) request = RequestFactory().get( reverse_lazy('admin:cases_case_changelist')) request.user = UserFactory(is_staff=True, is_superuser=True) qs = admin_obj.get_queryset(request) obj = qs.get(pk=case.pk) self.assertTrue(hasattr(obj, 'record_count')) self.assertEqual(admin_obj.record_count(obj), 25)
class ReceiveEmailTestCase(TestCase): def setUp(self): super(TestCase, self).setUp() self.mailbox = Mailbox.objects.create(from_email='*****@*****.**') @staticmethod def _get_email_object(filename): # See coddingtonbear/django-mailbox#89 path = join(dirname(__file__), 'messages', filename) fp = open(path, 'rb') return message_from_file(fp) def get_message(self, filename): message = self._get_email_object(filename) msg = self.mailbox._process_message(message) msg.save() return msg def test_user_identification(self): user = UserFactory(email='*****@*****.**') message = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=message) self.assertEqual(user, message.letter_set.all()[0].created_by) def test_cc_message(self): case = CaseFactory(pk=639) message = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=message) self.assertEqual(case, message.letter_set.all()[0].case)
def test_permission_filter(self): obj = CaseFactory() self.assertFalse( self.get_permission_filter_qs(user=UserFactory(), pk=obj.pk).exists()) user = UserFactory(is_staff=True) assign_perm('cases.can_view', user, obj) self.assertTrue( self.get_permission_filter_qs(user=user, pk=obj.pk).exists())
class CaseDetailViewTestCase(TestCase): def setUp(self): self.object = CaseFactory() self.client.login(username=self.object.created_by.username, password='******') def test_can_view(self): self.client.get(self.object.get_absolute_url()) def test_display_letter(self): obj = LetterFactory(case=self.object) resp = self.client.get(self.object.get_absolute_url()) self.assertContains(resp, obj.name) def test_hide_internal_letter(self): obj = LetterFactory(case=self.object, status='staff') obj2 = LetterFactory(case=self.object, status='done') self.object.created_by.is_staff = False self.object.created_by.save() resp = self.client.get(self.object.get_absolute_url()) self.assertNotContains(resp, obj.name) self.assertContains(resp, obj2.name)
def test_order_for_user(self): a = repr( CaseFactory(name="CaseA", last_action=now() + timedelta(days=0), last_send=now() + timedelta(days=+1))) b = repr( CaseFactory(name="CaseB", last_action=now() + timedelta(days=+2), last_send=now() + timedelta(days=-1))) c = repr( CaseFactory(name="CaseC", last_action=now() + timedelta(days=-1), last_send=now() + timedelta(days=+3))) user = UserFactory(is_staff=True) self.assertQuerysetEqual( Case.objects.order_for_user(user, True).all(), [c, a, b]) self.assertQuerysetEqual( Case.objects.order_for_user(user, False).all(), [b, a, c]) user = UserFactory(is_staff=False) self.assertQuerysetEqual( Case.objects.order_for_user(user, True).all(), [b, a, c]) self.assertQuerysetEqual( Case.objects.order_for_user(user, False).all(), [c, a, b])
def setUp(self): self.user = UserFactory() self.object = CaseFactory() self.client.login(username=self.user.username, password='******')
return msg def test_user_identification(self): user = UserFactory(email='*****@*****.**') message = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=message) self.assertEqual(user, message.letter_set.all()[0].created_by) def test_cc_message(self): case = CaseFactory(pk=639) message = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=message) self.assertEqual(case, message.letter_set.all()[0].case) def test_closed_to_free(self): case = CaseFactory(pk=639, status=Case.STATUS.closed) message = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=message) case.refresh_from_db() self.assertEqual(case.status, Case.STATUS.free) def test_closed_to_assigned(self): staff = UserFactory(is_staff=True) case = CaseFactory(pk=639, status=Case.STATUS.closed) assign_perm('cases.can_send_to_client', staff, case) msg = self.get_message('cc_message.eml') mail_process(sender=self.mailbox, message=msg) case.refresh_from_db()
def setUp(self): self.case = CaseFactory(has_project=False) self.url = reverse('letters:add', kwargs={'case_pk': self.case.pk})
class CaseTestCase(TestCase): def setUp(self): self.object = CaseFactory() def test_get_edit_url(self): self.assertEqual( CaseFactory(pk=50).get_edit_url(), '/sprawy/sprawa-50/edytuj/') @override_settings(PORADNIA_EMAIL_INPUT='case-(?P<pk>\d+)@example.com') def test_get_by_email(self): with self.assertRaises(Case.DoesNotExist): Case.get_by_email('*****@*****.**') Case.get_by_email('*****@*****.**') obj = CaseFactory(pk=22) obj2 = Case.get_by_email('*****@*****.**') self.assertEqual(obj, obj2) def test_perm_check(self): u1 = UserFactory() assign_perm('cases.can_view', u1) u2 = UserFactory() assign_perm('cases.can_view', u2, self.object) self.assertTrue(self.object.perm_check(u1, 'can_view')) self.assertTrue(self.object.perm_check(u2, 'can_view')) with self.assertRaises(PermissionDenied): self.object.perm_check(UserFactory(), 'can_view') def test_status_update_initial(self): self.assertEqual(self.object.status, Case.STATUS.free) def test_status_update_still_open(self): assign_perm('cases.can_send_to_client', UserFactory(is_staff=False), self.object) self.object.status_update() self.assertEqual(self.object.status, Case.STATUS.free) def test_status_update_assigned(self): assign_perm('cases.can_send_to_client', UserFactory(is_staff=True), self.object) self.object.status_update() self.assertEqual(self.object.status, Case.STATUS.assigned) def test_status_update_closed(self): self.object.status = Case.STATUS.closed self.object.status_update() self.assertEqual(self.object.status, Case.STATUS.closed) self.object.status_update(reopen=True) self.assertEqual(self.object.status, Case.STATUS.free) def test_update_counters_last_received_default(self): self.object.update_counters() self.assertEqual(self.object.last_received, None) def _make_letter(self): # Hack for Travis o = LetterFactory(case=self.object, created_by__is_staff=False) return Letter.objects.get(pk=o.pk) def test_update_counters_last_received_setup(self): l = self._make_letter() self.object.update_counters() self.assertEqual(l.created_on, self.object.last_received) def test_update_counters_last_received_update(self): self._make_letter() self.object.update_counters() new = self._make_letter() self.object.update_counters() self.assertEqual(new.created_on, self.object.last_received)
def test_with_record_count(self): obj = CaseFactory() LetterFactory.create_batch(size=25, case=obj) self.assertEqual( Case.objects.filter( pk=obj.pk).with_record_count().get().record_count, 25)
def test_with_perm(self): CaseFactory.create_batch(size=25) with self.assertNumQueries(2): qs = Case.objects.with_perm().all() for obj in qs: list(obj.caseuserobjectpermission_set.all())
def test_for_assign_can_view_created(self): # perm set by signal self.assertTrue( Case.objects.for_assign(self.user).filter(pk=CaseFactory( client=self.user).pk).exists())
def test_for_user_can_view_all(self): assign_perm('cases.can_view_all', self.user) self.assertTrue( Case.objects.for_user( self.user).filter(pk=CaseFactory().pk).exists())
def setUp(self): self.object = CaseFactory()
def test_with_case_count(self): user = UserFactory() CaseFactory.create_batch(size=25, client=user) self.assertEqual(User.objects.with_case_count().get(pk=user.pk).case_count, 25) self.assertEqual(User.objects.with_case_count().get(pk=UserFactory().pk).case_count, 0)
def test_get_edit_url(self): self.assertEqual( CaseFactory(pk=50).get_edit_url(), '/sprawy/sprawa-50/edytuj/')
def setUp(self): self.object = CaseFactory() self.client.login(username=self.object.created_by.username, password='******')
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.case = CaseFactory()
class CaseTestCase(TestCase): def setUp(self): self.object = CaseFactory() def test_get_edit_url(self): self.assertEqual(CaseFactory(pk=50).get_edit_url(), '/sprawy/sprawa-50/edytuj/') @override_settings(PORADNIA_EMAIL_INPUT='case-(?P<pk>\d+)@example.com') def test_get_by_email(self): with self.assertRaises(Case.DoesNotExist): Case.get_by_email('*****@*****.**') Case.get_by_email('*****@*****.**') obj = CaseFactory(pk=22) obj2 = Case.get_by_email('*****@*****.**') self.assertEqual(obj, obj2) def test_perm_check(self): u1 = UserFactory() assign_perm('cases.can_view', u1) u2 = UserFactory() assign_perm('cases.can_view', u2, self.object) self.assertTrue(self.object.perm_check(u1, 'can_view')) self.assertTrue(self.object.perm_check(u2, 'can_view')) with self.assertRaises(PermissionDenied): self.object.perm_check(UserFactory(), 'can_view') def test_status_update_initial(self): self.assertEqual(self.object.status, Case.STATUS.free) def test_status_update_still_open(self): assign_perm('cases.can_send_to_client', UserFactory(is_staff=False), self.object) self.object.status_update() self.assertEqual(self.object.status, Case.STATUS.free) def test_status_update_assigned(self): assign_perm('cases.can_send_to_client', UserFactory(is_staff=True), self.object) self.object.status_update() self.assertEqual(self.object.status, Case.STATUS.assigned) def test_status_update_closed(self): self.object.status = Case.STATUS.closed self.object.status_update() self.assertEqual(self.object.status, Case.STATUS.closed) self.object.status_update(reopen=True) self.assertEqual(self.object.status, Case.STATUS.free) def test_update_counters_last_received_default(self): self.object.update_counters() self.assertEqual(self.object.last_received, None) def _make_letter(self): # Hack for Travis o = LetterFactory(case=self.object, created_by__is_staff=False) return Letter.objects.get(pk=o.pk) def test_update_counters_last_received_setup(self): l = self._make_letter() self.object.update_counters() self.assertEqual(l.created_on, self.object.last_received) def test_update_counters_last_received_update(self): self._make_letter() self.object.update_counters() new = self._make_letter() self.object.update_counters() self.assertEqual(new.created_on, self.object.last_received)
def setUp(self): self.user = UserFactory() self.object = CaseFactory()
def test_for_user_cant(self): self.assertFalse( Case.objects.for_user( self.user).filter(pk=CaseFactory().pk).exists())