Files @ 3802c52995e5
Branch filter:

Location: majic-ansible-roles/roles/wsgi_website/molecule/default/tests/test_parameters_optional.py

branko
MAR-218: Fix include invocations:

- The old include keyword/task has been deprecated in favour of more
explicit include_tasks one.
import os

import testinfra.utils.ansible_runner


testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-optional')


def test_installed_packages(host):
    """
    Tests if additional packages are installed.
    """

    assert host.package('atftp').is_installed
    assert host.package('global').is_installed


def test_index_page(host):
    """
    Tests if index page is served correctly. This covers:

    - Basic WSGI application operation.
    - Handling of environment variables.
    - Handling of HTTP header overrides.
    """

    page = host.run('curl -H "Accept-Encoding: plain" https://parameters-optional.local/')

    assert page.rc == 0
    assert "This is the WSGI application at parameters-optional.local." in page.stdout
    assert "Requested URL was: https://parameters-optional.local/" in page.stdout
    assert "MY_ENV_VAR: My environment variable" in page.stdout
    assert "Accept-Encoding: None" in page.stdout
    assert "Python version: 3." in page.stdout


def test_static_file_serving(host):
    """
    Tests serving of static files.
    """

    page = host.run('curl https://parameters-optional.local/static/static_file.txt')
    assert page.rc == 0
    assert page.stdout == open("tests/data/static_file.txt", 'r').read()

    page = host.run('curl https://parameters-optional.local/media/media_file.txt')
    assert page.rc == 0
    assert page.stdout == open("tests/data/media_file.txt", 'r').read()


def test_additional_nginx_config(host):
    """
    Tests if additional Nginx configuration directives are properly deployed.
    """

    page = host.run('curl https://parameters-optional.local/static/missing_static_file.txt')
    assert page.rc == 0
    assert "Requested URL was: https://parameters-optional.local/my/own/error/page"


def test_environment_indicator(host):
    """
    Tests if environment indicator is applied correctly.
    """

    page = host.run('curl https://parameters-optional.local/')

    expected_content = """\
    <details open='true' style='position: fixed; left: 0; width: 100%; line-height: 1.5em; font-weight: bold; color: #00ff00;'>
        <summary style='background-color: #ff0000; list-style-type: none; position: fixed; bottom: 5px; z-index: 999999;'>&#128896;&#128898;</summary>
        <div style='background-color: #ff0000; width: 100%; text-align: center; position: fixed; bottom: 5px; z-index: 999998;'>parameters-optional</div>
    </details>"""

    assert page.rc == 0
    assert expected_content in page.stdout


def test_nginx_rewrite_config(host):
    """
    Tests if Nginx rewrite configuration is deployed correctly.
    """

    page = host.run('curl https://parameters-optional.local/rewrite1/some/path')
    assert page.rc == 0
    assert "Requested URL was: https://parameters-optional.local/rewritten1/" in page.stdout

    page = host.run('curl https://parameters-optional.local/rewrite2/some/other/path')
    assert page.rc == 0
    assert "Requested URL was: https://parameters-optional.local/rewritten2/some/other/path" in page.stdout


def test_website_application_user(host):
    """
    Tests if website application user has been created correctly.
    """

    app_user = "web-parameters-optional_local"

    expected_group = "web-parameters-optional_local"
    expected_home = "/var/www/parameters-optional.local"
    expected_uid = 5001

    user = host.user(app_user)

    assert user.exists
    assert user.uid == expected_uid
    assert user.group == expected_group
    assert user.groups == [expected_group]
    assert user.shell == '/bin/sh'
    assert user.home == expected_home

    with host.sudo():
        umask = host.run("su -l " + app_user + " -c 'bash -c umask'")
        assert umask.stdout == '0007\n'