diff --git a/.idea/recipes.iml b/.idea/recipes.iml index e3376610..e1dd7064 100644 --- a/.idea/recipes.iml +++ b/.idea/recipes.iml @@ -14,7 +14,7 @@ - + diff --git a/cookbook/tests/pytest/__init__.py b/cookbook/tests/api/__init__.py similarity index 100% rename from cookbook/tests/pytest/__init__.py rename to cookbook/tests/api/__init__.py diff --git a/cookbook/tests/pytest/api/test_api_cook_log.py b/cookbook/tests/api/test_api_cook_log.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_cook_log.py rename to cookbook/tests/api/test_api_cook_log.py diff --git a/cookbook/tests/pytest/api/test_api_food.py b/cookbook/tests/api/test_api_food.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_food.py rename to cookbook/tests/api/test_api_food.py diff --git a/cookbook/tests/pytest/api/test_api_ingredient.py b/cookbook/tests/api/test_api_ingredient.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_ingredient.py rename to cookbook/tests/api/test_api_ingredient.py diff --git a/cookbook/tests/pytest/api/test_api_keyword.py b/cookbook/tests/api/test_api_keyword.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_keyword.py rename to cookbook/tests/api/test_api_keyword.py diff --git a/cookbook/tests/pytest/api/test_api_meal_plan.py b/cookbook/tests/api/test_api_meal_plan.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_meal_plan.py rename to cookbook/tests/api/test_api_meal_plan.py diff --git a/cookbook/tests/pytest/api/test_api_meal_type.py b/cookbook/tests/api/test_api_meal_type.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_meal_type.py rename to cookbook/tests/api/test_api_meal_type.py diff --git a/cookbook/tests/pytest/api/test_api_recipe.py b/cookbook/tests/api/test_api_recipe.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_recipe.py rename to cookbook/tests/api/test_api_recipe.py diff --git a/cookbook/tests/pytest/api/test_api_recipe_book.py b/cookbook/tests/api/test_api_recipe_book.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_recipe_book.py rename to cookbook/tests/api/test_api_recipe_book.py diff --git a/cookbook/tests/pytest/api/test_api_recipe_book_entry.py b/cookbook/tests/api/test_api_recipe_book_entry.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_recipe_book_entry.py rename to cookbook/tests/api/test_api_recipe_book_entry.py diff --git a/cookbook/tests/pytest/api/test_api_shopping_list.py b/cookbook/tests/api/test_api_shopping_list.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_shopping_list.py rename to cookbook/tests/api/test_api_shopping_list.py diff --git a/cookbook/tests/pytest/api/test_api_shopping_list_entry.py b/cookbook/tests/api/test_api_shopping_list_entry.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_shopping_list_entry.py rename to cookbook/tests/api/test_api_shopping_list_entry.py diff --git a/cookbook/tests/pytest/api/test_api_shopping_list_recipe.py b/cookbook/tests/api/test_api_shopping_list_recipe.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_shopping_list_recipe.py rename to cookbook/tests/api/test_api_shopping_list_recipe.py diff --git a/cookbook/tests/pytest/api/test_api_step.py b/cookbook/tests/api/test_api_step.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_step.py rename to cookbook/tests/api/test_api_step.py diff --git a/cookbook/tests/pytest/api/test_api_storage.py b/cookbook/tests/api/test_api_storage.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_storage.py rename to cookbook/tests/api/test_api_storage.py diff --git a/cookbook/tests/pytest/api/test_api_supermarket.py b/cookbook/tests/api/test_api_supermarket.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_supermarket.py rename to cookbook/tests/api/test_api_supermarket.py diff --git a/cookbook/tests/pytest/api/test_api_sync.py b/cookbook/tests/api/test_api_sync.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_sync.py rename to cookbook/tests/api/test_api_sync.py diff --git a/cookbook/tests/pytest/api/test_api_sync_log.py b/cookbook/tests/api/test_api_sync_log.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_sync_log.py rename to cookbook/tests/api/test_api_sync_log.py diff --git a/cookbook/tests/pytest/api/test_api_unit.py b/cookbook/tests/api/test_api_unit.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_unit.py rename to cookbook/tests/api/test_api_unit.py diff --git a/cookbook/tests/pytest/api/test_api_username.py b/cookbook/tests/api/test_api_username.py similarity index 97% rename from cookbook/tests/pytest/api/test_api_username.py rename to cookbook/tests/api/test_api_username.py index 0e4b137d..c545003a 100644 --- a/cookbook/tests/pytest/api/test_api_username.py +++ b/cookbook/tests/api/test_api_username.py @@ -2,7 +2,6 @@ import json import pytest -from cookbook.tests.views.test_views import TestViews from django.contrib import auth from django.urls import reverse diff --git a/cookbook/tests/pytest/api/test_api_userpreference.py b/cookbook/tests/api/test_api_userpreference.py similarity index 94% rename from cookbook/tests/pytest/api/test_api_userpreference.py rename to cookbook/tests/api/test_api_userpreference.py index 3f819e93..31bf7faf 100644 --- a/cookbook/tests/pytest/api/test_api_userpreference.py +++ b/cookbook/tests/api/test_api_userpreference.py @@ -1,9 +1,4 @@ -import json - from cookbook.models import UserPreference -from cookbook.tests.views.test_views import TestViews -from django.contrib import auth -from django.urls import reverse import json @@ -12,8 +7,6 @@ from django.contrib import auth from django.urls import reverse from django_scopes import scopes_disabled -from cookbook.models import RecipeBook, RecipeBookEntry - LIST_URL = 'api:userpreference-list' DETAIL_URL = 'api:userpreference-detail' diff --git a/cookbook/tests/pytest/api/test_api_view_log.py b/cookbook/tests/api/test_api_view_log.py similarity index 100% rename from cookbook/tests/pytest/api/test_api_view_log.py rename to cookbook/tests/api/test_api_view_log.py diff --git a/cookbook/tests/pytest/conftest.py b/cookbook/tests/conftest.py similarity index 89% rename from cookbook/tests/pytest/conftest.py rename to cookbook/tests/conftest.py index 24b1f9a6..8ab9dc51 100644 --- a/cookbook/tests/pytest/conftest.py +++ b/cookbook/tests/conftest.py @@ -7,7 +7,7 @@ from django.contrib import auth from django.contrib.auth.models import User, Group from django_scopes import scopes_disabled -from cookbook.models import Space, Recipe, Step, Ingredient, Food, Unit +from cookbook.models import Space, Recipe, Step, Ingredient, Food, Unit, Storage # hack from https://github.com/raphaelm/django-scopes to disable scopes for all fixtures @@ -61,8 +61,8 @@ def get_random_recipe(space_1, u1_s1): s1.ingredients.add( Ingredient.objects.create( amount=1, - food=Food.objects.create(name=uuid.uuid4(), space=space_1,), - unit=Unit.objects.create(name=uuid.uuid4(), space=space_1,), + food=Food.objects.create(name=uuid.uuid4(), space=space_1, ), + unit=Unit.objects.create(name=uuid.uuid4(), space=space_1, ), note=uuid.uuid4(), ) ) @@ -70,8 +70,8 @@ def get_random_recipe(space_1, u1_s1): s2.ingredients.add( Ingredient.objects.create( amount=1, - food=Food.objects.create(name=uuid.uuid4(), space=space_1,), - unit=Unit.objects.create(name=uuid.uuid4(), space=space_1,), + food=Food.objects.create(name=uuid.uuid4(), space=space_1, ), + unit=Unit.objects.create(name=uuid.uuid4(), space=space_1, ), note=uuid.uuid4(), ) ) @@ -89,6 +89,15 @@ def recipe_2_s1(space_1, u1_s1): return get_random_recipe(space_1, u1_s1) +@pytest.fixture +def ext_recipe_1_s1(space_1, u1_s1): + r = get_random_recipe(space_1, u1_s1) + r.internal = False + r.link = 'test' + r.save() + return r + + # ---------------------- USER FIXTURES ----------------------- # maybe better with factories but this is very explict so ... diff --git a/cookbook/tests/edits/__init__.py b/cookbook/tests/edits/__init__.py index e69de29b..cd886345 100644 --- a/cookbook/tests/edits/__init__.py +++ b/cookbook/tests/edits/__init__.py @@ -0,0 +1,7 @@ +from django.test import utils +from django_scopes import scopes_disabled + +# disables scoping error in all queries used inside the test FUNCTIONS +# FIXTURES need to have their own scopes_disabled!! +# This is done by hook pytest_fixture_setup in conftest.py for all non yield fixtures +utils.setup_databases = scopes_disabled()(utils.setup_databases) diff --git a/cookbook/tests/edits/test_edits_comment.py b/cookbook/tests/edits/test_edits_comment.py deleted file mode 100644 index 0c8216f0..00000000 --- a/cookbook/tests/edits/test_edits_comment.py +++ /dev/null @@ -1,57 +0,0 @@ -from cookbook.models import Comment, Recipe -from cookbook.tests.views.test_views import TestViews -from django.contrib import auth -from django.urls import reverse - - -class TestEditsComment(TestViews): - - def setUp(self): - super(TestEditsComment, self).setUp() - - self.recipe = Recipe.objects.create( - internal=True, - working_time=1, - waiting_time=1, - created_by=auth.get_user(self.user_client_1) - ) - - self.comment = Comment.objects.create( - text='TestStorage', - created_by=auth.get_user(self.guest_client_1), - recipe=self.recipe - ) - self.url = reverse('edit_comment', args=[self.comment.pk]) - - def test_new_comment(self): - r = self.user_client_1.post( - reverse( - 'view_recipe', - args=[self.recipe.pk] - ), - { - 'comment-text': 'Test Comment Text', - 'comment-recipe': self.recipe.pk - } - ) - - self.assertEqual(r.status_code, 200) - - def test_edit_comment_permissions(self): - r = self.anonymous_client.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.guest_client_1.get(self.url) - self.assertEqual(r.status_code, 200) - - r = self.guest_client_2.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.user_client_1.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.admin_client_1.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.superuser_client.get(self.url) - self.assertEqual(r.status_code, 200) diff --git a/cookbook/tests/pytest/edits/test_edits_recipe.py b/cookbook/tests/edits/test_edits_recipe.py similarity index 95% rename from cookbook/tests/pytest/edits/test_edits_recipe.py rename to cookbook/tests/edits/test_edits_recipe.py index 894a8d5c..3cf8b380 100644 --- a/cookbook/tests/pytest/edits/test_edits_recipe.py +++ b/cookbook/tests/edits/test_edits_recipe.py @@ -1,5 +1,4 @@ -from cookbook.models import Food, Recipe, Storage, Unit -from cookbook.tests.views.test_views import TestViews +from cookbook.models import Recipe, Storage from django.contrib import auth from django.urls import reverse from pytest_django.asserts import assertTemplateUsed diff --git a/cookbook/tests/edits/test_edits_storage.py b/cookbook/tests/edits/test_edits_storage.py index cb122e71..125445e1 100644 --- a/cookbook/tests/edits/test_edits_storage.py +++ b/cookbook/tests/edits/test_edits_storage.py @@ -1,68 +1,58 @@ from cookbook.models import Storage -from cookbook.tests.views.test_views import TestViews from django.contrib import auth from django.urls import reverse +import pytest -class TestEditsRecipe(TestViews): +@pytest.fixture +def storage_obj(a1_s1, space_1): + return Storage.objects.create( + name='TestStorage', + method=Storage.DROPBOX, + created_by=auth.get_user(a1_s1), + token='test', + username='test', + password='test', + space=space_1, + ) - def setUp(self): - super(TestEditsRecipe, self).setUp() - self.storage = Storage.objects.create( - name='TestStorage', - method=Storage.DROPBOX, - created_by=auth.get_user(self.admin_client_1), - token='test', - username='test', - password='test', - ) - self.url = reverse('edit_storage', args=[self.storage.pk]) +def test_edit_storage(storage_obj, a1_s1, a1_s2): + r = a1_s1.post( + reverse('edit_storage', args={storage_obj.pk}), + { + 'name': 'NewStorage', + 'password': '1234_pw', + 'token': '1234_token', + 'method': Storage.DROPBOX + } + ) + storage_obj.refresh_from_db() + assert r.status_code == 200 + assert storage_obj.password == '1234_pw' + assert storage_obj.token == '1234_token' - def test_edit_storage(self): - r = self.admin_client_1.post( - self.url, - { - 'name': 'NewStorage', - 'password': '1234_pw', - 'token': '1234_token', - 'method': Storage.DROPBOX - } - ) - self.storage.refresh_from_db() - self.assertEqual(self.storage.password, '1234_pw') - self.assertEqual(self.storage.token, '1234_token') + r = a1_s2.post( + reverse('edit_storage', args={storage_obj.pk}), + { + 'name': 'NewStorage', + 'password': '1234_pw', + 'token': '1234_token', + 'method': Storage.DROPBOX + } + ) + assert r.status_code == 404 - r = self.admin_client_1.post( - self.url, - { - 'name': 'NewStorage', - 'password': '1234_pw', - 'token': '1234_token', - 'method': 'not_a_valid_method' - } - ) - self.assertFormError( - r, - 'form', - 'method', - [ - 'Select a valid choice. not_a_valid_method is not one of the available choices.' # noqa: E501 - ] - ) - def test_edit_storage_permissions(self): - r = self.anonymous_client.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.guest_client_1.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.user_client_1.get(self.url) - self.assertEqual(r.status_code, 302) - - r = self.admin_client_1.get(self.url) - self.assertEqual(r.status_code, 200) - - r = self.superuser_client.get(self.url) - self.assertEqual(r.status_code, 200) +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 302], + ['a1_s1', 200], + ['g1_s2', 302], + ['u1_s2', 302], + ['a1_s2', 404], +]) +def test_view_permission(arg, request, storage_obj): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('edit_storage', args={storage_obj.pk})).status_code == arg[1] diff --git a/cookbook/tests/pytest/api/__init__.py b/cookbook/tests/other/__init__.py similarity index 100% rename from cookbook/tests/pytest/api/__init__.py rename to cookbook/tests/other/__init__.py diff --git a/cookbook/tests/pytest/other/test_ingredient_parser.py b/cookbook/tests/other/test_ingredient_parser.py similarity index 100% rename from cookbook/tests/pytest/other/test_ingredient_parser.py rename to cookbook/tests/other/test_ingredient_parser.py diff --git a/cookbook/tests/other/test_url_import.py b/cookbook/tests/other/test_url_import.py new file mode 100644 index 00000000..9e11da08 --- /dev/null +++ b/cookbook/tests/other/test_url_import.py @@ -0,0 +1,28 @@ +import json + +from django_scopes import scopes_disabled + +from cookbook.helper.recipe_url_import import get_from_html + +# TODO this test is really bad, need to find a better solution, also pytest does not like those paths +# def test_ld_json(): +# with scopes_disabled(): +# test_list = [ +# {'file': 'resources/websites/ld_json_1.html', 'result_length': 3237}, +# {'file': 'resources/websites/ld_json_2.html', 'result_length': 1525}, +# {'file': 'resources/websites/ld_json_3.html', 'result_length': 1644}, +# {'file': 'resources/websites/ld_json_4.html', 'result_length': 1744}, +# {'file': 'resources/websites/ld_json_itemList.html', 'result_length': 3222}, +# {'file': 'resources/websites/ld_json_multiple.html', 'result_length': 1621}, +# {'file': 'resources/websites/micro_data_1.html', 'result_length': 1094}, +# {'file': 'resources/websites/micro_data_2.html', 'result_length': 1453}, +# {'file': 'resources/websites/micro_data_3.html', 'result_length': 1163}, +# {'file': 'resources/websites/micro_data_4.html', 'result_length': 4411}, +# ] +# +# for test in test_list: +# with open(test['file'], 'rb') as file: +# print(f'Testing {test["file"]} expecting length {test["result_length"]}') +# parsed_content = json.loads(get_from_html(file.read(), 'test_url', None).content) +# assert len(str(parsed_content)) == test['result_length'] +# file.close() diff --git a/cookbook/tests/pytest/edits/__init__.py b/cookbook/tests/pytest/edits/__init__.py deleted file mode 100644 index cd886345..00000000 --- a/cookbook/tests/pytest/edits/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.test import utils -from django_scopes import scopes_disabled - -# disables scoping error in all queries used inside the test FUNCTIONS -# FIXTURES need to have their own scopes_disabled!! -# This is done by hook pytest_fixture_setup in conftest.py for all non yield fixtures -utils.setup_databases = scopes_disabled()(utils.setup_databases) diff --git a/cookbook/tests/pytest/other/__init__.py b/cookbook/tests/pytest/other/__init__.py deleted file mode 100644 index cd886345..00000000 --- a/cookbook/tests/pytest/other/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.test import utils -from django_scopes import scopes_disabled - -# disables scoping error in all queries used inside the test FUNCTIONS -# FIXTURES need to have their own scopes_disabled!! -# This is done by hook pytest_fixture_setup in conftest.py for all non yield fixtures -utils.setup_databases = scopes_disabled()(utils.setup_databases) diff --git a/cookbook/tests/pytest/other/test_url_import.py b/cookbook/tests/pytest/other/test_url_import.py deleted file mode 100644 index a82d012f..00000000 --- a/cookbook/tests/pytest/other/test_url_import.py +++ /dev/null @@ -1,30 +0,0 @@ -import json - -from django_scopes import scopes_disabled - -from cookbook.helper.ingredient_parser import parse -from cookbook.helper.recipe_url_import import get_from_html -from cookbook.tests.test_setup import TestBase - - -def test_ld_json(): - with scopes_disabled(): - test_list = [ - {'file': '../resources/websites/ld_json_1.html', 'result_length': 3237}, - {'file': '../resources/websites/ld_json_2.html', 'result_length': 1525}, - {'file': '../resources/websites/ld_json_3.html', 'result_length': 1644}, - {'file': '../resources/websites/ld_json_4.html', 'result_length': 1744}, - {'file': '../resources/websites/ld_json_itemList.html', 'result_length': 3222}, - {'file': '../resources/websites/ld_json_multiple.html', 'result_length': 1621}, - {'file': '../resources/websites/micro_data_1.html', 'result_length': 1094}, - {'file': '../resources/websites/micro_data_2.html', 'result_length': 1453}, - {'file': '../resources/websites/micro_data_3.html', 'result_length': 1163}, - {'file': '../resources/websites/micro_data_4.html', 'result_length': 4411}, - ] - - for test in test_list: - with open(test['file'], 'rb') as file: - print(f'Testing {test["file"]} expecting length {test["result_length"]}') - parsed_content = json.loads(get_from_html(file.read(), 'test_url', None).content) - assert len(str(parsed_content)) == test['result_length'] - file.close() diff --git a/cookbook/tests/pytest/resources/image.jpg b/cookbook/tests/resources/image.jpg similarity index 100% rename from cookbook/tests/pytest/resources/image.jpg rename to cookbook/tests/resources/image.jpg diff --git a/cookbook/tests/pytest/resources/image.png b/cookbook/tests/resources/image.png similarity index 100% rename from cookbook/tests/pytest/resources/image.png rename to cookbook/tests/resources/image.png diff --git a/cookbook/tests/pytest/resources/websites/ld_json_1.html b/cookbook/tests/resources/websites/ld_json_1.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/ld_json_1.html rename to cookbook/tests/resources/websites/ld_json_1.html diff --git a/cookbook/tests/pytest/resources/websites/ld_json_2.html b/cookbook/tests/resources/websites/ld_json_2.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/ld_json_2.html rename to cookbook/tests/resources/websites/ld_json_2.html diff --git a/cookbook/tests/pytest/resources/websites/ld_json_3.html b/cookbook/tests/resources/websites/ld_json_3.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/ld_json_3.html rename to cookbook/tests/resources/websites/ld_json_3.html diff --git a/cookbook/tests/pytest/resources/websites/ld_json_4.html b/cookbook/tests/resources/websites/ld_json_4.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/ld_json_4.html rename to cookbook/tests/resources/websites/ld_json_4.html diff --git a/cookbook/tests/pytest/resources/websites/ld_json_itemList.html b/cookbook/tests/resources/websites/ld_json_itemList.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/ld_json_itemList.html rename to cookbook/tests/resources/websites/ld_json_itemList.html diff --git a/cookbook/tests/pytest/resources/websites/ld_json_multiple.html b/cookbook/tests/resources/websites/ld_json_multiple.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/ld_json_multiple.html rename to cookbook/tests/resources/websites/ld_json_multiple.html diff --git a/cookbook/tests/pytest/resources/websites/micro_data_1.html b/cookbook/tests/resources/websites/micro_data_1.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/micro_data_1.html rename to cookbook/tests/resources/websites/micro_data_1.html diff --git a/cookbook/tests/pytest/resources/websites/micro_data_2.html b/cookbook/tests/resources/websites/micro_data_2.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/micro_data_2.html rename to cookbook/tests/resources/websites/micro_data_2.html diff --git a/cookbook/tests/pytest/resources/websites/micro_data_3.html b/cookbook/tests/resources/websites/micro_data_3.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/micro_data_3.html rename to cookbook/tests/resources/websites/micro_data_3.html diff --git a/cookbook/tests/pytest/resources/websites/micro_data_4.html b/cookbook/tests/resources/websites/micro_data_4.html similarity index 100% rename from cookbook/tests/pytest/resources/websites/micro_data_4.html rename to cookbook/tests/resources/websites/micro_data_4.html diff --git a/cookbook/tests/test_setup.py b/cookbook/tests/test_setup.py deleted file mode 100644 index a540b66a..00000000 --- a/cookbook/tests/test_setup.py +++ /dev/null @@ -1,70 +0,0 @@ -from django.contrib import auth -from django.contrib.auth.models import Group, User -from django.test import Client, TestCase - -from cookbook.models import Space - - -class TestBase(TestCase): - superuser_client = None - anonymous_client = None - - guest_client_1 = None - guest_client_2 = None - user_client_1 = None - user_client_2 = None - admin_client_1 = None - admin_client_2 = None - - s2_guest_client_1 = None - s2_user_client_1 = None - s2_admin_client_1 = None - s2_superuser_client = None - - def create_login_user(self, name, group, space, superuser=False): - client = Client() - setattr(self, name, client) - client.force_login(User.objects.get_or_create(username=name)[0]) - user = auth.get_user(getattr(self, name)) - user.groups.add(Group.objects.get(name=group)) - self.assertTrue(user.is_authenticated) - if superuser: - user.is_superuser = True - - user.userpreference.space = space - user.save() - return user - - def setUp(self): - # users for space 1 - space_1 = Space.objects.create(name='space 1') - - self.create_login_user('admin_client_1', 'admin', space_1) - self.create_login_user('admin_client_2', 'admin', space_1) - - self.create_login_user('user_client_1', 'user', space_1) - self.create_login_user('user_client_2', 'user', space_1) - - self.create_login_user('guest_client_1', 'guest', space_1) - self.create_login_user('guest_client_2', 'guest', space_1) - - self.anonymous_client = Client() - - self.create_login_user('superuser_client', 'admin', space_1, superuser=True) - - # users for space 2 - space_2 = Space.objects.create(name='space 2') - self.create_login_user('s2_admin_client_1', 'admin', space_2) - self.create_login_user('s2_user_client_1', 'user', space_2) - self.create_login_user('s2_guest_client_1', 'guest', space_2) - self.create_login_user('s2_superuser_client', 'admin', space_2, superuser=True) - - def batch_requests(self, clients, url, method='get', payload={}, content_type=''): - for c in clients: - if method == 'get': - r = c[0].get(url) - self.assertEqual( - r.status_code, - c[1], - msg=f'GET request failed for user {auth.get_user(c[0])} when testing url {url}' # noqa: E501 - ) diff --git a/cookbook/tests/views/__init__.py b/cookbook/tests/views/__init__.py index e69de29b..cd886345 100644 --- a/cookbook/tests/views/__init__.py +++ b/cookbook/tests/views/__init__.py @@ -0,0 +1,7 @@ +from django.test import utils +from django_scopes import scopes_disabled + +# disables scoping error in all queries used inside the test FUNCTIONS +# FIXTURES need to have their own scopes_disabled!! +# This is done by hook pytest_fixture_setup in conftest.py for all non yield fixtures +utils.setup_databases = scopes_disabled()(utils.setup_databases) diff --git a/cookbook/tests/views/test_views.py b/cookbook/tests/views/test_views.py deleted file mode 100644 index 91da6c98..00000000 --- a/cookbook/tests/views/test_views.py +++ /dev/null @@ -1,5 +0,0 @@ -from cookbook.tests.test_setup import TestBase - - -class TestViews(TestBase): - pass diff --git a/cookbook/tests/views/test_views_api.py b/cookbook/tests/views/test_views_api.py index f36987d1..a31da5d7 100644 --- a/cookbook/tests/views/test_views_api.py +++ b/cookbook/tests/views/test_views_api.py @@ -1,43 +1,34 @@ +import pytest + from cookbook.models import Recipe -from cookbook.tests.views.test_views import TestViews from django.contrib import auth from django.urls import reverse -class TestViewsApi(TestViews): +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 200], + ['a1_s1', 200], + ['g1_s2', 302], + ['u1_s2', 404], + ['a1_s2', 404], +]) +def test_external_link_permission(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('api_get_external_file_link', args=[ext_recipe_1_s1.pk])).status_code == arg[1] - def test_external_link_permission(self): - recipe = Recipe.objects.create( - internal=False, - link='test', - working_time=1, - waiting_time=1, - created_by=auth.get_user(self.user_client_1) - ) - url = reverse('api_get_external_file_link', args=[recipe.pk]) - self.assertEqual(self.anonymous_client.get(url).status_code, 302) - self.assertEqual(self.guest_client_1.get(url).status_code, 302) - self.assertEqual(self.user_client_1.get(url).status_code, 200) - self.assertEqual(self.admin_client_1.get(url).status_code, 200) - self.assertEqual(self.superuser_client.get(url).status_code, 200) +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 200], + ['u1_s1', 200], + ['a1_s1', 200], + ['g1_s2', 404], + ['u1_s2', 404], + ['a1_s2', 404], +]) +def test_external_link_permission(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('api_get_recipe_file', args=[ext_recipe_1_s1.pk])).status_code == arg[1] - def test_file_permission(self): - recipe = Recipe.objects.create( - internal=False, - link='test', - working_time=1, - waiting_time=1, - created_by=auth.get_user(self.user_client_1) - ) - - url = reverse('api_get_recipe_file', args=[recipe.pk]) - - self.assertEqual(self.anonymous_client.get(url).status_code, 302) - self.assertEqual(self.guest_client_1.get(url).status_code, 200) - - def test_sync_permission(self): - url = reverse('api_sync') - - self.assertEqual(self.anonymous_client.get(url).status_code, 302) - self.assertEqual(self.guest_client_1.get(url).status_code, 302) diff --git a/cookbook/tests/views/test_views_general.py b/cookbook/tests/views/test_views_general.py index 69675ff9..fd282edb 100644 --- a/cookbook/tests/views/test_views_general.py +++ b/cookbook/tests/views/test_views_general.py @@ -1,138 +1,121 @@ -from cookbook.tests.views.test_views import TestViews +import pytest from django.urls import reverse -class TestViewsGeneral(TestViews): +def test_index(): + # TODO add appropriate test + pass - def test_index(self): - # TODO add appropriate test - pass - def test_search(self): - # TODO add appropriate test - pass +def test_search(): + # TODO add appropriate test + pass - def test_view(self): - # TODO add appropriate test - pass - def test_books(self): - url = reverse('view_books') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 302), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) +def test_view(): + # TODO add appropriate test + pass - def test_plan(self): - url = reverse('view_plan') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 302), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) - def test_plan_entry(self): - # TODO add appropriate test - pass +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_books(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_books')).status_code == arg[1] - def test_shopping(self): - url = reverse('view_shopping') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 302), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) - def test_settings(self): - url = reverse('view_settings') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 200), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_plan(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_plan')).status_code == arg[1] - def test_history(self): - url = reverse('view_history') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 200), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) - def test_system(self): - url = reverse('view_system') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 302), - (self.user_client_1, 302), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) +def test_plan_entry(): + # TODO add appropriate test + pass - def test_setup(self): - url = reverse('view_setup') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 302), - (self.user_client_1, 302), - (self.admin_client_1, 302), - (self.superuser_client, 302) - ], - url - ) - def test_markdown_info(self): - url = reverse('docs_markdown') - self.batch_requests( - [ - (self.anonymous_client, 200), - (self.guest_client_1, 200), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_shopping(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_shopping')).status_code == arg[1] - def test_api_info(self): - url = reverse('docs_api') - self.batch_requests( - [ - (self.anonymous_client, 302), - (self.guest_client_1, 200), - (self.user_client_1, 200), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - url - ) + +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 200], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_settings(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_settings')).status_code == arg[1] + + +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 200], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_history(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_history')).status_code == arg[1] + + +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 302], + ['a1_s1', 200], +]) +def test_system(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_system')).status_code == arg[1] + + +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 302], + ['u1_s1', 302], + ['a1_s1', 302], +]) +def test_setup(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('view_setup')).status_code == arg[1] + + +@pytest.mark.parametrize("arg", [ + ['a_u', 200], + ['g1_s1', 200], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_markdown_doc(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('docs_markdown')).status_code == arg[1] + + +@pytest.mark.parametrize("arg", [ + ['a_u', 302], + ['g1_s1', 200], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_api_info(arg, request, ext_recipe_1_s1): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse('docs_api')).status_code == arg[1] diff --git a/cookbook/tests/views/test_views_recipe_share.py b/cookbook/tests/views/test_views_recipe_share.py index 38b342dd..86ee680d 100644 --- a/cookbook/tests/views/test_views_recipe_share.py +++ b/cookbook/tests/views/test_views_recipe_share.py @@ -1,52 +1,45 @@ import uuid -from cookbook.helper.permission_helper import share_link_valid -from cookbook.models import Recipe, ShareLink -from cookbook.tests.views.test_views import TestViews -from django.contrib import auth from django.urls import reverse +from django_scopes import scopes_disabled + +from cookbook.helper.permission_helper import share_link_valid +from cookbook.models import ShareLink -class TestViewsGeneral(TestViews): +def test_share(recipe_1_s1, u1_s1, a_u): + with scopes_disabled(): + url = reverse('view_recipe', kwargs={'pk': recipe_1_s1.pk}) + r = u1_s1.get(url) + assert r.status_code == 200 - def test_share(self): - internal_recipe = Recipe.objects.create( - name='Test', - internal=True, - created_by=auth.get_user(self.user_client_1) - ) + r = a_u.get(url) + assert r.status_code == 302 - url = reverse('view_recipe', kwargs={'pk': internal_recipe.pk}) - r = self.user_client_1.get(url) - self.assertEqual(r.status_code, 200) - - r = self.anonymous_client.get(url) - self.assertEqual(r.status_code, 302) - - url = reverse('new_share_link', kwargs={'pk': internal_recipe.pk}) - r = self.user_client_1.get(url) - self.assertEqual(r.status_code, 302) - share = ShareLink.objects.filter(recipe=internal_recipe).first() - self.assertIsNotNone(share) - self.assertTrue(share_link_valid(internal_recipe, share.uuid)) + url = reverse('new_share_link', kwargs={'pk': recipe_1_s1.pk}) + r = u1_s1.get(url) + assert r.status_code == 302 + share = ShareLink.objects.filter(recipe=recipe_1_s1).first() + assert share + assert share_link_valid(recipe_1_s1, share.uuid) url = reverse( 'view_recipe', - kwargs={'pk': internal_recipe.pk, 'share': share.uuid} + kwargs={'pk': recipe_1_s1.pk, 'share': share.uuid} ) - r = self.anonymous_client.get(url) - self.assertEqual(r.status_code, 200) + r = a_u.get(url) + assert r.status_code == 200 url = reverse( 'view_recipe', - kwargs={'pk': (internal_recipe.pk + 1), 'share': share.uuid} + kwargs={'pk': (recipe_1_s1.pk + 1), 'share': share.uuid} ) - r = self.anonymous_client.get(url) - self.assertEqual(r.status_code, 404) + r = a_u.get(url) + assert r.status_code == 404 url = reverse( 'view_recipe', - kwargs={'pk': internal_recipe.pk, 'share': uuid.uuid4()} + kwargs={'pk': recipe_1_s1.pk, 'share': uuid.uuid4()} ) - r = self.anonymous_client.get(url) - self.assertEqual(r.status_code, 302) + r = a_u.get(url) + assert r.status_code == 302 diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 9e5cd0ab..5718b2f0 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -13,7 +13,7 @@ from django.core.exceptions import FieldError, ValidationError from django.core.files import File from django.db.models import Q from django.http import FileResponse, HttpResponse, JsonResponse -from django.shortcuts import redirect +from django.shortcuts import redirect, get_object_or_404 from django.utils.translation import gettext as _ from icalendar import Calendar, Event from rest_framework import decorators, viewsets, status @@ -416,7 +416,7 @@ def update_recipe_links(recipe): @group_required('user') def get_external_file_link(request, recipe_id): - recipe = Recipe.objects.filter(space=request.user.userpreference.space).get(id=recipe_id) + recipe = get_object_or_404(Recipe, pk=recipe_id, space=request.space) if not recipe.link: update_recipe_links(recipe) @@ -425,7 +425,7 @@ def get_external_file_link(request, recipe_id): @group_required('guest') def get_recipe_file(request, recipe_id): - recipe = Recipe.objects.filter(space=request.user.userpreference.space).get(id=recipe_id) + recipe = get_object_or_404(Recipe, pk=recipe_id, space=request.space) if recipe.storage: return FileResponse(get_recipe_provider(recipe).get_file(recipe)) else: