-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_commands.py
127 lines (105 loc) · 4.08 KB
/
test_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from io import StringIO
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase
from oauth2_provider.models import get_application_model
Application = get_application_model()
class CreateApplicationTest(TestCase):
def test_command_creates_application(self):
output = StringIO()
self.assertEqual(Application.objects.count(), 0)
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
stdout=output,
)
self.assertEqual(Application.objects.count(), 1)
self.assertIn("New application created successfully", output.getvalue())
def test_missing_required_args(self):
self.assertEqual(Application.objects.count(), 0)
with self.assertRaises(CommandError) as ctx:
call_command(
"createapplication",
"--redirect-uris=http://example.com http://example2.com",
)
self.assertIn("client_type", ctx.exception.args[0])
self.assertIn("authorization_grant_type", ctx.exception.args[0])
self.assertEqual(Application.objects.count(), 0)
def test_command_creates_application_with_skipped_auth(self):
self.assertEqual(Application.objects.count(), 0)
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--skip-authorization",
)
app = Application.objects.get()
self.assertTrue(app.skip_authorization)
def test_application_created_normally_with_no_skipped_auth(self):
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
)
app = Application.objects.get()
self.assertFalse(app.skip_authorization)
def test_application_created_with_name(self):
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--name=TEST",
)
app = Application.objects.get()
self.assertEqual(app.name, "TEST")
def test_application_created_with_client_secret(self):
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--client-secret=SECRET",
)
app = Application.objects.get()
self.assertEqual(app.client_secret, "SECRET")
def test_application_created_with_client_id(self):
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--client-id=someId",
)
app = Application.objects.get()
self.assertEqual(app.client_id, "someId")
def test_application_created_with_user(self):
User = get_user_model()
user = User.objects.create()
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--user=%s" % user.pk,
)
app = Application.objects.get()
self.assertEqual(app.user, user)
def test_validation_failed_message(self):
output = StringIO()
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--user=783",
stdout=output,
)
self.assertIn("user", output.getvalue())
self.assertIn("783", output.getvalue())
self.assertIn("does not exist", output.getvalue())