-
-
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.
feat: add management command
create_client
(#31)
- Loading branch information
1 parent
6e2f2bc
commit 5732ae5
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from django.core.management.base import BaseCommand, CommandError | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from durin.serializers import ClientSerializer | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Creates a new client with the given values." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"name", type=str, help=_("A unique identification name for the client.") | ||
) | ||
parser.add_argument( | ||
"--token-ttl", | ||
type=str, | ||
default="", | ||
help=_( | ||
"Token Time To Live (TTL) in timedelta. " | ||
"Format: <code>DAYS HH:MM:SS</code>." | ||
), | ||
) | ||
parser.add_argument( | ||
"--throttle-rate", | ||
type=str, | ||
default="", | ||
help=_( | ||
"Follows the same format as DRF's throttle rates. " | ||
"Format: <em>'number_of_requests/period'</em> " | ||
"where period should be one of: ('s', 'm', 'h', 'd'). " | ||
"Example: '100/h' implies 100 requests each hour." | ||
), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
client = ClientSerializer( | ||
data={ | ||
"name": options["name"], | ||
"token_ttl": options["token_ttl"], | ||
"throttle_rate": options["throttle_rate"], | ||
} | ||
) | ||
if client.is_valid(): | ||
client.save() | ||
self.stdout.write( | ||
self.style.SUCCESS("Client {} created!".format(client.data.get("name"))) | ||
) | ||
return | ||
raise CommandError( | ||
( | ||
"Can't create client!\n" | ||
+ "\n".join( | ||
( | ||
"{field}: {errors}".format(field=field, errors=" ".join(errors)) | ||
for field, errors in client.errors.items() | ||
) | ||
) | ||
) | ||
) |
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,57 @@ | ||
from io import StringIO | ||
|
||
from django.core import management | ||
from django.core.management import CommandError | ||
from django.test import TestCase | ||
|
||
from durin.models import Client | ||
|
||
|
||
class ClientCommandTestCase(TestCase): | ||
@staticmethod | ||
def call_command(*args, **kwargs): | ||
out = StringIO() | ||
management.call_command( | ||
"create_client", *args, stdout=out, stderr=StringIO(), **kwargs | ||
) | ||
return out.getvalue() | ||
|
||
def test__create_client__valid_name(self): | ||
client_name = "web" | ||
self.assertEqual(Client.objects.count(), 0) | ||
|
||
out = self.call_command(client_name) | ||
|
||
self.assertEqual(out, "Client {} created!\n".format(client_name)) | ||
self.assertEqual(Client.objects.count(), 1) | ||
|
||
def test__create_client__valid_data(self): | ||
client_name = "web" | ||
ttl = "14 00:00" | ||
throttle_rate = "1/s" | ||
self.assertEqual(Client.objects.count(), 0) | ||
|
||
out = self.call_command(client_name, token_ttl=ttl, throttle_rate=throttle_rate) | ||
|
||
self.assertEqual(out, "Client {} created!\n".format(client_name)) | ||
self.assertEqual(Client.objects.count(), 1) | ||
|
||
def test__create_client__name_not_set_raises_exc(self): | ||
with self.assertRaises(CommandError): | ||
self.call_command() | ||
|
||
def test_create_client__blank_name_raises_exc(self): | ||
with self.assertRaisesMessage( | ||
CommandError, "name: This field may not be blank." | ||
): | ||
self.call_command("") | ||
|
||
def test_create_client__invalid_ttl_raises_exc(self): | ||
with self.assertRaisesMessage( | ||
CommandError, | ||
( | ||
"token_ttl: Duration has wrong format. " | ||
"Use one of these formats instead: [DD] [HH:[MM:]]ss[.uuuuuu]." | ||
), | ||
): | ||
self.call_command("web", token_ttl="invalid") |