Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: coverage for staticfiles #133

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions tests/test_staticfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import os
from unittest.mock import patch

import pytest

import flask_restful_swagger
from flask_restful_swagger.swagger import StaticFiles

test_fixtures_renders = [
["index.html", None, None],
["o2c.html", None, None],
["swagger-ui.js", None, None],
["swagger-ui.min.js", None, None],
["lib/swagger-oauth.js", None, None],
]


@patch("flask_restful_swagger.swagger.render_page")
@patch("flask_restful_swagger.swagger._get_current_registry")
@pytest.mark.parametrize("dir1,dir2,dir3", test_fixtures_renders)
def test_get_valid_content_renders(registry, render_page, dir1, dir2, dir3):

static_files = StaticFiles()
registry.return_value = {"spec_endpoint_path": "dummy"}

static_files.get(dir1, dir2, dir3)
assert render_page.call_args[0] == (dir1, {"resource_list_url": "dummy"})


test_fixtures_none = [[None, None, None]]


@patch("flask_restful_swagger.swagger.render_page")
@patch("flask_restful_swagger.swagger._get_current_registry")
@pytest.mark.parametrize("dir1,dir2,dir3", test_fixtures_none)
def test_get_valid_content_renders_none(
registry, render_page, dir1, dir2, dir3
):

static_files = StaticFiles()
registry.return_value = {"spec_endpoint_path": "dummy"}

static_files.get(dir1, dir2, dir3)
assert render_page.call_args[0] == (
"index.html",
{"resource_list_url": "dummy"},
)


test_fixtures_mimes = [
["index2.html", "text/plain"],
["image.gif", "image/gif"],
["image.png", "image/png"],
["javascript.js", "text/javascript"],
["style.css", "text/css"],
]


@patch("flask_restful_swagger.swagger.Response", autospec=True)
@patch("flask_restful_swagger.swagger.open")
@patch("flask_restful_swagger.swagger.os.path.exists")
@patch("flask_restful_swagger.swagger._get_current_registry")
@pytest.mark.parametrize("dir1,mime", test_fixtures_mimes)
def test_get_valid_content_mime(
registry, mock_exists, mock_open, response, dir1, mime
):

mock_open.return_value = "file_handle"
mock_exists.return_value = True

static_files = StaticFiles()
static_files.get(dir1, None, None)
assert mock_exists.called
assert mock_open.called

args, kwargs = response.call_args_list[0]
assert args == ("file_handle",)
assert kwargs == {"mimetype": mime}


test_fixtures_mimes_does_not_exist = ["index2.html"]


@patch("flask_restful_swagger.swagger.os.path.exists")
@patch("flask_restful_swagger.swagger._get_current_registry")
@patch("flask_restful_swagger.swagger.abort")
@pytest.mark.parametrize("dir1", test_fixtures_mimes_does_not_exist)
def test_get_valid_content_mime_file_does_not_exist(
abort, registry, mock_exists, dir1
):

mock_exists.return_value = False
static_files = StaticFiles()
static_files.get(dir1, None, None)
assert mock_exists.called
assert abort.called


test_fixtures_paths = [
["paths", "index2.html", None, "paths/index2.html"],
["paths", "more_paths", "index2.html", "paths/more_paths/index2.html"],
]


@patch("flask_restful_swagger.swagger.Response", autospec=True)
@patch("flask_restful_swagger.swagger.os.path.exists")
@patch("flask_restful_swagger.swagger.open")
@patch("flask_restful_swagger.swagger.render_page")
@patch("flask_restful_swagger.swagger._get_current_registry")
@pytest.mark.parametrize("dir1,dir2,dir3,expected", test_fixtures_paths)
def test_get_valid_content_paths(
registry,
render_page,
mock_open,
mock_exists,
response,
dir1,
dir2,
dir3,
expected,
):

mock_open.return_value = "file_handle"
mock_exists.return_value = True

static_files = StaticFiles()
registry.return_value = {"spec_endpoint_path": "dummy"}

static_files.get(dir1, dir2, dir3)
module_path = os.path.dirname(flask_restful_swagger.__file__)
static_files = "static"
full_path = os.path.join(module_path, static_files, expected)

assert mock_exists.called
assert mock_open.call_args_list[0][0][0] == full_path

args, kwargs = response.call_args_list[0]
assert args == ("file_handle",)
assert kwargs == {"mimetype": "text/plain"}