Skip to content

Commit

Permalink
Reorganize tests in more apps
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 19, 2024
1 parent 2a3c21c commit e3e07b2
Show file tree
Hide file tree
Showing 36 changed files with 1,061 additions and 1,068 deletions.
2 changes: 1 addition & 1 deletion temba/airtime/migrations/0030_populate_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from temba.utils.uuid import uuid4


def populate_uuid(apps, schema_editor):
def populate_uuid(apps, schema_editor): # pragma: no cover
AirtimeTransfer = apps.get_model("airtime", "AirtimeTransfer")

tranfers = AirtimeTransfer.objects.filter(uuid=None)
Expand Down
Empty file added temba/airtime/tests/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import reverse

from temba.airtime.models import AirtimeTransfer
from temba.tests import CRUDLTestMixin, MigrationTest, TembaTest
from temba.tests import CRUDLTestMixin, TembaTest


class AirtimeCRUDLTest(TembaTest, CRUDLTestMixin):
Expand Down Expand Up @@ -72,28 +72,3 @@ def test_read(self):
self.assertContains(response, "Ben Haggerty")
self.assertNotContains(response, "+250 700 000 003")
self.assertFalse(response.context["show_logs"])


class AirtimeUUIDTest(MigrationTest):
app = "airtime"
migrate_from = "0029_airtimetransfer_external_id"
migrate_to = "0030_populate_uuid"

def setUpBeforeMigration(self, apps):
contact = self.create_contact("Eric", phone="+250788382382")
self.airtime1 = AirtimeTransfer.objects.create(
uuid=None,
org=self.org,
status="S",
contact=contact,
recipient="tel:+250788382382",
desired_amount=1.00,
actual_amount=1.00,
)

self.assertIsNone(self.airtime1.uuid)

def test_migration(self):
self.airtime1.refresh_from_db()

self.assertIsNotNone(self.airtime1.uuid)
2 changes: 1 addition & 1 deletion temba/api/internal/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import reverse
from django.utils import timezone

from temba.api.tests import APITestMixin
from temba.api.tests.mixins import APITestMixin
from temba.contacts.models import ContactExport
from temba.notifications.types import ExportFinishedNotificationType
from temba.templates.models import TemplateTranslation
Expand Down
Empty file added temba/api/tests/__init__.py
Empty file.
128 changes: 1 addition & 127 deletions temba/api/tests.py → temba/api/tests/mixins.py
Original file line number Diff line number Diff line change
@@ -1,132 +1,6 @@
from datetime import timedelta

from django.contrib.auth.models import Group
from django.db import connection
from django.test import override_settings
from django.urls import reverse
from django.utils import timezone

from temba.orgs.models import OrgRole
from temba.tests import CRUDLTestMixin, TembaTest

from .models import APIToken, Resthook, WebHookEvent
from .tasks import trim_webhook_events, update_tokens_used


class APITokenTest(TembaTest):
def setUp(self):
super().setUp()

self.admins_group = Group.objects.get(name="Administrators")
self.editors_group = Group.objects.get(name="Editors")

self.org2.add_user(self.admin, OrgRole.EDITOR) # our admin can act as editor for other org

def test_create(self):
token1 = APIToken.create(self.org, self.admin)
self.assertEqual(self.org, token1.org)
self.assertEqual(self.admin, token1.user)
self.assertTrue(token1.key)
self.assertEqual(str(token1), token1.key)

# can create another token for same user
token2 = APIToken.create(self.org, self.admin)
self.assertNotEqual(token1, token2)
self.assertNotEqual(token1.key, token2.key)

# can't create tokens for viewer or agent users
self.assertRaises(AssertionError, APIToken.create, self.org, self.agent)
self.assertRaises(AssertionError, APIToken.create, self.org, self.user)

def test_record_used(self):
token1 = APIToken.create(self.org, self.admin)
token2 = APIToken.create(self.org2, self.admin2)

token1.record_used()

update_tokens_used()

token1.refresh_from_db()
token2.refresh_from_db()

self.assertIsNotNone(token1.last_used_on)
self.assertIsNone(token2.last_used_on)


class APITokenCRUDLTest(CRUDLTestMixin, TembaTest):
def test_list(self):
tokens_url = reverse("api.apitoken_list")

self.assertRequestDisallowed(tokens_url, [None, self.user, self.agent])
self.assertListFetch(tokens_url, [self.admin], context_objects=[])

# add user to other org and create API tokens for both
self.org2.add_user(self.admin, OrgRole.EDITOR)
token1 = APIToken.create(self.org, self.admin)
token2 = APIToken.create(self.org, self.admin)
APIToken.create(self.org, self.editor) # other user
APIToken.create(self.org2, self.admin) # other org

response = self.assertListFetch(tokens_url, [self.admin], context_objects=[token1, token2], choose_org=self.org)
self.assertContentMenu(tokens_url, self.admin, ["New"], choose_org=self.org)

# can POST to create new token
response = self.client.post(tokens_url, {})
self.assertRedirect(response, tokens_url)
self.assertEqual(3, self.admin.get_api_tokens(self.org).count())
token3 = self.admin.get_api_tokens(self.org).order_by("created").last()

# and now option to create new token is gone because we've reached the limit
response = self.assertListFetch(
tokens_url, [self.admin], context_objects=[token1, token2, token3], choose_org=self.org
)
self.assertContentMenu(tokens_url, self.admin, [], choose_org=self.org)

# and POSTing is noop
response = self.client.post(tokens_url, {})
self.assertRedirect(response, tokens_url)
self.assertEqual(3, self.admin.get_api_tokens(self.org).count())

def test_delete(self):
token1 = APIToken.create(self.org, self.admin)
token2 = APIToken.create(self.org, self.editor)

delete_url = reverse("api.apitoken_delete", args=[token1.key])

self.assertRequestDisallowed(delete_url, [self.editor, self.admin2])

response = self.assertDeleteFetch(delete_url, [self.admin], as_modal=True)
self.assertContains(response, f"You are about to delete the API token <b>{token1.key[:6]}…</b>")

response = self.assertDeleteSubmit(delete_url, self.admin, object_deactivated=token1)
self.assertRedirect(response, "/apitoken/")

token1.refresh_from_db()
token2.refresh_from_db()

self.assertFalse(token1.is_active)
self.assertTrue(token2.is_active)


class WebHookTest(TembaTest):
def test_trim_events_and_results(self):
five_hours_ago = timezone.now() - timedelta(hours=5)

# create some events
resthook = Resthook.get_or_create(org=self.org, slug="registration", user=self.admin)
WebHookEvent.objects.create(org=self.org, resthook=resthook, data={}, created_on=five_hours_ago)

with override_settings(RETENTION_PERIODS={"webhookevent": None}):
trim_webhook_events()
self.assertTrue(WebHookEvent.objects.all())

with override_settings(RETENTION_PERIODS={"webhookevent": timedelta(hours=12)}): # older than our event
trim_webhook_events()
self.assertTrue(WebHookEvent.objects.all())

with override_settings(RETENTION_PERIODS={"webhookevent": timedelta(hours=2)}):
trim_webhook_events()
self.assertFalse(WebHookEvent.objects.all())
from temba.api.models import APIToken


class APITestMixin:
Expand Down
46 changes: 46 additions & 0 deletions temba/api/tests/test_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.contrib.auth.models import Group

from temba.api.models import APIToken
from temba.api.tasks import update_tokens_used
from temba.orgs.models import OrgRole
from temba.tests import TembaTest


class APITokenTest(TembaTest):
def setUp(self):
super().setUp()

self.admins_group = Group.objects.get(name="Administrators")
self.editors_group = Group.objects.get(name="Editors")

self.org2.add_user(self.admin, OrgRole.EDITOR) # our admin can act as editor for other org

def test_create(self):
token1 = APIToken.create(self.org, self.admin)
self.assertEqual(self.org, token1.org)
self.assertEqual(self.admin, token1.user)
self.assertTrue(token1.key)
self.assertEqual(str(token1), token1.key)

# can create another token for same user
token2 = APIToken.create(self.org, self.admin)
self.assertNotEqual(token1, token2)
self.assertNotEqual(token1.key, token2.key)

# can't create tokens for viewer or agent users
self.assertRaises(AssertionError, APIToken.create, self.org, self.agent)
self.assertRaises(AssertionError, APIToken.create, self.org, self.user)

def test_record_used(self):
token1 = APIToken.create(self.org, self.admin)
token2 = APIToken.create(self.org2, self.admin2)

token1.record_used()

update_tokens_used()

token1.refresh_from_db()
token2.refresh_from_db()

self.assertIsNotNone(token1.last_used_on)
self.assertIsNone(token2.last_used_on)
60 changes: 60 additions & 0 deletions temba/api/tests/test_tokencrudl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from django.urls import reverse

from temba.api.models import APIToken
from temba.orgs.models import OrgRole
from temba.tests import CRUDLTestMixin, TembaTest


class APITokenCRUDLTest(CRUDLTestMixin, TembaTest):
def test_list(self):
tokens_url = reverse("api.apitoken_list")

self.assertRequestDisallowed(tokens_url, [None, self.user, self.agent])
self.assertListFetch(tokens_url, [self.admin], context_objects=[])

# add user to other org and create API tokens for both
self.org2.add_user(self.admin, OrgRole.EDITOR)
token1 = APIToken.create(self.org, self.admin)
token2 = APIToken.create(self.org, self.admin)
APIToken.create(self.org, self.editor) # other user
APIToken.create(self.org2, self.admin) # other org

response = self.assertListFetch(tokens_url, [self.admin], context_objects=[token1, token2], choose_org=self.org)
self.assertContentMenu(tokens_url, self.admin, ["New"], choose_org=self.org)

# can POST to create new token
response = self.client.post(tokens_url, {})
self.assertRedirect(response, tokens_url)
self.assertEqual(3, self.admin.get_api_tokens(self.org).count())
token3 = self.admin.get_api_tokens(self.org).order_by("created").last()

# and now option to create new token is gone because we've reached the limit
response = self.assertListFetch(
tokens_url, [self.admin], context_objects=[token1, token2, token3], choose_org=self.org
)
self.assertContentMenu(tokens_url, self.admin, [], choose_org=self.org)

# and POSTing is noop
response = self.client.post(tokens_url, {})
self.assertRedirect(response, tokens_url)
self.assertEqual(3, self.admin.get_api_tokens(self.org).count())

def test_delete(self):
token1 = APIToken.create(self.org, self.admin)
token2 = APIToken.create(self.org, self.editor)

delete_url = reverse("api.apitoken_delete", args=[token1.key])

self.assertRequestDisallowed(delete_url, [self.editor, self.admin2])

response = self.assertDeleteFetch(delete_url, [self.admin], as_modal=True)
self.assertContains(response, f"You are about to delete the API token <b>{token1.key[:6]}…</b>")

response = self.assertDeleteSubmit(delete_url, self.admin, object_deactivated=token1)
self.assertRedirect(response, "/apitoken/")

token1.refresh_from_db()
token2.refresh_from_db()

self.assertFalse(token1.is_active)
self.assertTrue(token2.is_active)
29 changes: 29 additions & 0 deletions temba/api/tests/test_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import timedelta

from django.test import override_settings
from django.utils import timezone

from temba.api.models import Resthook, WebHookEvent
from temba.api.tasks import trim_webhook_events
from temba.tests import TembaTest


class WebHookTest(TembaTest):
def test_trim_events_and_results(self):
five_hours_ago = timezone.now() - timedelta(hours=5)

# create some events
resthook = Resthook.get_or_create(org=self.org, slug="registration", user=self.admin)
WebHookEvent.objects.create(org=self.org, resthook=resthook, data={}, created_on=five_hours_ago)

with override_settings(RETENTION_PERIODS={"webhookevent": None}):
trim_webhook_events()
self.assertTrue(WebHookEvent.objects.all())

with override_settings(RETENTION_PERIODS={"webhookevent": timedelta(hours=12)}): # older than our event
trim_webhook_events()
self.assertTrue(WebHookEvent.objects.all())

with override_settings(RETENTION_PERIODS={"webhookevent": timedelta(hours=2)}):
trim_webhook_events()
self.assertFalse(WebHookEvent.objects.all())
2 changes: 1 addition & 1 deletion temba/api/v2/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import reverse

from temba.api.tests import APITestMixin
from temba.api.tests.mixins import APITestMixin
from temba.msgs.models import Media
from temba.tests import TembaTest

Expand Down
Empty file.
Loading

0 comments on commit e3e07b2

Please sign in to comment.