<commit_before>from __future__ import absolute_import, print_function

from project.internal.test.tmpfile_utils import with_directory_contents
from project.local_state_file import LocalStateFile
from project.plugins.provider import ProvideContext, ProviderConfigContext
from project.plugins.registry import PluginRegistry
from project.project import Project
from project.project_file import PROJECT_FILENAME
from project.plugins.requirements.master_password import MasterPasswordRequirement
from project.plugins.providers.master_password import MasterPasswordProvider


def test_find_by_class_name():
    registry = PluginRegistry()
    found = registry.find_provider_by_class_name(class_name="MasterPasswordProvider")
    assert found is not None
    assert found.__class__.__name__ == "MasterPasswordProvider"


def _load_master_password_requirement(dirname):
    project = Project(dirname)
    for requirement in project.requirements:
        if isinstance(requirement, MasterPasswordRequirement):
            return requirement
    raise RuntimeError("No requirement for master password was in the project file, only %r" % (project.requirements))


def test_master_password_provider_with_value_set_in_environment():
    def check_set_in_environment(dirname):
        provider = MasterPasswordProvider()
        requirement = _load_master_password_requirement(dirname)
        local_state_file = LocalStateFile.load_for_directory(dirname)
        config_context = ProviderConfigContext(dict(), local_state_file, requirement)
        config = provider.read_config(config_context)
        assert dict() == config
        context = ProvideContext(environ=dict(ANACONDA_MASTER_PASSWORD='<PASSWORD>'),
                                 local_state_file=local_state_file,
                                 config=config)
        provider.provide(requirement, context=context)
        assert 'ANACONDA_MASTER_PASSWORD' in context.environ
        assert 'from_environ' == context.environ['ANACONDA_MASTER_PASSWORD']

    # set a default to be sure we prefer 'environ' instead
    with_directory_contents(
        {PROJECT_FILENAME: """
runtime:
  ANACONDA_MASTER_PASSWORD: { default: '<PASSWORD>' }
"""}, check_set_in_environment)


def test_master_password_provider_with_value_set_in_keyring():
    def check_set_in_keyring(dirname):
        from project.internal import keyring
        keyring.set('ANACONDA_MASTER_PASSWORD', 'from_keyring')
        try:
            provider = MasterPasswordProvider()<commit_msg>Add test for master password config_html with no password already set<commit_after>from __future__ import absolute_import, print_function

from project.internal.test.tmpfile_utils import with_directory_contents
from project.local_state_file import LocalStateFile
from project.plugins.provider import ProvideContext, ProviderConfigContext
from project.plugins.registry import PluginRegistry
from project.project import Project
from project.project_file import PROJECT_FILENAME
from project.plugins.requirements.master_password import MasterPasswordRequirement
from project.plugins.providers.master_password import MasterPasswordProvider


def test_find_by_class_name():
    registry = PluginRegistry()
    found = registry.find_provider_by_class_name(class_name="MasterPasswordProvider")
    assert found is not None
    assert found.__class__.__name__ == "MasterPasswordProvider"


def _load_master_password_requirement(dirname):
    project = Project(dirname)
    for requirement in project.requirements:
        if isinstance(requirement, MasterPasswordRequirement):
            return requirement
    raise RuntimeError("No requirement for master password was in the project file, only %r" % (project.requirements))


def test_master_password_provider_with_value_not_set():
    def check_not_set(dirname):
        provider = MasterPasswordProvider()
        requirement = _load_master_password_requirement(dirname)
        local_state_file = LocalStateFile.load_for_directory(dirname)
        config_context = ProviderConfigContext(dict(), local_state_file, requirement)
        config = provider.read_config(config_context)
        assert dict() == config
        status = requirement.check_status(dict())
        html = provider.config_html(config_context, status)
        assert 'type="password"' in html
        context = ProvideContext(environ=dict(), local_state_file=local_state_file, config=config)
        provider.provide(requirement, context=context)
        assert 'ANACONDA_MASTER_PASSWORD' not in context.environ

    with_directory_contents({PROJECT_FILENAME: """
runtime:
  ANACONDA_MASTER_PASSWORD: {}
"""}, check_not_set)


def test_master_password_provider_with_value_set_in_environment():
    def check_set_in_environment(dirname):
        provider = MasterPasswordProvider()
        requirement = _load_master_password_requirement(dirname)
        local_state_file = LocalStateFile.load_for_directory(dirname)
        config_context = ProviderConfigContext(dict(), local_state_file, requirement)
        config = provider.read_config(config_context)
        assert dict() == config
        context = ProvideContext(environ=dict(ANACONDA_MASTER_PASSWORD='<PASSWORD>'),
                                 local_state_file=local_state_file,
                                 config=config)
        provider.provide(requirement, context=context)
        assert 'ANACONDA_MASTER_PASSWORD' in context.environ
        assert 'from_environ' == context.environ['ANACONDA_MASTER_PASSWORD']

    # set a default to be sure we prefer 'environ' instead
    with_directory_contents(
        {PROJECT_FILENAME: """
runtime:
  ANACONDA_MASTER_PASSWORD: { default: '<PASSWORD>' }
"""}, check_set_in_environment)


def test_master_password_provider_with_value_set_in_keyring():
    def check_set_in_keyring(dirname):
        from project.internal import keyring
        keyring.set('ANACONDA_MASTER_PASSWORD', '<PASSWORD>key<PASSWORD>')
        try:
            provider = MasterPasswordProvider()
{'fraction_non_alphanumeric': 0.07006157430520886, 'fraction_numerical': 0.0, 'mean_word_length': 4.528978840846366, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5040461', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1572, 'n_words': 334}