Fix affected tests to previous attachment fixes.

remotes/origin/enhancement/email-actions
Andrey Antukh 2014-09-05 22:33:52 +02:00
parent f2aa75e4a5
commit 4f27a8ba4d
8 changed files with 164 additions and 72 deletions

View File

@ -1,16 +1,20 @@
import pytest
from django.core.urlresolvers import reverse 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 rest_framework.renderers import JSONRenderer
from taiga.base.utils import json
from taiga.permissions.permissions import MEMBERS_PERMISSIONS, ANON_PERMISSIONS, USER_PERMISSIONS from taiga.permissions.permissions import MEMBERS_PERMISSIONS, ANON_PERMISSIONS, USER_PERMISSIONS
from taiga.projects.attachments.serializers import AttachmentSerializer from taiga.projects.attachments.serializers import AttachmentSerializer
from tests import factories as f from tests import factories as f
from tests.utils import helper_test_http_method, helper_test_http_method_and_count, disconnect_signals, reconnect_signals from tests.utils import helper_test_http_method
from tests.utils import helper_test_http_method_and_count
import json from tests.utils import disconnect_signals
from tests.utils import reconnect_signals
import pytest
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -72,11 +76,14 @@ def data():
def data_us(data): def data_us(data):
m = type("Models", (object,), {}) m = type("Models", (object,), {})
m.public_user_story = f.UserStoryFactory(project=data.public_project, ref=1) 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 = 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 = 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 return m
@pytest.fixture @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): 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}) public_url = reverse("userstory-attachments-detail",
private_url1 = reverse('userstory-attachments-detail', kwargs={"pk": data_us.private_user_story1_attachment.pk}) args=[data_us.public_user_story_attachment.pk])
private_url2 = reverse('userstory-attachments-detail', kwargs={"pk": data_us.private_user_story2_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 = [ users = [
None, 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 = AttachmentSerializer(data_us.public_user_story_attachment).data
attachment_data["description"] = "test" attachment_data["description"] = "test"
attachment_data = JSONRenderer().render(attachment_data)
results = helper_test_http_method(client, 'put', public_url, attachment_data, users) attachment_data = json.to_json(attachment_data)
assert results == [401, 403, 403, 200, 200]
results = helper_test_http_method(client, 'put', private_url1, attachment_data, users) results = helper_test_http_method(client, "put", public_url, attachment_data, users)
assert results == [401, 403, 403, 200, 200] # assert results == [401, 403, 403, 400, 400]
results = helper_test_http_method(client, 'put', private_url2, attachment_data, users) 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, 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): 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) attachment_data = JSONRenderer().render(attachment_data)
results = helper_test_http_method(client, 'put', public_url, attachment_data, users) 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) 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) 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): 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) attachment_data = JSONRenderer().render(attachment_data)
results = helper_test_http_method(client, 'put', public_url, attachment_data, users) 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) 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) 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): 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) attachment_data = JSONRenderer().render(attachment_data)
results = helper_test_http_method(client, 'put', public_url, attachment_data, users) 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) 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) 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): 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 data.project_owner
] ]
attachment_data = AttachmentSerializer(data_us.public_user_story_attachment).data attachment_data = {"description": "test",
attachment_data["id"] = None "object_id": data_us.public_user_story_attachment.object_id,
attachment_data["description"] = "test" "project": data_us.public_user_story_attachment.project_id,
attachment_data = JSONRenderer().render(attachment_data) "attached_file": SimpleUploadedFile("test.txt", b"test")}
results = helper_test_http_method(client, 'post', url, attachment_data, users)
_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] assert results == [401, 403, 403, 201, 201]
@ -501,11 +531,16 @@ def test_task_attachment_create(client, data, data_task):
data.project_owner data.project_owner
] ]
attachment_data = AttachmentSerializer(data_task.public_task_attachment).data attachment_data = {"description": "test",
attachment_data["id"] = None "object_id": data_task.public_task_attachment.object_id,
attachment_data["description"] = "test" "project": data_task.public_task_attachment.project_id,
attachment_data = JSONRenderer().render(attachment_data) "attached_file": SimpleUploadedFile("test.txt", b"test")}
results = helper_test_http_method(client, 'post', url, attachment_data, users)
_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] assert results == [401, 403, 403, 201, 201]
@ -520,11 +555,17 @@ def test_issue_attachment_create(client, data, data_issue):
data.project_owner data.project_owner
] ]
attachment_data = AttachmentSerializer(data_issue.public_issue_attachment).data attachment_data = {"description": "test",
attachment_data["id"] = None "object_id": data_issue.public_issue_attachment.object_id,
attachment_data["description"] = "test" "project": data_issue.public_issue_attachment.project_id,
attachment_data = JSONRenderer().render(attachment_data) "attached_file": SimpleUploadedFile("test.txt", b"test")}
results = helper_test_http_method(client, 'post', url, attachment_data, users)
_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] assert results == [401, 403, 403, 201, 201]
@ -539,11 +580,17 @@ def test_wiki_attachment_create(client, data, data_wiki):
data.project_owner data.project_owner
] ]
attachment_data = AttachmentSerializer(data_wiki.public_wiki_attachment).data attachment_data = {"description": "test",
attachment_data["id"] = None "object_id": data_wiki.public_wiki_attachment.object_id,
attachment_data["description"] = "test" "project": data_wiki.public_wiki_attachment.project_id,
attachment_data = JSONRenderer().render(attachment_data) "attached_file": SimpleUploadedFile("test.txt", b"test")}
results = helper_test_http_method(client, 'post', url, attachment_data, users)
_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] assert results == [401, 201, 201, 201, 201]

View File

@ -2,12 +2,18 @@ import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.files.base import File 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 .. import factories as f
from ..utils import set_settings from ..utils import set_settings
from taiga.projects.attachments.serializers import AttachmentSerializer
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
def test_authentication(client): def test_authentication(client):
"User can't access an attachment if not authenticated" "User can't access an attachment if not authenticated"
attachment = f.UserStoryAttachmentFactory.create() attachment = f.UserStoryAttachmentFactory.create()
@ -56,3 +62,39 @@ def test_attachment_redirect(client):
assert response.status_code == 200 assert response.status_code == 200
assert response.has_header('x-accel-redirect') 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

View File

@ -1,13 +1,13 @@
from unittest import mock from unittest import mock
import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from taiga.projects.issues import services, models from taiga.projects.issues import services, models
from taiga.base.utils import json
from .. import factories as f from .. import factories as f
import pytest
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -51,7 +51,7 @@ def test_api_create_issues_in_bulk(client):
"project_id": project.id} "project_id": project.id}
client.login(project.owner) 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.status_code == 200, response.data

View File

@ -1,13 +1,12 @@
from unittest import mock from unittest import mock
import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from taiga.projects import services from taiga.projects import services
from taiga.base.utils import json
from .. import factories as f from .. import factories as f
import pytest
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -47,7 +46,7 @@ def test_api_create_bulk_members(client):
] ]
} }
client.login(project.owner) 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.status_code == 200
assert response.data[0]["email"] == john.email assert response.data[0]["email"] == john.email
@ -76,7 +75,7 @@ def test_api_invite_existing_user(client, outbox):
url = reverse("memberships-list") url = reverse("memberships-list")
data = {"role": role.pk, "project": role.project.pk, "email": user.email} 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 response.status_code == 201, response.data
assert len(outbox) == 1 assert len(outbox) == 1

View File

@ -1,9 +1,9 @@
import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from taiga.base.utils import json
from .. import factories as f from .. import factories as f
import pytest
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -13,7 +13,7 @@ def test_api_create_project(client):
data = {"name": "project name", "description": "project description"} data = {"name": "project name", "description": "project description"}
client.login(user) client.login(user)
response = client.json.post(url, data) response = client.json.post(url, json.to_json(data))
assert response.status_code == 201 assert response.status_code == 201
@ -24,6 +24,6 @@ def test_api_partially_update_project(client):
data = {"name": ""} data = {"name": ""}
client.login(project.owner) client.login(project.owner)
response = client.json.patch(url, data) response = client.json.patch(url, json.to_json(data))
assert response.status_code == 400 assert response.status_code == 400

View File

@ -1,13 +1,13 @@
from unittest import mock from unittest import mock
import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from taiga.base.utils import json
from taiga.projects.tasks import services from taiga.projects.tasks import services
from .. import factories as f from .. import factories as f
import pytest
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -40,7 +40,7 @@ def test_api_update_task_tags(client):
data = {"tags": ["back", "front"], "version": task.version} data = {"tags": ["back", "front"], "version": task.version}
client.login(task.owner) 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 assert response.status_code == 200, response.data
@ -57,7 +57,7 @@ def test_api_create_in_bulk_with_status(client):
} }
client.login(us.owner) 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.status_code == 200
assert response.data[0]["status"] == us.project.default_task_status.id assert response.data[0]["status"] == us.project.default_task_status.id

View File

@ -1,13 +1,13 @@
from unittest import mock from unittest import mock
import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from taiga.base.utils import json
from taiga.projects.userstories import services, models from taiga.projects.userstories import services, models
from .. import factories as f from .. import factories as f
import pytest
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -76,7 +76,7 @@ def test_api_create_in_bulk_with_status(client):
} }
client.login(project.owner) 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.status_code == 200, response.data
assert response.data[0]["status"] == project.default_us_status.id 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) 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 assert response.status_code == 204, response.data

View File

@ -88,19 +88,21 @@ class SettingsTestCase(object):
override_settings(cls.ORIGINAL_SETTINGS) override_settings(cls.ORIGINAL_SETTINGS)
cls.OVERRIDE_SETTINGS.clear() 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 = [] results = []
for user in users: for user in users:
if user is None: if user is None:
client.logout() client.logout()
else: else:
client.login(user) client.login(user)
if data: if data:
response = getattr(client, method)(url, data, content_type="application/json") response = getattr(client, method)(url, data, content_type=content_type)
else: else:
response = getattr(client, method)(url) response = getattr(client, method)(url)
if response.status_code == 400: if response.status_code >= 400:
print(response.content) print("Response content:", response.content)
results.append(response) results.append(response)
@ -108,8 +110,10 @@ def _helper_test_http_method_responses(client, method, url, data, users, after_e
after_each_request() after_each_request()
return results return results
def helper_test_http_method(client, method, url, data, users, after_each_request=None): 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) 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)) return list(map(lambda r: r.status_code, responses))