123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- # coding: utf-8
- import json
- from urllib.parse import urljoin
- import aiohttp
- import pytest
- from echod.utils import decode_json
- content_type = 'application/json; charset=utf-8'
- payload = {
- 'name': 'Joaozinho',
- 'age': 9,
- }
- str_payload = {
- 'name': 'Joaozinho',
- 'age': '9',
- }
- json_payload = json.dumps(payload)
- default_header = {
- 'Content-Length': '31',
- 'Content-Type': '',
- 'Host': 'localhost'
- }
- zero_length_header = {
- 'Content-Length': '0',
- 'Content-Type': '',
- 'Host': 'localhost'
- }
- request_headers = {
- 'content_type': 'application/json',
- 'accept': 'application/json',
- }
- # Mock tests
- @pytest.mark.asyncio
- def test_get_mock(api_server):
- url = urljoin(api_server, '/mocks/')
- response = yield from aiohttp.request('GET', url)
- assert response.status == 200
- assert response.reason == 'OK'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert response_json == {'mocks': {}}
- @pytest.mark.asyncio
- def test_put_mock(api_server):
- expectation = {
- 'method': 'POST',
- 'path': '/v1/users/',
- 'request': {'body': {'email': 'john@doe.com',
- 'name': 'John Doe',
- 'password': 'secret'},
- 'headers': {'accept': 'application/json',
- 'content_type': 'application/json'}},
- 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
- 'headers': {'content_type': 'application/json'},
- 'status_code': 201}
- }
- url = urljoin(api_server, '/mocks/')
- # Configure the mock
- response = yield from aiohttp.request('PUT', url,
- data=json.dumps(expectation),
- headers=request_headers)
- assert response.status == 201
- assert response.reason == 'Created'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert 'path' in response_json.keys()
- # Use the mock
- post_url = urljoin(api_server, response_json['path'])
- post_body = json.dumps(expectation['request']['body'])
- response = yield from aiohttp.request('POST', post_url,
- data=post_body,
- headers=request_headers)
- assert response.status == 201
- assert response.reason == 'Created'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert 'email' in response_json.keys()
- assert 'name' in response_json.keys()
- @pytest.mark.asyncio
- def test_put_mock_without_request_expectation(api_server):
- expectation = {
- 'method': 'PUT',
- 'path': '/v1/users/',
- 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
- 'headers': {'content_type': 'application/json'},
- 'status_code': 200}
- }
- url = urljoin(api_server, '/mocks/')
- # Configure the mock
- response = yield from aiohttp.request('PUT', url,
- data=json.dumps(expectation),
- headers=request_headers)
- assert response.status == 201
- assert response.reason == 'Created'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert 'path' in response_json.keys()
- # Use the mock
- put_url = urljoin(api_server, response_json['path'])
- put_body = json.dumps({})
- response = yield from aiohttp.request('PUT', put_url,
- data=put_body,
- headers=request_headers)
- assert response.status == 200
- assert response.reason == 'OK'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert 'email' in response_json.keys()
- assert 'name' in response_json.keys()
- @pytest.mark.asyncio
- def test_method_not_allowed(api_server):
- expectation = {
- 'method': 'GET',
- 'path': '/v1/users/',
- 'response': {'body': {'email': 'john@doe.com', 'name': 'John Doe'},
- 'headers': {'content_type': 'application/json'},
- 'status_code': 200}
- }
- url = urljoin(api_server, '/mocks/')
- # Configure the mock
- response = yield from aiohttp.request('PUT', url,
- data=json.dumps(expectation),
- headers=request_headers)
- assert response.status == 201
- assert response.reason == 'Created'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert 'path' in response_json.keys()
- # Use the mock
- put_url = urljoin(api_server, response_json['path'])
- response = yield from aiohttp.request('PUT', put_url)
- assert response.status == 405
- assert response.reason == 'Method Not Allowed'
- @pytest.mark.asyncio
- def test_not_found(api_server):
- post_url = urljoin(api_server, '/mock/noexist/url/')
- post_body = '{}'
- response = yield from aiohttp.request('POST', post_url,
- data=post_body,
- headers=request_headers)
- assert response.status == 404
- assert response.reason == 'Not Found'
- # Health
- @pytest.mark.asyncio
- def test_health(api_server):
- url = urljoin(api_server, '/health/')
- response = yield from aiohttp.request('GET', url)
- assert response.status == 200
- assert response.reason == 'OK'
- assert response.headers['content-type'] == content_type
- response_json = yield from response.json()
- assert response_json == {'status': 'ok'}
- # Callback tests
- @pytest.mark.asyncio
- def test_queue_request_return_status_200(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- response = yield from aiohttp.request('POST',
- url,
- data=json_payload)
- assert response.status == 200
- @pytest.mark.asyncio
- def test_queue_request_return_json(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- response = yield from aiohttp.request('POST', url,
- data=json_payload)
- response_json = yield from response.json()
- assert response_json['request']['method'] == 'POST'
- assert response_json['request']['data'] == payload
- @pytest.mark.asyncio
- def test_queue_request_store_request(api_server, redis):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- assert redis.llen('app-queue') == 1
- @pytest.mark.asyncio
- def test_queue_request_store_json(api_server, redis):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload,
- headers={'X-Region': 'Sao_Paulo'})
- db_json = decode_json(redis.rpop('app-queue').decode('utf-8'))
- assert db_json['header']['X-REGION'] == 'Sao_Paulo'
- assert db_json['method'] == 'POST'
- assert db_json['data'] == payload
- """
- @pytest.mark.asyncio
- def test_queue_request_with_additional_url_return_status_200(api_server):
- url = '/callbacks/app/queue/some/more/url/here'
- response = yield from aiohttp.request('POST', url,
- data=json_payload)
- assert response.status == 200
- @pytest.mark.asyncio
- def test_queue_request_with_additional_url_json(api_server, redis):
- url = '/callbacks/app/queue/some/more/url/here'
- yield from aiohttp.request('POST', url,
- data=json_payload)
- expected_data = {
- 'method': 'POST',
- 'data': payload,
- 'additional_url': 'some/more/url/here',
- 'header': default_header,
- }
- assert decode_json(redis.rpop('app-queue')) == expected_data
- """
- @pytest.mark.asyncio
- def test_queue_request_store_multiple_requests(api_server, redis):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- yield from aiohttp.request('POST', url, data=json_payload)
- yield from aiohttp.request('POST', url, data=json_payload)
- assert redis.llen('app-queue') == 3
- @pytest.mark.asyncio
- def test_queue_request_store_request_different_apps(api_server, redis):
- url1 = urljoin(api_server, '/callbacks/app1/queue/')
- url2 = urljoin(api_server, '/callbacks/app2/queue/')
- yield from aiohttp.request('POST', url1, data=json_payload)
- yield from aiohttp.request('POST', url2, data=json_payload)
- assert redis.llen('app1-queue') == 1
- assert redis.llen('app2-queue') == 1
- @pytest.mark.asyncio
- def test_queue_request_store_request_different_queues(api_server, redis):
- url1 = urljoin(api_server, '/callbacks/app/queue1/')
- url2 = urljoin(api_server, '/callbacks/app/queue2/')
- yield from aiohttp.request('POST', url1, data=json_payload)
- yield from aiohttp.request('POST', url2, data=json_payload)
- assert redis.llen('app-queue1') == 1
- assert redis.llen('app-queue2') == 1
- @pytest.mark.asyncio
- def test_get_requests_return_status_200(api_server):
- url = urljoin(api_server, '/callbacks/_all/app/queue/')
- response = yield from aiohttp.request('GET', url)
- assert response.status == 200
- @pytest.mark.asyncio
- def test_get_requests_empty(api_server):
- url = urljoin(api_server, '/callbacks/_all/app/queue/')
- response = yield from aiohttp.request('GET', url)
- response_json = yield from response.json()
- assert response_json == {'requests': []}
- @pytest.mark.asyncio
- def test_get_requests_one_request(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- url_all = urljoin(api_server, '/callbacks/_all/app/queue/')
- response = yield from aiohttp.request('GET', url_all)
- response_json = yield from response.json()
- assert response_json['requests'][0]['method'] == 'POST'
- assert response_json['requests'][0]['data'] == payload
- # TODO: Make it work
- """
- @pytest.mark.asyncio
- def test_get_requests_multiple_request(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- yield from aiohttp.request('PUT', url, data=json_payload)
- yield from aiohttp.request('PATCH', url, data=json_payload)
- # TODO: Make it work
- # yield from aiohttp.request('GET', url, query_string=payload)
- yield from aiohttp.request('DELETE', url)
- url_all = urljoin(api_server, '/callbacks/_all/app/queue/')
- response = yield from aiohttp.request('GET', )
- expected_data = [
- {'method': 'POST', 'data': payload, 'header': default_header,
- 'additional_url': ''},
- {'method': 'PUT', 'data': payload, 'header': default_header,
- 'additional_url': ''},
- {'method': 'PATCH', 'data': payload, 'header': default_header,
- 'additional_url': ''},
- {'method': 'GET', 'data': str_payload, 'header': zero_length_header,
- 'additional_url': ''},
- {'method': 'DELETE', 'data': {}, 'header': zero_length_header,
- 'additional_url': ''},
- ]
- response_json = yield from response.json()
- assert response_json == {'requests': expected_data}
- """
- @pytest.mark.asyncio
- def test_get_requests_remove_key(api_server, redis):
- url = urljoin(api_server, '/callbacks/app/queue/')
- url_all = urljoin(api_server, '/callbacks/_all/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- yield from aiohttp.request('GET', url_all)
- assert not redis.exists('app-queue')
- @pytest.mark.asyncio
- def test_first_request_return_status_200(api_server):
- url = urljoin(api_server, '/callbacks/_first/app/queue/')
- response = yield from aiohttp.request('GET', url)
- assert response.status == 200
- @pytest.mark.asyncio
- def test_first_request_empty(api_server):
- url = urljoin(api_server, '/callbacks/_first/app/queue/')
- response = yield from aiohttp.request('GET', url)
- response_json = yield from response.json()
- assert response_json == {'request': {}}
- @pytest.mark.asyncio
- def test_first_request_one_request(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- url_first = urljoin(api_server, '/callbacks/_first/app/queue/')
- response = yield from aiohttp.request('GET', url_first)
- response_json = yield from response.json()
- assert response_json['request']['method'] == 'POST'
- assert response_json['request']['data'] == payload
- @pytest.mark.asyncio
- def test_first_request_multiple_request(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- yield from aiohttp.request('PUT', url, data=json_payload)
- yield from aiohttp.request('PATCH', url, data=json_payload)
- # yield from aiohttp.request('GET', url, query_string=payload)
- yield from aiohttp.request('DELETE', url)
- url_first = urljoin(api_server, '/callbacks/_first/app/queue/')
- response = yield from aiohttp.request('GET', url_first)
- response_json = yield from response.json()
- assert response_json['request']['method'] == 'POST'
- assert response_json['request']['data'] == payload
- @pytest.mark.asyncio
- def test_last_request_return_status_200(api_server):
- url = urljoin(api_server, '/callbacks/_last/app/queue/')
- response = yield from aiohttp.request('GET', url)
- assert response.status == 200
- @pytest.mark.asyncio
- def test_last_request_empty(api_server):
- url = urljoin(api_server, '/callbacks/_last/app/queue/')
- response = yield from aiohttp.request('GET', url)
- response_json = yield from response.json()
- assert response_json == {'request': {}}
- @pytest.mark.asyncio
- def test_last_request_one_request(api_server):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- url_last = urljoin(api_server, '/callbacks/_last/app/queue/')
- response = yield from aiohttp.request('GET', url_last)
- response_json = yield from response.json()
- assert response_json['request']['method'] == 'POST'
- assert response_json['request']['data'] == payload
- """
- @pytest.mark.asyncio
- def test_last_request_multiple_request(api_server):
- yield from aiohttp.request('POST', urljoin(api_server, '/callbacks/app/que
- yield from aiohttp.request('PUT', urljoin(api_server, '/callbacks/app/queu
- yield from aiohttp.request('PATCH', urljoin(api_server, '/callbacks/app/qu
- yield from aiohttp.request('GET', urljoin(api_server, '/callbacks/app/queu
- yield from aiohttp.request('DELETE', urljoin(api_server, '/callbacks/app/q
- response = yield from aiohttp.request('GET', urljoin(api_server, '/callbac
- expected_data = {'method': 'DELETE', 'data': {}, 'additional_url': '', 'he
- assert decode_json(response.data) == {'request': expected_data}
- """
- @pytest.mark.asyncio
- def test_clean_requests_none_request(api_server, redis):
- url = urljoin(api_server, '/callbacks/_clean/app/queue/')
- yield from aiohttp.request('GET', url)
- assert not redis.exists('app-queue')
- @pytest.mark.asyncio
- def test_clean_requests_none_request_return_status_200(api_server):
- url = urljoin(api_server, '/callbacks/_clean/app/queue/')
- response = yield from aiohttp.request('GET', url)
- assert response.status == 200
- @pytest.mark.asyncio
- def test_clean_requests_none_request_return_empty_json(api_server):
- url = urljoin(api_server, '/callbacks/_clean/app/queue/')
- response = yield from aiohttp.request('GET', url)
- response_json = yield from response.json()
- assert response_json == {}
- @pytest.mark.asyncio
- def test_clean_requests_multiple_requests(api_server, redis):
- url = urljoin(api_server, '/callbacks/app/queue/')
- yield from aiohttp.request('POST', url, data=json_payload)
- yield from aiohttp.request('PUT', url, data=json_payload)
- yield from aiohttp.request('PATCH', url, data=json_payload)
- yield from aiohttp.request('DELETE', url)
- url_clean = urljoin(api_server, '/callbacks/_clean/app/queue/')
- yield from aiohttp.request('GET', url_clean)
- assert not redis.exists('app-queue')
|