|
| 1 | + |
| 2 | +from wagon_common.gh.gh_repo import GhRepo |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from dotenv import load_dotenv, find_dotenv |
| 9 | + |
| 10 | + |
| 11 | +class TestGhRepo(): |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def token(self): |
| 15 | + """ |
| 16 | + fetch gh token to perform the tests |
| 17 | + """ |
| 18 | + |
| 19 | + # Arrange |
| 20 | + load_dotenv(find_dotenv()) |
| 21 | + token = os.environ.get("GH_API_DELETE_TOKEN") |
| 22 | + |
| 23 | + # Act & Assert |
| 24 | + yield token |
| 25 | + |
| 26 | + # Cleanup |
| 27 | + |
| 28 | + def test_gh_repo_naming(self): |
| 29 | + |
| 30 | + # Arrange |
| 31 | + |
| 32 | + # Act |
| 33 | + repo = GhRepo("lewagon-test/data-solutions", token=None) |
| 34 | + |
| 35 | + lw_repo = GhRepo("myriad", token=None) |
| 36 | + |
| 37 | + # Assert |
| 38 | + assert repo.owner == "lewagon-test" |
| 39 | + assert repo.repo == "data-solutions" |
| 40 | + assert repo.name == "lewagon-test/data-solutions" |
| 41 | + |
| 42 | + assert repo.ssh_url == f"git@github.com:{repo.owner}/{repo.repo}.git" |
| 43 | + assert repo.https_url == f"https://None@github.com/{repo.owner}/{repo.repo}.git" |
| 44 | + |
| 45 | + assert lw_repo.owner == "lewagon" |
| 46 | + assert lw_repo.repo == "myriad" |
| 47 | + assert lw_repo.name == "lewagon/myriad" |
| 48 | + |
| 49 | + # Cleanup |
| 50 | + |
| 51 | + def test_gh_repo_delete_prod(self, token): |
| 52 | + """ |
| 53 | + verify that a production repo cannot be deleted |
| 54 | + """ |
| 55 | + |
| 56 | + exception_catched = False |
| 57 | + |
| 58 | + try: |
| 59 | + GhRepo("lewagon/data-solutions", token=token).delete() |
| 60 | + except NameError as e: |
| 61 | + exception_catched = True |
| 62 | + assert "cannot delete repo in" in str(e) |
| 63 | + |
| 64 | + assert exception_catched |
| 65 | + |
| 66 | + def test_gh_repo_delete_test_qa(self, token): |
| 67 | + """ |
| 68 | + verify that a test or QA repo can be deleted |
| 69 | + """ |
| 70 | + |
| 71 | + # test gh repo can be deleted |
| 72 | + exception_catched = False |
| 73 | + |
| 74 | + try: |
| 75 | + GhRepo("lewagon-test/data-solutions", token=token).delete() |
| 76 | + except NameError: |
| 77 | + exception_catched = True |
| 78 | + |
| 79 | + assert not exception_catched |
| 80 | + |
| 81 | + # QA gh repo can be deleted |
| 82 | + exception_catched = False |
| 83 | + |
| 84 | + try: |
| 85 | + GhRepo("lewagon-qa/data-solutions", token=token).delete() |
| 86 | + except NameError: |
| 87 | + exception_catched = True |
| 88 | + |
| 89 | + assert not exception_catched |
| 90 | + |
| 91 | + def test_gh_repo_crud(self, token): |
| 92 | + """ |
| 93 | + verifies repo crud |
| 94 | + """ |
| 95 | + |
| 96 | + # Arrange |
| 97 | + |
| 98 | + # Act |
| 99 | + repo = GhRepo("lewagon-qa/automated-test", token=token) |
| 100 | + create_response = repo.create() |
| 101 | + get_response = repo.get() |
| 102 | + update_response = repo.update(dict(description="automated update test")) |
| 103 | + repo.delete(dry_run=False) |
| 104 | + |
| 105 | + # Assert |
| 106 | + assert "node_id" in create_response |
| 107 | + assert "node_id" in get_response |
| 108 | + assert "node_id" in update_response |
| 109 | + |
| 110 | + create_node_id = create_response["node_id"] |
| 111 | + get_node_id = get_response["node_id"] |
| 112 | + update_node_id = update_response["node_id"] |
| 113 | + |
| 114 | + assert create_node_id == get_node_id |
| 115 | + assert create_node_id == update_node_id |
| 116 | + |
| 117 | + # Cleanup |
0 commit comments