def test_get_variables_happy_path(mock_request): request_constructor = mock.Mock() mock_request.Request = request_constructor response = mock.Mock() response.getcode.return_value = 200 response.read.return_value = api_response_to_vars_query mock_request.urlopen.return_value = response client = tfc.TerraformClient("my_token", "my_org", "my_workspace") variables = client.get_variables() request_constructor.assert_called_once_with( "https://app.terraform.io/api/v2/vars?filter%5Borganization%5D%5Bname%5D=my_org&filter%5Bworkspace%5D%5Bname%5D=my_workspace", headers={ "Authorization": "Bearer my_token", "Content-Type": "application/vnd.api+json" }, ) assert all( isinstance(v, tfc.TerraformVariable) for v in variables.values()) assert {k: v.__dict__ for (k, v) in variables.items()} == { "variable_1": { "id": "var-someid1", "name": "variable_1", "sensitive": False, "value": "value_1", }, "variable_2": { "id": "var-someid2", "name": "variable_2", "sensitive": True, "value": "value_2", }, }
def test_create_run_invalid_key_raise_terraform_error(mock_request): response = mock.Mock() response.getcode.return_value = 401 mock_request.urlopen.return_value = response client = tfc.TerraformClient("my_token", "my_org", "my_workspace") with pytest.raises(tfc.TerraformError, match="Received status code 401. Expected 200"): client.create_run("my_message")
def test_get_variables_invalid_org_or_workspace_raise_terraform_error( mock_request): response = mock.Mock() response.getcode.return_value = 404 mock_request.urlopen.return_value = response client = tfc.TerraformClient("my_token", "my_org", "my_workspace") with pytest.raises(tfc.TerraformError, match="Received status code 404. Expected 200"): client.get_variables()
def test_update_variable_happy_path(mock_request): request_constructor = mock.Mock() mock_request.Request = request_constructor response = mock.Mock() response.getcode.return_value = 200 mock_request.urlopen.return_value = response client = tfc.TerraformClient("my_token", "my_org", "my_workspace") client.update_variable("some_variable_id", "new_value") request_constructor.assert_called_once_with( "https://app.terraform.io/api/v2/vars/some_variable_id", data= b'{"data": {"type": "vars", "id": "some_variable_id", "attributes": {"value": "new_value"}}}', headers={ "Authorization": "Bearer my_token", "Content-Type": "application/vnd.api+json" }, method="PATCH", )
def test_create_run_happy_path(mock_request): request_constructor = mock.Mock() mock_request.Request = request_constructor response = mock.Mock() response.getcode.return_value = 201 response.read.return_value = api_response_to_create_run mock_request.urlopen.return_value = response client = tfc.TerraformClient("my_token", "my_org", "my_workspace") client._workspace_id = "workspaceid" run = client.create_run("my_message") request_constructor.assert_called_once_with( "https://app.terraform.io/api/v2/runs", data= b'{"data": {"attributes": {"is-destroy": false, "message": "my_message"}, "type": "runs", "relationships": {"workspace": {"data": {"type": "workspaces", "id": "workspaceid"}}}}}', headers={ "Authorization": "Bearer my_token", "Content-Type": "application/vnd.api+json" }, method="POST", ) assert run.id == "run-somerunid"