Beispiel #1
0
 def set_instances(
     self,
     local_filepath,
     global_filepath,
     local_instance=None,
     global_instance=None,
     default_instance=None,
 ):
     auth_config_data = deepcopy(TestAuthConfig.default_config)
     for i in range(1, 6):
         url = f"https://instance{i}.com"
         auth_config_data["instances"][url] = deepcopy(
             auth_config_data["instances"]["default"])
         auth_config_data["instances"][url]["url"] = url
     if local_instance:
         write_yaml(local_filepath, {"dashboard-url": local_instance})
     else:
         if os.path.isfile(local_filepath):
             os.remove(local_filepath)
     if global_instance:
         write_yaml(global_filepath, {"dashboard-url": global_instance})
     else:
         if os.path.isfile(global_filepath):
             os.remove(global_filepath)
     if default_instance:
         auth_config_data["default-instance"] = default_instance
     write_yaml(get_auth_config_filepath(), auth_config_data)
Beispiel #2
0
 def test_timezone_aware_expired(self):
     """
     GIVEN a config with a configured instance
     WHEN loading the config
     THEN the instance expiration date is timezone aware
     """
     write_yaml(get_auth_config_filepath(), self.default_config)
     config = Config()
     assert config.instances["default"].account.expire_at.tzinfo is not None
Beispiel #3
0
    def test_token_not_expiring(self):
        """
        GIVEN an auth config file with a token never expiring
        WHEN loading the AuthConfig
        THEN it works
        """
        raw_config = deepcopy(self.default_config)
        raw_config["instances"]["default"]["accounts"][0]["expire-at"] = None
        write_yaml(get_auth_config_filepath(), raw_config)

        config = Config()

        assert config.instances["default"].account.expire_at is None
Beispiel #4
0
    def test_invalid_format(self, capsys):
        """
        GIVEN an auth config file with invalid content
        WHEN loading AuthConfig
        THEN it raises
        """
        write_text(get_auth_config_filepath(), "Not a:\nyaml file.\n")

        Config()
        out, err = capsys.readouterr()
        sys.stdout.write(out)
        sys.stderr.write(err)

        assert f"Parsing error while reading {get_auth_config_filepath()}:" in out
Beispiel #5
0
    def test_load(self):
        """
        GIVEN a default auth config
        WHEN loading the config
        THEN when serializing it again, it matches the data.
        """
        write_yaml(get_auth_config_filepath(), self.default_config)

        config = Config()

        assert config.instances["default"].account.token_name == "my_token"

        config_data = config.auth_config.to_dict()
        replace_in_keys(config_data, old_char="_", new_char="-")
        assert config_data == self.default_config
Beispiel #6
0
    def test_save_file_not_existing(self):
        """
        GIVEN a config object and the auth config file not existing
        WHEN saving the config
        THEN it works and when loading the config again it has the correct values
        """
        config = Config()
        try:
            os.remove(get_auth_config_filepath())
        except FileNotFoundError:
            pass

        config.default_instance = "custom"
        config.save()
        updated_config = Config()

        assert updated_config.default_instance == "custom"
Beispiel #7
0
    def test_no_account(self, n):
        """
        GIVEN an auth config with a instance with 0 or more than 1 accounts
        WHEN loading the AuthConfig
        THEN it raises
        """
        raw_config = deepcopy(self.default_config)
        raw_config["instances"]["default"]["accounts"] = (
            raw_config["instances"]["default"]["accounts"] * n)
        write_yaml(get_auth_config_filepath(), raw_config)

        with pytest.raises(
                AssertionError,
                match=
                "Each GitGuardian instance should have exactly one account",
        ):
            Config()