|
10 | 10 |
|
11 | 11 | import pytest |
12 | 12 | import requests |
| 13 | +from _pytest.capture import CaptureFixture |
13 | 14 |
|
14 | | -from linodecli import api_request |
| 15 | +from linodecli import ExitCodes, api_request |
15 | 16 | from linodecli.baked.operation import ( |
16 | 17 | ExplicitEmptyDictValue, |
17 | 18 | ExplicitEmptyListValue, |
@@ -163,6 +164,71 @@ def test_build_request_body_non_null_field( |
163 | 164 | == result |
164 | 165 | ) |
165 | 166 |
|
| 167 | + def test_build_request_body_raw(self, mock_cli, create_operation): |
| 168 | + body = {"foo": "bar"} |
| 169 | + |
| 170 | + mock_cli.raw_body = json.dumps(body) |
| 171 | + |
| 172 | + result = api_request._build_request_body( |
| 173 | + mock_cli, |
| 174 | + create_operation, |
| 175 | + SimpleNamespace(), |
| 176 | + ) |
| 177 | + assert json.loads(result) == body |
| 178 | + |
| 179 | + def test_build_request_body_raw_with_defaults( |
| 180 | + self, mock_cli, create_operation |
| 181 | + ): |
| 182 | + body = {"foo": "bar"} |
| 183 | + mock_cli.raw_body = json.dumps(body) |
| 184 | + |
| 185 | + mock_cli.defaults = True |
| 186 | + mock_cli.config.get = lambda user, key, **kwargs: {"foo": "baz"} |
| 187 | + create_operation.allowed_defaults = ["foo"] |
| 188 | + |
| 189 | + result = api_request._build_request_body( |
| 190 | + mock_cli, |
| 191 | + create_operation, |
| 192 | + SimpleNamespace(), |
| 193 | + ) |
| 194 | + assert json.loads(result) == body |
| 195 | + |
| 196 | + def test_build_request_body_raw_conflict( |
| 197 | + self, mock_cli, create_operation, capsys: CaptureFixture |
| 198 | + ): |
| 199 | + mock_cli.raw_body = json.dumps({"foo": "bar"}) |
| 200 | + |
| 201 | + with pytest.raises(SystemExit) as err: |
| 202 | + api_request._build_request_body( |
| 203 | + mock_cli, |
| 204 | + create_operation, |
| 205 | + SimpleNamespace(foo="bar", bar="foo"), |
| 206 | + ) |
| 207 | + |
| 208 | + assert err.value.code == ExitCodes.ARGUMENT_ERROR |
| 209 | + assert ( |
| 210 | + "--raw-body cannot be specified with action arguments: --bar, --foo" |
| 211 | + in capsys.readouterr().err |
| 212 | + ) |
| 213 | + |
| 214 | + def test_build_request_body_raw_get( |
| 215 | + self, mock_cli, list_operation, capsys: CaptureFixture |
| 216 | + ): |
| 217 | + mock_cli.raw_body = json.dumps({"foo": "bar"}) |
| 218 | + |
| 219 | + with pytest.raises(SystemExit) as err: |
| 220 | + api_request._build_request_body( |
| 221 | + mock_cli, |
| 222 | + list_operation, |
| 223 | + SimpleNamespace(), |
| 224 | + ) |
| 225 | + |
| 226 | + assert err.value.code == ExitCodes.ARGUMENT_ERROR |
| 227 | + assert ( |
| 228 | + "--raw-body cannot be specified for actions with method get" |
| 229 | + in capsys.readouterr().err |
| 230 | + ) |
| 231 | + |
166 | 232 | def test_build_request_url_get(self, mock_cli, list_operation): |
167 | 233 | result = api_request._build_request_url( |
168 | 234 | mock_cli, list_operation, SimpleNamespace() |
|
0 commit comments