|
| 1 | +"""Tests for exhausted Model Target training allowance responses.""" |
| 2 | + |
| 3 | +from http import HTTPStatus |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import pytest |
| 7 | +import requests |
| 8 | + |
| 9 | +from mock_vws import MockVWS |
| 10 | +from mock_vws._flask_server.vws import VWS_FLASK_APP |
| 11 | + |
| 12 | +_AUTHORIZATION = ( |
| 13 | + "Bearer eyJhbGciOiJtb2NrIn0." |
| 14 | + "eyJzY29wZSI6Im1vZGVsdGFyZ2V0cy5hbGwifQ." |
| 15 | + "c2lnbmF0dXJl" |
| 16 | +) |
| 17 | +_REQUEST_BODY: dict[str, Any] = { |
| 18 | + "name": "dataset-name", |
| 19 | + "targetSdk": "10.18", |
| 20 | + "models": [ |
| 21 | + { |
| 22 | + "name": "model-name", |
| 23 | + "cadDataUrl": "https://example.com/model.glb", |
| 24 | + "views": [ |
| 25 | + { |
| 26 | + "name": "view-name", |
| 27 | + "guideViewPosition": { |
| 28 | + "translation": [0, 0, 5], |
| 29 | + "rotation": [0, 0, 0, 1], |
| 30 | + }, |
| 31 | + }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + ], |
| 35 | +} |
| 36 | +_EXPECTED_BODY = { |
| 37 | + "error": { |
| 38 | + "code": "TRAINING_ALLOWANCE_EXCEEDED", |
| 39 | + "message": "User has reached total number of allowed trainings", |
| 40 | + "target": "userId:mock", |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + argnames="dataset_path", |
| 47 | + argvalues=[ |
| 48 | + pytest.param("modeltargets/datasets", id="standard"), |
| 49 | + pytest.param("modeltargets/advancedDatasets", id="advanced"), |
| 50 | + ], |
| 51 | +) |
| 52 | +def test_requests_mock_training_allowance_exceeded(dataset_path: str) -> None: |
| 53 | + """The in-process mock can reject creation when allowance is spent.""" |
| 54 | + with MockVWS(model_target_training_allowance_exceeded=True): |
| 55 | + response = requests.post( |
| 56 | + url=f"https://vws.vuforia.com/{dataset_path}", |
| 57 | + headers={"Authorization": _AUTHORIZATION}, |
| 58 | + json=_REQUEST_BODY, |
| 59 | + timeout=30, |
| 60 | + ) |
| 61 | + |
| 62 | + assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY |
| 63 | + assert response.json() == _EXPECTED_BODY |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + argnames="dataset_path", |
| 68 | + argvalues=[ |
| 69 | + pytest.param("/modeltargets/datasets", id="standard"), |
| 70 | + pytest.param("/modeltargets/advancedDatasets", id="advanced"), |
| 71 | + ], |
| 72 | +) |
| 73 | +def test_flask_training_allowance_exceeded( |
| 74 | + *, |
| 75 | + dataset_path: str, |
| 76 | + monkeypatch: pytest.MonkeyPatch, |
| 77 | +) -> None: |
| 78 | + """The Flask mock supports the exhausted allowance configuration.""" |
| 79 | + monkeypatch.setenv( |
| 80 | + name="MODEL_TARGET_TRAINING_ALLOWANCE_EXCEEDED", |
| 81 | + value="true", |
| 82 | + ) |
| 83 | + monkeypatch.setenv( |
| 84 | + name="TARGET_MANAGER_BASE_URL", |
| 85 | + value="http://target-manager.example.com", |
| 86 | + ) |
| 87 | + |
| 88 | + with VWS_FLASK_APP.test_client() as client: |
| 89 | + response = client.post( |
| 90 | + path=dataset_path, |
| 91 | + headers={"Authorization": _AUTHORIZATION}, |
| 92 | + json=_REQUEST_BODY, |
| 93 | + ) |
| 94 | + |
| 95 | + assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY |
| 96 | + assert response.json == _EXPECTED_BODY |
0 commit comments