diff --git a/patchwork/tests/api/test_series.py b/patchwork/tests/api/test_series.py index 597b61a9..801e9449 100644 --- a/patchwork/tests/api/test_series.py +++ b/patchwork/tests/api/test_series.py @@ -2,6 +2,7 @@ # Copyright (C) 2018 Stephen Finucane # # SPDX-License-Identifier: GPL-2.0-or-later +import json from django.test import override_settings from django.urls import NoReverseMatch @@ -16,6 +17,7 @@ from patchwork.tests.utils import create_project from patchwork.tests.utils import create_series from patchwork.tests.utils import create_user +from patchwork.models import Person @override_settings(ENABLE_REST_API=True) @@ -203,3 +205,75 @@ def test_create_update_delete(self): resp = self.client.delete(self.api_url(series.id)) self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) + + def _generate_related_ids_payload(self, series, id): + related_ids = series.related_series.all().values_list('id', flat=True) + return list(related_ids) + [id] + + def test_series_linking(self): + user = create_user() + person = Person.objects.get(user=user) + project_obj = create_project(linkname='myproject') + series_a = create_series(project=project_obj, submitter=person) + create_cover(series=series_a) + create_patch(series=series_a) + + self.client.authenticate(user=user) + url = reverse('api-series-detail', kwargs={'pk': series_a.id}) + + # Link to another series + series_b = create_series( + project=series_a.project, submitter=series_a.submitter + ) + + resp = self.client.patch( + url, + data={ + 'related_series': self._generate_related_ids_payload( + series_a, series_b.id + ) + }, + ) + related_series = json.loads(resp.content).get('related_series') + self.assertEqual(resp.status_code, status.HTTP_200_OK) + self.assertEqual(len(related_series), 1) + self.assertEqual( + related_series[0]['web_url'], + f'http://example.com/project/myproject/list/?series={series_b.id}', + ) + + # Link to more than one series + series_c = create_series( + project=series_a.project, submitter=series_a.submitter + ) + resp = self.client.patch( + url, + data={ + 'related_series': self._generate_related_ids_payload( + series_a, series_c.id + ) + }, + ) + + related_series = json.loads(resp.content).get('related_series') + self.assertEqual(resp.status_code, status.HTTP_200_OK) + self.assertEqual(len(related_series), 2) + self.assertEqual( + related_series[1]['web_url'], + f'http://example.com/project/myproject/list/?series={series_c.id}', + ) + + # Link to a series from a different project + series_d = create_series(submitter=series_a.submitter) + + resp = self.client.patch( + url, + data={ + 'related_series': self._generate_related_ids_payload( + series_a, series_d.id + ) + }, + ) + + related_series = json.loads(resp.content).get('related_series') + self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) diff --git a/patchwork/tests/test_series.py b/patchwork/tests/test_series.py index ce114042..220cb81e 100644 --- a/patchwork/tests/test_series.py +++ b/patchwork/tests/test_series.py @@ -8,11 +8,13 @@ import unittest from django.test import TestCase +from rest_framework.request import HttpRequest from patchwork import models from patchwork import parser from patchwork.tests import utils from patchwork.views.utils import patch_to_mbox +from patchwork.api.series import SeriesSerializer TEST_SERIES_DIR = os.path.join(os.path.dirname(__file__), 'series') @@ -804,3 +806,49 @@ def test_custom_name(self): self.assertEqual(series.name, series_name) mbox.close() + + +class SeriesSerializerTestCase(TestCase): + def _mock_request(self): + mock_request = HttpRequest() + mock_request.version = '1.4' + mock_request.META['SERVER_NAME'] = 'example.com' + mock_request.META['SERVER_PORT'] = '8000' + + return mock_request + + def _create_serializer(self, series, related_series, mock_request): + related_ids = list( + series.related_series.all().values_list('id', flat=True) + ) + [related_series.pk] + + return SeriesSerializer( + series, + context={'request': mock_request}, + data={'related_series': related_ids}, + partial=True, + ) + + def test_related_series_validation_equal_project_id(self): + series_a = utils.create_series() + series_b = utils.create_series(project=series_a.project) + + mock_request = self._mock_request() + serializer = self._create_serializer(series_a, series_b, mock_request) + serializer.is_valid() + serializer.save() + + related_series_urls = serializer.data['related_series'] + self.assertEqual(len(related_series_urls), 1) + self.assertEqual( + f'series={series_b.id}' in related_series_urls[0]['web_url'], True + ) + + def test_related_series_validation_different_project_id(self): + series_a = utils.create_series() + series_b = utils.create_series() + + mock_request = self._mock_request() + serializer = self._create_serializer(series_a, series_b, mock_request) + is_valid = serializer.is_valid() + self.assertFalse(is_valid)