Skip to content

Commit

Permalink
Add filtering with an organisation id (#2137)
Browse files Browse the repository at this point in the history
* Add filtering with an organisation id

* Update tests/app/email_branding/test_rest.py

Co-authored-by: Andrew <[email protected]>

* test

---------

Co-authored-by: Andrew <[email protected]>
  • Loading branch information
jzbahrai and andrewleith authored Mar 21, 2024
1 parent 1d576ce commit da922e1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
4 changes: 3 additions & 1 deletion app/dao/email_branding_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from app.models import EmailBranding


def dao_get_email_branding_options():
def dao_get_email_branding_options(filter_by_organisation_id=None):
if filter_by_organisation_id:
return EmailBranding.query.filter_by(organisation_id=filter_by_organisation_id).all()
return EmailBranding.query.all()


Expand Down
5 changes: 4 additions & 1 deletion app/email_branding/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

@email_branding_blueprint.route("", methods=["GET"])
def get_email_branding_options():
email_branding_options = [o.serialize() for o in dao_get_email_branding_options()]
filter_by_organisation_id = request.args.get("organisation_id", None)
email_branding_options = [
o.serialize() for o in dao_get_email_branding_options(filter_by_organisation_id=filter_by_organisation_id)
]
return jsonify(email_branding=email_branding_options)


Expand Down
12 changes: 10 additions & 2 deletions tests/app/dao/test_email_branding_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
dao_update_email_branding,
)
from app.models import EmailBranding
from tests.app.db import create_email_branding
from tests.app.db import create_email_branding, create_organisation


def test_get_email_branding_options_gets_all_email_branding(notify_db, notify_db_session):
email_branding_1 = create_email_branding(name="test_email_branding_1")
org_1 = create_organisation()
email_branding_1 = create_email_branding(name="test_email_branding_1", organisation_id=org_1.id)
email_branding_2 = create_email_branding(name="test_email_branding_2")

email_branding = dao_get_email_branding_options()
Expand All @@ -18,6 +19,13 @@ def test_get_email_branding_options_gets_all_email_branding(notify_db, notify_db
assert email_branding_1 == email_branding[0]
assert email_branding_2 == email_branding[1]

org_1_id = email_branding_1.organisation_id

email_branding = dao_get_email_branding_options(filter_by_organisation_id=org_1_id)
assert len(email_branding) == 1
assert email_branding_1 == email_branding[0]
assert email_branding[0].organisation_id == org_1_id


def test_get_email_branding_by_id_gets_correct_email_branding(notify_db, notify_db_session):
email_branding = create_email_branding()
Expand Down
4 changes: 3 additions & 1 deletion tests/app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,15 @@ def create_service_callback_api(
return service_callback_api


def create_email_branding(colour="blue", logo="test_x2.png", name="test_org_1", text="DisplayName"):
def create_email_branding(colour="blue", logo="test_x2.png", name="test_org_1", text="DisplayName", organisation_id=None):
data = {
"colour": colour,
"logo": logo,
"name": name,
"text": text,
}
if organisation_id:
data["organisation_id"] = organisation_id
email_branding = EmailBranding(**data)
dao_create_email_branding(email_branding)

Expand Down
17 changes: 17 additions & 0 deletions tests/app/email_branding/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def test_get_email_branding_options(admin_request, notify_db, notify_db_session,
assert email_branding[1]["organisation_id"] == ""


def test_get_email_branding_options_filter_org(admin_request, notify_db, notify_db_session, sample_organisation):
email_branding1 = EmailBranding(colour="#FFFFFF", logo="/path/image.png", name="Org1", organisation_id=sample_organisation.id)
email_branding2 = EmailBranding(colour="#000000", logo="/path/other.png", name="Org2")
notify_db.session.add_all([email_branding1, email_branding2])
notify_db.session.commit()
email_branding = admin_request.get("email_branding.get_email_branding_options", organisation_id=sample_organisation.id)[
"email_branding"
]

assert len(email_branding) == 1
assert email_branding[0]["organisation_id"] == str(sample_organisation.id)

email_branding2 = admin_request.get("email_branding.get_email_branding_options")["email_branding"]

assert len(email_branding2) == 2


def test_get_email_branding_by_id(admin_request, notify_db, notify_db_session):
email_branding = EmailBranding(colour="#FFFFFF", logo="/path/image.png", name="Some Org", text="My Org")
notify_db.session.add(email_branding)
Expand Down

0 comments on commit da922e1

Please sign in to comment.