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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
from io import StringIO
import pytest
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import check_password
from django.core.management import call_command
from django.core.management.base import CommandError
from oauth2_provider.models import get_application_model
from . import presets
from .common_testing import OAuth2ProviderTestCase as TestCase
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("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.assertTrue(check_password("SECRET", app.client_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)
@pytest.mark.usefixtures("oauth2_settings")
@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RW)
def test_application_created_with_algorithm(self):
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--algorithm=RS256",
)
app = Application.objects.get()
self.assertEqual(app.algorithm, "RS256")
def test_validation_failed_message(self):
import django
output = StringIO()
call_command(
"createapplication",
"confidential",
"authorization-code",
"--redirect-uris=http://example.com http://example2.com",
"--user=783",
stdout=output,
)
output_str = output.getvalue()
self.assertIn("user", output_str)
self.assertIn("783", output_str)
if django.VERSION < (5, 2):
self.assertIn("does not exist", output_str)
else:
self.assertIn("is not a valid choice", output_str)
|