diff --git a/patchwork/tests/api/test_series.py b/patchwork/tests/api/test_series.py index 597b61a9..485dc1d0 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 @@ -203,3 +204,49 @@ 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 test_series_linking(self): + series_a = self._create_series() + series_b = create_series( + project=series_a.project, submitter=series_a.submitter + ) + series_c = create_series( + project=series_a.project, submitter=series_a.submitter + ) + series_d = create_series(submitter=series_a.submitter) + + self.client.authenticate(user=series_a.submitter.user) + + url = reverse( + 'api-series-link', + kwargs={'pk': series_a.id, 'related_series_id': series_b.id}, + ) + resp = self.client.patch(url) + related_series = json.loads(resp.content).get('related_series') + self.assertEqual(len(related_series), 1) + self.assertEqual( + related_series[0]['web_url'], + f'http://example.com/project/myproject/list/?series={series_b.id}', + ) + + url = reverse( + 'api-series-link', + kwargs={'pk': series_a.id, 'related_series_id': series_c.id}, + ) + resp = self.client.patch(url) + related_series = json.loads(resp.content).get('related_series') + self.assertEqual(len(related_series), 2) + self.assertEqual( + related_series[1]['web_url'], + f'http://example.com/project/myproject/list/?series={series_c.id}', + ) + + url = reverse( + 'api-series-link', + kwargs={'pk': series_a.id, 'related_series_id': series_d.id}, + ) + resp = self.client.patch(url) + self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(len(related_series), 2) + + print(series_a.related_series.all()) 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)