forked from rapidpro/rapidpro
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a3c21c
commit e3e07b2
Showing
36 changed files
with
1,061 additions
and
1,068 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.