"""An Area Business Manager can manage any number of headquarters; only an Admin can change that.""" import uuid from sqlalchemy import select from app.organization.models import Headquarters API = "/api/v1/headquarters" async def _third_hq(db, org) -> Headquarters: hq = Headquarters( id=uuid.uuid4(), company_id=org["company"].id, code="CTC", name="Cuttack", district="Cuttack", state="Odisha", ) db.add(hq) await db.commit() return hq def _hq_ids(resp) -> set[str]: return {row["headquarters_id"] for row in resp.json()} class TestAreaManagerHeadquarters: async def test_one_am_can_hold_several_headquarters(self, client, auth, org, db): hq3 = await _third_hq(db, org) wanted = [str(org["hq1"].id), str(org["hq2"].id), str(hq3.id)] resp = await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": wanted}, ) assert resp.status_code == 200, resp.text assert _hq_ids(resp) == set(wanted) listing = await client.get(f"{API}/area-managers", headers=auth["admin"]) mine = {r["headquarters_id"] for r in listing.json() if r["area_manager_user_id"] == str(org["am1"].id)} assert mine == set(wanted) async def test_a_headquarters_can_have_several_area_managers(self, client, auth, org): # am2 manages hq2; give hq2 to am1 as well — am2 keeps it. resp = await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(org["hq1"].id), str(org["hq2"].id)]}, ) assert resp.status_code == 200, resp.text listing = (await client.get(f"{API}/area-managers", headers=auth["admin"])).json() managers = {r["area_manager_user_id"] for r in listing if r["headquarters_id"] == str(org["hq2"].id)} assert managers == {str(org["am1"].id), str(org["am2"].id)} # Both see it, so either may review its MRs' reports and forms. for am in ("am1", "am2"): assert str(org["hq2"].id) in {h["id"] for h in (await client.get(API, headers=auth[am])).json()} # Saving the same set again adds nothing twice. await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(org["hq1"].id), str(org["hq2"].id)]}, ) again = (await client.get(f"{API}/area-managers", headers=auth["admin"])).json() assert len(again) == len(listing) async def test_omitted_headquarters_are_released(self, client, auth, org): resp = await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": []}, ) assert resp.status_code == 200 assert resp.json() == [] # am1 loses scope over hq1 straight away. hqs = await client.get(API, headers=auth["am1"]) assert hqs.json() == [] async def test_am_sees_all_of_their_headquarters(self, client, auth, org): await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(org["hq1"].id), str(org["hq2"].id)]}, ) hqs = await client.get(API, headers=auth["am1"]) assert {h["code"] for h in hqs.json()} == {"BBSR", "SBP"} async def test_setting_the_same_set_twice_is_a_no_op(self, client, auth, org): body = {"headquarters_ids": [str(org["hq1"].id)]} first = await client.put(f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json=body) second = await client.put(f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json=body) assert first.status_code == second.status_code == 200 assert len(second.json()) == 1 async def test_only_an_am_can_be_assigned(self, client, auth, org): resp = await client.put( f"{API}/area-managers/{org['mr1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(org["hq1"].id)]}, ) assert resp.status_code in (400, 422), resp.text async def test_unknown_headquarters_is_rejected(self, client, auth, org): resp = await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(uuid.uuid4())]}, ) assert resp.status_code == 404, resp.text async def test_permissions(self, client, auth, org): body = {"headquarters_ids": [str(org["hq1"].id)]} for who in ("gm", "am1", "mr1"): put = await client.put(f"{API}/area-managers/{org['am1'].id}", headers=auth[who], json=body) assert put.status_code == 403, (who, put.text) # AGM may read the mappings (to see who manages what), ABM and MR may not. assert (await client.get(f"{API}/area-managers", headers=auth["gm"])).status_code == 200 for who in ("am1", "mr1"): assert (await client.get(f"{API}/area-managers", headers=auth[who])).status_code == 403