Skip to content

Commit eee80d6

Browse files
committed
[fix] Add tests for user credentials data migration
1 parent 2f408bd commit eee80d6

3 files changed

Lines changed: 71 additions & 27 deletions

File tree

openwisp_radius/migrations/0043_alter_organizationradiussettings_sms_meta_data_and_more.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by Django 5.2.5 on 2025-10-22 18:34
22

3+
import django.core.serializers.json
34
from django.db import migrations, models
45

56

@@ -15,6 +16,7 @@ class Migration(migrations.Migration):
1516
name="sms_meta_data",
1617
field=models.JSONField(
1718
blank=True,
19+
encoder=django.core.serializers.json.DjangoJSONEncoder,
1820
help_text=(
1921
"Additional configuration for SMS backend in JSON format "
2022
"(optional, leave blank if unsure)"
@@ -26,6 +28,11 @@ class Migration(migrations.Migration):
2628
migrations.AlterField(
2729
model_name="radiusbatch",
2830
name="user_credentials",
29-
field=models.JSONField(blank=True, null=True, verbose_name="PDF"),
31+
field=models.JSONField(
32+
blank=True,
33+
encoder=django.core.serializers.json.DjangoJSONEncoder,
34+
null=True,
35+
verbose_name="PDF",
36+
),
3037
),
3138
]

openwisp_radius/migrations/0044_convert_user_credentials_data.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import json
44
import logging
55

6-
import django.core.serializers.json
7-
from django.db import migrations, models
6+
from django.db import migrations
87

98
logger = logging.getLogger(__name__)
109

@@ -40,30 +39,6 @@ class Migration(migrations.Migration):
4039
]
4140

4241
operations = [
43-
migrations.AlterField(
44-
model_name="organizationradiussettings",
45-
name="sms_meta_data",
46-
field=models.JSONField(
47-
blank=True,
48-
encoder=django.core.serializers.json.DjangoJSONEncoder,
49-
help_text=(
50-
"Additional configuration for SMS backend in JSON format "
51-
"(optional, leave blank if unsure)"
52-
),
53-
null=True,
54-
verbose_name="SMS meta data",
55-
),
56-
),
57-
migrations.AlterField(
58-
model_name="radiusbatch",
59-
name="user_credentials",
60-
field=models.JSONField(
61-
blank=True,
62-
encoder=django.core.serializers.json.DjangoJSONEncoder,
63-
null=True,
64-
verbose_name="PDF",
65-
),
66-
),
6742
migrations.RunPython(
6843
convert_user_credentials_data, reverse_code=migrations.RunPython.noop
6944
),
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import importlib
2+
import json
3+
from unittest.mock import MagicMock
4+
5+
from django.db import connection
6+
from django.test import TestCase
7+
8+
from openwisp_radius.models import RadiusBatch
9+
from openwisp_users.tests.utils import TestOrganizationMixin
10+
11+
migration_module = importlib.import_module(
12+
"openwisp_radius.migrations.0044_convert_user_credentials_data"
13+
)
14+
convert_user_credentials_data = migration_module.convert_user_credentials_data
15+
16+
17+
class Test0044Migration(TestOrganizationMixin, TestCase):
18+
def test_convert_user_credentials_data(self):
19+
org = self._get_org()
20+
batch = RadiusBatch.objects.create(
21+
name="test_batch_migration",
22+
strategy="prefix",
23+
prefix="test",
24+
organization=org,
25+
)
26+
RadiusBatch.objects.filter(pk=batch.pk).update(
27+
user_credentials=json.dumps({"user1": "pass1"})
28+
)
29+
30+
apps = MagicMock()
31+
apps.get_model.return_value = RadiusBatch
32+
33+
schema_editor = MagicMock()
34+
schema_editor.connection = connection
35+
36+
convert_user_credentials_data(apps, schema_editor)
37+
38+
batch.refresh_from_db()
39+
self.assertEqual(batch.user_credentials, {"user1": "pass1"})
40+
41+
def test_convert_user_credentials_data_invalid_json(self):
42+
org = self._get_org()
43+
batch = RadiusBatch.objects.create(
44+
name="test_batch_invalid",
45+
strategy="prefix",
46+
prefix="test2",
47+
organization=org,
48+
)
49+
RadiusBatch.objects.filter(pk=batch.pk).update(
50+
user_credentials="invalid_json_string"
51+
)
52+
53+
apps = MagicMock()
54+
apps.get_model.return_value = RadiusBatch
55+
56+
schema_editor = MagicMock()
57+
schema_editor.connection = connection
58+
59+
convert_user_credentials_data(apps, schema_editor)
60+
61+
batch.refresh_from_db()
62+
self.assertEqual(batch.user_credentials, "invalid_json_string")

0 commit comments

Comments
 (0)