Skip to content

Commit

Permalink
Reorganize API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Nov 29, 2024
1 parent b95b4cc commit 279e38a
Show file tree
Hide file tree
Showing 22 changed files with 5,559 additions and 5,356 deletions.
5,356 changes: 0 additions & 5,356 deletions temba/api/v2/tests.py

This file was deleted.

33 changes: 33 additions & 0 deletions temba/api/v2/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.urls import reverse

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


class APITest(APITestMixin, TembaTest):
BASE_SESSION_QUERIES = 4 # number of queries required for any request using session auth
BASE_TOKEN_QUERIES = 2 # number of queries required for any request using token auth

def upload_media(self, user, filename: str):
self.login(user)

with open(filename, "rb") as data:
response = self.client.post(
reverse("api.v2.media") + ".json", {"file": data}, HTTP_X_FORWARDED_HTTPS="https"
)
self.assertEqual(201, response.status_code)

return Media.objects.get(uuid=response.json()["uuid"])

def assertResultsById(self, response, expected):
self.assertEqual(response.status_code, 200)
self.assertEqual([r["id"] for r in response.json()["results"]], [o.pk for o in expected])

def assertResultsByUUID(self, response, expected):
self.assertEqual(response.status_code, 200)
self.assertEqual([r["uuid"] for r in response.json()["results"]], [str(o.uuid) for o in expected])

def assert404(self, response):
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json(), {"detail": "Not found."})
146 changes: 146 additions & 0 deletions temba/api/v2/tests/test_archives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from datetime import datetime

from django.urls import reverse

from temba.archives.models import Archive

from . import APITest


class ArchivesEndpointTest(APITest):
def test_endpoint(self):
endpoint_url = reverse("api.v2.archives") + ".json"

self.assertGetNotPermitted(endpoint_url, [None, self.user, self.agent])
self.assertPostNotAllowed(endpoint_url)
self.assertDeleteNotAllowed(endpoint_url)

# create some archives
Archive.objects.create(
org=self.org,
start_date=datetime(2017, 4, 5),
build_time=12,
record_count=34,
size=345,
hash="c4ca4238a0b923820dcc509a6f75849b",
archive_type=Archive.TYPE_MSG,
period=Archive.PERIOD_DAILY,
)
archive2 = Archive.objects.create(
org=self.org,
start_date=datetime(2017, 5, 5),
build_time=12,
record_count=34,
size=345,
hash="c81e728d9d4c2f636f067f89cc14862c",
archive_type=Archive.TYPE_MSG,
period=Archive.PERIOD_MONTHLY,
)
archive3 = Archive.objects.create(
org=self.org,
start_date=datetime(2017, 6, 5),
build_time=12,
record_count=34,
size=345,
hash="eccbc87e4b5ce2fe28308fd9f2a7baf3",
archive_type=Archive.TYPE_FLOWRUN,
period=Archive.PERIOD_DAILY,
)
archive4 = Archive.objects.create(
org=self.org,
start_date=datetime(2017, 7, 5),
build_time=12,
record_count=34,
size=345,
hash="a87ff679a2f3e71d9181a67b7542122c",
archive_type=Archive.TYPE_FLOWRUN,
period=Archive.PERIOD_MONTHLY,
)
# this archive has been rolled up and it should not be included in the API responses
Archive.objects.create(
org=self.org,
start_date=datetime(2017, 5, 1),
build_time=12,
record_count=34,
size=345,
hash="e4da3b7fbbce2345d7772b0674a318d5",
archive_type=Archive.TYPE_FLOWRUN,
period=Archive.PERIOD_DAILY,
rollup=archive2,
)

# create archive for other org
Archive.objects.create(
org=self.org2,
start_date=datetime(2017, 5, 1),
build_time=12,
record_count=34,
size=345,
hash="1679091c5a880faf6fb5e6087eb1b2dc",
archive_type=Archive.TYPE_FLOWRUN,
period=Archive.PERIOD_DAILY,
)

# there should be 4 archives in the response, because one has been rolled up
self.assertGet(
endpoint_url,
[self.editor],
results=[
{
"archive_type": "run",
"download_url": "",
"hash": "a87ff679a2f3e71d9181a67b7542122c",
"period": "monthly",
"record_count": 34,
"size": 345,
"start_date": "2017-07-05",
},
{
"archive_type": "run",
"download_url": "",
"hash": "eccbc87e4b5ce2fe28308fd9f2a7baf3",
"period": "daily",
"record_count": 34,
"size": 345,
"start_date": "2017-06-05",
},
{
"archive_type": "message",
"download_url": "",
"hash": "c81e728d9d4c2f636f067f89cc14862c",
"period": "monthly",
"record_count": 34,
"size": 345,
"start_date": "2017-05-05",
},
{
"archive_type": "message",
"download_url": "",
"hash": "c4ca4238a0b923820dcc509a6f75849b",
"period": "daily",
"record_count": 34,
"size": 345,
"start_date": "2017-04-05",
},
],
num_queries=self.BASE_SESSION_QUERIES + 2,
)

self.assertGet(endpoint_url + "?after=2017-05-01", [self.editor], results=[archive4, archive3, archive2])
self.assertGet(endpoint_url + "?after=2017-05-01&archive_type=run", [self.editor], results=[archive4, archive3])

# unknown archive type
self.assertGet(endpoint_url + "?archive_type=invalid", [self.editor], results=[])

# only for dailies
self.assertGet(
endpoint_url + "?after=2017-05-01&archive_type=run&period=daily", [self.editor], results=[archive3]
)

# only for monthlies
self.assertGet(endpoint_url + "?period=monthly", [self.editor], results=[archive4, archive2])

# test access from a user with no org
self.login(self.non_org_user)
response = self.client.get(endpoint_url)
self.assertEqual(403, response.status_code)
142 changes: 142 additions & 0 deletions temba/api/v2/tests/test_boundaries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from django.contrib.gis.geos import GEOSGeometry
from django.urls import reverse

from temba.locations.models import BoundaryAlias
from temba.tests import matchers

from . import APITest


class BoundariesEndpointTest(APITest):
def test_endpoint(self):
endpoint_url = reverse("api.v2.boundaries") + ".json"

self.assertGetNotPermitted(endpoint_url, [None])
self.assertPostNotAllowed(endpoint_url)
self.assertDeleteNotAllowed(endpoint_url)

self.setUpLocations()

BoundaryAlias.create(self.org, self.admin, self.state1, "Kigali")
BoundaryAlias.create(self.org, self.admin, self.state2, "East Prov")
BoundaryAlias.create(self.org2, self.admin2, self.state1, "Other Org") # shouldn't be returned

self.state1.simplified_geometry = GEOSGeometry("MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)))")
self.state1.save()

# test without geometry
self.assertGet(
endpoint_url,
[self.user, self.editor, self.admin],
results=[
{
"osm_id": "1708283",
"name": "Kigali City",
"parent": {"osm_id": "171496", "name": "Rwanda"},
"level": 1,
"aliases": ["Kigali", "Kigari"],
"geometry": None,
},
{
"osm_id": "171113181",
"name": "Kageyo",
"parent": {"osm_id": "R1711131", "name": "Gatsibo"},
"level": 3,
"aliases": [],
"geometry": None,
},
{
"osm_id": "1711142",
"name": "Rwamagana",
"parent": {"osm_id": "171591", "name": "Eastern Province"},
"level": 2,
"aliases": [],
"geometry": None,
},
{
"osm_id": "1711163",
"name": "Kay\u00f4nza",
"parent": {"osm_id": "171591", "name": "Eastern Province"},
"level": 2,
"aliases": [],
"geometry": None,
},
{
"osm_id": "171116381",
"name": "Kabare",
"parent": {"osm_id": "1711163", "name": "Kay\u00f4nza"},
"level": 3,
"aliases": [],
"geometry": None,
},
{"osm_id": "171496", "name": "Rwanda", "parent": None, "level": 0, "aliases": [], "geometry": None},
{
"osm_id": "171591",
"name": "Eastern Province",
"parent": {"osm_id": "171496", "name": "Rwanda"},
"level": 1,
"aliases": ["East Prov"],
"geometry": None,
},
{
"osm_id": "3963734",
"name": "Nyarugenge",
"parent": {"osm_id": "1708283", "name": "Kigali City"},
"level": 2,
"aliases": [],
"geometry": None,
},
{
"osm_id": "R1711131",
"name": "Gatsibo",
"parent": {"osm_id": "171591", "name": "Eastern Province"},
"level": 2,
"aliases": [],
"geometry": None,
},
{
"osm_id": "VMN.49.1_1",
"name": "Bukure",
"parent": {"osm_id": "1711142", "name": "Rwamagana"},
"level": 3,
"aliases": [],
"geometry": None,
},
],
num_queries=self.BASE_SESSION_QUERIES + 3,
)

# test with geometry
self.assertGet(
endpoint_url + "?geometry=true",
[self.admin],
results=[
{
"osm_id": "1708283",
"name": "Kigali City",
"parent": {"osm_id": "171496", "name": "Rwanda"},
"level": 1,
"aliases": ["Kigali", "Kigari"],
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[1.0, 1.0], [1.0, -1.0], [-1.0, -1.0], [-1.0, 1.0], [1.0, 1.0]]]],
},
},
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
matchers.Dict(),
],
num_queries=self.BASE_SESSION_QUERIES + 3,
)

# if org doesn't have a country, just return no results
self.org.country = None
self.org.save(update_fields=("country",))

self.assertGet(endpoint_url, [self.admin], results=[])
Loading

0 comments on commit 279e38a

Please sign in to comment.