|
| 1 | +""" |
| 2 | +GitLab API: |
| 3 | +https://docs.gitlab.com/ee/api/instance_level_ci_variables.html |
| 4 | +https://docs.gitlab.com/ee/api/project_level_variables.html |
| 5 | +https://docs.gitlab.com/ee/api/group_level_variables.html |
| 6 | +""" |
| 7 | + |
| 8 | +import re |
| 9 | + |
| 10 | +import pytest |
| 11 | +import responses |
| 12 | + |
| 13 | +from gitlab.v4.objects import GroupVariable, ProjectVariable, Variable |
| 14 | + |
| 15 | + |
| 16 | +key = "TEST_VARIABLE_1" |
| 17 | +value = "TEST_1" |
| 18 | +new_value = "TEST_2" |
| 19 | + |
| 20 | +variable_content = { |
| 21 | + "key": key, |
| 22 | + "variable_type": "env_var", |
| 23 | + "value": value, |
| 24 | + "protected": False, |
| 25 | + "masked": True, |
| 26 | +} |
| 27 | +variables_url = re.compile( |
| 28 | + r"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables" |
| 29 | +) |
| 30 | +variables_key_url = re.compile( |
| 31 | + rf"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables/{key}" |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def resp_list_variables(): |
| 37 | + with responses.RequestsMock() as rsps: |
| 38 | + rsps.add( |
| 39 | + method=responses.GET, |
| 40 | + url=variables_url, |
| 41 | + json=[variable_content], |
| 42 | + content_type="application/json", |
| 43 | + status=200, |
| 44 | + ) |
| 45 | + yield rsps |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def resp_get_variable(): |
| 50 | + with responses.RequestsMock() as rsps: |
| 51 | + rsps.add( |
| 52 | + method=responses.GET, |
| 53 | + url=variables_key_url, |
| 54 | + json=variable_content, |
| 55 | + content_type="application/json", |
| 56 | + status=200, |
| 57 | + ) |
| 58 | + yield rsps |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def resp_create_variable(): |
| 63 | + with responses.RequestsMock() as rsps: |
| 64 | + rsps.add( |
| 65 | + method=responses.POST, |
| 66 | + url=variables_url, |
| 67 | + json=variable_content, |
| 68 | + content_type="application/json", |
| 69 | + status=200, |
| 70 | + ) |
| 71 | + yield rsps |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def resp_update_variable(): |
| 76 | + updated_content = dict(variable_content) |
| 77 | + updated_content["value"] = new_value |
| 78 | + |
| 79 | + with responses.RequestsMock() as rsps: |
| 80 | + rsps.add( |
| 81 | + method=responses.PUT, |
| 82 | + url=variables_key_url, |
| 83 | + json=updated_content, |
| 84 | + content_type="application/json", |
| 85 | + status=200, |
| 86 | + ) |
| 87 | + yield rsps |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +def resp_delete_variable(no_content): |
| 92 | + with responses.RequestsMock() as rsps: |
| 93 | + rsps.add( |
| 94 | + method=responses.DELETE, |
| 95 | + url=variables_key_url, |
| 96 | + json=no_content, |
| 97 | + content_type="application/json", |
| 98 | + status=204, |
| 99 | + ) |
| 100 | + yield rsps |
| 101 | + |
| 102 | + |
| 103 | +def test_list_instance_variables(gl, resp_list_variables): |
| 104 | + variables = gl.variables.list() |
| 105 | + assert isinstance(variables, list) |
| 106 | + assert isinstance(variables[0], Variable) |
| 107 | + assert variables[0].value == value |
| 108 | + |
| 109 | + |
| 110 | +def test_get_instance_variable(gl, resp_get_variable): |
| 111 | + variable = gl.variables.get(key) |
| 112 | + assert isinstance(variable, Variable) |
| 113 | + assert variable.value == value |
| 114 | + |
| 115 | + |
| 116 | +def test_create_instance_variable(gl, resp_create_variable): |
| 117 | + variable = gl.variables.create({"key": key, "value": value}) |
| 118 | + assert isinstance(variable, Variable) |
| 119 | + assert variable.value == value |
| 120 | + |
| 121 | + |
| 122 | +def test_update_instance_variable(gl, resp_update_variable): |
| 123 | + variable = gl.variables.get(key, lazy=True) |
| 124 | + variable.value = new_value |
| 125 | + variable.save() |
| 126 | + assert variable.value == new_value |
| 127 | + |
| 128 | + |
| 129 | +def test_delete_instance_variable(gl, resp_delete_variable): |
| 130 | + variable = gl.variables.get(key, lazy=True) |
| 131 | + variable.delete() |
| 132 | + |
| 133 | + |
| 134 | +def test_list_project_variables(project, resp_list_variables): |
| 135 | + variables = project.variables.list() |
| 136 | + assert isinstance(variables, list) |
| 137 | + assert isinstance(variables[0], ProjectVariable) |
| 138 | + assert variables[0].value == value |
| 139 | + |
| 140 | + |
| 141 | +def test_get_project_variable(project, resp_get_variable): |
| 142 | + variable = project.variables.get(key) |
| 143 | + assert isinstance(variable, ProjectVariable) |
| 144 | + assert variable.value == value |
| 145 | + |
| 146 | + |
| 147 | +def test_create_project_variable(project, resp_create_variable): |
| 148 | + variable = project.variables.create({"key": key, "value": value}) |
| 149 | + assert isinstance(variable, ProjectVariable) |
| 150 | + assert variable.value == value |
| 151 | + |
| 152 | + |
| 153 | +def test_update_project_variable(project, resp_update_variable): |
| 154 | + variable = project.variables.get(key, lazy=True) |
| 155 | + variable.value = new_value |
| 156 | + variable.save() |
| 157 | + assert variable.value == new_value |
| 158 | + |
| 159 | + |
| 160 | +def test_delete_project_variable(project, resp_delete_variable): |
| 161 | + variable = project.variables.get(key, lazy=True) |
| 162 | + variable.delete() |
| 163 | + |
| 164 | + |
| 165 | +def test_list_group_variables(group, resp_list_variables): |
| 166 | + variables = group.variables.list() |
| 167 | + assert isinstance(variables, list) |
| 168 | + assert isinstance(variables[0], GroupVariable) |
| 169 | + assert variables[0].value == value |
| 170 | + |
| 171 | + |
| 172 | +def test_get_group_variable(group, resp_get_variable): |
| 173 | + variable = group.variables.get(key) |
| 174 | + assert isinstance(variable, GroupVariable) |
| 175 | + assert variable.value == value |
| 176 | + |
| 177 | + |
| 178 | +def test_create_group_variable(group, resp_create_variable): |
| 179 | + variable = group.variables.create({"key": key, "value": value}) |
| 180 | + assert isinstance(variable, GroupVariable) |
| 181 | + assert variable.value == value |
| 182 | + |
| 183 | + |
| 184 | +def test_update_group_variable(group, resp_update_variable): |
| 185 | + variable = group.variables.get(key, lazy=True) |
| 186 | + variable.value = new_value |
| 187 | + variable.save() |
| 188 | + assert variable.value == new_value |
| 189 | + |
| 190 | + |
| 191 | +def test_delete_group_variable(group, resp_delete_variable): |
| 192 | + variable = group.variables.get(key, lazy=True) |
| 193 | + variable.delete() |
0 commit comments