Skip to content

Commit

Permalink
fix: override model save method to call full_clean
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Sep 21, 2023
1 parent d7d0714 commit 55119e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions license_manager/apps/subscriptions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,13 @@ class SubscriptionLicenseSource(TimeStampedModel):

history = HistoricalRecords()

def save(self, *args, **kwargs):
"""
Override to ensure that model.clean is always called.
"""
self.full_clean()
return super().save(*args, **kwargs)

def __str__(self):
"""
String representation of source.
Expand Down
15 changes: 15 additions & 0 deletions license_manager/apps/subscriptions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import ddt
import freezegun
import pytest
from django.forms import ValidationError
from django.test import TestCase
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -434,3 +435,17 @@ def test_license_source_creation(self):
license_uuid=self.active_current_license.uuid,
source_id='000000000000000000',
)

def test_license_source_creation_with_invalid_souce_id(self):
"""
Verify that SubscriptionLicenseSource model raises exception if source id format is wrong.
"""
with pytest.raises(ValidationError) as raised_exception:
SubscriptionLicenseSourceFactory(
license=self.active_current_license,
source_id='000000000',
source_type=SubscriptionLicenseSourceType.get_source_type(SubscriptionLicenseSourceType.AMT)
)

exception_message = ['Ensure this value has at least 18 characters (it has 9).']
assert raised_exception.value.args[0]['source_id'][0].messages == exception_message

0 comments on commit 55119e2

Please sign in to comment.