Skip to content

Commit

Permalink
data service methods update and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lrdossan committed Sep 29, 2023
1 parent a791460 commit 29a3d01
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
40 changes: 22 additions & 18 deletions caimira/apps/calculator/data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ def _is_valid(self, access_token):
# decode access_token
# check validity
return False

async def fetch_post_request(self, url: str, json_body: str):
http_client = AsyncHTTPClient()
return await http_client.fetch(HTTPRequest(
url=url,
method='POST',
headers={'Content-type': 'application/json'},
body=json.dumps(json_body),
),
raise_error=True)

async def fetch_get_request(self, url: str, headers: dict):
http_client = AsyncHTTPClient()
return await http_client.fetch(HTTPRequest(
url=url,
method='GET',
headers=headers,
),
raise_error=True)

async def _login(self):
if self._is_valid(self._access_token):
Expand All @@ -42,33 +61,18 @@ async def _login(self):
# If the credentials are not defined, an exception is raised.
raise Exception("DataService credentials not set")

http_client = AsyncHTTPClient()
headers = {'Content-type': 'application/json'}
json_body = { "email": f"{client_email}", "password": f"{client_password}"}

response = await http_client.fetch(HTTPRequest(
url=self.host + '/login',
method='POST',
headers=headers,
body=json.dumps(json_body),
),
raise_error=True)
response = await self.fetch_post_request(url=self.host + '/login', json_body=json.dumps(json_body))

self._access_token = json.loads(response.body)['access_token']
return self._access_token

async def fetch(self):
access_token = await self._login()

http_client = AsyncHTTPClient()
headers = {'Authorization': f'Bearer {access_token}'}

response = await http_client.fetch(HTTPRequest(
url=self.host + '/data',
method='GET',
headers=headers,
),
raise_error=True)
url = self.host + '/data'
response = await self.fetch_get_request(url=url, headers=headers)

return json.loads(response.body)

41 changes: 32 additions & 9 deletions caimira/tests/apps/calculator/test_data_service_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import os
import re
import json

from caimira.apps.calculator.data_service import DataService

Expand All @@ -13,7 +14,7 @@ def data_service_credentials():
return {
'data_service_client_email': os.environ.get('DATA_SERVICE_CLIENT_EMAIL', None),
'data_service_client_password': os.environ.get('DATA_SERVICE_CLIENT_PASSWORD', None),
}
}

async def test_fetch_method(data_service_enabled, data_service_credentials):
if not data_service_enabled:
Expand All @@ -22,23 +23,45 @@ async def test_fetch_method(data_service_enabled, data_service_credentials):
# Initialize the DataService
data_service = DataService(data_service_credentials)

if (data_service_credentials["data_service_client_email"] == None or
data_service_credentials["data_service_client_password"] == None):
if (data_service_credentials["data_service_client_email"] is None or
data_service_credentials["data_service_client_password"] is None):
# Check if an error is thrown
with pytest.raises(
Exception,
match=re.escape("DataService credentials not set")
):
await DataService(data_service_credentials).fetch()

else:
# Call the fetch method
response = await data_service.fetch()
# Set the credentials
client_email = data_service_credentials["data_service_client_email"]
client_password = data_service_credentials["data_service_client_password"]

# Call the fetch request method
response = await data_service.fetch_post_request(
url=data_service.host + '/login',
json_body={ "email": f"{client_email}", "password": f"{client_password}"}
)

# Assert the response is not None
assert response is not None
assert response.code == 200

response_body = json.loads(response.body)

# Call the get request method
response = await data_service.fetch_get_request(
url=data_service.host + '/data',
headers={'Authorization': f'Bearer {response_body["access_token"]}'},
)

# Assert the response is not None
assert response is not None
assert response.code == 200

response_body = json.loads(response.body)

# Assert that the response contains data
assert "data" in response
assert isinstance(response["data"], dict)
assert response["data"] is not None
assert "data" in response_body
assert isinstance(response_body["data"], dict)
assert response_body["data"] is not None

0 comments on commit 29a3d01

Please sign in to comment.