all tests migrated and improved
This commit is contained in:
2
.idea/recipes.iml
generated
2
.idea/recipes.iml
generated
@ -14,7 +14,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<excludeFolder url="file://$MODULE_DIR$/cookbook/tests/pytest/resources" />
|
<excludeFolder url="file://$MODULE_DIR$/cookbook/tests/resources" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/staticfiles" />
|
<excludeFolder url="file://$MODULE_DIR$/staticfiles" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||||
</content>
|
</content>
|
||||||
|
@ -2,7 +2,6 @@ import json
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from cookbook.tests.views.test_views import TestViews
|
|
||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
@ -1,9 +1,4 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
from cookbook.models import UserPreference
|
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
|
import json
|
||||||
|
|
||||||
@ -12,8 +7,6 @@ from django.contrib import auth
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django_scopes import scopes_disabled
|
from django_scopes import scopes_disabled
|
||||||
|
|
||||||
from cookbook.models import RecipeBook, RecipeBookEntry
|
|
||||||
|
|
||||||
LIST_URL = 'api:userpreference-list'
|
LIST_URL = 'api:userpreference-list'
|
||||||
DETAIL_URL = 'api:userpreference-detail'
|
DETAIL_URL = 'api:userpreference-detail'
|
||||||
|
|
@ -7,7 +7,7 @@ from django.contrib import auth
|
|||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from django_scopes import scopes_disabled
|
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
|
# 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(
|
s1.ingredients.add(
|
||||||
Ingredient.objects.create(
|
Ingredient.objects.create(
|
||||||
amount=1,
|
amount=1,
|
||||||
food=Food.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,),
|
unit=Unit.objects.create(name=uuid.uuid4(), space=space_1, ),
|
||||||
note=uuid.uuid4(),
|
note=uuid.uuid4(),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -70,8 +70,8 @@ def get_random_recipe(space_1, u1_s1):
|
|||||||
s2.ingredients.add(
|
s2.ingredients.add(
|
||||||
Ingredient.objects.create(
|
Ingredient.objects.create(
|
||||||
amount=1,
|
amount=1,
|
||||||
food=Food.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,),
|
unit=Unit.objects.create(name=uuid.uuid4(), space=space_1, ),
|
||||||
note=uuid.uuid4(),
|
note=uuid.uuid4(),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -89,6 +89,15 @@ def recipe_2_s1(space_1, u1_s1):
|
|||||||
return get_random_recipe(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 -----------------------
|
# ---------------------- USER FIXTURES -----------------------
|
||||||
# maybe better with factories but this is very explict so ...
|
# maybe better with factories but this is very explict so ...
|
||||||
|
|
@ -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)
|
||||||
|
@ -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)
|
|
@ -1,5 +1,4 @@
|
|||||||
from cookbook.models import Food, Recipe, Storage, Unit
|
from cookbook.models import Recipe, Storage
|
||||||
from cookbook.tests.views.test_views import TestViews
|
|
||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from pytest_django.asserts import assertTemplateUsed
|
from pytest_django.asserts import assertTemplateUsed
|
@ -1,68 +1,58 @@
|
|||||||
from cookbook.models import Storage
|
from cookbook.models import Storage
|
||||||
from cookbook.tests.views.test_views import TestViews
|
|
||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.urls import reverse
|
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(
|
def test_edit_storage(storage_obj, a1_s1, a1_s2):
|
||||||
name='TestStorage',
|
r = a1_s1.post(
|
||||||
method=Storage.DROPBOX,
|
reverse('edit_storage', args={storage_obj.pk}),
|
||||||
created_by=auth.get_user(self.admin_client_1),
|
{
|
||||||
token='test',
|
'name': 'NewStorage',
|
||||||
username='test',
|
'password': '1234_pw',
|
||||||
password='test',
|
'token': '1234_token',
|
||||||
)
|
'method': Storage.DROPBOX
|
||||||
self.url = reverse('edit_storage', args=[self.storage.pk])
|
}
|
||||||
|
)
|
||||||
|
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 = a1_s2.post(
|
||||||
r = self.admin_client_1.post(
|
reverse('edit_storage', args={storage_obj.pk}),
|
||||||
self.url,
|
{
|
||||||
{
|
'name': 'NewStorage',
|
||||||
'name': 'NewStorage',
|
'password': '1234_pw',
|
||||||
'password': '1234_pw',
|
'token': '1234_token',
|
||||||
'token': '1234_token',
|
'method': Storage.DROPBOX
|
||||||
'method': Storage.DROPBOX
|
}
|
||||||
}
|
)
|
||||||
)
|
assert r.status_code == 404
|
||||||
self.storage.refresh_from_db()
|
|
||||||
self.assertEqual(self.storage.password, '1234_pw')
|
|
||||||
self.assertEqual(self.storage.token, '1234_token')
|
|
||||||
|
|
||||||
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):
|
@pytest.mark.parametrize("arg", [
|
||||||
r = self.anonymous_client.get(self.url)
|
['a_u', 302],
|
||||||
self.assertEqual(r.status_code, 302)
|
['g1_s1', 302],
|
||||||
|
['u1_s1', 302],
|
||||||
r = self.guest_client_1.get(self.url)
|
['a1_s1', 200],
|
||||||
self.assertEqual(r.status_code, 302)
|
['g1_s2', 302],
|
||||||
|
['u1_s2', 302],
|
||||||
r = self.user_client_1.get(self.url)
|
['a1_s2', 404],
|
||||||
self.assertEqual(r.status_code, 302)
|
])
|
||||||
|
def test_view_permission(arg, request, storage_obj):
|
||||||
r = self.admin_client_1.get(self.url)
|
c = request.getfixturevalue(arg[0])
|
||||||
self.assertEqual(r.status_code, 200)
|
assert c.get(reverse('edit_storage', args={storage_obj.pk})).status_code == arg[1]
|
||||||
|
|
||||||
r = self.superuser_client.get(self.url)
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
|
28
cookbook/tests/other/test_url_import.py
Normal file
28
cookbook/tests/other/test_url_import.py
Normal file
@ -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()
|
@ -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)
|
|
@ -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)
|
|
@ -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()
|
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
@ -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
|
|
||||||
)
|
|
@ -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)
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
from cookbook.tests.test_setup import TestBase
|
|
||||||
|
|
||||||
|
|
||||||
class TestViews(TestBase):
|
|
||||||
pass
|
|
@ -1,43 +1,34 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
from cookbook.models import Recipe
|
from cookbook.models import Recipe
|
||||||
from cookbook.tests.views.test_views import TestViews
|
|
||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.urls import reverse
|
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)
|
@pytest.mark.parametrize("arg", [
|
||||||
self.assertEqual(self.guest_client_1.get(url).status_code, 302)
|
['a_u', 302],
|
||||||
self.assertEqual(self.user_client_1.get(url).status_code, 200)
|
['g1_s1', 200],
|
||||||
self.assertEqual(self.admin_client_1.get(url).status_code, 200)
|
['u1_s1', 200],
|
||||||
self.assertEqual(self.superuser_client.get(url).status_code, 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)
|
|
||||||
|
@ -1,138 +1,121 @@
|
|||||||
from cookbook.tests.views.test_views import TestViews
|
import pytest
|
||||||
from django.urls import reverse
|
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):
|
def test_search():
|
||||||
# TODO add appropriate test
|
# TODO add appropriate test
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_view(self):
|
|
||||||
# TODO add appropriate test
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_books(self):
|
def test_view():
|
||||||
url = reverse('view_books')
|
# TODO add appropriate test
|
||||||
self.batch_requests(
|
pass
|
||||||
[
|
|
||||||
(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(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):
|
@pytest.mark.parametrize("arg", [
|
||||||
# TODO add appropriate test
|
['a_u', 302],
|
||||||
pass
|
['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):
|
@pytest.mark.parametrize("arg", [
|
||||||
url = reverse('view_settings')
|
['a_u', 302],
|
||||||
self.batch_requests(
|
['g1_s1', 302],
|
||||||
[
|
['u1_s1', 200],
|
||||||
(self.anonymous_client, 302),
|
['a1_s1', 200],
|
||||||
(self.guest_client_1, 200),
|
])
|
||||||
(self.user_client_1, 200),
|
def test_plan(arg, request, ext_recipe_1_s1):
|
||||||
(self.admin_client_1, 200),
|
c = request.getfixturevalue(arg[0])
|
||||||
(self.superuser_client, 200)
|
assert c.get(reverse('view_plan')).status_code == arg[1]
|
||||||
],
|
|
||||||
url
|
|
||||||
)
|
|
||||||
|
|
||||||
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):
|
def test_plan_entry():
|
||||||
url = reverse('view_system')
|
# TODO add appropriate test
|
||||||
self.batch_requests(
|
pass
|
||||||
[
|
|
||||||
(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_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):
|
@pytest.mark.parametrize("arg", [
|
||||||
url = reverse('docs_markdown')
|
['a_u', 302],
|
||||||
self.batch_requests(
|
['g1_s1', 302],
|
||||||
[
|
['u1_s1', 200],
|
||||||
(self.anonymous_client, 200),
|
['a1_s1', 200],
|
||||||
(self.guest_client_1, 200),
|
])
|
||||||
(self.user_client_1, 200),
|
def test_shopping(arg, request, ext_recipe_1_s1):
|
||||||
(self.admin_client_1, 200),
|
c = request.getfixturevalue(arg[0])
|
||||||
(self.superuser_client, 200)
|
assert c.get(reverse('view_shopping')).status_code == arg[1]
|
||||||
],
|
|
||||||
url
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_api_info(self):
|
|
||||||
url = reverse('docs_api')
|
@pytest.mark.parametrize("arg", [
|
||||||
self.batch_requests(
|
['a_u', 302],
|
||||||
[
|
['g1_s1', 200],
|
||||||
(self.anonymous_client, 302),
|
['u1_s1', 200],
|
||||||
(self.guest_client_1, 200),
|
['a1_s1', 200],
|
||||||
(self.user_client_1, 200),
|
])
|
||||||
(self.admin_client_1, 200),
|
def test_settings(arg, request, ext_recipe_1_s1):
|
||||||
(self.superuser_client, 200)
|
c = request.getfixturevalue(arg[0])
|
||||||
],
|
assert c.get(reverse('view_settings')).status_code == arg[1]
|
||||||
url
|
|
||||||
)
|
|
||||||
|
@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]
|
||||||
|
@ -1,52 +1,45 @@
|
|||||||
import uuid
|
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.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):
|
r = a_u.get(url)
|
||||||
internal_recipe = Recipe.objects.create(
|
assert r.status_code == 302
|
||||||
name='Test',
|
|
||||||
internal=True,
|
|
||||||
created_by=auth.get_user(self.user_client_1)
|
|
||||||
)
|
|
||||||
|
|
||||||
url = reverse('view_recipe', kwargs={'pk': internal_recipe.pk})
|
url = reverse('new_share_link', kwargs={'pk': recipe_1_s1.pk})
|
||||||
r = self.user_client_1.get(url)
|
r = u1_s1.get(url)
|
||||||
self.assertEqual(r.status_code, 200)
|
assert r.status_code == 302
|
||||||
|
share = ShareLink.objects.filter(recipe=recipe_1_s1).first()
|
||||||
r = self.anonymous_client.get(url)
|
assert share
|
||||||
self.assertEqual(r.status_code, 302)
|
assert share_link_valid(recipe_1_s1, share.uuid)
|
||||||
|
|
||||||
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(
|
url = reverse(
|
||||||
'view_recipe',
|
'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)
|
r = a_u.get(url)
|
||||||
self.assertEqual(r.status_code, 200)
|
assert r.status_code == 200
|
||||||
|
|
||||||
url = reverse(
|
url = reverse(
|
||||||
'view_recipe',
|
'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)
|
r = a_u.get(url)
|
||||||
self.assertEqual(r.status_code, 404)
|
assert r.status_code == 404
|
||||||
|
|
||||||
url = reverse(
|
url = reverse(
|
||||||
'view_recipe',
|
'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)
|
r = a_u.get(url)
|
||||||
self.assertEqual(r.status_code, 302)
|
assert r.status_code == 302
|
||||||
|
@ -13,7 +13,7 @@ from django.core.exceptions import FieldError, ValidationError
|
|||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.http import FileResponse, HttpResponse, JsonResponse
|
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 django.utils.translation import gettext as _
|
||||||
from icalendar import Calendar, Event
|
from icalendar import Calendar, Event
|
||||||
from rest_framework import decorators, viewsets, status
|
from rest_framework import decorators, viewsets, status
|
||||||
@ -416,7 +416,7 @@ def update_recipe_links(recipe):
|
|||||||
|
|
||||||
@group_required('user')
|
@group_required('user')
|
||||||
def get_external_file_link(request, recipe_id):
|
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:
|
if not recipe.link:
|
||||||
update_recipe_links(recipe)
|
update_recipe_links(recipe)
|
||||||
|
|
||||||
@ -425,7 +425,7 @@ def get_external_file_link(request, recipe_id):
|
|||||||
|
|
||||||
@group_required('guest')
|
@group_required('guest')
|
||||||
def get_recipe_file(request, recipe_id):
|
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:
|
if recipe.storage:
|
||||||
return FileResponse(get_recipe_provider(recipe).get_file(recipe))
|
return FileResponse(get_recipe_provider(recipe).get_file(recipe))
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user