|
| 1 | +import os |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from django.urls import reverse |
| 5 | +from django.test import Client, TestCase, override_settings |
| 6 | +from django.contrib.auth.models import User |
| 7 | +from django.core.files.uploadedfile import SimpleUploadedFile |
| 8 | +from plugins.models import Plugin, PluginVersion |
| 9 | +from plugins.forms import PluginForm |
| 10 | + |
| 11 | +def do_nothing(*args, **kwargs): |
| 12 | + pass |
| 13 | + |
| 14 | +TESTFILE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "testfiles")) |
| 15 | + |
| 16 | +class PluginRenameTestCase(TestCase): |
| 17 | + fixtures = [ |
| 18 | + "fixtures/styles.json", |
| 19 | + "fixtures/auth.json", |
| 20 | + "fixtures/simplemenu.json", |
| 21 | + ] |
| 22 | + |
| 23 | + @override_settings(MEDIA_ROOT="api/tests") |
| 24 | + def setUp(self): |
| 25 | + self.client = Client() |
| 26 | + self.url_upload = reverse('plugin_upload') |
| 27 | + |
| 28 | + # Create a test user |
| 29 | + self.user = User.objects.create_user( |
| 30 | + username='testuser', |
| 31 | + password='testpassword', |
| 32 | + |
| 33 | + ) |
| 34 | + |
| 35 | + # Log in the test user |
| 36 | + self.client.login(username='testuser', password='testpassword') |
| 37 | + |
| 38 | + # Upload a plugin for renaming test. |
| 39 | + # This process is already tested in test_plugin_upload |
| 40 | + valid_plugin = os.path.join(TESTFILE_DIR, "valid_plugin.zip_") |
| 41 | + with open(valid_plugin, "rb") as file: |
| 42 | + uploaded_file = SimpleUploadedFile( |
| 43 | + "valid_plugin.zip_", file.read(), |
| 44 | + content_type="application/zip") |
| 45 | + |
| 46 | + self.client.post(self.url_upload, { |
| 47 | + 'package': uploaded_file, |
| 48 | + }) |
| 49 | + |
| 50 | + self.plugin = Plugin.objects.get(name='Test Plugin') |
| 51 | + self.plugin.save() |
| 52 | + |
| 53 | + @patch("plugins.tasks.generate_plugins_xml.delay", new=do_nothing) |
| 54 | + @patch("plugins.validator._check_url_link", new=do_nothing) |
| 55 | + def test_change_maintainer(self): |
| 56 | + """ |
| 57 | + Test change maintainer for plugin update |
| 58 | + """ |
| 59 | + package_name = self.plugin.package_name |
| 60 | + self.url_plugin_update = reverse('plugin_update', args=[package_name]) |
| 61 | + self.url_add_version = reverse('version_create', args=[package_name]) |
| 62 | + |
| 63 | + # Test GET request |
| 64 | + response = self.client.get(self.url_plugin_update) |
| 65 | + self.assertEqual(response.status_code, 200) |
| 66 | + self.assertIsInstance(response.context['form'], PluginForm) |
| 67 | + self.assertEqual(response.context['form']['maintainer'].value(), self.user.pk) |
| 68 | + |
| 69 | + |
| 70 | + # Test POST request to change maintainer |
| 71 | + |
| 72 | + response = self.client.post(self.url_plugin_update, { |
| 73 | + 'description': self.plugin.description, |
| 74 | + 'about': self.plugin.about, |
| 75 | + 'author': self.plugin.author, |
| 76 | + 'email': self.plugin.email, |
| 77 | + 'tracker': self.plugin.tracker, |
| 78 | + 'repository': self.plugin.repository, |
| 79 | + 'maintainer': 1, |
| 80 | + }) |
| 81 | + self.assertEqual(response.status_code, 302) |
| 82 | + self.assertEqual(Plugin.objects.get(name='Test Plugin').maintainer.pk, 1) |
| 83 | + |
| 84 | + # Test POST request with new version |
| 85 | + |
| 86 | + valid_plugin = os.path.join(TESTFILE_DIR, "valid_plugin_0.0.2.zip_") |
| 87 | + with open(valid_plugin, "rb") as file: |
| 88 | + uploaded_file = SimpleUploadedFile( |
| 89 | + "valid_plugin_0.0.2.zip_", file.read(), |
| 90 | + content_type="application/zip_") |
| 91 | + |
| 92 | + response = self.client.post(self.url_add_version, { |
| 93 | + 'package': uploaded_file, |
| 94 | + 'experimental': False, |
| 95 | + 'changelog': '' |
| 96 | + }) |
| 97 | + self.assertEqual(response.status_code, 302) |
| 98 | + self.assertEqual(Plugin.objects.get(name='Test Plugin').maintainer.pk, 1) |
| 99 | + |
| 100 | + def tearDown(self): |
| 101 | + self.client.logout() |
0 commit comments