<s> from collections import namedtuple <EOL> import os <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> import yaml <EOL> from passpie . importers import find_importer , BaseImporter , get_instances <EOL> from passpie . importers . default_importer import DefaultImporter <EOL> from passpie . importers . pysswords_importer import PysswordsImporter <EOL> def mock_open ( ) : <EOL> try : <EOL> from mock import mock_open as mopen <EOL> except : <EOL> from unittest . mock import mock_open as mopen <EOL> return mopen ( ) <EOL> def test_find_importer_returns_first_match_default_importer ( mocker ) : <EOL> mock_importer = mocker . Mock ( ) <EOL> mock_importer2 = mocker . Mock ( ) <EOL> mock_importer . match . return_value = False <EOL> mock_importer2 . match . return_value = True <EOL> mocker . patch ( '<STR_LIT>' , <EOL> return_value = [ mock_importer , mock_importer2 ] ) <EOL> importer = find_importer ( '<STR_LIT>' ) <EOL> assert importer is mock_importer2 <EOL> def test_default_importer_match_passpie_exported_yaml ( mocker ) : <EOL> dict_content = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:version>' : <NUM_LIT:1.0> } <EOL> mocker . patch ( '<STR_LIT>' , <EOL> mock_open ( ) , create = True ) <EOL> mocker . patch ( '<STR_LIT>' , <EOL> return_value = dict_content ) <EOL> result = DefaultImporter ( ) . match ( '<STR_LIT>' ) <EOL> assert result is True <EOL> def test_default_importer_returns_false_when_bad_file ( mocker ) : <EOL> mockopen = mocker . patch ( <EOL> '<STR_LIT>' , mock_open ( ) , create = True ) ( ) <EOL> mockopen . read . side_effect = OSError <EOL> result = DefaultImporter ( ) . match ( '<STR_LIT>' ) <EOL> assert result is False <EOL> def test_default_importer_returns_false_when_missing_version_key ( mocker ) : <EOL> dict_content = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mocker . patch ( '<STR_LIT>' , <EOL> mock_open ( ) , create = True ) <EOL> mocker . patch ( '<STR_LIT>' , <EOL> return_value = dict_content ) <EOL> result = DefaultImporter ( ) . match ( '<STR_LIT>' ) <EOL> assert result is False <EOL> def test_default_importer_returns_false_when_missing_handler_key ( mocker ) : <EOL> dict_content = { '<STR_LIT:version>' : <NUM_LIT:1.0> } <EOL> mocker . patch ( '<STR_LIT>' , <EOL> mock_open ( ) , create = True ) <EOL> mocker . patch ( '<STR_LIT>' , <EOL> return_value = dict_content ) <EOL> result = DefaultImporter ( ) . match ( '<STR_LIT>' ) <EOL> assert result is False <EOL> def test_default_importer_returns_false_when_version_keys_isnt_float ( mocker ) : <EOL> dict_content = { '<STR_LIT:version>' : '<STR_LIT:1.0>' } <EOL> mocker . patch ( '<STR_LIT>' , <EOL> mock_open ( ) , create = True ) <EOL> mocker . patch ( '<STR_LIT>' , <EOL> return_value = dict_content ) <EOL> result = DefaultImporter ( ) . match ( '<STR_LIT>' ) <EOL> assert result is False <EOL> def test_default_importer_returns_loaded_credentials_from_yaml_file ( mocker ) : <EOL> dict_content = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:name>' : '<STR_LIT:bar>' } } <EOL> mocker . patch ( '<STR_LIT>' , <EOL> mock_open ( ) , create = True ) <EOL> mocker . patch ( '<STR_LIT>' , <EOL> return_value = dict_content ) <EOL> result = DefaultImporter ( ) . handle ( '<STR_LIT>' ) <EOL> assert result is dict_content . get ( '<STR_LIT>' ) <EOL> def test_default_importer_match_returns_false_when_bad_yaml ( mocker ) : <EOL> mocker . patch ( '<STR_LIT>' , <EOL> mock_open ( ) , create = True ) <EOL> mocker . patch ( '<STR_LIT>' , <EOL> side_effect = [ yaml . scanner . ScannerError ] ) <EOL> result = DefaultImporter ( ) . match ( '<STR_LIT>' ) <EOL> assert result is False </s>
