add api test for home and auth route

This commit is contained in:
morteza-koohgard
2022-01-11 07:23:27 +03:30
parent 1dbec2f2a8
commit 23e1195921
2 changed files with 36 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ sys.path.insert(0, os.getcwd())
ENV_VALUES = {}
if jh.is_jesse_project():
if jh.is_jesse_project() or jh.is_unit_testing():
# load env
load_dotenv()
@@ -24,6 +24,7 @@ if jh.is_jesse_project():
ENV_VALUES['REDIS_HOST'] = 'localhost'
ENV_VALUES['REDIS_PORT'] = '6379'
ENV_VALUES['REDIS_PASSWORD'] = ''
ENV_VALUES['PASSWORD'] = 'password'
# validation for existence of .env file
if len(list(ENV_VALUES.keys())) == 0:
@@ -40,4 +41,5 @@ if jh.is_jesse_project():
# raise FileNotFoundError('.env file is missing from within your local project. This usually happens when you\'re in the wrong directory. You can create one by running "cp .env.example .env"')
if not jh.is_unit_testing() and ENV_VALUES['PASSWORD'] == '':
raise EnvironmentError('You forgot to set the PASSWORD in your .env file')
raise EnvironmentError(
'You forgot to set the PASSWORD in your .env file')

32
tests/test_fast_api.py Normal file
View File

@@ -0,0 +1,32 @@
from fastapi.testclient import TestClient
from jesse.services.web import fastapi_app
from hashlib import sha256
client = TestClient(fastapi_app)
def test_home_route():
response = client.get("/")
assert response.status_code == 200
def test_auth_end_point():
# password set's to password for tests
# first check wrong password
response = client.post(
'/auth',
json={"password": "some"}
)
assert response.status_code == 401
assert response.json() == {
'message': 'Invalid password'
}
# now check correct password
response = client.post(
'/auth',
json={"password": "password"}
)
assert response.status_code == 200
assert response.json() == {
'auth_token': sha256('password'.encode('utf-8')).hexdigest()
}