diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..cd03256 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit=venv/* \ No newline at end of file diff --git a/consumer_api/tests/__init__.py b/consumer_api/tests/__init__.py index 883d9c7..95f505d 100644 --- a/consumer_api/tests/__init__.py +++ b/consumer_api/tests/__init__.py @@ -1,6 +1,5 @@ import os # Setup environment variables for testing -os.environ["INITIAL_SERVERS"] = "192.168.111.22" os.environ["LOCAL_UUID"] = "d8b2e5e2-f675-4194-9324-af58e4b70c54" os.environ["REDIS_URL"] = "redis://192.168.111.121/0" diff --git a/consumer_api/tests/consumers_view_test.py b/consumer_api/tests/consumers_view_test.py index 82087e9..03f3019 100644 --- a/consumer_api/tests/consumers_view_test.py +++ b/consumer_api/tests/consumers_view_test.py @@ -1,21 +1,22 @@ -import json -import os +import db import pytest -from flask import current_app + @pytest.fixture -def client(): - current_app.config['TESTING'] = True +def client(mocker): + mocker.patch("db.redis_client") + db.redis_client.get.side_effect=lambda a: None - with current_app.test_client() as client: + from app import app + + app.config['TESTING'] = True + + with app.test_client() as client: yield client def test_response_length(client): - data = { - "uuid": os.environ["LOCAL_UUID"], - "message": "Hello There!" - } - r = client.get('/consumers', data = json.dumps(data)) - assert len(r) == 0 + r = client.get('/consumers') + + assert r.status_code == 200 diff --git a/consumer_api/tests/log_view_test.py b/consumer_api/tests/log_view_test.py index 195be38..70bb3fa 100644 --- a/consumer_api/tests/log_view_test.py +++ b/consumer_api/tests/log_view_test.py @@ -1,16 +1,16 @@ -import json -import os - +import db import pytest -from flask import current_app - - @pytest.fixture -def client(): - current_app.config['TESTING'] = True +def client(mocker): + mocker.patch("db.redis_client") + db.redis_client.get.side_effect=lambda a: None - with current_app.test_client() as client: + from app import app + + app.config['TESTING'] = True + + with app.test_client() as client: yield client @@ -22,9 +22,9 @@ def test_log_code_get(client): def test_log_code_post(client): data = { - "uuid": os.environ["LOCAL_UUID"], + "uuid": "asdasdasd", "message": "Hello There!" } - r = client.post('/log', data = json.dumps(data)) + r = client.post('/log', json=data) assert r.status_code == 204 diff --git a/consumer_api/tests/sync_view_test.py b/consumer_api/tests/sync_view_test.py index 7f9552c..7e940b3 100644 --- a/consumer_api/tests/sync_view_test.py +++ b/consumer_api/tests/sync_view_test.py @@ -1,19 +1,20 @@ -import json -import os - +import db import pytest -from flask import current_app - - @pytest.fixture -def client(): - current_app.config['TESTING'] = True +def client(mocker): + mocker.patch("db.redis_client") + db.redis_client.get.side_effect=lambda a: None - with current_app.test_client() as client: + from app import app + + app.config['TESTING'] = True + + with app.test_client() as client: yield client + def test_log_code_get(client): r = client.get('/sync') @@ -22,8 +23,8 @@ def test_log_code_get(client): def test_log_code_post(client): data = { - "uuid": os.environ["LOCAL_UUID"] + "uuid": "tesuuid" } - r = client.post('/sync', data = json.dumps(data)) + r = client.post('/sync', json=data) - assert r.status_code == 204 + assert r.status_code == 200 diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index 9888702..1876507 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -1,7 +1,7 @@ import json from db import redis_client -from flask import jsonify, request +from flask import jsonify from flask_classful import FlaskView diff --git a/consumer_api/views/sync_view.py b/consumer_api/views/sync_view.py index bfd860c..5a761a8 100644 --- a/consumer_api/views/sync_view.py +++ b/consumer_api/views/sync_view.py @@ -9,7 +9,15 @@ class SyncView(FlaskView): def post(self): remote_uuid = request.json['uuid'] - remote_ip = request.remote_addr + + if 'ip' in request.json: + remote_ip = request.json['ip'] + + if request.remote_addr != remote_ip: + current_app.logger.debug(f"IP was overriden by the remote consumer {remote_ip} instead of {request.remote_addr}") + + else: + remote_ip = request.remote_addr cust_key = f"consumer_{remote_uuid}" diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..cd4f820 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,4 @@ +pytest +pytest-mock +mock +coverage \ No newline at end of file