<commit_before>import glob
import os
import pytest
import shutil
import requests

from moulinette import m18n
from moulinette.utils.filesystem import mkdir

from yunohost.app import app_install, app_remove, app_ssowatconf, _is_installed, app_upgrade
from yunohost.domain import _get_maindomain, domain_add, domain_remove, domain_list
from yunohost.utils.error import YunohostError
from yunohost.tests.test_permission import check_LDAP_db_integrity, check_permission_for_apps


def setup_function(function):

    clean()

def teardown_function(function):

    clean()

def clean():

    # Make sure we have a ssowat
    os.system("mkdir -p /etc/ssowat/")
    app_ssowatconf()

    # Gotta first remove break yo system
    # because some remaining stuff might
    # make the other app_remove crashs ;P
    if _is_installed("break_yo_system"):
        app_remove("break_yo_system")

    if _is_installed("legacy_app"):
        app_remove("legacy_app")

    if _is_installed("full_domain_app"):
        app_remove("full_domain_app")

    to_remove = []
    to_remove += glob.glob("/etc/nginx/conf.d/*.d/*legacy*")
    to_remove += glob.glob("/etc/nginx/conf.d/*.d/*full_domain*")
    to_remove += glob.glob("/etc/nginx/conf.d/*.d/*break_yo_system*")
    for filepath in to_remove:
        os.remove(filepath)

    to_remove = []
    to_remove += glob.glob("/etc/yunohost/apps/*legacy_app*")
    to_remove += glob.glob("/etc/yunohost/apps/*full_domain_app*")
    to_remove += glob.glob("/etc/yunohost/apps/*break_yo_system*")
    to_remove += glob.glob("/var/www/*legacy*")
    to_remove += glob.glob("/var/www/*full_domain*")
    for folderpath in to_remove:
        shutil.rmtree(folderpath, ignore_errors=True)

    os.system("systemctl reset-failed nginx")  # Reset failed quota for service to avoid running into start-limit rate ?
    os.system("systemctl start nginx")


@pytest.fixture(autouse=True)
def check_LDAP_db_integrity_call():
    check_LDAP_db_integrity()
    yield
    check_LDAP_db_integrity()


@pytest.fixture(autouse=True)
def check_permission_for_apps_call():
    check_permission_for_apps()
    yield
    check_permission_for_apps()

@pytest.fixture(scope="session")
def secondary_domain(request):

    if "example.test" not in domain_list()["domains"]:
        domain_add("example.test")

    def remove_example_domain():
        domain_remove("example.test")
    request.addfinalizer(remove_example_domain)

    return "example.test"


#
# Helpers                                                                    #
#

def app_expected_files(domain, app):

    yield "/etc/nginx/conf.d/%s.d/%s.conf" % (domain, app)
    if app.startswith("legacy_app"):
        yield "/var/www/%s/index.html" % app
    yield "/etc/yunohost/apps/%s/settings.yml" % app
    yield "/etc/yunohost/apps/%s/manifest.json" % app
    yield "/etc/yunohost/apps/%s/scripts/install" % app
    yield "/etc/yunohost/apps/%s/scripts/remove" % app


def app_is_installed(domain, app):

    return _is_installed(app) and all(os.path.exists(f) for f in app_expected_files(domain, app))


def app_is_not_installed(domain, app):

    return not _is_installed(app) and not all(os.path.exists(f) for f in app_expected_files(domain, app))


def app_is_exposed_on_http(domain, path, message_in_page):

    try:
        r = requests.get("http://127.0.0.1" + path + "/", headers={"Host": domain}, timeout=10)
        return r.status_code == 200 and message_in_page in r.text
    except Exception:
        return False


def install_legacy_app(domain, path):

    app_install("./tests/apps/legacy_app_ynh",
                args="domain=%s&path=%s" % (domain, path),
                force=True)


def install_full_domain_app(domain):

    app_install("./tests/apps/full_domain_app_ynh",
                args="domain=%s" % domain,
                force=True)


def install_break_yo_system(domain, breakwhat):

    app_install("./tests/apps/break_yo_system_ynh",
                args="domain=%s&breakwhat=%s" % (domain, breakwhat),
                force=True)


def test_legacy_app_install_main_domain():

    main_domain = _get_maindomain()

    install_legacy_app(main_domain, "/legacy")

    assert app_is_installed(main_domain, "legacy_app")
    assert app_is_exposed_on_http(main_domain, "/legacy", "This is a dummy app")

    app_remove("legacy_app")

    assert app_is_not_installed(main_domain, "legacy_app")


def test_legacy_app_install_secondary_domain(secondary_domain):

    install_legacy_app(secondary_domain, "/legacy")

    assert app_is_installed(secondary_domain, "legacy_app")
    assert app_is_exposed_on_http(secondary_domain, "/legacy", "This is a dummy app")

    app_remove("legacy_app")

    assert app_is_not_installed(secondary_domain, "legacy_app")


def test_legacy_app_install_secondary_domain_on_root(secondary_domain):

    install_legacy_app(secondary_domain, "/")

    assert app_is_installed(secondary_domain, "legacy_app")
    assert app_is_exposed_on_http(secondary_domain, "/", "This is a dummy app")

    app_remove("legacy_app")

    assert app_is_not_installed(secondary_domain, "legacy_app")


def test_legacy_app_install_private(secondary_domain):

    install_legacy_app(secondary_domain, "/legacy")

    settings = open("/etc/yunohost/apps/legacy_app/settings.yml", "r").read()
    new_settings = settings.replace("\nunprotected_uris: /", "")
    assert new_settings != settings
    open("/etc/yunohost/apps/legacy_app/settings.yml", "w").write(new_settings)
    app_ssowatconf()

    assert app_is_installed(secondary_domain, "legacy_app")
    assert not app_is_exposed_on_http(secondary_domain, "/legacy", "This is a dummy app")
<commit_msg>Fix test about private app installs<commit_after>import glob
import os
import pytest
import shutil
import requests

from moulinette import m18n
from moulinette.utils.filesystem import mkdir

from yunohost.app import app_install, app_remove, app_ssowatconf, _is_installed, app_upgrade
from yunohost.domain import _get_maindomain, domain_add, domain_remove, domain_list
from yunohost.utils.error import YunohostError
from yunohost.tests.test_permission import check_LDAP_db_integrity, check_permission_for_apps


def setup_function(function):

    clean()

def teardown_function(function):

    clean()

def clean():

    # Make sure we have a ssowat
    os.system("mkdir -p /etc/ssowat/")
    app_ssowatconf()

    # Gotta first remove break yo system
    # because some remaining stuff might
    # make the other app_remove crashs ;P
    if _is_installed("break_yo_system"):
        app_remove("break_yo_system")

    if _is_installed("legacy_app"):
        app_remove("legacy_app")

    if _is_installed("full_domain_app"):
        app_remove("full_domain_app")

    to_remove = []
    to_remove += glob.glob("/etc/nginx/conf.d/*.d/*legacy*")
    to_remove += glob.glob("/etc/nginx/conf.d/*.d/*full_domain*")
    to_remove += glob.glob("/etc/nginx/conf.d/*.d/*break_yo_system*")
    for filepath in to_remove:
        os.remove(filepath)

    to_remove = []
    to_remove += glob.glob("/etc/yunohost/apps/*legacy_app*")
    to_remove += glob.glob("/etc/yunohost/apps/*full_domain_app*")
    to_remove += glob.glob("/etc/yunohost/apps/*break_yo_system*")
    to_remove += glob.glob("/var/www/*legacy*")
    to_remove += glob.glob("/var/www/*full_domain*")
    for folderpath in to_remove:
        shutil.rmtree(folderpath, ignore_errors=True)

    os.system("systemctl reset-failed nginx")  # Reset failed quota for service to avoid running into start-limit rate ?
    os.system("systemctl start nginx")


@pytest.fixture(autouse=True)
def check_LDAP_db_integrity_call():
    check_LDAP_db_integrity()
    yield
    check_LDAP_db_integrity()


@pytest.fixture(autouse=True)
def check_permission_for_apps_call():
    check_permission_for_apps()
    yield
    check_permission_for_apps()

@pytest.fixture(scope="session")
def secondary_domain(request):

    if "example.test" not in domain_list()["domains"]:
        domain_add("example.test")

    def remove_example_domain():
        domain_remove("example.test")
    request.addfinalizer(remove_example_domain)

    return "example.test"


#
# Helpers                                                                    #
#

def app_expected_files(domain, app):

    yield "/etc/nginx/conf.d/%s.d/%s.conf" % (domain, app)
    if app.startswith("legacy_app"):
        yield "/var/www/%s/index.html" % app
    yield "/etc/yunohost/apps/%s/settings.yml" % app
    yield "/etc/yunohost/apps/%s/manifest.json" % app
    yield "/etc/yunohost/apps/%s/scripts/install" % app
    yield "/etc/yunohost/apps/%s/scripts/remove" % app


def app_is_installed(domain, app):

    return _is_installed(app) and all(os.path.exists(f) for f in app_expected_files(domain, app))


def app_is_not_installed(domain, app):

    return not _is_installed(app) and not all(os.path.exists(f) for f in app_expected_files(domain, app))


def app_is_exposed_on_http(domain, path, message_in_page):

    try:
        r = requests.get("http://127.0.0.1" + path + "/", headers={"Host": domain}, timeout=10)
        return r.status_code == 200 and message_in_page in r.text
    except Exception:
        return False


def install_legacy_app(domain, path, public=True):

    app_install("./tests/apps/legacy_app_ynh",
                args="domain=%s&path=%s&is_public=%s" % (domain, path, 1 if public else 0),
                force=True)


def install_full_domain_app(domain):

    app_install("./tests/apps/full_domain_app_ynh",
                args="domain=%s" % domain,
                force=True)


def install_break_yo_system(domain, breakwhat):

    app_install("./tests/apps/break_yo_system_ynh",
                args="domain=%s&breakwhat=%s" % (domain, breakwhat),
                force=True)


def test_legacy_app_install_main_domain():

    main_domain = _get_maindomain()

    install_legacy_app(main_domain, "/legacy")

    assert app_is_installed(main_domain, "legacy_app")
    assert app_is_exposed_on_http(main_domain, "/legacy", "This is a dummy app")

    app_remove("legacy_app")

    assert app_is_not_installed(main_domain, "legacy_app")


def test_legacy_app_install_secondary_domain(secondary_domain):

    install_legacy_app(secondary_domain, "/legacy")

    assert app_is_installed(secondary_domain, "legacy_app")
    assert app_is_exposed_on_http(secondary_domain, "/legacy", "This is a dummy app")

    app_remove("legacy_app")

    assert app_is_not_installed(secondary_domain, "legacy_app")


def test_legacy_app_install_secondary_domain_on_root(secondary_domain):

    install_legacy_app(secondary_domain, "/")

    assert app_is_installed(secondary_domain, "legacy_app")
    assert app_is_exposed_on_http(secondary_domain, "/", "This is a dummy app")

    app_remove("legacy_app")

    assert app_is_not_installed(secondary_domain, "legacy_app")


def test_legacy_app_install_private(secondary_domain):

    install_legacy_app(secondary_domain, "/legacy", public=False)

    assert app_is_installed(secondary_domain, "legacy_app")
    assert not app_is_exposed_on_http(secondary_domain, "/legacy", "This is a dummy app")

{'fraction_non_alphanumeric': 0.11724014014913305, 'fraction_numerical': 0.0025154972599047707, 'mean_word_length': 3.9147902869757174, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3199550', 'n_tokens_mistral': 3871, 'n_tokens_neox': 3707, 'n_words': 679}