async def test_get_mergify_config_location_from_cache( redis_cache: utils.RedisCache, ) -> None: client = mock.AsyncMock() client.auth.owner = "foo" client.item.side_effect = [ http.HTTPNotFound("Not Found", request=mock.Mock(), response=mock.Mock()), http.HTTPNotFound("Not Found", request=mock.Mock(), response=mock.Mock()), github_types.GitHubContentFile({ "content": encodebytes("whatever".encode()).decode(), "type": "file", "path": ".github/mergify.yml", "sha": github_types.SHAType("zeazeaze"), }), ] installation = context.Installation( github_types.GitHubAccountIdType(0), github_types.GitHubLogin("foo"), subscription.Subscription(redis_cache, 0, False, "", frozenset()), client, redis_cache, ) repository = context.Repository(installation, github_types.GitHubRepositoryName("bar")) await repository.get_mergify_config_file() assert client.item.call_count == 3 client.item.assert_has_calls([ mock.call("/repos/foo/bar/contents/.mergify.yml"), mock.call("/repos/foo/bar/contents/.mergify/config.yml"), mock.call("/repos/foo/bar/contents/.github/mergify.yml"), ]) client.item.reset_mock() client.item.side_effect = [ github_types.GitHubContentFile({ "content": encodebytes("whatever".encode()).decode(), "type": "file", "path": ".github/mergify.yml", "sha": github_types.SHAType("zeazeaze"), }), ] repository._cache = context.RepositoryCache() await repository.get_mergify_config_file() assert client.item.call_count == 1 client.item.assert_has_calls([ mock.call("/repos/foo/bar/contents/.github/mergify.yml"), ])
def test_get_mergify_config_location_from_cache(): client = mock.Mock() client.auth.owner = "foo" client.item.side_effect = [ http.HTTPNotFound("Not Found", request=mock.Mock(), response=mock.Mock()), http.HTTPNotFound("Not Found", request=mock.Mock(), response=mock.Mock()), { "content": encodebytes("whatever".encode()).decode() }, ] filename, content = rules.get_mergify_config_content(client, "bar") assert client.item.call_count == 3 client.item.assert_has_calls([ mock.call("/repos/foo/bar/contents/.mergify.yml"), mock.call("/repos/foo/bar/contents/.mergify/config.yml"), mock.call("/repos/foo/bar/contents/.github/mergify.yml"), ]) client.item.reset_mock() client.item.side_effect = [ { "content": encodebytes("whatever".encode()).decode() }, ] filename, content = rules.get_mergify_config_content(client, "bar") assert client.item.call_count == 1 client.item.assert_has_calls([ mock.call("/repos/foo/bar/contents/.github/mergify.yml"), ])
async def test_team_permissions_missing(redis_cache): action = request_reviews.RequestReviewsAction.get_schema()({ "random_count": 2, "teams": { "foobar": 2, "@other/foobaz": 1, }, "users": { "jd": 2, "sileht": 1, }, }, ) client = mock.MagicMock() client.auth.installation.__getitem__.return_value = 123 client.get = mock.AsyncMock(side_effect=http.HTTPNotFound( message="not found", response=mock.ANY, request=mock.ANY)) ctxt = await prepare_context(client, redis_cache) result = await action.run(ctxt, None) assert result.conclusion == check_api.Conclusion.FAILURE assert result.title == "Invalid requested teams" for error in ( "Team `foobar` does not exist or has not access to this repository", "Team `@other/foobaz` is not part of the organization `Mergifyio`", ): assert error in result.summary
async def test_team_permissions_missing( context_getter: conftest.ContextGetterFixture, ) -> None: action = request_reviews.RequestReviewsAction.get_schema()({ "random_count": 2, "teams": { "foobar": 2, "@other/foobaz": 1, }, "users": { "jd": 2, "sileht": 1, }, }, ) client = mock.MagicMock() client.get = mock.AsyncMock(side_effect=http.HTTPNotFound( message="not found", response=mock.ANY, request=mock.ANY)) ctxt = await context_getter(github_types.GitHubPullRequestNumber(1)) ctxt.repository.installation.client = client result = await action.run(ctxt, None) assert result.conclusion == check_api.Conclusion.FAILURE assert result.title == "Invalid requested teams" for error in ( "Team `foobar` does not exist or has not access to this repository", "Team `@other/foobaz` is not part of the organization `Mergifyio`", ): assert error in result.summary
def item_call(url, *args, **kwargs): if url == "/repos/Mergifyio/mergify-engine/branches/main": return {"commit": {"sha": "sha1"}, "protection": {"enabled": False}} if url == "/repos/Mergifyio/mergify-engine/branches/main/protection": raise http.HTTPNotFound( message="boom", response=mock.Mock(), request=mock.Mock() ) else: raise Exception(f"url not mocked: {url}")
async def get(self, url, *args, **kwargs): self.called += 1 if (url == f"/orgs/{self.owner}/teams/team-ok/repos/{self.owner}/{self.repo}" ): return {} elif (url == f"/orgs/{self.owner}/teams/team-nok/repos/{self.owner}/{self.repo}" ): raise http.HTTPNotFound(message="Not found", request=mock.ANY, response=mock.ANY) elif (url == f"/orgs/{self.owner}/teams/team-also-nok/repos/{self.owner}/{self.repo}" ): raise http.HTTPNotFound(message="Not found", request=mock.ANY, response=mock.ANY) raise ValueError(f"Unknown test URL `{url}`")
async def get( self, url: str, *args: typing.Any, **kwargs: typing.Any) -> typing.Any: # type: ignore[override] self.called += 1 if (url == f"/orgs/{self.owner}/teams/team-ok/repos/{self.owner}/{self.repo}" ): return {} elif (url == f"/orgs/{self.owner}/teams/team-nok/repos/{self.owner}/{self.repo}" ): raise http.HTTPNotFound(message="Not found", request=mock.ANY, response=mock.ANY) elif (url == f"/orgs/{self.owner}/teams/team-also-nok/repos/{self.owner}/{self.repo}" ): raise http.HTTPNotFound(message="Not found", request=mock.ANY, response=mock.ANY) raise ValueError(f"Unknown test URL `{url}`")
async def test_get_mergify_config_location_from_cache( redis_cache: utils.RedisCache, ) -> None: client = mock.AsyncMock() client.auth.owner = "foo" client.item.side_effect = [ http.HTTPNotFound("Not Found", request=mock.Mock(), response=mock.Mock()), http.HTTPNotFound("Not Found", request=mock.Mock(), response=mock.Mock()), github_types.GitHubContentFile({ "content": encodebytes("whatever".encode()).decode(), "type": "file", "path": ".github/mergify.yml", "sha": github_types.SHAType("zeazeaze"), }), ] gh_owner = github_types.GitHubAccount({ "login": github_types.GitHubLogin("foobar"), "id": github_types.GitHubAccountIdType(0), "type": "User", "avatar_url": "", }) gh_repo = github_types.GitHubRepository({ "full_name": "foobar/xyz", "name": github_types.GitHubRepositoryName("xyz"), "private": False, "id": github_types.GitHubRepositoryIdType(0), "owner": gh_owner, "archived": False, "url": "", "html_url": "", "default_branch": github_types.GitHubRefType("ref"), }) installation = context.Installation( github_types.GitHubAccountIdType(0), github_types.GitHubLogin("foobar"), subscription.Subscription(redis_cache, 0, False, "", frozenset()), client, redis_cache, ) repository = context.Repository(installation, gh_repo) await repository.get_mergify_config_file() assert client.item.call_count == 3 client.item.assert_has_calls([ mock.call("/repos/foobar/xyz/contents/.mergify.yml", params={}), mock.call("/repos/foobar/xyz/contents/.mergify/config.yml", params={}), mock.call("/repos/foobar/xyz/contents/.github/mergify.yml", params={}), ]) client.item.reset_mock() client.item.side_effect = [ github_types.GitHubContentFile({ "content": encodebytes("whatever".encode()).decode(), "type": "file", "path": ".github/mergify.yml", "sha": github_types.SHAType("zeazeaze"), }), ] repository._cache = context.RepositoryCache() await repository.get_mergify_config_file() assert client.item.call_count == 1 client.item.assert_has_calls([ mock.call("/repos/foobar/xyz/contents/.github/mergify.yml", params={}), ])