Best Python code snippet using localstack_python
test_enrich.py
Source:test_enrich.py  
...77    return request.param78def test_enrich_call_without_jwt_with_unsupported_type_success(79        route, client, unsupported_type_json, get_public_key,80        expected_payload_unsupported_type):81    pd_api_request.side_effect = get_public_key()82    response = client.post(route, json=unsupported_type_json)83    assert response.get_json() == expected_payload_unsupported_type84@fixture(scope='module')85def unsupported_type_json():86    return [{'type': 'unknown', 'value': 'https://google.com'}]87def test_enrich_call_with_unsupported_type_success(88        route, client, valid_jwt, unsupported_type_json,89        expected_payload_unsupported_type):90    response = client.post(route,91                           headers=headers(valid_jwt),92                           json=unsupported_type_json)93    assert response.get_json() == expected_payload_unsupported_type94@mock.patch('api.enrich.get_related_entities')95def test_enrich_call_success(mock_related_entities,...test_authorization.py
Source:test_authorization.py  
1from http import HTTPStatus2from unittest.mock import patch3from pytest import fixture4from requests import Session5from requests.exceptions import InvalidURL, ConnectionError6from api.utils import (7    NO_AUTH_HEADER,8    WRONG_AUTH_TYPE,9    WRONG_JWKS_HOST,10    WRONG_PAYLOAD_STRUCTURE,11    JWKS_HOST_MISSING,12    WRONG_KEY,13    WRONG_JWT_STRUCTURE,14    WRONG_AUDIENCE,15    KID_NOT_FOUND16)17from api.errors import AUTH_ERROR, INVALID_ARGUMENT, CONNECTION_ERROR18from .utils import headers19def routes():20    yield '/health'21    yield '/respond/observables'22    yield '/respond/trigger'23@fixture(scope='module', params=routes(), ids=lambda route: f'POST{route}')24def route(request):25    return request.param26@fixture(scope='module')27def authorization_errors_expected_payload(route: str):28    def _make_payload_message(message):29        payload = {30            'errors': [31                {32                    'code': AUTH_ERROR,33                    'message': f'Authorization failed: {message}',34                    'type': 'fatal'}35            ],36        }37        if route.endswith('/trigger'):38            payload.update({'data': {'status': 'failure'}})39        return payload40    return _make_payload_message41@fixture(scope='module')42def unauthorized_creds_expected_payload(route):43    def _make_payload_message(code, message):44        payload = {45            'errors': [46                {47                    'code': code,48                    'message': f'{message}',49                    'type': 'fatal'50                }51            ],52        }53        if route.endswith('/trigger'):54            payload.update({'data': {'status': 'failure'}})55        return payload56    return _make_payload_message57def test_call_with_authorization_header_failure(58        route, client, valid_json,59        authorization_errors_expected_payload60):61    response = client.post(route, json=valid_json)62    assert response.status_code == HTTPStatus.OK63    assert response.json == authorization_errors_expected_payload(64        NO_AUTH_HEADER65    )66def test_call_with_wrong_authorization_type(67        route, client, valid_jwt, valid_json,68        authorization_errors_expected_payload69):70    response = client.post(71        route, json=valid_json,72        headers=headers(valid_jwt(), auth_type='wrong_type')73    )74    assert response.status_code == HTTPStatus.OK75    assert response.json == authorization_errors_expected_payload(76        WRONG_AUTH_TYPE77    )78def test_call_with_wrong_jwt_structure(79        route, client, valid_json,80        authorization_errors_expected_payload,81        get_public_key82):83    with patch.object(Session, 'request') as request_mock:84        request_mock.side_effect = get_public_key85        response = client.post(86            route, headers=headers('this_is_not_a_jwt'), json=valid_json87        )88    assert response.status_code == HTTPStatus.OK89    assert response.json == authorization_errors_expected_payload(90        WRONG_JWT_STRUCTURE91    )92def test_call_with_wrong_kid(93        route, client, valid_json,94        authorization_errors_expected_payload,95        get_public_key, valid_jwt96):97    with patch.object(Session, 'request') as request_mock:98        request_mock.side_effect = get_public_key99        response = client.post(100            route, headers=headers(valid_jwt(kid='wrong_kid')), json=valid_json101        )102    assert response.status_code == HTTPStatus.OK103    assert response.json == authorization_errors_expected_payload(104       KID_NOT_FOUND105    )106def test_call_with_wrong_jwks_host(107    route, client, valid_json, valid_jwt,108    authorization_errors_expected_payload109):110    for error in (ConnectionError, InvalidURL):111        with patch.object(Session, 'request') as request_mock:112            request_mock.side_effect = error()113            response = client.post(114                route, json=valid_json, headers=headers(valid_jwt())115            )116        assert response.status_code == HTTPStatus.OK117        assert response.json == authorization_errors_expected_payload(118            WRONG_JWKS_HOST119        )120def test_call_with_wrong_jwt_payload_structure(121        route, client, valid_json, valid_jwt,122        authorization_errors_expected_payload,123        get_public_key124):125    with patch.object(Session, 'request') as request_mock:126        request_mock.return_value = get_public_key127        response = client.post(128            route, json=valid_json,129            headers=headers(valid_jwt(wrong_structure=True))130        )131    assert response.status_code == HTTPStatus.OK132    assert response.json == authorization_errors_expected_payload(133        WRONG_PAYLOAD_STRUCTURE)134def test_call_without_jwks_host(135        route, client, valid_json, valid_jwt,136        authorization_errors_expected_payload,137        get_public_key138):139    with patch.object(Session, 'request') as request_mock:140        request_mock.side_effect = get_public_key141    response = client.post(142        route, json=valid_json,143        headers=headers(valid_jwt(missing_jwks_host=True))144    )145    assert response.status_code == HTTPStatus.OK146    assert response.json == authorization_errors_expected_payload(147        JWKS_HOST_MISSING)148def test_call_with_unauthorized_access_token(149        route, client, valid_jwt, valid_json,150        akamai_response_unauthorized_creds,151        unauthorized_creds_expected_payload,152        get_public_key153):154    with patch.object(Session, 'request') as request_mock:155        request_mock.side_effect = (156            get_public_key,157            akamai_response_unauthorized_creds(158                HTTPStatus.UNAUTHORIZED,159                'Invalid authorization access token'160            )161        )162        response = client.post(163            route, headers=headers(valid_jwt()), json=valid_json164        )165        assert response.status_code == HTTPStatus.OK166        assert response.json == unauthorized_creds_expected_payload(167            AUTH_ERROR,168            'Authorization failed: Invalid authorization access token'169        )170def test_call_with_unauthorized_client_token(171        route, client, valid_jwt, valid_json,172        akamai_response_unauthorized_creds,173        unauthorized_creds_expected_payload,174        get_public_key175):176    with patch.object(Session, 'request') as request_mock:177        request_mock.side_effect = (178            get_public_key,179            akamai_response_unauthorized_creds(180                HTTPStatus.BAD_REQUEST,181                'Invalid authorization client token')182        )183        response = client.post(184            route, headers=headers(valid_jwt()), json=valid_json185        )186        assert response.status_code == HTTPStatus.OK187        assert response.json == unauthorized_creds_expected_payload(188            INVALID_ARGUMENT,189            'Unexpected response from Akamai: '190            'Invalid authorization client token'191        )192def test_call_with_unauthorized_signature(193        route, client, valid_jwt, valid_json,194        akamai_response_unauthorized_creds,195        unauthorized_creds_expected_payload,196        get_public_key197):198    with patch.object(Session, 'request') as request_mock:199        request_mock.side_effect = (200            get_public_key,201            akamai_response_unauthorized_creds(202                HTTPStatus.UNAUTHORIZED, 'The signature does not match'203            )204        )205        response = client.post(206            route, headers=headers(valid_jwt()), json=valid_json,207        )208        assert response.status_code == HTTPStatus.OK209        assert response.json == unauthorized_creds_expected_payload(210            AUTH_ERROR,211            'Authorization failed: The signature does not match'212        )213def test_call_with_unauthorized_base_url(214        route, client, valid_jwt, valid_json,215        unauthorized_creds_expected_payload,216        get_public_key217):218    with patch.object(Session, 'request') as request_mock:219        request_mock.side_effect = (get_public_key, ConnectionError)220        response = client.post(221            route, headers=headers(valid_jwt()), json=valid_json222        )223        assert response.status_code == HTTPStatus.OK224        assert response.json == unauthorized_creds_expected_payload(225            CONNECTION_ERROR,226            'Unable to connect Akamai, validate the configured baseUrl: xxx'227        )228def test_call_with_wrong_audience(229        route, client, valid_json, valid_jwt, get_public_key,230        authorization_errors_expected_payload,231):232    with patch.object(Session, 'request') as request_mock:233        request_mock.return_value = get_public_key234        response = client.post(235            route, json=valid_json,236            headers=headers(valid_jwt(aud='wrong_aud'))237        )238    assert response.status_code == HTTPStatus.OK239    assert response.json == authorization_errors_expected_payload(240        WRONG_AUDIENCE241    )242def test_call_with_wrong_public_key(243        route, client, valid_json, valid_jwt, get_wrong_public_key,244        authorization_errors_expected_payload,245):246    with patch.object(Session, 'request') as request_mock:247        request_mock.return_value = get_wrong_public_key248        response = client.post(249            route, json=valid_json,250            headers=headers(valid_jwt())251        )252    assert response.status_code == HTTPStatus.OK253    assert response.json == authorization_errors_expected_payload(254        WRONG_KEY...test_health.py
Source:test_health.py  
...16    return request.param17def test_health_call_without_jwt_failure(18        route, client, pd_api_request, get_public_key19):20    pd_api_request.side_effect = get_public_key()21    response = client.post(route)22    assert response.status_code == HTTPStatus.OK23    assert response.get_json() == EXPECTED_PAYLOAD_WITHOUT_JWT24@fixture(scope="function")25def pd_api_request():26    with patch("requests.get") as mock_request:27        yield mock_request28def pd_api_response(ok):29    mock_response = MagicMock()30    mock_response.ok = ok31    if ok:32        mock_response.status_code = HTTPStatus.OK33        payload = {34            "iid": 19,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
