Skip to content

Commit

Permalink
Retry Test Github action
Browse files Browse the repository at this point in the history
Update deprecated syntax
Update Status codes and responses for register_user
  • Loading branch information
DMalone87 committed Oct 13, 2024
1 parent 7c3a970 commit a8a48ae
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 66 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ jobs:
test_db:
image: neo4j:5.23-community
env:
GRAPH_USER: neo4j
GRAPH_PASSWORD: neo4j
GRAPH_DB: police_data_test
NEO4J_AUTH: neo4j/test_pwd
ports:
- 7687:7687
options: >-
--health-cmd "cypher-shell -u neo4j -p test_pwd 'RETURN 1'"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Python 3.12 Setup
Expand All @@ -34,7 +37,10 @@ jobs:
run: python -m pytest
env:
GRAPH_USER: neo4j
GRAPH_PASSWORD: neo4j
GRAPH_DB: police_data_test
GRAPH_URI: test_db
GRAPH_PASSWORD: test_pwd
GRAPH_URI: localhost
GRAPH_PORT: 7687
MIXPANEL_TOKEN: mixpanel_token
- name: Output Neo4j logs
if: failure()
run: docker logs $(docker ps -q --filter ancestor=neo4j:5.23-community)
6 changes: 5 additions & 1 deletion backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ class ProductionConfig(Config):
class TestingConfig(Config):
ENV = "testing"
TESTING = True
POSTGRES_DB = "police_data_test"
GRAPH_DB = "police_data_test"
GRAPH_URI = "localhost"
GRAPH_PORT = 7687
GRAPH_USER = "neo4j"
GRAPH_PASSWORD = "test_pwd"
SECRET_KEY = "my-secret-key"
JWT_SECRET_KEY = "my-jwt-secret-key"
MIXPANEL_TOKEN = "mixpanel-token"
Expand Down
2 changes: 1 addition & 1 deletion backend/dto/user/login_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LoginUserDTO(BaseModel):
password: str

class Config:
schema_extra = {
json_schema_extra = {
"example": {
"email": "[email protected]",
"password": "password",
Expand Down
10 changes: 5 additions & 5 deletions backend/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def register():
user = User.nodes.first_or_none(email=body.email)
if user is not None:
return {
"status": "ok",
"status": "Conflict",
"message": "Error. Email matches existing account.",
}, 400
}, 409
# Verify all fields included and create user
if body.password is not None and body.email is not None:
user = User(
Expand Down Expand Up @@ -105,7 +105,7 @@ def register():

resp = jsonify(
{
"status": "ok",
"status": "OK",
"message": "Successfully registered.",
"access_token": token,
}
Expand All @@ -126,10 +126,10 @@ def register():
if key not in body.keys() or body.get(key) is None:
missing_fields.append(key)
return {
"status": "ok",
"status": "Unprocessable Entity",
"message": "Failed to register. Please include the following"
" fields: " + ", ".join(missing_fields),
}, 400
}, 422


@bp.route("/refresh", methods=["POST"])
Expand Down
2 changes: 1 addition & 1 deletion backend/routes/incidents.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SearchIncidentsSchema(BaseModel):

class Config:
extra = "forbid"
schema_extra = {
json_schema_extra = {
"example": {
"description": "Test description",
"dateEnd": "2019-12-01",
Expand Down
2 changes: 1 addition & 1 deletion backend/routes/officers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SearchOfficerSchema(BaseModel):

class Config:
extra = "forbid"
schema_extra = {
json_schema_extra = {
"example": {
"officerName": "John Doe",
"location" : "New York",
Expand Down
2 changes: 1 addition & 1 deletion backend/routes/partners.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class AddMemberSchema(BaseModel):
class Config:
extra = "forbid"
schema_extra = {
json_schema_extra = {
"example": {
"user_email": "[email protected]",
"role": "ADMIN",
Expand Down
1 change: 1 addition & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_db_driver():
url=cfg.GRAPH_URI,
port=cfg.GRAPH_PORT
)
print(f"Connecting to test database at {test_db_url}")
config.DATABASE_URL = test_db_url

yield db_driver
Expand Down
135 changes: 85 additions & 50 deletions backend/tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,142 @@
import flask_user
import pytest
from unittest.mock import patch, MagicMock
from backend.database import User
from flask_jwt_extended import decode_token


@pytest.mark.parametrize(
("email", "password", "expected_status_code"),
(
"email", "password", "firstname", "lastname",
"phone_number", "expected_status_code"
),
[
("[email protected]", "my_password", 200),
("bad_email", "bad_password", 422),
(None, None, 422),
("[email protected]", "my_password", "John", "Doe", "1234567890", 200),
("[email protected]", "my_password", "John", "Doe", "1234567890", 409),
("bad_email", "bad_password", None, None, None, 422),
(None, None, None, None, None, 422),
],
)
def test_register(db_session, client, email, password, expected_status_code):
def test_register(
db_session, client, email, password,
firstname, lastname, phone_number,
expected_status_code
):
res = client.post(
"api/v1/auth/register",
json={
"email": email,
"password": password,
},
"firstname": firstname,
"lastname": lastname,
"phone_number": phone_number,
}
)

db_user = User.get_by_email(email)
if db_user is not None:
add_test_label(db_user)
assert ("Set-Cookie" in res.headers) == (expected_status_code == 200)
assert (db_user is not None) == (expected_status_code == 200)
assert res.status_code == expected_status_code

if expected_status_code == 200:
assert res.json["status"] == "ok"
assert res.json["message"] == "Successfully registered."
assert res.json["access_token"] == "mocked_access_token"
assert "Set-Cookie" in res.headers
elif expected_status_code == 409 and email == "[email protected]":
assert res.json["status"] == "Conflict"
assert res.json["message"] == "Error. Email matches existing account."
elif expected_status_code == 422:
assert res.json["status"] == "Unprocessable Entity"
assert "Invalid request body" in res.json["message"]
else:
assert res.json["status"] == "ok"
assert "Failed to register" in res.json["message"]


@pytest.mark.parametrize(
("password", "expected_status_code"),
[("my_password", 200), ("bad_password", 401), (None, 422)],
)
def test_login(
db_session,
mock_db_session,
client, example_user, password, expected_status_code
):
res = client.post(
"api/v1/auth/login",
json={
"email": example_user.email,
"password": password,
},
)
with patch('backend.database.User.get_by_email') as mock_get_by_email:
mock_get_by_email.return_value = example_user

assert ("Set-Cookie" in res.headers) == (expected_status_code == 200)
assert res.status_code == expected_status_code
res = client.post(
"api/v1/auth/login",
json={
"email": example_user.email,
"password": password,
},
)

assert ("Set-Cookie" in res.headers) == (expected_status_code == 200)
assert res.status_code == expected_status_code

def test_jwt(client, db_session, example_user):
res = client.post(
"api/v1/auth/login",
json={
"email": "[email protected]",
"password": "my_password",
},
)

assert res.status_code == 200
def test_jwt(client, mock_db_session, example_user, mock_access_token):
with patch('backend.database.User.nodes.get_or_none') as mock_get_or_none, \
patch('flask_jwt_extended.decode_token') as mock_decode_token:

mock_get_or_none.return_value = example_user
mock_decode_token.return_value = {"sub": "mock_user_uid"}

res = client.post(
"api/v1/auth/login",
json={
"email": "[email protected]",
"password": "my_password",
},
)

user_uid = decode_token(res.json["access_token"])["sub"]
db_user = User.nodes.get_or_none(uid=user_uid)
assert res.status_code == 200

assert db_user.email == example_user.email
assert res.status_code == 200
user_uid = decode_token(res.json["access_token"])["sub"]
db_user = User.nodes.get_or_none(uid=user_uid)

assert db_user.email == example_user.email
assert res.status_code == 200

def test_auth_test_header(db_session, client, example_user):

def test_auth_test_header(
mock_db_session, client, example_user, mock_access_token):
login_res = client.post(
"api/v1/auth/login",
json={"email": example_user.email, "password": "my_password"},
)

client.set_cookie(domain="localhost", key="access_token_cookie", value="")

test_res = client.get(
"api/v1/auth/whoami",
headers={
"Authorization": "Bearer {0}".format(login_res.json["access_token"])
},
)
with patch('backend.auth.jwt_required') as mock_jwt_required:
mock_jwt_required.return_value = None # Mock the jwt_required decorator
test_res = client.get(
"api/v1/auth/whoami",
headers={
"Authorization": f"Bearer {mock_access_token}"
},
)

assert test_res.status_code == 200


def test_auth_test_cookie(db_session, client, example_user):
def test_auth_test_cookie(mock_db_session, client, example_user):
client.post(
"api/v1/auth/login",
json={"email": example_user.email, "password": "my_password"},
)

test_res = client.get(
"api/v1/auth/whoami",
)
with patch('backend.auth.jwt_required') as mock_jwt_required:
mock_jwt_required.return_value = None # Mock the jwt_required decorator
test_res = client.get(
"api/v1/auth/whoami",
)

assert test_res.status_code == 200


def test_access_token_fixture(mock_db_session, mock_access_token):
assert len(mock_access_token) > 0


# @pytest.mark.parametrize(("use_correct_email"), [(True), (False)])
# def test_forgot_email(mocker, client, example_user, use_correct_email):
# mock_send_reset_password_email = mocker.spy(
Expand Down Expand Up @@ -146,7 +185,3 @@ def test_auth_test_cookie(db_session, client, example_user):
# )

# assert (login_res.status_code == 200) == use_correct_token


def test_access_token_fixture(db_session, access_token):
assert len(access_token) > 0

0 comments on commit a8a48ae

Please sign in to comment.