41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
import uuid
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
|
|
from apps.application.models import Application, ApplicationStatus
|
|
from apps.common.utils import generate_client_id, generate_client_secret, hash_token
|
|
from apps.oauth.models import OAuthScope
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class ApplicationTests(TestCase):
|
|
def setUp(self):
|
|
self.user = User.objects.create_user(
|
|
email="dev@example.com", password="S3cure-Pass-123", status="active"
|
|
)
|
|
self.scope = OAuthScope.objects.create(code="openid", is_system=True)
|
|
|
|
def test_client_id_generated_and_secret_hashed(self):
|
|
raw_secret = generate_client_secret()
|
|
app = Application.objects.create(
|
|
product_key="bermooda",
|
|
name="Bermooda",
|
|
client_secret_hash=hash_token(raw_secret),
|
|
status=ApplicationStatus.ACTIVE,
|
|
created_by=self.user,
|
|
)
|
|
self.assertTrue(app.client_id.startswith("app_"))
|
|
self.assertNotEqual(app.client_secret_hash, raw_secret)
|
|
self.assertIsInstance(app.id, uuid.UUID)
|
|
|
|
def test_application_scopes(self):
|
|
app = Application.objects.create(
|
|
product_key="hamsoo",
|
|
name="Hamsoo",
|
|
client_secret_hash=hash_token(generate_client_secret()),
|
|
created_by=self.user,
|
|
)
|
|
app.scopes.add(self.scope)
|
|
self.assertEqual(list(app.scopes.values_list("code", flat=True)), ["openid"]) |