Unverified Commit 010ce68e authored by Dan Jones's avatar Dan Jones
Browse files

refactor: rebase from move to requirements.txt

parent 113597e5
...@@ -2,7 +2,7 @@ from flask import Flask ...@@ -2,7 +2,7 @@ from flask import Flask
from flask_cors import CORS from flask_cors import CORS
from flask_restful import Api from flask_restful import Api
from endpoints.clients import Client, ClientList from endpoints.client import Client, ClientList
from endpoints.notify import Notify from endpoints.notify import Notify
from endpoints.receive import Receive from endpoints.receive import Receive
from endpoints.send import Send from endpoints.send import Send
...@@ -30,7 +30,9 @@ def create_app(): ...@@ -30,7 +30,9 @@ def create_app():
return app return app
flask_host = os.getenv("FLASK_HOST", "localhost") # Sets to whatever MQ_HOST is, or defaults to localhost flask_host = os.getenv(
"FLASK_HOST", "localhost"
) # Sets to whatever MQ_HOST is, or defaults to localhost
if __name__ == "__main__": if __name__ == "__main__":
app = create_app() app = create_app()
......
...@@ -22,9 +22,7 @@ def test_get_receive( ...@@ -22,9 +22,7 @@ def test_get_receive(
) as mock_read_from_queue, app.test_client() as app_test_client: ) as mock_read_from_queue, app.test_client() as app_test_client:
mock_read_from_queue.return_value = mock_read_from_queue_return mock_read_from_queue.return_value = mock_read_from_queue_return
auth_header = get_auth_header(app_test_client, mock_client_credentials) auth_header = get_auth_header(app_test_client, mock_client_credentials)
response = app_test_client.get( response = app_test_client.get("/receive", headers=auth_header)
"/receive", headers=auth_header
)
assert response.status_code == 200 assert response.status_code == 200
mock_read_from_queue.assert_called_once_with( mock_read_from_queue.assert_called_once_with(
queue_name="client-1-inbox", max_msgs=10 queue_name="client-1-inbox", max_msgs=10
...@@ -48,7 +46,7 @@ def test_get_receive_max_messages( ...@@ -48,7 +46,7 @@ def test_get_receive_max_messages(
auth_header = get_auth_header(app_test_client, mock_client_credentials) auth_header = get_auth_header(app_test_client, mock_client_credentials)
max_messages = 2 max_messages = 2
response = app_test_client.get( response = app_test_client.get(
"/receive", query_string={ "max_messages": max_messages }, headers=auth_header "/receive", query_string={"max_messages": max_messages}, headers=auth_header
) )
assert response.status_code == 200 assert response.status_code == 200
mock_read_from_queue.assert_called_once_with( mock_read_from_queue.assert_called_once_with(
...@@ -57,12 +55,8 @@ def test_get_receive_max_messages( ...@@ -57,12 +55,8 @@ def test_get_receive_max_messages(
assert response.json == mock_read_from_queue_return assert response.json == mock_read_from_queue_return
@pytest.mark.usefixtures( @pytest.mark.usefixtures("mock_clients", "mock_read_from_queue_return")
"mock_clients", "mock_read_from_queue_return" def test_get_receive_no_token(mock_clients, mock_read_from_queue_return):
)
def test_get_receive_no_token(
mock_clients, mock_read_from_queue_return
):
app = create_app() app = create_app()
with patch( with patch(
"builtins.open", mock_open(read_data=json.dumps(mock_clients)) "builtins.open", mock_open(read_data=json.dumps(mock_clients))
...@@ -70,19 +64,13 @@ def test_get_receive_no_token( ...@@ -70,19 +64,13 @@ def test_get_receive_no_token(
receive, "read_from_queue" receive, "read_from_queue"
) as mock_read_from_queue, app.test_client() as app_test_client: ) as mock_read_from_queue, app.test_client() as app_test_client:
mock_read_from_queue.return_value = mock_read_from_queue_return mock_read_from_queue.return_value = mock_read_from_queue_return
response = app_test_client.get( response = app_test_client.get("/receive")
"/receive"
)
assert response.status_code == 403 assert response.status_code == 403
mock_read_from_queue.assert_not_called() mock_read_from_queue.assert_not_called()
@pytest.mark.usefixtures( @pytest.mark.usefixtures("mock_clients", "mock_read_from_queue_return")
"mock_clients", "mock_read_from_queue_return" def test_get_receive_invalid_token(mock_clients, mock_read_from_queue_return):
)
def test_get_receive_invalid_token(
mock_clients, mock_read_from_queue_return
):
app = create_app() app = create_app()
with patch( with patch(
"builtins.open", mock_open(read_data=json.dumps(mock_clients)) "builtins.open", mock_open(read_data=json.dumps(mock_clients))
...@@ -91,8 +79,6 @@ def test_get_receive_invalid_token( ...@@ -91,8 +79,6 @@ def test_get_receive_invalid_token(
) as mock_read_from_queue, app.test_client() as app_test_client: ) as mock_read_from_queue, app.test_client() as app_test_client:
mock_read_from_queue.return_value = mock_read_from_queue_return mock_read_from_queue.return_value = mock_read_from_queue_return
auth_header = {"Authorization": "made-up-token"} auth_header = {"Authorization": "made-up-token"}
response = app_test_client.get( response = app_test_client.get("/receive", headers=auth_header)
"/receive", headers=auth_header
)
assert response.status_code == 403 assert response.status_code == 403
mock_read_from_queue.assert_not_called() mock_read_from_queue.assert_not_called()
...@@ -97,6 +97,6 @@ def mock_read_from_queue_return(): ...@@ -97,6 +97,6 @@ def mock_read_from_queue_return():
return [ return [
{ {
"topic": "soar.client-1.something", "topic": "soar.client-1.something",
"message": "this is a pub/sub message from client-1" "message": "this is a pub/sub message from client-1",
} }
] ]
...@@ -3,7 +3,6 @@ FROM python:alpine3.17 ...@@ -3,7 +3,6 @@ FROM python:alpine3.17
WORKDIR /app WORKDIR /app
COPY requirements.txt requirements.txt COPY requirements.txt requirements.txt
COPY requirements-dev.txt requirements-dev.txt RUN pip install -r requirements.txt
RUN pip install -r requirements-dev.txt
ENTRYPOINT [ "python" ] ENTRYPOINT [ "python" ]
\ No newline at end of file
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
from flask_restful import Resource, request, abort
from marshmallow import Schema, fields
import json
import os
import random
import string
class ClientSchema(Schema):
client_id = fields.Str(required=True)
client_name = fields.Str(required=True)
subscription = fields.Str(required=True)
class ClientsFile:
file = "./data/clients.json"
mtime = 0
clients = {}
parser = None
def __init__(self):
self.get()
def get(self):
try:
mtime = os.path.getmtime(self.file)
if mtime > self.mtime:
with open(self.file, "r") as client_file:
self.clients = json.load(client_file)
except FileNotFoundError as error:
self.clients = {}
self.save()
return self.clients
def find(self, client_id):
self.get()
if client_id in self.clients:
client = self.clients[client_id]
else:
client = None
return client
def add(self, client):
client['secret'] = self.secret()
self.clients[client["client_id"]] = client
self.save()
return client
def remove(self, client):
del self.clients[client["client_id"]]
self.save()
def update(self, client_updates):
client = self.find(client_updates["client_id"])
client.update(client_updates)
self.clients[client["client_id"]] = client
self.save()
return client
def save(self):
try:
with open(self.file, "w") as client_file:
client_file.write(json.dumps(self.clients, indent=2))
return True
except OSError as error:
print(str(error))
return False
def secret(self, chars=36):
res = "".join(
random.choices(
string.ascii_lowercase + string.ascii_uppercase + string.digits, k=chars
)
)
return str(res)
clients_file = ClientsFile()
# Client
class Client(Resource):
clients_file = None
def __init__(self):
self.schema = ClientSchema()
self.clients_file = ClientsFile()
def get(self, client_id):
client = self.clients_file.find(client_id)
del client['secret']
if not client:
abort(404, message="No client with id: {}".format(client_id))
return client
def delete(self, todo_id):
client = self.clients_file.find(client_id)
if not client:
abort(404, message="No client with id: {}".format(client_id))
else:
self.clients_file.remove(client)
return client, 204
def put(self, client_id):
args = request.get_json()
errors = self.schema.validate(args)
if errors:
abort(400, message=str(errors))
client = self.clients_file.find(client_id)
if not client:
abort(404, message="No client with id: {}".format(client_id))
else:
client = self.clients_file.update(args)
return client, 201
# ClientList
class ClientList(Resource):
def __init__(self):
self.schema = ClientSchema()
self.clients_file = ClientsFile()
def get(self):
return {
client_id: (client, client.pop("secret", None))[0]
for client_id, client in self.clients_file.get().items()
}
def post(self):
args = request.get_json()
errors = self.schema.validate(args)
if errors:
abort(400, message=str(errors))
client = self.clients_file.find(args["client_id"])
if client:
abort(403, message="Duplicate client id: {}".format(client_id))
else:
client = self.clients_file.add(args)
return client, 201
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment