"""An Area 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_taking_a_headquarters_moves_it_from_the_other_am(self, client, auth, org): # am2 currently manages hq2; give hq2 to am1 as well. await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(org["hq1"].id), str(org["hq2"].id)]}, ) listing = (await client.get(f"{API}/area-managers", headers=auth["admin"])).json() owners = {r["headquarters_id"]: r["area_manager_user_id"] for r in listing} assert owners[str(org["hq2"].id)] == str(org["am1"].id) # one AM per HQ still holds assert len(listing) == len(owners) 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) # GM may read the mappings (to see who manages what), AM 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