Skip to content

Commit

Permalink
Update repository url when creating or updating a plugin version (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix authored Jan 31, 2024
1 parent 9a87f0f commit a062b30
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
136 changes: 136 additions & 0 deletions qgis-app/plugins/tests/test_plugin_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import os
from unittest.mock import patch

from django.urls import reverse
from django.test import Client, TestCase, override_settings
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from plugins.models import Plugin, PluginVersion
from plugins.forms import PluginVersionForm

def do_nothing(*args, **kwargs):
pass

TESTFILE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "testfiles"))

class PluginUpdateTestCase(TestCase):
fixtures = [
"fixtures/styles.json",
"fixtures/auth.json",
"fixtures/simplemenu.json",
]

@override_settings(MEDIA_ROOT="api/tests")
def setUp(self):
self.client = Client()
self.url_upload = reverse('plugin_upload')

# Create a test user
self.user = User.objects.create_user(
username='testuser',
password='testpassword',
email='[email protected]'
)

# Log in the test user
self.client.login(username='testuser', password='testpassword')

# Upload a plugin for renaming test.
# This process is already tested in test_plugin_upload
valid_plugin = os.path.join(TESTFILE_DIR, "valid_plugin.zip_")
with open(valid_plugin, "rb") as file:
uploaded_file = SimpleUploadedFile(
"valid_plugin.zip_", file.read(),
content_type="application/zip")

self.client.post(self.url_upload, {
'package': uploaded_file,
})

self.plugin = Plugin.objects.get(name='Test Plugin')

@patch("plugins.tasks.generate_plugins_xml.delay", new=do_nothing)
@patch("plugins.validator._check_url_link", new=do_nothing)
def test_plugin_new_version(self):
"""
Test upload a new plugin version with a modified metadata
"""
package_name = self.plugin.package_name
self.assertEqual(self.plugin.homepage, "https://example.net/")
self.assertEqual(self.plugin.tracker, "https://example.net/")
self.assertEqual(self.plugin.repository, "https://example.net/")
self.url_add_version = reverse('version_create', args=[package_name])

# Test POST request without allowing name from metadata
valid_plugin = os.path.join(TESTFILE_DIR, "change_metadata.zip_")
with open(valid_plugin, "rb") as file:
uploaded_file = SimpleUploadedFile(
"change_metadata.zip_", file.read(),
content_type="application/zip_")

response = self.client.post(self.url_add_version, {
'package': uploaded_file,
'experimental': False,
'changelog': ''
})
self.assertEqual(response.status_code, 302)

# The old version should always exist when creating a new version
self.assertTrue(PluginVersion.objects.filter(
plugin__name='Test Plugin',
version='0.0.1').exists()
)
self.assertTrue(PluginVersion.objects.filter(
plugin__name='Test Plugin',
version='0.0.2').exists()
)

self.plugin = Plugin.objects.get(name='Test Plugin')
self.assertEqual(self.plugin.homepage, "https://github.com/")
self.assertEqual(self.plugin.tracker, "https://github.com/")
self.assertEqual(self.plugin.repository, "https://github.com/")

@patch("plugins.tasks.generate_plugins_xml.delay", new=do_nothing)
@patch("plugins.validator._check_url_link", new=do_nothing)
def test_plugin_version_update(self):
"""
Test update a plugin version with a modified metadata
"""
package_name = self.plugin.package_name
self.assertEqual(self.plugin.homepage, "https://example.net/")
self.assertEqual(self.plugin.tracker, "https://example.net/")
self.assertEqual(self.plugin.repository, "https://example.net/")
self.url_add_version = reverse('version_update', args=[package_name, '0.0.1'])

# Test POST request without allowing name from metadata
valid_plugin = os.path.join(TESTFILE_DIR, "change_metadata.zip_")
with open(valid_plugin, "rb") as file:
uploaded_file = SimpleUploadedFile(
"change_metadata.zip_", file.read(),
content_type="application/zip_")

response = self.client.post(self.url_add_version, {
'package': uploaded_file,
'experimental': False,
'changelog': ''
})
self.assertEqual(response.status_code, 302)

# The old version should not exist anymore
self.assertFalse(PluginVersion.objects.filter(
plugin__name='Test Plugin',
version='0.0.1').exists()
)
self.assertTrue(PluginVersion.objects.filter(
plugin__name='Test Plugin',
version='0.0.2').exists()
)

self.plugin = Plugin.objects.get(name='Test Plugin')
self.assertEqual(self.plugin.homepage, "https://github.com/")
self.assertEqual(self.plugin.tracker, "https://github.com/")
self.assertEqual(self.plugin.repository, "https://github.com/")


def tearDown(self):
self.client.logout()
Binary file not shown.
2 changes: 1 addition & 1 deletion qgis-app/plugins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ def _main_plugin_update(request, plugin, form):
Updates the main plugin object from version metadata
"""
# Check if update name from metadata is allowed
metadata_fields = ["author", "email", "description", "about", "homepage", "tracker"]
metadata_fields = ["author", "email", "description", "about", "homepage", "tracker", "repository"]
if plugin.allow_update_name:
metadata_fields.insert(0, "name")

Expand Down

0 comments on commit a062b30

Please sign in to comment.