From f8c25b5eaa85c55d1db7aa1fb57b5fe80abb92be Mon Sep 17 00:00:00 2001 From: andrepapoti Date: Mon, 18 Mar 2024 11:41:59 -0300 Subject: [PATCH] tests: Add tests for linking series feature Closes #506 Signed-off-by: andrepapoti --- patchwork/tests/api/test_series.py | 21 +++++++++++++ patchwork/tests/test_series.py | 48 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/patchwork/tests/api/test_series.py b/patchwork/tests/api/test_series.py index 597b61a9..7043884b 100644 --- a/patchwork/tests/api/test_series.py +++ b/patchwork/tests/api/test_series.py @@ -203,3 +203,24 @@ 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 + ) + + 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.options(url) + resp = self.client.patch(url) + + related_series = resp.data.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}', + ) diff --git a/patchwork/tests/test_series.py b/patchwork/tests/test_series.py index ce114042..3e44d89b 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 test_related_series_validation_equal_project_id(self): + series_a = utils.create_series() + series_b = utils.create_series(project=series_a.project) + + mock_request = HttpRequest() + mock_request.version = '1.4' + mock_request.META['SERVER_NAME'] = 'example.com' + mock_request.META['SERVER_PORT'] = '8000' + + related_series_ids_a = list( + series_a.related_series.all().values_list('id', flat=True) + ) + serializer = SeriesSerializer( + series_a, + context={'request': mock_request}, + data={'related_series': related_series_ids_a + [series_b.pk]}, + partial=True, + ) + 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 = HttpRequest() + mock_request.version = '1.4' + mock_request.META['SERVER_NAME'] = 'example.com' + mock_request.META['SERVER_PORT'] = '8000' + + serializer = SeriesSerializer( + series_a, + context={'request': mock_request}, + data={'related_series': [series_b.pk]}, + ) + is_valid = serializer.is_valid() + self.assertFalse(is_valid)