diff --git a/tests/integration/resources_permissions/test_attachment_resources.py b/tests/integration/resources_permissions/test_attachment_resources.py index 9c0e14f6..a809d7cc 100644 --- a/tests/integration/resources_permissions/test_attachment_resources.py +++ b/tests/integration/resources_permissions/test_attachment_resources.py @@ -1,16 +1,20 @@ -import pytest from django.core.urlresolvers import reverse +from django.core.files.uploadedfile import SimpleUploadedFile +from django.test.client import MULTIPART_CONTENT from rest_framework.renderers import JSONRenderer +from taiga.base.utils import json from taiga.permissions.permissions import MEMBERS_PERMISSIONS, ANON_PERMISSIONS, USER_PERMISSIONS from taiga.projects.attachments.serializers import AttachmentSerializer from tests import factories as f -from tests.utils import helper_test_http_method, helper_test_http_method_and_count, disconnect_signals, reconnect_signals - -import json +from tests.utils import helper_test_http_method +from tests.utils import helper_test_http_method_and_count +from tests.utils import disconnect_signals +from tests.utils import reconnect_signals +import pytest pytestmark = pytest.mark.django_db @@ -72,11 +76,14 @@ def data(): def data_us(data): m = type("Models", (object,), {}) m.public_user_story = f.UserStoryFactory(project=data.public_project, ref=1) - m.public_user_story_attachment = f.UserStoryAttachmentFactory(project=data.public_project, content_object=m.public_user_story) + m.public_user_story_attachment = f.UserStoryAttachmentFactory(project=data.public_project, + content_object=m.public_user_story) m.private_user_story1 = f.UserStoryFactory(project=data.private_project1, ref=5) - m.private_user_story1_attachment = f.UserStoryAttachmentFactory(project=data.private_project1, content_object=m.private_user_story1) + m.private_user_story1_attachment = f.UserStoryAttachmentFactory(project=data.private_project1, + content_object=m.private_user_story1) m.private_user_story2 = f.UserStoryFactory(project=data.private_project2, ref=9) - m.private_user_story2_attachment = f.UserStoryAttachmentFactory(project=data.private_project2, content_object=m.private_user_story2) + m.private_user_story2_attachment = f.UserStoryAttachmentFactory(project=data.private_project2, + content_object=m.private_user_story2) return m @pytest.fixture @@ -197,9 +204,12 @@ def test_wiki_attachment_retrieve(client, data, data_wiki): def test_user_story_attachment_update(client, data, data_us): - public_url = reverse('userstory-attachments-detail', kwargs={"pk": data_us.public_user_story_attachment.pk}) - private_url1 = reverse('userstory-attachments-detail', kwargs={"pk": data_us.private_user_story1_attachment.pk}) - private_url2 = reverse('userstory-attachments-detail', kwargs={"pk": data_us.private_user_story2_attachment.pk}) + public_url = reverse("userstory-attachments-detail", + args=[data_us.public_user_story_attachment.pk]) + private_url1 = reverse("userstory-attachments-detail", + args=[data_us.private_user_story1_attachment.pk]) + private_url2 = reverse("userstory-attachments-detail", + args=[data_us.private_user_story2_attachment.pk]) users = [ None, @@ -211,14 +221,20 @@ def test_user_story_attachment_update(client, data, data_us): attachment_data = AttachmentSerializer(data_us.public_user_story_attachment).data attachment_data["description"] = "test" - attachment_data = JSONRenderer().render(attachment_data) - results = helper_test_http_method(client, 'put', public_url, attachment_data, users) - assert results == [401, 403, 403, 200, 200] - results = helper_test_http_method(client, 'put', private_url1, attachment_data, users) - assert results == [401, 403, 403, 200, 200] - results = helper_test_http_method(client, 'put', private_url2, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + attachment_data = json.to_json(attachment_data) + + results = helper_test_http_method(client, "put", public_url, attachment_data, users) + # assert results == [401, 403, 403, 400, 400] + assert results == [405, 405, 405, 405, 405] + + results = helper_test_http_method(client, "put", private_url1, attachment_data, users) + # assert results == [401, 403, 403, 400, 400] + assert results == [405, 405, 405, 405, 405] + + results = helper_test_http_method(client, "put", private_url2, attachment_data, users) + # assert results == [401, 403, 403, 400, 400] + assert results == [405, 405, 405, 405, 405] def test_task_attachment_update(client, data, data_task): @@ -239,11 +255,14 @@ def test_task_attachment_update(client, data, data_task): attachment_data = JSONRenderer().render(attachment_data) results = helper_test_http_method(client, 'put', public_url, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] results = helper_test_http_method(client, 'put', private_url1, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] results = helper_test_http_method(client, 'put', private_url2, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] def test_issue_attachment_update(client, data, data_issue): @@ -264,11 +283,14 @@ def test_issue_attachment_update(client, data, data_issue): attachment_data = JSONRenderer().render(attachment_data) results = helper_test_http_method(client, 'put', public_url, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] results = helper_test_http_method(client, 'put', private_url1, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] results = helper_test_http_method(client, 'put', private_url2, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] def test_wiki_attachment_update(client, data, data_wiki): @@ -289,11 +311,14 @@ def test_wiki_attachment_update(client, data, data_wiki): attachment_data = JSONRenderer().render(attachment_data) results = helper_test_http_method(client, 'put', public_url, attachment_data, users) - assert results == [401, 200, 200, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 200, 200, 200, 200] results = helper_test_http_method(client, 'put', private_url1, attachment_data, users) - assert results == [401, 200, 200, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 200, 200, 200, 200] results = helper_test_http_method(client, 'put', private_url2, attachment_data, users) - assert results == [401, 403, 403, 200, 200] + assert results == [405, 405, 405, 405, 405] + # assert results == [401, 403, 403, 200, 200] def test_user_story_attachment_patch(client, data, data_us): @@ -482,11 +507,16 @@ def test_user_story_attachment_create(client, data, data_us): data.project_owner ] - attachment_data = AttachmentSerializer(data_us.public_user_story_attachment).data - attachment_data["id"] = None - attachment_data["description"] = "test" - attachment_data = JSONRenderer().render(attachment_data) - results = helper_test_http_method(client, 'post', url, attachment_data, users) + attachment_data = {"description": "test", + "object_id": data_us.public_user_story_attachment.object_id, + "project": data_us.public_user_story_attachment.project_id, + "attached_file": SimpleUploadedFile("test.txt", b"test")} + + _after_each_request_hook = lambda: attachment_data["attached_file"].seek(0) + + results = helper_test_http_method(client, 'post', url, attachment_data, users, + content_type=MULTIPART_CONTENT, + after_each_request=_after_each_request_hook) assert results == [401, 403, 403, 201, 201] @@ -501,11 +531,16 @@ def test_task_attachment_create(client, data, data_task): data.project_owner ] - attachment_data = AttachmentSerializer(data_task.public_task_attachment).data - attachment_data["id"] = None - attachment_data["description"] = "test" - attachment_data = JSONRenderer().render(attachment_data) - results = helper_test_http_method(client, 'post', url, attachment_data, users) + attachment_data = {"description": "test", + "object_id": data_task.public_task_attachment.object_id, + "project": data_task.public_task_attachment.project_id, + "attached_file": SimpleUploadedFile("test.txt", b"test")} + + _after_each_request_hook = lambda: attachment_data["attached_file"].seek(0) + + results = helper_test_http_method(client, 'post', url, attachment_data, users, + content_type=MULTIPART_CONTENT, + after_each_request=_after_each_request_hook) assert results == [401, 403, 403, 201, 201] @@ -520,11 +555,17 @@ def test_issue_attachment_create(client, data, data_issue): data.project_owner ] - attachment_data = AttachmentSerializer(data_issue.public_issue_attachment).data - attachment_data["id"] = None - attachment_data["description"] = "test" - attachment_data = JSONRenderer().render(attachment_data) - results = helper_test_http_method(client, 'post', url, attachment_data, users) + attachment_data = {"description": "test", + "object_id": data_issue.public_issue_attachment.object_id, + "project": data_issue.public_issue_attachment.project_id, + "attached_file": SimpleUploadedFile("test.txt", b"test")} + + _after_each_request_hook = lambda: attachment_data["attached_file"].seek(0) + + results = helper_test_http_method(client, 'post', url, attachment_data, users, + content_type=MULTIPART_CONTENT, + after_each_request=_after_each_request_hook) + assert results == [401, 403, 403, 201, 201] @@ -539,11 +580,17 @@ def test_wiki_attachment_create(client, data, data_wiki): data.project_owner ] - attachment_data = AttachmentSerializer(data_wiki.public_wiki_attachment).data - attachment_data["id"] = None - attachment_data["description"] = "test" - attachment_data = JSONRenderer().render(attachment_data) - results = helper_test_http_method(client, 'post', url, attachment_data, users) + attachment_data = {"description": "test", + "object_id": data_wiki.public_wiki_attachment.object_id, + "project": data_wiki.public_wiki_attachment.project_id, + "attached_file": SimpleUploadedFile("test.txt", b"test")} + + _after_each_request_hook = lambda: attachment_data["attached_file"].seek(0) + + results = helper_test_http_method(client, 'post', url, attachment_data, users, + content_type=MULTIPART_CONTENT, + after_each_request=_after_each_request_hook) + assert results == [401, 201, 201, 201, 201] diff --git a/tests/integration/test_attachments.py b/tests/integration/test_attachments.py index 72d8ff17..f44d34e1 100644 --- a/tests/integration/test_attachments.py +++ b/tests/integration/test_attachments.py @@ -2,12 +2,18 @@ import pytest from django.core.urlresolvers import reverse from django.core.files.base import File +from django.core.files.uploadedfile import SimpleUploadedFile + +from rest_framework.renderers import JSONRenderer from .. import factories as f from ..utils import set_settings +from taiga.projects.attachments.serializers import AttachmentSerializer + pytestmark = pytest.mark.django_db + def test_authentication(client): "User can't access an attachment if not authenticated" attachment = f.UserStoryAttachmentFactory.create() @@ -56,3 +62,39 @@ def test_attachment_redirect(client): assert response.status_code == 200 assert response.has_header('x-accel-redirect') + + +# Bug test "Don't create attachments without attached_file" +def test_create_user_story_attachment_without_file(client): + us = f.UserStoryFactory.create() + attachment = f.UserStoryAttachmentFactory(project=us.project, content_object=us) + + attachment_data = AttachmentSerializer(attachment).data + attachment_data["id"] = None + attachment_data["description"] = "test" + attachment_data["attached_file"] = None + + url = reverse('userstory-attachments-list') + + client.login(us.owner) + response = client.post(url, attachment_data) + assert response.status_code == 400 + + +def test_create_attachment_on_wrong_project(client): + issue1 = f.create_issue() + issue2 = f.create_issue(owner=issue1.owner) + + assert issue1.owner == issue2.owner + assert issue1.project.owner == issue2.project.owner + + url = reverse("issue-attachments-list") + + data = {"description": "test", + "object_id": issue2.pk, + "project": issue1.project.id, + "attached_file": SimpleUploadedFile("test.txt", b"test")} + + client.login(issue1.owner) + response = client.post(url, data) + assert response.status_code == 400 diff --git a/tests/integration/test_issues.py b/tests/integration/test_issues.py index f367d6df..db9502f2 100644 --- a/tests/integration/test_issues.py +++ b/tests/integration/test_issues.py @@ -1,13 +1,13 @@ from unittest import mock -import pytest - from django.core.urlresolvers import reverse from taiga.projects.issues import services, models +from taiga.base.utils import json from .. import factories as f +import pytest pytestmark = pytest.mark.django_db @@ -51,7 +51,7 @@ def test_api_create_issues_in_bulk(client): "project_id": project.id} client.login(project.owner) - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 200, response.data diff --git a/tests/integration/test_memberships.py b/tests/integration/test_memberships.py index 400c8a6f..5ff6f1dd 100644 --- a/tests/integration/test_memberships.py +++ b/tests/integration/test_memberships.py @@ -1,13 +1,12 @@ from unittest import mock - -import pytest - from django.core.urlresolvers import reverse from taiga.projects import services +from taiga.base.utils import json from .. import factories as f +import pytest pytestmark = pytest.mark.django_db @@ -47,7 +46,7 @@ def test_api_create_bulk_members(client): ] } client.login(project.owner) - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 200 assert response.data[0]["email"] == john.email @@ -76,7 +75,7 @@ def test_api_invite_existing_user(client, outbox): url = reverse("memberships-list") data = {"role": role.pk, "project": role.project.pk, "email": user.email} - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 201, response.data assert len(outbox) == 1 diff --git a/tests/integration/test_projects.py b/tests/integration/test_projects.py index a9996cc8..fb1f8c98 100644 --- a/tests/integration/test_projects.py +++ b/tests/integration/test_projects.py @@ -1,9 +1,9 @@ -import pytest - from django.core.urlresolvers import reverse +from taiga.base.utils import json from .. import factories as f +import pytest pytestmark = pytest.mark.django_db @@ -13,7 +13,7 @@ def test_api_create_project(client): data = {"name": "project name", "description": "project description"} client.login(user) - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 201 @@ -24,6 +24,6 @@ def test_api_partially_update_project(client): data = {"name": ""} client.login(project.owner) - response = client.json.patch(url, data) + response = client.json.patch(url, json.to_json(data)) assert response.status_code == 400 diff --git a/tests/integration/test_tasks.py b/tests/integration/test_tasks.py index c6ed5d9b..d84205ae 100644 --- a/tests/integration/test_tasks.py +++ b/tests/integration/test_tasks.py @@ -1,13 +1,13 @@ from unittest import mock -import pytest - from django.core.urlresolvers import reverse +from taiga.base.utils import json from taiga.projects.tasks import services from .. import factories as f +import pytest pytestmark = pytest.mark.django_db @@ -40,7 +40,7 @@ def test_api_update_task_tags(client): data = {"tags": ["back", "front"], "version": task.version} client.login(task.owner) - response = client.json.patch(url, data) + response = client.json.patch(url, json.to_json(data)) assert response.status_code == 200, response.data @@ -57,7 +57,7 @@ def test_api_create_in_bulk_with_status(client): } client.login(us.owner) - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 200 assert response.data[0]["status"] == us.project.default_task_status.id diff --git a/tests/integration/test_userstories.py b/tests/integration/test_userstories.py index 8ae87dfb..856002b4 100644 --- a/tests/integration/test_userstories.py +++ b/tests/integration/test_userstories.py @@ -1,13 +1,13 @@ from unittest import mock -import pytest - from django.core.urlresolvers import reverse +from taiga.base.utils import json from taiga.projects.userstories import services, models from .. import factories as f +import pytest pytestmark = pytest.mark.django_db @@ -76,7 +76,7 @@ def test_api_create_in_bulk_with_status(client): } client.login(project.owner) - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 200, response.data assert response.data[0]["status"] == project.default_us_status.id @@ -94,6 +94,6 @@ def test_api_update_order_in_bulk(client): } client.login(project.owner) - response = client.json.post(url, data) + response = client.json.post(url, json.to_json(data)) assert response.status_code == 204, response.data diff --git a/tests/utils.py b/tests/utils.py index 4537c911..f326505e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -88,19 +88,21 @@ class SettingsTestCase(object): override_settings(cls.ORIGINAL_SETTINGS) cls.OVERRIDE_SETTINGS.clear() -def _helper_test_http_method_responses(client, method, url, data, users, after_each_request=None): +def _helper_test_http_method_responses(client, method, url, data, users, after_each_request=None, + content_type="application/json"): results = [] + for user in users: if user is None: client.logout() else: client.login(user) if data: - response = getattr(client, method)(url, data, content_type="application/json") + response = getattr(client, method)(url, data, content_type=content_type) else: response = getattr(client, method)(url) - if response.status_code == 400: - print(response.content) + if response.status_code >= 400: + print("Response content:", response.content) results.append(response) @@ -108,8 +110,10 @@ def _helper_test_http_method_responses(client, method, url, data, users, after_e after_each_request() return results -def helper_test_http_method(client, method, url, data, users, after_each_request=None): - responses = _helper_test_http_method_responses(client, method, url, data, users, after_each_request) +def helper_test_http_method(client, method, url, data, users, after_each_request=None, + content_type="application/json"): + responses = _helper_test_http_method_responses(client, method, url, data, users, after_each_request, + content_type=content_type) return list(map(lambda r: r.status_code, responses))