Skip to content

Commit

Permalink
feat(IDX): introduce integration tests (#92)
Browse files Browse the repository at this point in the history
* wip

* wip

* test

* add test

* fix

* test

* test output

* lint

* try again

* use real token

* updates

* update
  • Loading branch information
cgundy authored Dec 16, 2024
1 parent bfaaac1 commit 63edbc8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 10 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/python_lint_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ jobs:
black reusable_workflows/
flake8 reusable_workflows/
- name: Create GitHub App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.CLA_BOT_APP_ID }}
private-key: ${{ secrets.CLA_BOT_PRIVATE_KEY }}

- name: Run all tests
run: pytest --runslow reusable_workflows/
run: pytest --integration-tests reusable_workflows/
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sorting/target
**/__pycache__
.env/**
.venv/**
14 changes: 7 additions & 7 deletions reusable_workflows/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

def pytest_addoption(parser):
parser.addoption(
"--runslow", action="store_true", default=False, help="run slow tests"
"--integration-tests", action="store_true", default=False, help="run integration tests"
)


def pytest_configure(config):
config.addinivalue_line("markers", "slow: mark test as slow to run")
config.addinivalue_line("markers", "integration: mark test as integration test")


def pytest_collection_modifyitems(config, items):
if config.getoption("--runslow"):
# --runslow given in cli: do not skip slow tests
if config.getoption("--integration-tests"):
# --integration-tests given in cli: do not skip integration tests
return
skip_slow = pytest.mark.skip(reason="need --runslow option to run")
skip_integration = pytest.mark.skip(reason="need --integration-tests option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
if "integration" in item.keywords:
item.add_marker(skip_integration)
7 changes: 7 additions & 0 deletions reusable_workflows/tests/test_check_external_contrib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from unittest import mock

import github3
import pytest

from check_membership.check_external_contrib import (
Expand Down Expand Up @@ -53,3 +54,9 @@ def test_github_token_not_passed_in(github_login_mock):
assert (
str(exc.value) == "github login failed - maybe GH_TOKEN was not correctly set"
)

@pytest.mark.integration
def test_check_repos_open_to_contributions_accessible():
gh = github3.login(token=os.getenv("GH_TOKEN"))

get_repos_open_to_contributions(gh)
55 changes: 55 additions & 0 deletions reusable_workflows/tests/test_compliance_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import sys
from unittest import mock

import os
import pytest
import github3
from github3.exceptions import NotFoundError

from compliance.check_compliance.compliance_checks import (
Expand Down Expand Up @@ -318,3 +320,56 @@ def test_repo_permissions_team_name_not_found(get_team_name_mock):
repo_permissions_check.check(helper)
assert repo_permissions_check.succeeds is False
assert repo_permissions_check.message == "Raised error: Only one team can be listed for repo-level codeowners." # fmt: skip


def integration_test_setup() -> ComplianceCheckHelper:
gh = github3.login(token=os.getenv("GH_TOKEN"))
repo = gh.repository("dfinity", "test-compliant-repository-public")
org = gh.organization("dfinity")
helper = ComplianceCheckHelper(repo, org)
return helper

@pytest.mark.integration
def test_get_code_owners():
helper = integration_test_setup()

code_owners = helper.get_code_owners()

assert code_owners == "* @dfinity/idx\n"

@pytest.mark.integration
def test_get_repo_permissions():
helper = integration_test_setup()
repo_permissions = RepoPermissions()

repo_permissions.check(helper)

assert repo_permissions.succeeds is True
assert repo_permissions.message == "Team idx has maintain permissions."

@pytest.mark.integration
def test_branch_protection():
helper = integration_test_setup()
branch_protection = BranchProtection()

branch_protection.check(helper)

assert branch_protection.succeeds is True

@pytest.mark.integration
def test_license():
helper = integration_test_setup()
license = License()

license.check(helper)

assert license.succeeds is True

@pytest.mark.integration
def test_readme():
helper = integration_test_setup()
readme = Readme()

readme.check(helper)

assert readme.succeeds is True
9 changes: 9 additions & 0 deletions reusable_workflows/tests/test_membership.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from unittest import mock

import github3
from github3.exceptions import NotFoundError
import pytest

Expand Down Expand Up @@ -129,3 +130,11 @@ def test_github_token_not_passed_in(github_login_mock):
assert (
str(exc.value) == "github login failed - maybe GH_TOKEN was not correctly set"
)

@pytest.mark.integration
def test_is_member_of_org():
gh = github3.login(token=os.getenv("GH_TOKEN"))

is_member = is_member_of_org(gh, "dfinity", "sa-github-api")

assert is_member is True
4 changes: 2 additions & 2 deletions reusable_workflows/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_download_file_succeeds_first_try():
assert repo.file_contents.call_count == 1


@pytest.mark.slow
@pytest.mark.integration
def test_download_file_succeeds_third_try():
repo = mock.MagicMock()
file_content_obj = mock.Mock()
Expand All @@ -35,7 +35,7 @@ def test_download_file_succeeds_third_try():
assert repo.file_contents.call_count == 3


@pytest.mark.slow
@pytest.mark.integration
@mock.patch("requests.get")
def test_download_file_fails(mock_get):
repo = mock.MagicMock()
Expand Down

0 comments on commit 63edbc8

Please sign in to comment.