Skip to content

Commit

Permalink
Updating Create Partners endpoint/tests (codeforboston#356)
Browse files Browse the repository at this point in the history
* create_partners endpoint, fixing failing tests

* Updating UserRole after MemberRole changes to Admin/Publisher for Create Partner endpoint
  • Loading branch information
hrauniya authored Mar 26, 2024
1 parent e24765b commit ebaf9a6
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 26 deletions.
85 changes: 59 additions & 26 deletions backend/routes/partners.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,68 @@ def get_partners(partner_id: int):
@min_role_required(UserRole.PUBLIC)
@validate(json=CreatePartnerSchema)
def create_partner():
"""Create a contributing partner.
"""Create a contributing partner."""

Cannot be called in production environments
"""
if current_app.env == "production":
abort(418)

try:
partner = partner_to_orm(request.context.json)
except Exception:
abort(400)

created = partner.create()
make_admin = PartnerMember(
partner_id=created.id,
user_id=get_jwt()["sub"],
role=MemberRole.ADMIN,
)
make_admin.create()
body = request.context.json
if (
body.name is not None
and body.url is not None
and body.contact_email is not None
and body.name != ""
and body.url != ""
and body.contact_email != ""
):
try:
partner = partner_to_orm(body)
except Exception:
abort(400)
partner_query_email = Partner.query.filter_by(
contact_email=body.contact_email).first()
partner_query_url = Partner.query.filter_by(url=body.url).first()
if partner_query_email:
return {
"status": "error",
"message": "Error. Entered email or url details "
+ "matches existing record.",

track_to_mp(
request,
"create_partner",
{
}, 400
if partner_query_url:
return {
"status": "error",
"message": "Error. Entered email or url details "
+ "matches existing record.",
}, 400
"""
add to database if all fields are present
and instance not already in db.
"""
created = partner.create()
resp = partner_orm_to_json(created)

make_admin = PartnerMember(
partner_id=created.id,
user_id=get_jwt()["sub"],
role=MemberRole.ADMIN,
)
make_admin.create()
# update to UserRole contributor status
user_id = get_jwt()["sub"]
user = User.query.filter_by(
id=user_id
).first()
user.role = UserRole.CONTRIBUTOR

track_to_mp(request, "create_partner", {
"partner_name": partner.name,
"partner_contact": partner.contact_email,
},
)
return partner_orm_to_json(created)
"partner_contact": partner.contact_email
})
return resp
else:
return {
"status": "error",
"message": "Failed to create partner. " +
"Please include all of the following"
}, 400


@bp.route("/", methods=["GET"])
Expand Down
139 changes: 139 additions & 0 deletions backend/tests/test_partners.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,44 @@ def test_create_partner(db_session, example_user, example_partners):
assert association_obj.is_administrator() is True


def test_create_partner_role_change(
client,
example_user

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

res = client.post(
"/api/v1/partners/create",
headers={"Authorization": f"Bearer {access_token}"},
json={
"name" : "Example Partner 1",
"url": "examplep.com",
"contact_email": "[email protected]",
}
)
assert res.status_code == 200
partner_member_obj = Partner.query.filter_by(
url="examplep.com"
).first()

assert partner_member_obj.name == "Example Partner 1"
assert partner_member_obj.url == "examplep.com"
assert partner_member_obj.contact_email == "[email protected]"

# Check if UserRole in updated
user = User.query.filter_by(
email=example_user.email
).first()
assert user.role == UserRole.CONTRIBUTOR


def test_get_partner(client, db_session, access_token):
# Create a partner in the database
partner_name = "Test Partner"
Expand Down Expand Up @@ -836,3 +874,104 @@ def test_role_change3(
partner_id=-1,
).first()
assert role_change_instance is None


"""
Test for creating a new partner
and adding existing partner already created
"""


def test_create_new_partner(
client,
partner_admin

):
# test for creating new partner
access_token = res = client.post(
"api/v1/auth/login",
json={
"email": partner_admin.email,
"password": example_password
},
).json["access_token"]

res = client.post(
"/api/v1/partners/create",
headers={"Authorization": f"Bearer {access_token}"},
json={
"name": "Citizens Police Data Project",
"url": "https://cpdp.co",
"contact_email": "[email protected]",
}
)
assert res.status_code == 200
partner_obj = Partner.query.filter_by(
url="https://cpdp.co"
).first()
assert partner_obj.name == "Citizens Police Data Project"
assert partner_obj.url == "https://cpdp.co"
assert partner_obj.contact_email == "[email protected]"

# test for adding duplicate partner that already exists
res = client.post(
"/api/v1/partners/create",
headers={"Authorization": f"Bearer {access_token}"},
json={
"name": "Citizens Police Data Project",
"url": "https://cpdp.co",
"contact_email": "[email protected]",
}
)
assert res.status_code == 400


"""
Validation tests for creating
new partners
"""


def test_create_partner_validation(
client,
partner_admin
):
# adding partner with blank fields
access_token = res = client.post(
"api/v1/auth/login",
json={
"email": partner_admin.email,
"password": example_password
},
).json["access_token"]
res = client.post(
"/api/v1/partners/create",
headers={"Authorization": f"Bearer {access_token}"},
json={
"name": "",
"url": "https://cpdp.co",
"contact_email": "[email protected]",
}
)
assert res.status_code == 400
res = client.post(
"/api/v1/partners/create",
headers={"Authorization": f"Bearer {access_token}"},
json={
"name": "Citizens Police Data Project",
"url": "",
"contact_email": "[email protected]",
}
)
assert res.status_code == 400

res = client.post(
"/api/v1/partners/create",
headers={"Authorization": f"Bearer {access_token}"},
json={
"name": "Citizens Police Data Project",
"url": None ,
"contact_email": "[email protected]",
}
)
assert res.status_code == 400

0 comments on commit ebaf9a6

Please sign in to comment.