Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/tests/views/test_content.py
Show First 20 Lines • Show All 255 Lines • ▼ Show 20 Lines | def test_content_raw_text(client, archive_data, content_text): | ||||
resp = check_http_get_response( | resp = check_http_get_response( | ||||
client, url, status_code=200, content_type="text/plain" | client, url, status_code=200, content_type="text/plain" | ||||
) | ) | ||||
content_data = archive_data.content_get_data(content_text["sha1"])["data"] | content_data = archive_data.content_get_data(content_text["sha1"])["data"] | ||||
assert resp["Content-Type"] == "text/plain" | assert resp["Content-Type"] == "text/plain" | ||||
assert resp["Content-disposition"] == ( | assert resp["Content-disposition"] == (f'filename="sha1_{content_text["sha1"]}"') | ||||
"filename=%s_%s" % ("sha1", content_text["sha1"]) | assert b"".join(resp.streaming_content) == content_data | ||||
) | |||||
assert resp.content == content_data | |||||
filename = content_text["path"].split("/")[-1] | filename = content_text["path"].split("/")[-1] | ||||
url = reverse( | url = reverse( | ||||
"browse-content-raw", | "browse-content-raw", | ||||
url_args={"query_string": content_text["sha1"]}, | url_args={"query_string": content_text["sha1"]}, | ||||
query_params={"filename": filename}, | query_params={"filename": filename}, | ||||
) | ) | ||||
resp = check_http_get_response( | resp = check_http_get_response( | ||||
client, url, status_code=200, content_type="text/plain" | client, url, status_code=200, content_type="text/plain" | ||||
) | ) | ||||
assert resp["Content-Type"] == "text/plain" | assert resp["Content-Type"] == "text/plain" | ||||
assert resp["Content-disposition"] == "filename=%s" % filename | assert resp["Content-disposition"] == f'filename="{filename}"' | ||||
assert resp.content == content_data | assert b"".join(resp.streaming_content) == content_data | ||||
def test_content_raw_no_utf8_text(client, content_text_non_utf8): | def test_content_raw_no_utf8_text(client, content_text_non_utf8): | ||||
url = reverse( | url = reverse( | ||||
"browse-content-raw", url_args={"query_string": content_text_non_utf8["sha1"]} | "browse-content-raw", url_args={"query_string": content_text_non_utf8["sha1"]} | ||||
) | ) | ||||
resp = check_http_get_response( | resp = check_http_get_response( | ||||
client, url, status_code=200, content_type="text/plain" | client, url, status_code=200, content_type="text/plain" | ||||
) | ) | ||||
_, encoding = get_mimetype_and_encoding_for_content(resp.content) | _, encoding = get_mimetype_and_encoding_for_content( | ||||
b"".join(resp.streaming_content) | |||||
) | |||||
assert encoding == content_text_non_utf8["encoding"] | assert encoding == content_text_non_utf8["encoding"] | ||||
def test_content_raw_bin(client, archive_data, content_image_type): | def test_content_raw_bin(client, archive_data, content_image_type): | ||||
url = reverse( | url = reverse( | ||||
"browse-content-raw", url_args={"query_string": content_image_type["sha1"]} | "browse-content-raw", url_args={"query_string": content_image_type["sha1"]} | ||||
) | ) | ||||
resp = check_http_get_response( | resp = check_http_get_response( | ||||
client, url, status_code=200, content_type="application/octet-stream" | client, url, status_code=200, content_type="application/octet-stream" | ||||
) | ) | ||||
filename = content_image_type["path"].split("/")[-1] | filename = content_image_type["path"].split("/")[-1] | ||||
content_data = archive_data.content_get_data(content_image_type["sha1"])["data"] | content_data = archive_data.content_get_data(content_image_type["sha1"])["data"] | ||||
assert resp["Content-Type"] == "application/octet-stream" | assert resp["Content-Type"] == "application/octet-stream" | ||||
assert resp["Content-disposition"] == "attachment; filename=%s_%s" % ( | assert ( | ||||
"sha1", | resp["Content-disposition"] | ||||
content_image_type["sha1"], | == f'attachment; filename="sha1_{content_image_type["sha1"]}"' | ||||
) | ) | ||||
assert resp.content == content_data | assert b"".join(resp.streaming_content) == content_data | ||||
url = reverse( | url = reverse( | ||||
"browse-content-raw", | "browse-content-raw", | ||||
url_args={"query_string": content_image_type["sha1"]}, | url_args={"query_string": content_image_type["sha1"]}, | ||||
query_params={"filename": filename}, | query_params={"filename": filename}, | ||||
) | ) | ||||
resp = check_http_get_response( | resp = check_http_get_response( | ||||
client, url, status_code=200, content_type="application/octet-stream" | client, url, status_code=200, content_type="application/octet-stream" | ||||
) | ) | ||||
assert resp["Content-Type"] == "application/octet-stream" | assert resp["Content-Type"] == "application/octet-stream" | ||||
assert resp["Content-disposition"] == "attachment; filename=%s" % filename | assert resp["Content-disposition"] == f'attachment; filename="{filename}"' | ||||
assert resp.content == content_data | assert b"".join(resp.streaming_content) == content_data | ||||
@pytest.mark.django_db | @pytest.mark.django_db | ||||
@pytest.mark.parametrize("staff_user_logged_in", [False, True]) | @pytest.mark.parametrize("staff_user_logged_in", [False, True]) | ||||
def test_content_request_errors( | def test_content_request_errors( | ||||
client, staff_user, invalid_sha1, unknown_content, staff_user_logged_in | client, staff_user, invalid_sha1, unknown_content, staff_user_logged_in | ||||
): | ): | ||||
▲ Show 20 Lines • Show All 778 Lines • Show Last 20 Lines |