Skip to content

Commit

Permalink
unit test update + other fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Shaanjot Gill <[email protected]>
  • Loading branch information
shaangill025 committed May 24, 2024
1 parent f14d50b commit fc2035f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
11 changes: 7 additions & 4 deletions notify-api/src/notify_api/models/safe_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +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

@classmethod
def delete_email(cls, email: str):
def delete_email(self):
"""delete email from safe list."""
db_email = SafeList(email=email)
db.session.delete(db_email)
db.session.delete(self)
db.session.commit()
3 changes: 2 additions & 1 deletion notify-api/src/notify_api/resources/v2/safe_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def delete_email(email: str):
logger.debug("Email not found in safe list.")
return {}, HTTPStatus.OK
try:
SafeList.delete_email(email)
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
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]")
31 changes: 18 additions & 13 deletions notify-api/tests/unit/resources/v2/test_safe_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,36 @@
# limitations under the License.
"""Tests to assure the safe list end-point."""

import json

from notify_api.models.safe_list import SafeList
from notify_api.utils.enums import Role
from tests.factories.jwt import create_header


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]"
del_response = client.delete(f'/api/v2/safe_list/{"[email protected]"}', headers=headers)
assert del_response.status_code == 200
assert not SafeList.is_in_safe_list("[email protected]")
assert SafeList.is_in_safe_list("[email protected]")
request_json = json.dumps({"email": ["[email protected]"]})
add_response = client.post("/api/v2/safe_list", json=request_json, headers=headers)
# 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
assert add_response.json == {}
assert SafeList.is_in_safe_list("[email protected]")
assert SafeList.is_in_safe_list("[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 safelist.is_in_safe_list("[email protected]")
assert safelist.is_in_safe_list("[email protected]")

0 comments on commit fc2035f

Please sign in to comment.