2626import pytest
2727
2828import utilities as util
29- from sonar import errcodes
29+ from sonar import errcodes , exceptions
30+ from sonar import organizations
3031import cli .options as opt
3132from cli import config
3233
3334CMD = "config.py"
3435SC_OPTS = [f"-{ opt .URL_SHORT } " , "https://sonarcloud.io" , f"-{ opt .TOKEN_SHORT } " , os .getenv ("SONAR_TOKEN_SONARCLOUD" )]
3536
3637OPTS = [CMD ] + SC_OPTS + [f"-{ opt .EXPORT_SHORT } " , f"-{ opt .REPORT_FILE_SHORT } " , util .JSON_FILE ]
37-
38+ MY_ORG_1 = "okorach"
39+ MY_ORG_2 = "okorach-github"
3840
3941def test_sc_config_export () -> None :
4042 """test_sc_config_export"""
@@ -55,3 +57,64 @@ def test_sc_config_export_no_org() -> None:
5557 config .main ()
5658 assert int (str (e .value )) == errcodes .ARGS_ERROR
5759 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