Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string method to the token exchange config method #14

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions token_exchange/migrations/0002_tokenexchangeconfiguration_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.21 on 2023-11-08 15:02

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("token_exchange", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="tokenexchangeconfiguration",
name="label",
field=models.CharField(
blank=True,
help_text="The label of this token exchange configuration",
max_length=250,
verbose_name="label",
),
),
]
17 changes: 17 additions & 0 deletions token_exchange/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@


class TokenExchangeConfiguration(models.Model):
label = models.CharField(
verbose_name=_("label"),
help_text=_("The label of this token exchange configuration"),
max_length=250,
blank=True,
)
service = models.OneToOneField(
to="zgw_consumers.Service",
verbose_name=_("service"),
Expand All @@ -20,3 +26,14 @@ class TokenExchangeConfiguration(models.Model):

class Meta:
verbose_name = _("Token exchange plugin configuration")

def save(self, *args, **kwargs) -> None:
if not self.label and (self.service and self.service.label):
self.label = self.service.label
return super().save(*args, **kwargs)

def __str__(self) -> str:
if not self.label:
return super().__str__()

return self.label
24 changes: 24 additions & 0 deletions token_exchange/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.test import TestCase

from .factories import TokenExchangeConfigurationFactory


class TokenExchangeConfigurationTests(TestCase):
def test_string_method_with_service_label(self):
config = TokenExchangeConfigurationFactory.create(service__label="A label")

self.assertEqual("A label", str(config))

def test_string_method_withou_service_label(self):
config = TokenExchangeConfigurationFactory.create()

self.assertEqual(
f"TokenExchangeConfiguration object ({config.pk})", str(config)
)

def test_save_does_not_overwrite_label(self):
config = TokenExchangeConfigurationFactory.create(
label="A config label", service__label="A service label"
)

self.assertEqual("A config label", str(config))
Loading