Skip to content

Commit

Permalink
Reorganize tests in the orgs and tickets apps
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 20, 2024
1 parent 83f1541 commit 4f7719f
Show file tree
Hide file tree
Showing 31 changed files with 6,369 additions and 6,274 deletions.
1,905 changes: 0 additions & 1,905 deletions temba/orgs/tests.py

This file was deleted.

Empty file added temba/orgs/tests/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions temba/orgs/tests/test_backuptoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from temba.orgs.models import BackupToken
from temba.tests import TembaTest


class BackupTokenTest(TembaTest):
def test_model(self):
admin_tokens = BackupToken.generate_for_user(self.admin)
BackupToken.generate_for_user(self.editor)

self.assertEqual(10, len(admin_tokens))
self.assertEqual(10, self.admin.backup_tokens.count())
self.assertEqual(10, self.editor.backup_tokens.count())
self.assertEqual(str(admin_tokens[0].token), str(admin_tokens[0]))

# regenerate tokens for admin user
new_admin_tokens = BackupToken.generate_for_user(self.admin)
self.assertEqual(10, len(new_admin_tokens))
self.assertNotEqual([t.token for t in admin_tokens], [t.token for t in new_admin_tokens])
self.assertEqual(10, self.admin.backup_tokens.count())
688 changes: 688 additions & 0 deletions temba/orgs/tests/test_definitionexport.py

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions temba/orgs/tests/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from datetime import date, timedelta

from django.core.files.storage import default_storage
from django.utils import timezone

from temba.orgs.models import Export
from temba.orgs.tasks import trim_exports
from temba.tests import TembaTest
from temba.tickets.models import TicketExport


class ExportTest(TembaTest):
def test_trim_task(self):
export1 = TicketExport.create(
self.org, self.admin, start_date=date.today() - timedelta(days=7), end_date=date.today(), with_fields=()
)
export2 = TicketExport.create(
self.org, self.admin, start_date=date.today() - timedelta(days=7), end_date=date.today(), with_fields=()
)
export1.perform()
export2.perform()

self.assertTrue(default_storage.exists(export1.path))
self.assertTrue(default_storage.exists(export2.path))

# make export 1 look old
export1.created_on = timezone.now() - timedelta(days=100)
export1.save(update_fields=("created_on",))

trim_exports()

self.assertFalse(Export.objects.filter(id=export1.id).exists())
self.assertTrue(Export.objects.filter(id=export2.id).exists())

self.assertFalse(default_storage.exists(export1.path))
self.assertTrue(default_storage.exists(export2.path))
70 changes: 70 additions & 0 deletions temba/orgs/tests/test_invitation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from datetime import timedelta

from django.core import mail
from django.utils import timezone

from temba.orgs.models import Invitation, OrgRole, User
from temba.orgs.tasks import expire_invitations
from temba.tests import TembaTest
from temba.tickets.models import Team


class InvitationTest(TembaTest):
def test_model(self):
invitation = Invitation.create(self.org, self.admin, "[email protected]", OrgRole.EDITOR)

self.assertEqual(OrgRole.EDITOR, invitation.role)

invitation.send()

self.assertEqual(1, len(mail.outbox))
self.assertEqual(["[email protected]"], mail.outbox[0].recipients())
self.assertEqual("RapidPro Invitation", mail.outbox[0].subject)
self.assertIn(f"https://app.rapidpro.io/org/join/{invitation.secret}/", mail.outbox[0].body)

new_editor = User.create("[email protected]", "Bob", "", "Qwerty123", "en-US")
invitation.accept(new_editor)

self.assertEqual(1, self.admin.notifications.count())
self.assertFalse(invitation.is_active)
self.assertEqual({self.editor, new_editor}, set(self.org.get_users(roles=[OrgRole.EDITOR])))

# invite an agent user to a specific team
sales = Team.create(self.org, self.admin, "Sales", topics=[])
invitation = Invitation.create(self.org, self.admin, "[email protected]", OrgRole.AGENT, team=sales)

self.assertEqual(OrgRole.AGENT, invitation.role)
self.assertEqual(sales, invitation.team)

invitation.send()
new_agent = User.create("[email protected]", "Bob", "", "Qwerty123", "en-US")
invitation.accept(new_agent)

self.assertEqual({self.agent, new_agent}, set(self.org.get_users(roles=[OrgRole.AGENT])))
self.assertEqual({new_agent}, set(sales.get_users()))

def test_expire_task(self):
invitation1 = Invitation.objects.create(
org=self.org,
role_code="E",
email="[email protected]",
created_by=self.admin,
created_on=timezone.now() - timedelta(days=31),
modified_by=self.admin,
)
invitation2 = Invitation.objects.create(
org=self.org,
role_code="T",
email="[email protected]",
created_by=self.admin,
created_on=timezone.now() - timedelta(days=29),
modified_by=self.admin,
)

expire_invitations()

invitation1.refresh_from_db()
invitation2.refresh_from_db()

self.assertFalse(invitation1.is_active)
self.assertTrue(invitation2.is_active)
24 changes: 24 additions & 0 deletions temba/orgs/tests/test_itemcount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from temba.orgs.tasks import squash_item_counts
from temba.tests import TembaTest


class ItemCountTest(TembaTest):
def test_model(self):
self.org.counts.create(scope="foo:1", count=2)
self.org.counts.create(scope="foo:1", count=3)
self.org.counts.create(scope="foo:2", count=1)
self.org.counts.create(scope="foo:3", count=4)
self.org2.counts.create(scope="foo:4", count=1)
self.org2.counts.create(scope="foo:4", count=1)

self.assertEqual(9, self.org.counts.filter(scope__in=("foo:1", "foo:3")).sum())
self.assertEqual(10, self.org.counts.prefix("foo:").sum())
self.assertEqual(4, self.org.counts.count())

squash_item_counts()

self.assertEqual(9, self.org.counts.filter(scope__in=("foo:1", "foo:3")).sum())
self.assertEqual(10, self.org.counts.prefix("foo:").sum())
self.assertEqual(3, self.org.counts.count())

self.org.counts.all().delete()
Loading

0 comments on commit 4f7719f

Please sign in to comment.