|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# sonar-tools tests |
| 4 | +# Copyright (C) 2024 Olivier Korach |
| 5 | +# mailto:olivier.korach AT gmail DOT com |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 3 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this program; if not, write to the Free Software Foundation, |
| 19 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | +# |
| 21 | +""" sonarcloud tests """ |
| 22 | + |
| 23 | +import os |
| 24 | +import sys |
| 25 | +from unittest.mock import patch |
| 26 | +import pytest |
| 27 | + |
| 28 | +import utilities as util |
| 29 | +from sonar import errcodes, exceptions |
| 30 | +from sonar import organizations |
| 31 | +import cli.options as opt |
| 32 | +from cli import config |
| 33 | + |
| 34 | +CMD = "config.py" |
| 35 | +SC_OPTS = [f"-{opt.URL_SHORT}", "https://sonarcloud.io", f"-{opt.TOKEN_SHORT}", os.getenv("SONAR_TOKEN_SONARCLOUD")] |
| 36 | + |
| 37 | +OPTS = [CMD] + SC_OPTS + [f"-{opt.EXPORT_SHORT}", f"-{opt.REPORT_FILE_SHORT}", util.JSON_FILE] |
| 38 | +MY_ORG_1 = "okorach" |
| 39 | +MY_ORG_2 = "okorach-github" |
| 40 | + |
| 41 | +def test_sc_config_export() -> None: |
| 42 | + """test_sc_config_export""" |
| 43 | + util.clean(util.JSON_FILE) |
| 44 | + with pytest.raises(SystemExit) as e: |
| 45 | + with patch.object(sys, "argv", OPTS + [f"-{opt.ORG_SHORT}", "okorach"]): |
| 46 | + config.main() |
| 47 | + assert int(str(e.value)) == errcodes.OK |
| 48 | + assert util.file_not_empty(util.JSON_FILE) |
| 49 | + util.clean(util.JSON_FILE) |
| 50 | + |
| 51 | + |
| 52 | +def test_sc_config_export_no_org() -> None: |
| 53 | + """test_sc_config_export""" |
| 54 | + util.clean(util.JSON_FILE) |
| 55 | + with pytest.raises(SystemExit) as e: |
| 56 | + with patch.object(sys, "argv", OPTS): |
| 57 | + config.main() |
| 58 | + assert int(str(e.value)) == errcodes.ARGS_ERROR |
| 59 | + assert not os.path.isfile(util.JSON_FILE) |
| 60 | + |
| 61 | + |
| 62 | +def test_org_search() -> None: |
| 63 | + """test_org_search""" |
| 64 | + org_list = organizations.search(endpoint=util.SC) |
| 65 | + assert MY_ORG_1 in org_list |
| 66 | + assert MY_ORG_2 in org_list |
| 67 | + |
| 68 | + |
| 69 | +def test_org_get_list() -> None: |
| 70 | + """test_org_search""" |
| 71 | + org_list = organizations.get_list(endpoint=util.SC) |
| 72 | + assert MY_ORG_1 in org_list |
| 73 | + assert MY_ORG_2 in org_list |
| 74 | + |
| 75 | + org_list = organizations.get_list(endpoint=util.SC, key_list=[MY_ORG_1]) |
| 76 | + assert MY_ORG_1 in org_list |
| 77 | + assert MY_ORG_2 not in org_list |
| 78 | + |
| 79 | + |
| 80 | +def test_org_get_non_existing() -> None: |
| 81 | + """test_org_search_sq""" |
| 82 | + with pytest.raises(exceptions.ObjectNotFound): |
| 83 | + _ = organizations.Organization.get_object(endpoint=util.SC, key="oko_foo_bar") |
| 84 | + |
| 85 | + with pytest.raises(exceptions.ObjectNotFound): |
| 86 | + _ = organizations.get_list(endpoint=util.SC, key_list=["oko_foo_bar"]) |
| 87 | + |
| 88 | + |
| 89 | +def test_org_str() -> None: |
| 90 | + """test_org_str""" |
| 91 | + org = organizations.Organization.get_object(endpoint=util.SC, key=MY_ORG_1) |
| 92 | + assert str(org) == f"organization key '{MY_ORG_1}'" |
| 93 | + |
| 94 | + |
| 95 | +def test_org_export() -> None: |
| 96 | + """test_org_export""" |
| 97 | + org = organizations.Organization.get_object(endpoint=util.SC, key=MY_ORG_1) |
| 98 | + exp = org.export() |
| 99 | + assert "newCodePeriod" in exp |
| 100 | + |
| 101 | + |
| 102 | +def test_org_attr() -> None: |
| 103 | + """test_org_attr""" |
| 104 | + org = organizations.Organization.get_object(endpoint=util.SC, key=MY_ORG_1) |
| 105 | + assert org.key == MY_ORG_1 |
| 106 | + assert org.name == "Olivier Korach" |
| 107 | + assert org._json["url"] == "https://github.com/okorach" |
| 108 | + (nc_type, nc_val) = org.new_code_period() |
| 109 | + assert nc_type == "PREVIOUS_VERSION" |
| 110 | + assert org.subscription() == "FREE" |
| 111 | + assert org.alm()["key"] == "github" |
| 112 | + |
| 113 | + |
| 114 | +def test_org_search_sq() -> None: |
| 115 | + """test_org_search_sq""" |
| 116 | + with pytest.raises(exceptions.UnsupportedOperation): |
| 117 | + _ = organizations.search(endpoint=util.SQ) |
| 118 | + |
| 119 | + with pytest.raises(exceptions.UnsupportedOperation): |
| 120 | + _ = organizations.get_list(endpoint=util.SQ) |
0 commit comments