11import os
22from unittest .mock import patch
33
4- from django .urls import reverse
5- from django .test import Client , TestCase , override_settings
4+ from django .conf import settings
65from django .contrib .auth .models import User
6+ from django .core import mail
77from django .core .files .uploadedfile import SimpleUploadedFile
8- from plugins .models import Plugin , PluginVersion
8+ from django .test import Client , TestCase , override_settings
9+ from django .urls import reverse
910from plugins .forms import PluginVersionForm
10- from django . core import mail
11- from django . conf import settings
11+ from plugins . models import Plugin , PluginVersion
12+
1213
1314def do_nothing (* args , ** kwargs ):
1415 pass
1516
17+
1618TESTFILE_DIR = os .path .abspath (os .path .join (os .path .dirname (__file__ ), "testfiles" ))
1719
20+
1821class PluginUpdateTestCase (TestCase ):
1922 fixtures = [
2023 "fixtures/auth.json" ,
@@ -23,31 +26,32 @@ class PluginUpdateTestCase(TestCase):
2326 @override_settings (MEDIA_ROOT = "api/tests" )
2427 def setUp (self ):
2528 self .client = Client ()
26- self .url_upload = reverse (' plugin_upload' )
29+ self .url_upload = reverse (" plugin_upload" )
2730
2831 # Create a test user
2932 self .user = User .objects .create_user (
30- username = 'testuser' ,
31- password = 'testpassword' ,
32- email = 'test@example.com'
33+ username = "testuser" , password = "testpassword" , email = "test@example.com"
3334 )
3435
3536 # Log in the test user
36- self .client .login (username = ' testuser' , password = ' testpassword' )
37+ self .client .login (username = " testuser" , password = " testpassword" )
3738
38- # Upload a plugin for renaming test.
39+ # Upload a plugin for renaming test.
3940 # This process is already tested in test_plugin_upload
4041 valid_plugin = os .path .join (TESTFILE_DIR , "valid_plugin.zip_" )
4142 with open (valid_plugin , "rb" ) as file :
4243 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- })
44+ "valid_plugin.zip_" , file .read (), content_type = "application/zip"
45+ )
46+
47+ self .client .post (
48+ self .url_upload ,
49+ {
50+ "package" : uploaded_file ,
51+ },
52+ )
4953
50- self .plugin = Plugin .objects .get (name = ' Test Plugin' )
54+ self .plugin = Plugin .objects .get (name = " Test Plugin" )
5155
5256 @patch ("plugins.tasks.generate_plugins_xml" , new = do_nothing )
5357 @patch ("plugins.validator._check_url_link" , new = do_nothing )
@@ -56,59 +60,54 @@ def test_plugin_new_version(self):
5660 Test upload a new plugin version with a modified metadata
5761 """
5862 package_name = self .plugin .package_name
59- self .assertEqual (self .plugin .homepage , "https://example.net /" )
60- self .assertEqual (self .plugin .tracker , "https://example.net /" )
61- self .assertEqual (self .plugin .repository , "https://example.net /" )
62- self .url_add_version = reverse (' version_create' , args = [package_name ])
63+ self .assertEqual (self .plugin .homepage , "https://qgis.org /" )
64+ self .assertEqual (self .plugin .tracker , "https://qgis.org /" )
65+ self .assertEqual (self .plugin .repository , "https://qgis.org /" )
66+ self .url_add_version = reverse (" version_create" , args = [package_name ])
6367
6468 # Test POST request without allowing name from metadata
6569 valid_plugin = os .path .join (TESTFILE_DIR , "change_metadata.zip_" )
6670 with open (valid_plugin , "rb" ) as file :
6771 uploaded_file = SimpleUploadedFile (
68- "change_metadata.zip_" , file .read (),
69- content_type = "application/zip_" )
70-
71- response = self .client .post (self .url_add_version , {
72- 'package' : uploaded_file ,
73- 'experimental' : False ,
74- 'changelog' : ''
75- })
72+ "change_metadata.zip_" , file .read (), content_type = "application/zip_"
73+ )
74+
75+ response = self .client .post (
76+ self .url_add_version ,
77+ {"package" : uploaded_file , "experimental" : False , "changelog" : "" },
78+ )
7679 self .assertEqual (response .status_code , 302 )
7780
7881 # The old version should always exist when creating a new version
79- self .assertTrue (PluginVersion .objects .filter (
80- plugin__name = 'Test Plugin' ,
81- version = '0.0.1' ).exists ()
82+ self .assertTrue (
83+ PluginVersion .objects .filter (
84+ plugin__name = "Test Plugin" , version = "0.0.1"
85+ ).exists ()
8286 )
83- self .assertTrue (PluginVersion .objects .filter (
84- plugin__name = 'Test Plugin' ,
85- version = '0.0.2' ).exists ()
87+ self .assertTrue (
88+ PluginVersion .objects .filter (
89+ plugin__name = "Test Plugin" , version = "0.0.2"
90+ ).exists ()
8691 )
8792
88- self .plugin = Plugin .objects .get (name = ' Test Plugin' )
93+ self .plugin = Plugin .objects .get (name = " Test Plugin" )
8994 self .assertEqual (self .plugin .homepage , "https://github.com/" )
9095 self .assertEqual (self .plugin .tracker , "https://github.com/" )
9196 self .assertEqual (self .plugin .repository , "https://github.com/" )
9297
9398 self .assertIn (
94- ' staff.recipient@example.com' ,
99+ " staff.recipient@example.com" ,
95100 mail .outbox [0 ].recipients (),
96101 )
97102
98103 self .assertNotIn (
99- ' admin@admin.it' ,
104+ " admin@admin.it" ,
100105 mail .outbox [0 ].recipients (),
101106 )
102- self .assertNotIn (
103- 'staff@staff.it' ,
104- mail .outbox [0 ].recipients ()
105- )
107+ self .assertNotIn ("staff@staff.it" , mail .outbox [0 ].recipients ())
106108
107109 # Should use the new email
108- self .assertEqual (
109- mail .outbox [0 ].from_email ,
110- settings .DEFAULT_FROM_EMAIL
111- )
110+ self .assertEqual (mail .outbox [0 ].from_email , settings .DEFAULT_FROM_EMAIL )
112111
113112 @patch ("plugins.tasks.generate_plugins_xml" , new = do_nothing )
114113 @patch ("plugins.validator._check_url_link" , new = do_nothing )
@@ -117,74 +116,69 @@ def test_plugin_version_update(self):
117116 Test update a plugin version with a modified metadata
118117 """
119118 package_name = self .plugin .package_name
120- self .assertEqual (self .plugin .homepage , "https://example.net /" )
121- self .assertEqual (self .plugin .tracker , "https://example.net /" )
122- self .assertEqual (self .plugin .repository , "https://example.net /" )
123- self .url_add_version = reverse (' version_update' , args = [package_name , ' 0.0.1' ])
119+ self .assertEqual (self .plugin .homepage , "https://qgis.org /" )
120+ self .assertEqual (self .plugin .tracker , "https://qgis.org /" )
121+ self .assertEqual (self .plugin .repository , "https://qgis.org /" )
122+ self .url_add_version = reverse (" version_update" , args = [package_name , " 0.0.1" ])
124123
125124 # Test POST request without allowing name from metadata
126125 valid_plugin = os .path .join (TESTFILE_DIR , "change_metadata.zip_" )
127126 with open (valid_plugin , "rb" ) as file :
128127 uploaded_file = SimpleUploadedFile (
129- "change_metadata.zip_" , file .read (),
130- content_type = "application/zip_" )
131-
132- response = self .client .post (self .url_add_version , {
133- 'package' : uploaded_file ,
134- 'experimental' : False ,
135- 'changelog' : ''
136- })
128+ "change_metadata.zip_" , file .read (), content_type = "application/zip_"
129+ )
130+
131+ response = self .client .post (
132+ self .url_add_version ,
133+ {"package" : uploaded_file , "experimental" : False , "changelog" : "" },
134+ )
137135 self .assertEqual (response .status_code , 302 )
138136
139- self .assertFalse (PluginVersion .objects .filter (
140- plugin__name = 'Test Plugin' ,
141- version = '0.0.1' ).exists ()
137+ self .assertFalse (
138+ PluginVersion .objects .filter (
139+ plugin__name = "Test Plugin" , version = "0.0.1"
140+ ).exists ()
142141 )
143- self .assertTrue (PluginVersion .objects .filter (
144- plugin__name = 'Test Plugin' ,
145- version = '0.0.2' ).exists ()
142+ self .assertTrue (
143+ PluginVersion .objects .filter (
144+ plugin__name = "Test Plugin" , version = "0.0.2"
145+ ).exists ()
146146 )
147147
148- self .plugin = Plugin .objects .get (name = ' Test Plugin' )
148+ self .plugin = Plugin .objects .get (name = " Test Plugin" )
149149 self .assertEqual (self .plugin .homepage , "https://github.com/" )
150150 self .assertEqual (self .plugin .tracker , "https://github.com/" )
151- self .assertEqual (self .plugin .repository , "https://github.com/" )
151+ self .assertEqual (self .plugin .repository , "https://github.com/" )
152152
153153 self .assertIn (
154- ' staff.recipient@example.com' ,
154+ " staff.recipient@example.com" ,
155155 mail .outbox [0 ].recipients (),
156156 )
157157
158158 self .assertNotIn (
159- ' admin@admin.it' ,
159+ " admin@admin.it" ,
160160 mail .outbox [0 ].recipients (),
161161 )
162- self .assertNotIn (
163- 'staff@staff.it' ,
164- mail .outbox [0 ].recipients ()
165- )
162+ self .assertNotIn ("staff@staff.it" , mail .outbox [0 ].recipients ())
166163
167164 # Should use the new email
168- self .assertEqual (
169- mail .outbox [0 ].from_email ,
170- settings .DEFAULT_FROM_EMAIL
171- )
165+ self .assertEqual (mail .outbox [0 ].from_email , settings .DEFAULT_FROM_EMAIL )
172166
173167 def test_plugin_version_approved_update (self ):
174168 """
175169 Test update a plugin version that is already approved
176170 """
177171 package_name = self .plugin .package_name
178- self .url_add_version = reverse (' version_update' , args = [package_name , ' 0.0.1' ])
179- version = PluginVersion .objects .get (plugin__name = ' Test Plugin' , version = ' 0.0.1' )
172+ self .url_add_version = reverse (" version_update" , args = [package_name , " 0.0.1" ])
173+ version = PluginVersion .objects .get (plugin__name = " Test Plugin" , version = " 0.0.1" )
180174 version .approved = True
181175 version .save ()
182176 self .assertTrue (version .approved )
183177
184178 response = self .client .get (self .url_add_version )
185179 # Should redirect to the plugin details page
186180 self .assertEqual (response .status_code , 302 )
187- self .assertRedirects (response , reverse (' plugin_detail' , args = [package_name ]))
181+ self .assertRedirects (response , reverse (" plugin_detail" , args = [package_name ]))
188182
189183 def tearDown (self ):
190184 self .client .logout ()
0 commit comments