aa-fleetfinder 2.5.1__py3-none-any.whl → 2.6.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of aa-fleetfinder might be problematic. Click here for more details.

Files changed (27) hide show
  1. {aa_fleetfinder-2.5.1.dist-info → aa_fleetfinder-2.6.1.dist-info}/METADATA +2 -2
  2. {aa_fleetfinder-2.5.1.dist-info → aa_fleetfinder-2.6.1.dist-info}/RECORD +27 -25
  3. fleetfinder/__init__.py +1 -1
  4. fleetfinder/locale/cs_CZ/LC_MESSAGES/django.po +20 -7
  5. fleetfinder/locale/de/LC_MESSAGES/django.mo +0 -0
  6. fleetfinder/locale/de/LC_MESSAGES/django.po +23 -10
  7. fleetfinder/locale/django.pot +21 -8
  8. fleetfinder/locale/es/LC_MESSAGES/django.po +23 -8
  9. fleetfinder/locale/fr_FR/LC_MESSAGES/django.po +20 -7
  10. fleetfinder/locale/it_IT/LC_MESSAGES/django.po +20 -7
  11. fleetfinder/locale/ja/LC_MESSAGES/django.po +20 -7
  12. fleetfinder/locale/ko_KR/LC_MESSAGES/django.po +23 -8
  13. fleetfinder/locale/nl_NL/LC_MESSAGES/django.po +20 -7
  14. fleetfinder/locale/pl_PL/LC_MESSAGES/django.po +20 -7
  15. fleetfinder/locale/ru/LC_MESSAGES/django.po +23 -8
  16. fleetfinder/locale/sk/LC_MESSAGES/django.po +20 -7
  17. fleetfinder/locale/uk/LC_MESSAGES/django.po +23 -8
  18. fleetfinder/locale/zh_Hans/LC_MESSAGES/django.po +23 -8
  19. fleetfinder/tasks.py +121 -150
  20. fleetfinder/templates/fleetfinder/dashboard.html +60 -47
  21. fleetfinder/templates/fleetfinder/fleet-details.html +7 -1
  22. fleetfinder/templates/fleetfinder/partials/body/form-fleet-details.html +8 -8
  23. fleetfinder/tests/test_tasks.py +140 -0
  24. fleetfinder/tests/test_views.py +416 -0
  25. fleetfinder/views.py +176 -62
  26. {aa_fleetfinder-2.5.1.dist-info → aa_fleetfinder-2.6.1.dist-info}/WHEEL +0 -0
  27. {aa_fleetfinder-2.5.1.dist-info → aa_fleetfinder-2.6.1.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,416 @@
1
+ """
2
+ Test the views for the Fleet Finder application.
3
+ """
4
+
5
+ # Standard Library
6
+ import json
7
+ from http import HTTPStatus
8
+ from types import SimpleNamespace
9
+ from unittest.mock import Mock, patch
10
+
11
+ # Django
12
+ from django.contrib.auth.models import Group
13
+ from django.test import TestCase
14
+ from django.urls import reverse
15
+ from django.utils.datetime_safe import datetime
16
+ from django.utils.timezone import now
17
+
18
+ # Alliance Auth
19
+ from allianceauth.groupmanagement.models import AuthGroup
20
+
21
+ # Alliance Auth (External Libs)
22
+ from app_utils.testing import create_fake_user
23
+
24
+ # AA Fleet Finder
25
+ from fleetfinder.models import Fleet
26
+
27
+
28
+ def dt_to_iso(dt: datetime) -> str:
29
+ """
30
+ Helper :: Convert a datetime object to ISO 8601 format.
31
+
32
+ @see https://github.com/django/django/blob/main/django/core/serializers/json.py#L92-L98
33
+
34
+ :param dt:
35
+ :type dt:
36
+ :return:
37
+ :rtype:
38
+ """
39
+
40
+ r = dt.isoformat()
41
+
42
+ if dt.microsecond:
43
+ r = r[:23] + r[26:]
44
+
45
+ if r.endswith("+00:00"):
46
+ r = r.removesuffix("+00:00") + "Z"
47
+
48
+ return r
49
+
50
+
51
+ class FleetfinderTestViews(TestCase):
52
+ """
53
+ Base test case for Fleet Finder views.
54
+ This class sets up the necessary users and fleet ID for testing.
55
+ It includes a user with the `fleetfinder.manage_fleets` permission
56
+ and a user with `fleetfinder.access_fleetfinder` access permissions.
57
+ The fleet ID is set to a predefined value for testing purposes.
58
+ """
59
+
60
+ @classmethod
61
+ def setUp(cls):
62
+ """
63
+ Set up the test case.
64
+
65
+ :return:
66
+ :rtype:
67
+ """
68
+
69
+ cls.user_with_manage_perms = create_fake_user(
70
+ character_id=1000,
71
+ character_name="Jean Luc Picard",
72
+ permissions=["fleetfinder.access_fleetfinder", "fleetfinder.manage_fleets"],
73
+ )
74
+ cls.user_with_basic_acces_perms = create_fake_user(
75
+ character_id=1001,
76
+ character_name="William Riker",
77
+ permissions=["fleetfinder.access_fleetfinder"],
78
+ )
79
+
80
+ cls.fleet_created_at = now()
81
+
82
+ cls.fleet = Fleet(
83
+ fleet_id=12345,
84
+ name="Starfleet",
85
+ fleet_commander=cls.user_with_manage_perms.profile.main_character,
86
+ created_at=cls.fleet_created_at,
87
+ is_free_move=False,
88
+ )
89
+ cls.fleet.save()
90
+
91
+ cls.fleet_id = 12345
92
+
93
+
94
+ class TestAjaxDashboardView(FleetfinderTestViews):
95
+ """
96
+ Test the ajax_dashboard view in the Fleet Finder application.
97
+ This view is responsible for rendering the dashboard with fleet data.
98
+ It should return a JSON response containing fleet information,
99
+ including fleet names, commanders, and group associations.
100
+ If no fleets are available, it should return an empty list.
101
+ It should also filter fleets based on the user's groups.
102
+ """
103
+
104
+ # python
105
+ @patch("fleetfinder.views.get_all_characters_from_user")
106
+ def test_renders_dashboard_with_fleet_data(self, mock_get_characters):
107
+ """
108
+ Test that the ajax_dashboard view renders the dashboard with fleet data.
109
+
110
+ :param mock_portrait_url:
111
+ :type mock_portrait_url:
112
+ :param mock_get_characters:
113
+ :type mock_get_characters:
114
+ :return:
115
+ :rtype:
116
+ """
117
+
118
+ mock_get_characters.return_value = [
119
+ self.user_with_manage_perms.profile.main_character
120
+ ]
121
+
122
+ fleet = self.fleet
123
+ fleet.groups.set([])
124
+
125
+ self.client.force_login(self.user_with_basic_acces_perms)
126
+ url = reverse("fleetfinder:ajax_dashboard")
127
+ response = self.client.get(url)
128
+
129
+ expected_resonse = [
130
+ {
131
+ "fleet_commander": {
132
+ "html": '<img class="rounded eve-character-portrait" src="https://images.evetech.net/characters/1000/portrait?size=32" alt="Jean Luc Picard" loading="lazy">Jean Luc Picard',
133
+ "sort": "Jean Luc Picard",
134
+ },
135
+ "fleet_name": "Starfleet",
136
+ "created_at": dt_to_iso(self.fleet_created_at),
137
+ "join": '<a href="/fleetfinder/fleet/12345/join/" class="btn btn-sm btn-primary">Join fleet</a>',
138
+ "details": "",
139
+ "edit": "",
140
+ }
141
+ ]
142
+
143
+ self.assertEqual(response.status_code, HTTPStatus.OK)
144
+ self.assertIn("Starfleet", response.json()[0]["fleet_name"])
145
+ self.assertIn("Jean Luc Picard", response.json()[0]["fleet_commander"]["html"])
146
+ self.assertEqual(response.json(), expected_resonse)
147
+
148
+ @patch("fleetfinder.views.get_all_characters_from_user")
149
+ def test_returns_empty_data_when_no_fleets_available(self, mock_get_characters):
150
+ """
151
+ Test that the ajax_dashboard view returns an empty list when no fleets are available.
152
+
153
+ :param mock_get_characters:
154
+ :type mock_get_characters:
155
+ :return:
156
+ :rtype:
157
+ """
158
+
159
+ mock_get_characters.return_value = [
160
+ self.user_with_manage_perms.profile.main_character
161
+ ]
162
+
163
+ Fleet.objects.all().delete() # Remove all fleets
164
+
165
+ self.client.force_login(self.user_with_basic_acces_perms)
166
+ url = reverse("fleetfinder:ajax_dashboard")
167
+ response = self.client.get(url)
168
+
169
+ self.assertEqual(response.status_code, HTTPStatus.OK)
170
+ self.assertEqual(response.json(), [])
171
+
172
+ @patch("fleetfinder.views.get_all_characters_from_user")
173
+ def test_filters_fleets_by_user_groups(self, mock_get_characters):
174
+ mock_get_characters.return_value = [
175
+ self.user_with_manage_perms.profile.main_character
176
+ ]
177
+
178
+ group_obj = Group.objects.create(name="Starfleet Officers")
179
+ auth_group, _ = AuthGroup.objects.get_or_create(group=group_obj)
180
+ fleet = self.fleet
181
+ fleet.groups.set([auth_group])
182
+
183
+ self.client.force_login(self.user_with_basic_acces_perms)
184
+ self.user_with_basic_acces_perms.groups.add(group_obj)
185
+ url = reverse("fleetfinder:ajax_dashboard")
186
+ response = self.client.get(url)
187
+
188
+ self.assertEqual(response.status_code, HTTPStatus.OK)
189
+ self.assertIn("Starfleet", response.json()[0]["fleet_name"])
190
+
191
+
192
+ class TestFleetEditView(FleetfinderTestViews):
193
+ """
194
+ Test the edit_fleet view in the Fleet Finder application.
195
+ This view is responsible for editing fleet details.
196
+ """
197
+
198
+ @patch("fleetfinder.views.Fleet.objects.get")
199
+ @patch("fleetfinder.views.AuthGroup.objects.filter")
200
+ def test_renders_edit_fleet_template_with_correct_context(
201
+ self, mock_filter_groups, mock_get_fleet
202
+ ):
203
+ """
204
+ Test that the edit_fleet view renders the correct template and context.
205
+
206
+ :param mock_filter_groups:
207
+ :type mock_filter_groups:
208
+ :param mock_get_fleet:
209
+ :type mock_get_fleet:
210
+ :return:
211
+ :rtype:
212
+ """
213
+
214
+ mock_get_fleet.return_value = self.fleet
215
+ group1 = Mock(spec=AuthGroup)
216
+ group1.name = "Group1"
217
+ group2 = Mock(spec=AuthGroup)
218
+ group2.name = "Group2"
219
+ mock_filter_groups.return_value = [group1, group2]
220
+
221
+ self.client.force_login(self.user_with_manage_perms)
222
+ url = reverse("fleetfinder:edit_fleet", args=[self.fleet_id])
223
+ response = self.client.get(url)
224
+
225
+ self.assertTemplateUsed(response, "fleetfinder/edit-fleet.html")
226
+ self.assertEqual(response.context["fleet"].name, "Starfleet")
227
+ self.assertEqual(response.context["character_id"], 1000)
228
+ self.assertEqual(len(response.context["auth_groups"]), 2)
229
+
230
+ @patch("fleetfinder.views.Fleet.objects.get")
231
+ def test_redirects_to_dashboard_if_fleet_does_not_exist(self, mock_get_fleet):
232
+ """
233
+ Test that the edit_fleet view redirects to the dashboard if the fleet does not exist.
234
+
235
+ :param mock_get_fleet:
236
+ :type mock_get_fleet:
237
+ :return:
238
+ :rtype:
239
+ """
240
+
241
+ mock_get_fleet.side_effect = Fleet.DoesNotExist
242
+
243
+ self.client.force_login(self.user_with_manage_perms)
244
+ url = reverse("fleetfinder:edit_fleet", args=[99999])
245
+ response = self.client.get(url)
246
+
247
+ self.assertEqual(response.status_code, HTTPStatus.FOUND)
248
+ self.assertRedirects(response, reverse("fleetfinder:dashboard"))
249
+
250
+
251
+ class TestJoinFleetView(FleetfinderTestViews):
252
+ """
253
+ Test the join_fleet view in the Fleet Finder application.
254
+ This view is responsible for allowing users to join a fleet.
255
+ It should redirect to the fleet details page after joining.
256
+ If the fleet does not exist, it should return a 404 status code.
257
+ """
258
+
259
+ @patch("fleetfinder.views.Fleet.objects.get")
260
+ def test_join_fleet_redirects_to_dashboard(self, mock_get_fleet):
261
+ """
262
+ Test that the join_fleet view redirects to the dashboard after joining a fleet.
263
+
264
+ :param mock_get_fleet:
265
+ :type mock_get_fleet:
266
+ :return:
267
+ :rtype:
268
+ """
269
+
270
+ mock_get_fleet.return_value = Fleet(fleet_id=self.fleet_id)
271
+
272
+ self.client.force_login(self.user_with_manage_perms)
273
+ url = reverse("fleetfinder:join_fleet", args=[self.fleet_id])
274
+ response = self.client.get(url)
275
+
276
+ self.assertEqual(response.status_code, HTTPStatus.OK)
277
+ self.assertTemplateUsed(response, "fleetfinder/join-fleet.html")
278
+
279
+ @patch("fleetfinder.views.Fleet.objects.get")
280
+ def test_join_fleet_handles_non_existent_fleet(self, mock_get_fleet):
281
+ """
282
+ Test that the join_fleet view handles a non-existent fleet correctly.
283
+
284
+ :param mock_get_fleet:
285
+ :type mock_get_fleet:
286
+ :return:
287
+ :rtype:
288
+ """
289
+
290
+ mock_get_fleet.side_effect = Fleet.DoesNotExist
291
+
292
+ self.client.force_login(self.user_with_manage_perms)
293
+ url = reverse("fleetfinder:join_fleet", args=[123456]) # Non-existent fleet ID
294
+ response = self.client.get(url)
295
+
296
+ self.assertEqual(response.status_code, HTTPStatus.FOUND)
297
+ self.assertRedirects(response, reverse("fleetfinder:dashboard"))
298
+
299
+
300
+ class TestFleetDetailsView(FleetfinderTestViews):
301
+ """
302
+ Test the fleet_details view in the Fleet Finder application.
303
+ This view is responsible for rendering the fleet details page.
304
+ It should render the correct template and require the user to have
305
+ the 'fleetfinder.manage_fleets' permission to access it.
306
+ If the fleet does not exist, it should return a 404 status code.
307
+ """
308
+
309
+ @patch("fleetfinder.views.Fleet.objects.get")
310
+ def test_fleet_details_renders_correct_template(self, mock_get_fleet):
311
+ """
312
+ Test that the fleet_details view renders the correct template.
313
+
314
+ :param mock_get_fleet:
315
+ :type mock_get_fleet:
316
+ :return:
317
+ :rtype:
318
+ """
319
+
320
+ mock_get_fleet.return_value = Fleet(fleet_id=self.fleet_id)
321
+
322
+ self.client.force_login(self.user_with_manage_perms)
323
+ url = reverse("fleetfinder:fleet_details", args=[self.fleet_id])
324
+ response = self.client.get(url)
325
+
326
+ self.assertEqual(response.status_code, HTTPStatus.OK)
327
+ self.assertTemplateUsed(response, "fleetfinder/fleet-details.html")
328
+
329
+ @patch("fleetfinder.views.Fleet.objects.get")
330
+ def test_fleet_details_requires_manage_permission(self, mock_get_fleet):
331
+ """
332
+ Test that the fleet_details view requires the user to have the 'fleetfinder.manage_fleets' permission.
333
+
334
+ :param mock_get_fleet:
335
+ :type mock_get_fleet:
336
+ :return:
337
+ :rtype:
338
+ """
339
+
340
+ mock_get_fleet.return_value = Fleet(fleet_id=self.fleet_id)
341
+
342
+ self.client.force_login(self.user_with_basic_acces_perms)
343
+ url = reverse("fleetfinder:fleet_details", args=[self.fleet_id])
344
+ response = self.client.get(url)
345
+
346
+ self.assertEqual(response.status_code, HTTPStatus.FOUND)
347
+
348
+
349
+ class TestAjaxFleetDetailsView(FleetfinderTestViews):
350
+ """
351
+ Test the ajax_fleet_details view in the Fleet Finder application.
352
+ This view is responsible for returning fleet details in JSON format.
353
+ It should return the fleet members and their ship types, or an empty list if the fleet is empty.
354
+ """
355
+
356
+ @patch("fleetfinder.views.get_fleet_composition")
357
+ def test_returns_correct_fleet_details(self, mock_get_fleet_composition):
358
+ """
359
+ Test that the ajax_fleet_details view returns the correct fleet details.
360
+
361
+ :param mock_get_fleet_composition:
362
+ :type mock_get_fleet_composition:
363
+ :return:
364
+ :rtype:
365
+ """
366
+
367
+ mock_get_fleet_composition.return_value = SimpleNamespace(
368
+ fleet=[{"name": "Pilot1"}, {"name": "Pilot2"}, {"name": "Pilot3"}],
369
+ aggregate={"Frigate": 2, "Cruiser": 1},
370
+ )
371
+
372
+ self.client.force_login(user=self.user_with_manage_perms)
373
+
374
+ url = reverse("fleetfinder:ajax_fleet_details", args=[self.fleet_id])
375
+ response = self.client.get(url)
376
+
377
+ expected_fleet_composition = {
378
+ "fleet_member": [
379
+ {"name": "Pilot1"},
380
+ {"name": "Pilot2"},
381
+ {"name": "Pilot3"},
382
+ ],
383
+ "fleet_composition": [
384
+ {"ship_type_name": "Frigate", "number": 2},
385
+ {"ship_type_name": "Cruiser", "number": 1},
386
+ ],
387
+ }
388
+
389
+ self.assertEqual(response.status_code, HTTPStatus.OK)
390
+ self.assertEqual(json.loads(response.content), expected_fleet_composition)
391
+
392
+ @patch("fleetfinder.views.get_fleet_composition")
393
+ def test_handles_empty_fleet(self, mock_get_fleet_composition):
394
+ """
395
+ Test that the ajax_fleet_details view handles an empty fleet correctly.
396
+
397
+ :param mock_get_fleet_composition:
398
+ :type mock_get_fleet_composition:
399
+ :return:
400
+ :rtype:
401
+ """
402
+
403
+ mock_get_fleet_composition.return_value = SimpleNamespace(
404
+ fleet=[],
405
+ aggregate={},
406
+ )
407
+
408
+ self.client.force_login(user=self.user_with_manage_perms)
409
+
410
+ url = reverse("fleetfinder:ajax_fleet_details", args=[self.fleet_id])
411
+ response = self.client.get(url)
412
+
413
+ expected_fleet_composition = {"fleet_member": [], "fleet_composition": []}
414
+
415
+ self.assertEqual(response.status_code, HTTPStatus.OK)
416
+ self.assertEqual(json.loads(response.content), expected_fleet_composition)