From f7507b19a942a89679522dea955d219f131e008e Mon Sep 17 00:00:00 2001 From: duker Date: Sun, 22 Jul 2018 09:15:19 +0300 Subject: [PATCH 1/2] stb#250 Create initial tests for refarm_redirects app --- tests/refarm_redirects/test_views.py | 54 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/tests/refarm_redirects/test_views.py b/tests/refarm_redirects/test_views.py index 26aee97..a332982 100644 --- a/tests/refarm_redirects/test_views.py +++ b/tests/refarm_redirects/test_views.py @@ -1,2 +1,52 @@ -# @todo #140:30m Move test case to custom redirects app -# You can find example at SE's tests_views.Redirects app +import unittest + +from django.contrib.redirects.models import Redirect +from django.contrib.sites.models import Site +from django.test import TestCase + + +class Redirects(TestCase): + + def test_redirect_from_existing_page(self): + """`refarm-site.redirects` app should redirect from existing url too.""" + # take some existing `url_from` + # @todo #360:30m Remove hardcoded fixture data. + # Replace `url_from` and `url_to` with urls, generated from db. + # It'll be much more short and clear. + url_from = '/catalog/categories/category-0/tags/6-v/' + # create redirect from `url_from` to another existing one - `url_to` + url_to = '/catalog/categories/category-0/' + Redirect.objects.create( + site=Site.objects.first(), + old_path=url_from, + new_path=url_to + ) + + # `url_from` should redirect to `url_to` + response = self.client.get(url_from) + self.assertEqual(response.status_code, 301) + + # @todo #360:60m Add db constraint for looped redirect. + # Example of looped redirect: + # `/news/one-two/ --> /news/one-two/` + # `60m` because schema and data migrations are needed. + # And fix test. + @unittest.expectedFailure + def test_looped_redirect(self): + """ + Redirect like `/news/one-two/ --> /news/one-two/` should fail. + + It should meet db constraint while adding. + """ + # hardcoded fixtures will be fixed with task in test ahead. + url_from, url_to = 2 * ['/catalog/categories/category-0/tags/6-v/'] + # should raise exception, but not. Pdd task ahead will fix it. + Redirect.objects.create( + site=Site.objects.first(), + old_path=url_from, + new_path=url_to + ) + + # `url_from` should redirect to `url_to` + response = self.client.get(url_from) + self.assertEqual(response.status_code, 200) From 7ff22bf14c49c729dbf80b327eca3212c93f2d88 Mon Sep 17 00:00:00 2001 From: duker Date: Sun, 22 Jul 2018 21:11:38 +0300 Subject: [PATCH 2/2] stb#250 Review#1 minor fixes. Change loop redirects test condition --- tests/refarm_redirects/test_views.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/refarm_redirects/test_views.py b/tests/refarm_redirects/test_views.py index a332982..28f8fe7 100644 --- a/tests/refarm_redirects/test_views.py +++ b/tests/refarm_redirects/test_views.py @@ -2,6 +2,7 @@ from django.contrib.redirects.models import Redirect from django.contrib.sites.models import Site +from django.db.utils import IntegrityError from django.test import TestCase @@ -39,14 +40,11 @@ def test_looped_redirect(self): It should meet db constraint while adding. """ # hardcoded fixtures will be fixed with task in test ahead. - url_from, url_to = 2 * ['/catalog/categories/category-0/tags/6-v/'] + url_from = url_to = '/catalog/categories/category-0/tags/6-v/' # should raise exception, but not. Pdd task ahead will fix it. - Redirect.objects.create( - site=Site.objects.first(), - old_path=url_from, - new_path=url_to - ) - - # `url_from` should redirect to `url_to` - response = self.client.get(url_from) - self.assertEqual(response.status_code, 200) + with self.assertRaises(IntegrityError): + Redirect.objects.create( + site=Site.objects.first(), + old_path=url_from, + new_path=url_to + )