def test_load_config_malformed_yaml(self):
        # Setup
        tmpfile = tempfile.NamedTemporaryFile(mode="wb", prefix="xrdsst-", suffix='.yaml', delete=False)
        tmpfile.write(bytes("{a", "utf-8"))
        tmpfile.close()

        # Given
        config_file = tmpfile.name
        base_controller = BaseController()
        base_controller.app = Mock()
        base_controller.app.close = Mock(return_value=None)

        # When
        base_controller.load_config(config_file)

        # Cleanup
        os.remove(config_file)

        # Then
        out, err = self.capsys.readouterr()
        assert out.count("Error parsing config") > 0
        assert out.count("line 1") > 0

        with self.capsys.disabled():
            sys.stdout.write(out)
            sys.stderr.write(err)

        base_controller.app.close.assert_called_once_with(os.EX_CONFIG)
    def test_load_config_serverless(self):
        # Setup
        config_data = [
            'logging:\n',
            '  file: logfile-test.log\n',
            'security_server:\n'
        ]

        tmpfile = tempfile.NamedTemporaryFile(mode="w", prefix="xrdsst-", suffix='.yaml', delete=False)
        tmpfile.writelines(config_data)
        tmpfile.close()

        # Given
        config_file = tmpfile.name
        base_controller = BaseController()
        base_controller.app = Mock()
        base_controller.app.close = Mock(return_value=None)
        base_controller.is_output_tabulated = Mock(return_value=True)

        # When
        base_controller.load_config(config_file)

        # Cleanup
        os.remove(config_file)

        # Then
        out, err = self.capsys.readouterr()
        assert err.count("No security servers defined") > 0

        with self.capsys.disabled():
            sys.stdout.write(out)
            sys.stderr.write(err)

        base_controller.app.close.assert_called_once_with(os.EX_CONFIG)
    def test_load_config_spelunked_keys(self):
        # Setup
        tmpfile = tempfile.NamedTemporaryFile(mode="w", prefix="xrdsst-", suffix='.yaml', delete=False)
        tmpfile.writelines([
            'kogging:\n',
            '  file: asdqwe\n',
            'security_server:\n',
            '  name: xnnn\n',
            '  ull: http://somesite.com'
        ])
        tmpfile.close()

        # Given
        config_file = tmpfile.name
        base_controller = BaseController()
        base_controller.app = Mock()
        base_controller.app.close = Mock(return_value=None)

        # When
        base_controller.load_config(config_file)

        # Cleanup
        os.remove(config_file)

        # Then
        out, err = self.capsys.readouterr()
        assert err.count("kogging NOT AMONG") > 0
        assert err.count("ull NOT AMONG") > 0
        assert out.count("Invalid configuration keys encountered") > 0

        with self.capsys.disabled():
            sys.stdout.write(out)
            sys.stderr.write(err)

        base_controller.app.close.assert_called_once_with(os.EX_CONFIG)
 def test_unsuccessful_app_exit_with_nonexistant_config_spec(self):
     base_controller = BaseController()
     base_controller.app = Mock()
     base_controller.app.pargs = Mock()
     base_controller.app.pargs.configfile = 'just/not/there/at/all'
     base_controller.app.close = Mock(return_value=None)
     base_controller.load_config(baseconfig=None)
     base_controller.app.close.assert_called_once_with(os.EX_CONFIG)
    def test_load_config_servers_name_or_url_non_unique(self):
        # Setup
        config_data = [
            'logging:\n',
            '  file: logfile-test.log\n',
            'security_server:\n',
            '  - name: first\n',
            '    url: first_url\n',
            '  - name: second\n',
            '    url: second_url\n',
            '  - url: third_url\n',
            '    name: third\n',
            '  - name: fourth\n',
            '    url: third_url\n',
            '    security_server_code: QED\n',
            '  - name: fifth\n',
            '    url: second_url\n',
            '  - name: first\n',
            '    url: faraway.com\n'
        ]

        tmpfile = tempfile.NamedTemporaryFile(mode="w", prefix="xrdsst-", suffix='.yaml', delete=False)
        tmpfile.writelines(config_data)
        tmpfile.close()

        # Given
        config_file = tmpfile.name
        base_controller = BaseController()
        base_controller.app = Mock()
        base_controller.app.close = Mock(return_value=None)
        base_controller.is_output_tabulated = Mock(return_value=True)

        # When
        base_controller.load_config(config_file)

        # Cleanup
        os.remove(config_file)

        # Then
        out, err = self.capsys.readouterr()
        assert err.count("security_server[1] 'name' value 'first' is non-unique") == 1
        assert err.count("security_server[6] 'name' value 'first' is non-unique") == 1
        assert err.count("security_server[2] 'url' value 'second_url' is non-unique") == 1
        assert err.count("security_server[5] 'url' value 'second_url' is non-unique") == 1
        assert err.count("security_server[3] 'url' value 'third_url' is non-unique") == 1
        assert err.count("security_server[4] 'url' value 'third_url' is non-unique") == 1

        with self.capsys.disabled():
            sys.stdout.write(out)
            sys.stderr.write(err)

        base_controller.app.close.assert_called_once_with(os.EX_CONFIG)
    def test_load_config_access_exception(self):
        base_controller = BaseController()
        base_controller.app = Mock()
        config_file = "/etc/shadow"
        base_controller.load_config(config_file)

        out, err = self.capsys.readouterr()
        assert out.count("ermission") > 0
        assert out.count("ould not read file") > 0

        with self.capsys.disabled():
            sys.stdout.write(out)
            sys.stderr.write(err)
    def setUp(self):
        with XRDSSTTest() as app:
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

            idx = 0
            for arg in sys.argv:
                idx += 1
                if arg == "-c":
                    self.config_file = sys.argv[idx]
            base = BaseController()
            base.app = app
            self.config = base.load_config(baseconfig=self.config_file)
            for security_server in self.config["security_server"]:
                api_key = base.get_api_key(self.config, security_server)
                self.create_api_key(api_key)
    def test_load_config(self):
        tmpfile = tempfile.NamedTemporaryFile(mode="wb", prefix="xrdsst-", suffix='.yaml', delete=False)
        tmpfile.close()

        temp_file_name = tmpfile.name
        with open(temp_file_name, "w") as yml_file:
            yaml.dump(TestBaseController._ss_config, yml_file)
        base_controller = BaseController()
        with open(temp_file_name, "r") as yml_file:
            cfg = yaml.safe_load(yml_file)

        assert cfg is not None

        ctr_config = base_controller.load_config(temp_file_name)
        os.remove(temp_file_name)

        assert ctr_config == cfg