|
| 1 | +import asyncio |
1 | 2 | import time |
| 3 | +from asyncio import StreamReader |
| 4 | +from unittest.mock import MagicMock, AsyncMock |
| 5 | + |
2 | 6 | import pytest |
| 7 | +from aiohttp import ClientResponse |
| 8 | +from multidict import CIMultiDictProxy, CIMultiDict |
| 9 | +from yarl import URL |
3 | 10 |
|
4 | 11 | import aws_lambda_mpic.mpic_dcv_checker_lambda.mpic_dcv_checker_lambda_function as mpic_dcv_checker_lambda_function |
5 | 12 | from open_mpic_core.common_domain.validation_error import MpicValidationError |
|
9 | 16 | from open_mpic_core_test.test_util.valid_check_creator import ValidCheckCreator |
10 | 17 |
|
11 | 18 |
|
| 19 | +# noinspection PyMethodMayBeStatic |
12 | 20 | class TestDcvCheckerLambda: |
13 | 21 | @staticmethod |
14 | 22 | @pytest.fixture(scope='class') |
@@ -53,6 +61,31 @@ def lambda_handler__should_return_appropriate_status_code_given_errors_in_respon |
53 | 61 | result = mpic_dcv_checker_lambda_function.lambda_handler(dcv_check_request, None) |
54 | 62 | assert result == mock_return_value |
55 | 63 |
|
| 64 | + def lambda_handler__should_ensure_dcv_checker_is_fully_initialized_to_perform_http_based_checks(self, set_env_variables, mocker): |
| 65 | + dcv_check_request = ValidCheckCreator.create_valid_http_check_request() |
| 66 | + expected_challenge_value = dcv_check_request.dcv_check_parameters.validation_details.challenge_value |
| 67 | + |
| 68 | + # this test requires getting pretty far into the Dcv Checker execution; need to mock an aiohttp.ClientResponse |
| 69 | + event_loop = asyncio.get_event_loop() |
| 70 | + response = ClientResponse( |
| 71 | + method='GET', url=URL('http://example.com'), writer=MagicMock(), continue100=None, |
| 72 | + timer=AsyncMock(), request_info=AsyncMock(), traces=[], loop=event_loop, session=AsyncMock() |
| 73 | + ) |
| 74 | + response.status = 200 |
| 75 | + response.content = StreamReader(loop=event_loop) |
| 76 | + response.content.feed_data(bytes(expected_challenge_value.encode('utf-8'))) |
| 77 | + response.content.feed_eof() |
| 78 | + response._headers = CIMultiDictProxy(CIMultiDict({ |
| 79 | + 'Content-Type': 'text/plain; charset=utf-8', 'Content-Length': str(len(expected_challenge_value)) |
| 80 | + })) |
| 81 | + |
| 82 | + mocker.patch( |
| 83 | + 'aiohttp.ClientSession.get', |
| 84 | + side_effect=lambda *args, **kwargs: AsyncMock(__aenter__=AsyncMock(return_value=response)) |
| 85 | + ) |
| 86 | + result = mpic_dcv_checker_lambda_function.lambda_handler(dcv_check_request, None) |
| 87 | + assert result['statusCode'] == 200 |
| 88 | + |
56 | 89 | @staticmethod |
57 | 90 | def create_dcv_check_response(): |
58 | 91 | return DcvCheckResponse(perspective_code='us-east-1', check_passed=True, |
|
0 commit comments