Skip to content

Commit

Permalink
Merge pull request bcgov#140 from shaangill025/feat_safelist_email_ad…
Browse files Browse the repository at this point in the history
…d_and_rm

20463 - Feat: Support Safe List Addition and Removal
  • Loading branch information
pwei1018 authored May 27, 2024
2 parents 0ac6e10 + fc2035f commit 3e79797
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
10 changes: 10 additions & 0 deletions notify-api/src/notify_api/models/safe_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ def is_in_safe_list(cls, email: str) -> bool:

return is_safe

@classmethod
def find_by_email(cls, email: str) -> SafeList:
"""return safe list."""
return (cls.query.filter_by(email=email).all())[0]

@classmethod
def find_all(cls) -> List[SafeList]:
"""Return all of the safe emails."""
safe_emails = cls.query.all()
return safe_emails

def delete_email(self):
"""delete email from safe list."""
db.session.delete(self)
db.session.commit()
16 changes: 16 additions & 0 deletions notify-api/src/notify_api/resources/v2/safe_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ def safe_list(body: SafeListRequest): # pylint: disable=unused-argument
return {}, HTTPStatus.OK


@bp.route("/<string:email>", methods=["DELETE"])
@jwt.requires_auth
@jwt.has_one_of_roles([Role.SYSTEM.value, Role.STAFF.value])
@validate()
def delete_email(email: str):
if not SafeList.is_in_safe_list(email):
logger.debug("Email not found in safe list.")
return {}, HTTPStatus.OK
try:
safe_list = SafeList.find_by_email(email)
safe_list.delete_email()
except Exception as err: # NOQA # pylint: disable=broad-except
logger.debug(err)
return {}, HTTPStatus.OK


@bp.route("/", methods=["OPTIONS"])
@validate()
def get_safe_list_preflight():
Expand Down
21 changes: 21 additions & 0 deletions notify-api/tests/unit/models/test_safe_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""The Unit Test for the SafeList Model."""

from notify_api.models.safe_list import SafeList


def test_safe_list():
"""Assert the test safe list model vaildation."""
safelist = SafeList()
safelist.add_email("[email protected]")
safelist.add_email("[email protected]")
assert safelist.is_in_safe_list("[email protected]")
assert safelist.is_in_safe_list("[email protected]")
# Test delete email
safelist_to_delete = safelist.find_by_email("[email protected]")
safelist_to_delete.delete_email()
assert not safelist.is_in_safe_list("[email protected]")
assert safelist.is_in_safe_list("[email protected]")
# Test add email
safelist.add_email("[email protected]")
assert safelist.is_in_safe_list("[email protected]")
assert safelist.is_in_safe_list("[email protected]")
21 changes: 19 additions & 2 deletions notify-api/tests/unit/resources/v2/test_safe_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,29 @@

def test_safe_list(session, client, jwt): # pylint: disable=unused-argument
"""Assert that the safe list returns."""
headers = create_header(jwt, [Role.STAFF.value], **{"Accept-Version": "v2"})
safelist = SafeList()
headers = create_header(jwt, [Role.STAFF.value], **{"Accept-Version": "v2"})
safelist.add_email("[email protected]")
safelist.add_email("[email protected]")
response = client.get("/api/v2/safe_list/", headers=headers)
assert response.status_code == 200
assert response.json
assert len(response.json) == 2
assert response.json[0]["email"] == "[email protected]"
# Test delete endpoint
delete_response = client.delete(f"/api/v2/safe_list/{'[email protected]'}", headers=headers)
assert delete_response.status_code == 200
response = client.get("/api/v2/safe_list/", headers=headers)
assert response.status_code == 200
assert response.json
assert len(response.json) == 1
assert safelist.is_in_safe_list("[email protected]")
# Test add post endpoint
add_request_data = {"email": ["[email protected]"]}
add_response = client.post("/api/v2/safe_list/", json=add_request_data, headers=headers)
assert add_response.status_code == 200
response = client.get("/api/v2/safe_list/", headers=headers)
assert response.status_code == 200
assert response.json
assert len(response.json) == 2
assert safelist.is_in_safe_list("[email protected]")
assert safelist.is_in_safe_list("[email protected]")

0 comments on commit 3e79797

Please sign in to comment.