Skip to content

Commit 7e7cb62

Browse files
Merge pull request #646 from maykinmedia/feature/573-Add-tests-for-generate_token.py
✅ [#573] Add tests for generate_token.py
2 parents d1f6fde + 7097248 commit 7e7cb62

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,9 @@ section-order = [
147147
line-ending = "lf"
148148

149149
[tool.ruff.lint.flake8-tidy-imports.banned-api]
150-
"logging".msg = "Use `structlog.stdlib.get_logger(__name__)` instead of the stdlib logging module."
150+
"logging".msg = "Use `structlog.stdlib.get_logger(__name__)` instead of the stdlib logging module."
151+
152+
[tool.coverage.run]
153+
branch = true
154+
source = ["src"]
155+
omit = ["*/test_*.py"]

requirements/ci.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ commonground-api-common==2.7.0
117117
# -r requirements/base.txt
118118
commonmark==0.9.1
119119
# via recommonmark
120-
coverage==4.5.4
120+
coverage==7.10.1
121121
# via
122122
# -r requirements/test-tools.in
123123
# codecov

requirements/dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ commonmark==0.9.1
148148
# -c requirements/ci.txt
149149
# -r requirements/ci.txt
150150
# recommonmark
151-
coverage==4.5.4
151+
coverage==7.10.1
152152
# via
153153
# -c requirements/ci.txt
154154
# -r requirements/ci.txt

requirements/test-tools.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
codecov
66
pytest
77
pytest-benchmark
8-
coverage < 5.0
8+
coverage
99
django-webtest
1010
factory-boy
1111
freezegun
@@ -15,4 +15,4 @@ tblib
1515
vcrpy
1616

1717
# Code formatting
18-
ruff
18+
ruff
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from io import StringIO
2+
3+
from django.core.management import CommandError, call_command
4+
from django.test import TestCase
5+
6+
from objects.token.models import TokenAuth
7+
8+
9+
class GenerateTokenCommandTests(TestCase):
10+
def test_token_created_with_required_args(self):
11+
out = StringIO()
12+
contact_person = "John Doe"
13+
14+
15+
call_command(
16+
"generate_token",
17+
contact_person,
18+
email,
19+
stdout=out,
20+
)
21+
22+
self.assertEqual(TokenAuth.objects.count(), 1)
23+
token = TokenAuth.objects.first()
24+
self.assertEqual(token.contact_person, contact_person)
25+
self.assertEqual(token.email, email)
26+
self.assertEqual(token.organization, "")
27+
self.assertIn("Token", out.getvalue())
28+
29+
def test_token_created_with_all_optional_args(self):
30+
out = StringIO()
31+
call_command(
32+
"generate_token",
33+
"Alice Smith",
34+
35+
"--organization",
36+
"Acme Inc",
37+
"--application",
38+
"WeatherApp",
39+
"--administration",
40+
"City Council",
41+
stdout=out,
42+
)
43+
44+
token = TokenAuth.objects.get(email="[email protected]")
45+
self.assertEqual(token.contact_person, "Alice Smith")
46+
self.assertEqual(token.organization, "Acme Inc")
47+
self.assertEqual(token.application, "WeatherApp")
48+
self.assertEqual(token.administration, "City Council")
49+
self.assertIn("Token", out.getvalue())
50+
51+
def test_missing_required_arguments(self):
52+
with self.assertRaises(CommandError):
53+
call_command("generate_token")
54+
55+
def test_stdout_contains_token(self):
56+
out = StringIO()
57+
call_command("generate_token", "Bob", "[email protected]", stdout=out)
58+
output = out.getvalue()
59+
self.assertIn("Token", output)
60+
self.assertIn(TokenAuth.objects.first().token, output)
61+
62+
def test_duplicate_email_allowed_with_unique_identifier(self):
63+
TokenAuth.objects.create(
64+
contact_person="Bob", email="[email protected]", identifier="unique-1"
65+
)
66+
out = StringIO()
67+
call_command("generate_token", "Bob", "[email protected]", stdout=out)
68+
69+
self.assertEqual(TokenAuth.objects.filter(email="[email protected]").count(), 2)

0 commit comments

Comments
 (0)