Compare commits
	
		
			24 Commits
		
	
	
		
			dev-fabian
			...
			dev-unitte
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 858e18362f | |||
| b226256187 | |||
| 32648d0f7a | |||
| 7a565e0511 | |||
| 2c5444a245 | |||
| 9fec4e81cc | |||
| 6ead97eee0 | |||
| 2227554977 | |||
| c58b6449aa | |||
| f643b91a8b | |||
| d6b8c528de | |||
| 2e8bc421c6 | |||
| 7fbd62659d | |||
| d34756d9fc | |||
| 67d71fa43e | |||
| eb03474392 | |||
| 58d7de6073 | |||
| f39fa6f936 | |||
| 2c6ab3d96d | |||
| 2376c1355d | |||
| 93beb10c49 | |||
| 269c08e5de | |||
| 19a74de675 | |||
| 5585574eac | 
							
								
								
									
										2
									
								
								.coveragerc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.coveragerc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
[run]
 | 
			
		||||
omit=venv/*
 | 
			
		||||
@@ -9,5 +9,5 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
 | 
			
		||||
 | 
			
		||||
RUN pip3 install -r requirements.txt
 | 
			
		||||
 | 
			
		||||
EXPOSE 8000
 | 
			
		||||
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
 | 
			
		||||
EXPOSE 80
 | 
			
		||||
CMD ["gunicorn", "-b", "0.0.0.0:80", "app:app"]
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
import sentry_sdk
 | 
			
		||||
from sentry_sdk.integrations.flask import FlaskIntegration
 | 
			
		||||
from flask import Flask
 | 
			
		||||
import os
 | 
			
		||||
from db import redis_client
 | 
			
		||||
@@ -14,16 +15,27 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team"
 | 
			
		||||
__module_name__ = "app"
 | 
			
		||||
__version__text__ = "1"
 | 
			
		||||
 | 
			
		||||
sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10")
 | 
			
		||||
# Setup sentry
 | 
			
		||||
SENTRY_DSN = os.environ.get("SENTRY_DSN")
 | 
			
		||||
if SENTRY_DSN:
 | 
			
		||||
    sentry_sdk.init(
 | 
			
		||||
        dsn = SENTRY_DSN,
 | 
			
		||||
        integrations = [FlaskIntegration()],
 | 
			
		||||
        send_default_pii = True,
 | 
			
		||||
        release = os.environ.get('RELEASE_ID', 'test'),
 | 
			
		||||
        environment = os.environ.get('RELEASEMODE', 'dev')
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
app.config['REDIS_URL'] = os.environ['REDIS_URL']
 | 
			
		||||
app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID']
 | 
			
		||||
app.config['CUSTOMER_TIMEOUT'] = int(os.environ.get('CUSTOMER_TIMEOUT', 30))
 | 
			
		||||
app.config['PRODUCER_TIMEOUT'] = int(os.environ.get('PRODUCER_TIMEOUT', 60))
 | 
			
		||||
 | 
			
		||||
redis_client.init_app(app)
 | 
			
		||||
 | 
			
		||||
for view in [ConsumersView, LogView, SyncView]:
 | 
			
		||||
    view.register(app, trailing_slash=False)
 | 
			
		||||
    view.register(app, trailing_slash = False)
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    app.run(debug=True)
 | 
			
		||||
    app.run(debug = True)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										5
									
								
								consumer_api/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								consumer_api/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
# Setup environment variables for testing
 | 
			
		||||
os.environ["LOCAL_UUID"] = "d8b2e5e2-f675-4194-9324-af58e4b70c54"
 | 
			
		||||
os.environ["REDIS_URL"] = "redis://192.168.111.121/0"
 | 
			
		||||
							
								
								
									
										22
									
								
								consumer_api/tests/consumers_view_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								consumer_api/tests/consumers_view_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
import db
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def client(mocker):
 | 
			
		||||
    mocker.patch("db.redis_client")
 | 
			
		||||
    db.redis_client.get.side_effect=lambda a: None
 | 
			
		||||
 | 
			
		||||
    from app import app
 | 
			
		||||
 | 
			
		||||
    app.config['TESTING'] = True
 | 
			
		||||
 | 
			
		||||
    with app.test_client() as client:
 | 
			
		||||
        yield client
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_response_length(client):
 | 
			
		||||
    r = client.get('/consumers')
 | 
			
		||||
 | 
			
		||||
    assert r.status_code == 200
 | 
			
		||||
							
								
								
									
										30
									
								
								consumer_api/tests/log_view_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								consumer_api/tests/log_view_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
import db
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def client(mocker):
 | 
			
		||||
    mocker.patch("db.redis_client")
 | 
			
		||||
    db.redis_client.get.side_effect=lambda a: None
 | 
			
		||||
 | 
			
		||||
    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('/log')
 | 
			
		||||
 | 
			
		||||
    assert r.status_code == 405
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_log_code_post(client):
 | 
			
		||||
    data = {
 | 
			
		||||
        "uuid": "asdasdasd",
 | 
			
		||||
        "message": "Hello There!"
 | 
			
		||||
    }
 | 
			
		||||
    r = client.post('/log', json=data)
 | 
			
		||||
 | 
			
		||||
    assert r.status_code == 204
 | 
			
		||||
							
								
								
									
										30
									
								
								consumer_api/tests/sync_view_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								consumer_api/tests/sync_view_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
import db
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def client(mocker):
 | 
			
		||||
    mocker.patch("db.redis_client")
 | 
			
		||||
    db.redis_client.get.side_effect=lambda a: None
 | 
			
		||||
 | 
			
		||||
    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')
 | 
			
		||||
 | 
			
		||||
    assert r.status_code == 405
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_log_code_post(client):
 | 
			
		||||
    data = {
 | 
			
		||||
        "uuid": "tesuuid"
 | 
			
		||||
    }
 | 
			
		||||
    r = client.post('/sync', json=data)
 | 
			
		||||
 | 
			
		||||
    assert r.status_code == 200
 | 
			
		||||
@@ -8,5 +8,12 @@ from flask_classful import FlaskView
 | 
			
		||||
class ConsumersView(FlaskView):
 | 
			
		||||
 | 
			
		||||
    def get(self):
 | 
			
		||||
        consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
 | 
			
		||||
        return jsonify([v['ip'] for k, v in consumer_list.items()])
 | 
			
		||||
        keys = redis_client.keys('consumer_*')
 | 
			
		||||
 | 
			
		||||
        list_of_customer_ips = []
 | 
			
		||||
 | 
			
		||||
        for key in keys:
 | 
			
		||||
            info = json.loads((redis_client.get(key) or b"{}").decode('utf-8'))
 | 
			
		||||
            list_of_customer_ips.append(info['ip'])
 | 
			
		||||
 | 
			
		||||
        return jsonify(list_of_customer_ips)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,32 @@
 | 
			
		||||
from flask import request, current_app, Response
 | 
			
		||||
from flask_classful import FlaskView
 | 
			
		||||
from db import redis_client
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LogView(FlaskView):
 | 
			
		||||
 | 
			
		||||
    def post(self):
 | 
			
		||||
        # Record the IP address of the producer
 | 
			
		||||
        remote_uuid = request.json['uuid']
 | 
			
		||||
        remote_ip = request.remote_addr
 | 
			
		||||
 | 
			
		||||
        prod_key = f"producer_{remote_uuid}"
 | 
			
		||||
 | 
			
		||||
        last_known_remote_ip = redis_client.get(prod_key)
 | 
			
		||||
        if last_known_remote_ip:
 | 
			
		||||
            last_known_remote_ip = last_known_remote_ip.decode('utf-8')
 | 
			
		||||
 | 
			
		||||
        if not last_known_remote_ip:
 | 
			
		||||
            current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}")
 | 
			
		||||
        elif last_known_remote_ip != remote_ip:
 | 
			
		||||
            current_app.logger.info(
 | 
			
		||||
                f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
 | 
			
		||||
 | 
			
		||||
        # update expirity
 | 
			
		||||
        redis_client.set(prod_key, remote_ip.encode('utf-8'))
 | 
			
		||||
        redis_client.expire(prod_key, current_app.config["PRODUCER_TIMEOUT"])
 | 
			
		||||
 | 
			
		||||
        # print out message
 | 
			
		||||
        current_app.logger.info(f"New message: {request.json['message']}")
 | 
			
		||||
 | 
			
		||||
        return Response(status = 204)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
import json
 | 
			
		||||
import time
 | 
			
		||||
from flask import request, current_app, jsonify
 | 
			
		||||
from flask_classful import FlaskView
 | 
			
		||||
from db import redis_client  # ez nagyon otvar
 | 
			
		||||
@@ -10,20 +11,25 @@ class SyncView(FlaskView):
 | 
			
		||||
        remote_uuid = request.json['uuid']
 | 
			
		||||
        remote_ip = request.remote_addr
 | 
			
		||||
 | 
			
		||||
        consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
 | 
			
		||||
        cust_key = f"consumer_{remote_uuid}"
 | 
			
		||||
 | 
			
		||||
        # Log something about it
 | 
			
		||||
        if remote_uuid not in consumer_list.keys():
 | 
			
		||||
        last_known_info = json.loads((redis_client.get(cust_key) or b"{}").decode('utf-8'))
 | 
			
		||||
 | 
			
		||||
        if not last_known_info:
 | 
			
		||||
            current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")
 | 
			
		||||
        else:  # known
 | 
			
		||||
            if consumer_list[remote_uuid]['ip'] != remote_ip:
 | 
			
		||||
 | 
			
		||||
        else:
 | 
			
		||||
            if last_known_info['ip'] != remote_ip:
 | 
			
		||||
                current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
 | 
			
		||||
 | 
			
		||||
        consumer_list.update(
 | 
			
		||||
            {remote_uuid: {"ip": remote_ip}}
 | 
			
		||||
        )
 | 
			
		||||
        info = {
 | 
			
		||||
            "uuid": remote_uuid,
 | 
			
		||||
            "ip": remote_ip,
 | 
			
		||||
            "last_seen": time.time()
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8'))
 | 
			
		||||
        redis_client.set(cust_key, json.dumps(info).encode('utf-8'))
 | 
			
		||||
        redis_client.expire(cust_key, current_app.config["CUSTOMER_TIMEOUT"])
 | 
			
		||||
 | 
			
		||||
        response = {
 | 
			
		||||
            "uuid": current_app.config['LOCAL_UUID']
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								requirements_dev.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								requirements_dev.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
pytest
 | 
			
		||||
pytest-mock
 | 
			
		||||
mock
 | 
			
		||||
coverage
 | 
			
		||||
		Reference in New Issue
	
	Block a user