-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…modules (#17)
- Loading branch information
Showing
14 changed files
with
282 additions
and
76 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
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
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
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,13 @@ | ||
from durin import permissions | ||
|
||
TEST_CLIENT_NAME = "web-browser-client-test" | ||
|
||
|
||
class CustomAllowSpecificClients(permissions.AllowSpecificClients): | ||
|
||
allowed_clients_name = (TEST_CLIENT_NAME,) | ||
|
||
|
||
class CustomDisallowSpecificClients(permissions.DisallowSpecificClients): | ||
|
||
disallowed_clients_name = (TEST_CLIENT_NAME,) |
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
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,64 @@ | ||
from importlib import reload | ||
|
||
from django.db import reset_queries | ||
from django.test import override_settings | ||
from django.urls import reverse | ||
from rest_framework.test import APIRequestFactory | ||
|
||
from durin import auth | ||
from durin.models import AuthToken, Client | ||
from durin.settings import durin_settings | ||
|
||
from . import CustomTestCase | ||
|
||
root_url = reverse("api-root") | ||
|
||
new_settings = durin_settings.defaults.copy() | ||
|
||
|
||
class AuthTestCase(CustomTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
# authenticate client | ||
self.token_instance = AuthToken.objects.create(self.user, self.authclient) | ||
self.client.credentials( | ||
HTTP_AUTHORIZATION=("Token %s" % self.token_instance.token) | ||
) | ||
# reset queries | ||
reset_queries() | ||
self.assertNumQueries(0, msg="Queries were reset") | ||
|
||
def test_authtoken_lookup_1_sql_query(self): | ||
with self.assertNumQueries( | ||
1, | ||
msg="Since we use ``select_related`` it should take only 1 query", | ||
): | ||
resp = self.client.get(root_url) | ||
self.assertEqual(resp.status_code, 200) | ||
|
||
def test_authtoken_lookup_2_sql_query(self): | ||
# override settings | ||
new_settings["AUTHTOKEN_SELECT_RELATED_LIST"] = False | ||
with override_settings(REST_DURIN=new_settings): | ||
reload(auth) | ||
with self.assertNumQueries( | ||
2, | ||
msg="Since we didn't use ``select_related`` it should take 2 queries", | ||
): | ||
resp = self.client.get(root_url) | ||
self.assertEqual(resp.status_code, 200) | ||
|
||
def test_update_token_key(self): | ||
self.assertEqual(AuthToken.objects.count(), 1) | ||
self.assertEqual(Client.objects.count(), 1) | ||
rf = APIRequestFactory() | ||
request = rf.get("/") | ||
request.META = { | ||
"HTTP_AUTHORIZATION": "Token {}".format(self.token_instance.token) | ||
} | ||
(auth_user, auth_token) = auth.TokenAuthentication().authenticate(request) | ||
self.assertEqual( | ||
self.token_instance.token, | ||
auth_token.token, | ||
) | ||
self.assertEqual(self.user, auth_user) |
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,45 @@ | ||
from django.core.exceptions import ValidationError as DjValidationError | ||
from django.test import TestCase | ||
|
||
from durin.models import Client | ||
|
||
|
||
class ClientTestCase(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.client_names = ["web", "mobile", "cli"] | ||
return super().setUpClass() | ||
|
||
def test_create_clients(self): | ||
Client.objects.all().delete() | ||
self.assertEqual(Client.objects.count(), 0) | ||
for name in self.client_names: | ||
Client.objects.create(name=name) | ||
self.assertEqual(Client.objects.count(), len(self.client_names)) | ||
|
||
def test_throttle_rate_validation_ok(self): | ||
testclient = Client.objects.create( | ||
name="test_throttle_rate_validation", throttle_rate="2/m" | ||
) | ||
testclient.full_clean() | ||
|
||
self.assertIsNotNone(testclient.pk) | ||
self.assertIsNotNone(testclient.token_ttl) | ||
self.assertIsNotNone(testclient.throttle_rate) | ||
|
||
def test_throttle_rate_validation_raises_exc(self): | ||
|
||
with self.assertRaises(DjValidationError): | ||
testclient1 = Client.objects.create( | ||
name="testclient1", throttle_rate="blahblah" | ||
) | ||
testclient1.full_clean() | ||
testclient1.delete() | ||
|
||
with self.assertRaises(DjValidationError): | ||
testclient2 = Client.objects.create( | ||
name="testclient2", | ||
throttle_rate="2/minute", | ||
) | ||
testclient2.full_clean() | ||
testclient2.delete() |
Oops, something went wrong.