-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create incidents endpoint and tests #355
base: main
Are you sure you want to change the base?
Changes from 2 commits
7147de0
d037299
7436e12
2dc32a2
1c6d646
df48772
3bc368c
5e677a5
001eec3
a242d4b
69df8a8
18656ae
b36d558
7515e9f
9d607f8
951a6f1
8e0f7cd
f111cc0
98138fb
dfc8f9d
982e21d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
from backend.database import Incident, Partner, PrivacyStatus, User | ||
from typing import Any | ||
|
||
member_email = "[email protected]" | ||
example_password = "my_password" | ||
mock_partners = { | ||
"cpdp": {"name": "Citizens Police Data Project"}, | ||
"mpv": {"name": "Mapping Police Violence"}, | ||
|
@@ -53,18 +55,25 @@ | |
|
||
|
||
@pytest.fixture | ||
def example_incidents(db_session, client, contributor_access_token): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For issue 2, added the code to make sure that a user is turned into a contributor once they become an admin or a publisher for a partner. This is possible through two endpoints AFAIK, create_partners, and role_change. Since create_partners has another PR open right now, I can add the updated tests through that PR and tests for it as well. |
||
def example_incidents(db_session, client , partner_admin): | ||
for id, mock in mock_partners.items(): | ||
db_session.add(Partner(**mock)) | ||
db_session.commit() | ||
|
||
created = {} | ||
access_token = res = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": partner_admin.email , | ||
"password": "my_password" | ||
}, | ||
).json["access_token"] | ||
for name, mock in mock_incidents.items(): | ||
res = client.post( | ||
"/api/v1/incidents/create", | ||
json=mock, | ||
headers={ | ||
"Authorization": "Bearer {0}".format(contributor_access_token) | ||
"Authorization": "Bearer {0}".format(access_token) | ||
}, | ||
) | ||
assert res.status_code == 200 | ||
|
@@ -73,9 +82,6 @@ def example_incidents(db_session, client, contributor_access_token): | |
|
||
|
||
def test_create_incident(db_session, example_incidents): | ||
# TODO: test that the User actually has permission to create an | ||
# incident for the partner | ||
# expected = mock_incidents["domestic"] | ||
created = example_incidents["domestic"] | ||
|
||
incident_obj = ( | ||
|
@@ -88,7 +94,95 @@ def test_create_incident(db_session, example_incidents): | |
incident_obj.perpetrators[i].id == created["perpetrators"][i]["id"] | ||
) | ||
assert incident_obj.use_of_force[0].id == created["use_of_force"][0]["id"] | ||
# assert incident_obj.source == expected["source"] | ||
assert incident_obj.location == created["location"] | ||
assert incident_obj.description == created["description"] | ||
|
||
|
||
""" | ||
test for creating a new incident and | ||
creating same incident | ||
""" | ||
|
||
|
||
def test_create_incident_exists( | ||
client, | ||
partner_admin, | ||
|
||
): | ||
created = {} | ||
access_token = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": partner_admin.email , | ||
"password": "my_password" | ||
}, | ||
).json["access_token"] | ||
# creating new incident | ||
res = client.post( | ||
"/api/v1/incidents/create", | ||
headers={"Authorization": f"Bearer {access_token}"}, | ||
json=mock_incidents["domestic"] | ||
) | ||
created["domestic"] = res.json | ||
domestic_instance = created["domestic"] | ||
print(created) | ||
assert res.status_code == 200 | ||
expected = mock_incidents["domestic"] | ||
incident_obj = Incident.query.filter_by( | ||
time_of_incident=expected["time_of_incident"] | ||
).first() | ||
date_format = '%Y-%m-%d %H:%M:%S' | ||
date_obj = datetime.strptime( | ||
expected["time_of_incident"], | ||
date_format) | ||
assert incident_obj.time_of_incident == date_obj | ||
for i in [0, 1]: | ||
assert ( | ||
incident_obj.perpetrators[i].id == | ||
domestic_instance["perpetrators"][i]["id"] | ||
) | ||
assert (incident_obj.use_of_force[0].id == | ||
domestic_instance["use_of_force"][0]["id"]) | ||
assert incident_obj.location == domestic_instance["location"] | ||
assert incident_obj.description == domestic_instance["description"] | ||
|
||
# creating the same incident | ||
# should not be able to create incident | ||
res = client.post( | ||
"/api/v1/incidents/create", | ||
headers={"Authorization": f"Bearer {access_token}"}, | ||
json=mock_incidents["domestic"] | ||
) | ||
|
||
assert res.status_code == 409 | ||
|
||
|
||
""" | ||
creating incident when user | ||
does not have permission | ||
""" | ||
|
||
|
||
def test_create_incident_no_permission( | ||
client, | ||
example_user | ||
|
||
): | ||
access_token = client.post( | ||
"api/v1/auth/login", | ||
json={ | ||
"email": example_user.email, | ||
"password": "my_password" | ||
}, | ||
).json["access_token"] | ||
print(access_token) | ||
# creating new incident | ||
res = client.post( | ||
"/api/v1/incidents/create", | ||
headers={"Authorization": f"Bearer {access_token}"}, | ||
json=mock_incidents["domestic"] | ||
) | ||
assert res.status_code == 403 | ||
|
||
|
||
def test_get_incident(app, client, db_session, access_token): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to consider: we should make sure that the source included request body is the same source that is being used to publish the incident. It's may seem strange, but it could be that the Partners endpoint is actually the best place for the incident creation function.
Just opining though. No changes needed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to move the create_incidents to routes/partners.py , but seems like it'll break other tests in test_incidents as it is needed to create mock incidents for testing other endpoints. I could figure out a work around to this, but wasn't sure if I should do it.