|
| 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 | + |
| 10 | +migration_module = importlib.import_module( |
| 11 | + "openwisp_radius.migrations.0044_convert_user_credentials_data" |
| 12 | +) |
| 13 | +convert_user_credentials_data = migration_module.convert_user_credentials_data |
| 14 | + |
| 15 | + |
| 16 | +class Test0044Migration(TestCase): |
| 17 | + def test_convert_user_credentials_data(self): |
| 18 | + batch = RadiusBatch.objects.create( |
| 19 | + name="test_batch_migration", strategy="prefix", prefix="test" |
| 20 | + ) |
| 21 | + RadiusBatch.objects.filter(pk=batch.pk).update( |
| 22 | + user_credentials=json.dumps({"user1": "pass1"}) |
| 23 | + ) |
| 24 | + |
| 25 | + apps = MagicMock() |
| 26 | + apps.get_model.return_value = RadiusBatch |
| 27 | + |
| 28 | + schema_editor = MagicMock() |
| 29 | + schema_editor.connection = connection |
| 30 | + |
| 31 | + convert_user_credentials_data(apps, schema_editor) |
| 32 | + |
| 33 | + batch.refresh_from_db() |
| 34 | + self.assertEqual(batch.user_credentials, {"user1": "pass1"}) |
| 35 | + |
| 36 | + def test_convert_user_credentials_data_invalid_json(self): |
| 37 | + batch = RadiusBatch.objects.create( |
| 38 | + name="test_batch_invalid", strategy="prefix", prefix="test2" |
| 39 | + ) |
| 40 | + RadiusBatch.objects.filter(pk=batch.pk).update( |
| 41 | + user_credentials="invalid_json_string" |
| 42 | + ) |
| 43 | + |
| 44 | + apps = MagicMock() |
| 45 | + apps.get_model.return_value = RadiusBatch |
| 46 | + |
| 47 | + schema_editor = MagicMock() |
| 48 | + schema_editor.connection = connection |
| 49 | + |
| 50 | + convert_user_credentials_data(apps, schema_editor) |
| 51 | + |
| 52 | + batch.refresh_from_db() |
| 53 | + self.assertEqual(batch.user_credentials, "invalid_json_string") |
0 commit comments