async def create_bay( bay: BayInWrite = Body(...), _user: UserInfo = Depends(Authentication(require_admin=True)), ) -> Bay: db_bay = DbBay(id=uuid4(), **bay.dict()) await collections.bay_collection.insert_one(db_bay) return Bay.validate(db_bay)
def test_bay(monkeypatch, motor_mock): monkeypatch.setattr(Authentication, '__call__', MockAuthentication.__call__) with TestClient(app) as client: clear_all() create_bay = BayInWrite(external_id='bay_1', name="Bay 1", description="Top Left") resp = client.post( '/api/v1/depot/bays', data=create_bay.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_bay = Bay.validate(resp.json()) assert BayInWrite.validate(created_bay) == create_bay resp = client.get('/api/v1/depot/bays', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text bays = [Bay.validate(b) for b in resp.json()] assert len(bays) == 1 assert bays[0] == created_bay resp = client.get(f'/api/v1/depot/bays/{created_bay.id}', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text assert Bay.validate(resp.json()) == created_bay update_bay = BayInWrite(external_id='bay_1_upd', name="Bay 1 Upd", description="Top Right") resp = client.put(f'/api/v1/depot/bays/{created_bay.id}', data=update_bay.json(), auth=MockAuth(sub='admin1', roles=['admin'])) assert resp.status_code == 200, resp.text updated_bay = Bay.validate(resp.json()) assert BayInWrite.validate(updated_bay) == update_bay resp = client.delete(f'/api/v1/depot/bays/{created_bay.id}', auth=MockAuth(sub='admin1', roles=['admin'])) assert resp.status_code == 200, resp.text resp = client.get('/api/v1/depot/bays', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text assert len(resp.json()) == 0
async def get_bay( bay_id: UUID, _user: UserInfo = Depends(Authentication()), ) -> Bay: bay_data = await collections.bay_collection.find_one({'_id': bay_id}) if bay_data is None: raise HTTPException(404) return Bay.validate(bay_data)
async def update_bay( bay_id: UUID, bay: BayInWrite = Body(...), _user: UserInfo = Depends(Authentication(require_admin=True)), ) -> Bay: db_bay = DbBay(id=bay_id, **bay.dict()) if not await collections.bay_collection.replace_one(db_bay): raise HTTPException(404, f"No bay for id {bay_id}") return Bay.validate(db_bay)
def test_overlap(monkeypatch, motor_mock): monkeypatch.setattr(Authentication, '__call__', MockAuthentication.__call__) with TestClient(app) as client: clear_all() create_bay = BayInWrite(external_id='bay_1', name="Bay 1", description="Top Left") resp = client.post( '/api/v1/depot/bays', data=create_bay.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_bay_1 = Bay.validate(resp.json()) item_ids = [] for i in range(3): create_item = ItemInWrite( external_id=f'item_{i}', name=f"Item {i}", description=f"{i}. Item", condition=ItemCondition.Good, condition_comment="Very Good", purchase_date=date.today(), last_service=date.today(), picture_id=None, group_id=f'item_group_{i}', tags=['item'], bay_id=created_bay_1.id, change_comment="Created", ) resp = client.post( '/api/v1/depot/items', data=create_item.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_item = Item.validate(resp.json()) item_ids.append(created_item.id) create_reservation = ReservationInWrite( type=ReservationType.PRIVATE, name="My Reservation", start=date.today() + timedelta(days=5), end=date.today() + timedelta(days=8), team_id='my-team', contact="12345", items=item_ids, ) resp = client.post( '/api/v1/depot/reservations', data=create_reservation.json(), auth=MockAuth(sub='user1', groups=['my-team']), ) assert resp.status_code == 201, resp.text created_reservation = Reservation.validate(resp.json()) create_reservation.user_id = 'user1' assert ReservationInWrite.validate( created_reservation) == create_reservation create_reservation = ReservationInWrite( type=ReservationType.PRIVATE, name="My Reservation 2", start=date.today() + timedelta(days=8), end=date.today() + timedelta(days=9), contact="12345", items=item_ids[2:], ) resp = client.post( '/api/v1/depot/reservations', data=create_reservation.json(), auth=MockAuth(sub='user2'), ) assert resp.status_code == 400, resp.text create_reservation = ReservationInWrite( type=ReservationType.PRIVATE, name="My Reservation 2", start=date.today() + timedelta(days=4), end=date.today() + timedelta(days=5), contact="12345", items=item_ids[:1], ) resp = client.post( '/api/v1/depot/reservations', data=create_reservation.json(), auth=MockAuth(sub='user2'), ) assert resp.status_code == 400, resp.text start = (date.today() + timedelta(days=4)).isoformat() end = (date.today() + timedelta(days=5)).isoformat() resp = client.get( f'/api/v1/depot/reservations/items?start={start}&end={end}', auth=MockAuth(sub='user2'), ) assert resp.status_code == 200, resp.text assert set(resp.json()) == set(str(item_id) for item_id in item_ids) start = (date.today() + timedelta(days=8)).isoformat() end = (date.today() + timedelta(days=9)).isoformat() resp = client.get( f'/api/v1/depot/reservations/items?start={start}&end={end}', auth=MockAuth(sub='user2'), ) assert resp.status_code == 200, resp.text assert set(resp.json()) == set(str(item_id) for item_id in item_ids) start = (date.today() + timedelta(days=4)).isoformat() end = (date.today() + timedelta(days=5)).isoformat() resp = client.get( f'/api/v1/depot/reservations/items?start={start}&end={end}&skip_reservation_id={created_reservation.id}', auth=MockAuth(sub='user2'), ) assert resp.status_code == 200, resp.text assert set(resp.json()) == set()
def test_reservation(monkeypatch, motor_mock): monkeypatch.setattr(Authentication, '__call__', MockAuthentication.__call__) with TestClient(app) as client: clear_all() create_bay = BayInWrite(external_id='bay_1', name="Bay 1", description="Top Left") resp = client.post( '/api/v1/depot/bays', data=create_bay.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_bay_1 = Bay.validate(resp.json()) item_ids = [] for i in range(3): create_item = ItemInWrite( external_id=f'item_{i}', name=f"Item {i}", description=f"{i}. Item", condition=ItemCondition.Good, condition_comment="Very Good", purchase_date=date.today(), last_service=date.today(), picture_id=None, group_id=f'item_group_{i}', tags=['item'], bay_id=created_bay_1.id, change_comment="Created", ) resp = client.post( '/api/v1/depot/items', data=create_item.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_item = Item.validate(resp.json()) item_ids.append(created_item.id) create_reservation = ReservationInWrite( type=ReservationType.PRIVATE, name="My Reservation", start=date.today() + timedelta(days=5), end=date.today() + timedelta(days=8), team_id='my-team', contact="12345", items=item_ids, ) resp = client.post( '/api/v1/depot/reservations', data=create_reservation.json(), auth=MockAuth(sub='user1', groups=['my-team']), ) assert resp.status_code == 201, resp.text created_reservation = Reservation.validate(resp.json()) create_reservation.user_id = 'user1' assert ReservationInWrite.validate( created_reservation) == create_reservation resp = client.get('/api/v1/depot/reservations', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text reservations = [Reservation.validate(b) for b in resp.json()] assert len(reservations) == 1 assert reservations[0] == created_reservation resp = client.get( f'/api/v1/depot/reservations/{created_reservation.id}', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text assert Reservation.validate(resp.json()) == created_reservation update_reservation = ReservationInWrite( type=ReservationType.TEAM, name="My Reservation Upd", start=date.today() + timedelta(days=6), end=date.today() + timedelta(days=9), user_id='user1', team_id='my-team-upd', contact="123456789", items=[item_ids[0]], ) resp = client.put( f'/api/v1/depot/reservations/{created_reservation.id}', data=update_reservation.json(), auth=MockAuth(sub='user2', groups=['my-team', 'my-team-upd']), ) assert resp.status_code == 200, resp.text updated_reservation = Reservation.validate(resp.json()) update_reservation.user_id = 'user1' assert ReservationInWrite.validate( updated_reservation) == update_reservation resp = client.delete( f'/api/v1/depot/reservations/{created_reservation.id}', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text resp = client.get('/api/v1/depot/reservations', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text assert len(resp.json()) == 0
def test_item(monkeypatch, motor_mock): monkeypatch.setattr(Authentication, '__call__', MockAuthentication.__call__) with TestClient(app) as client: clear_all() create_bay = BayInWrite(external_id='bay_1', name="Bay 1", description="Top Left") resp = client.post( '/api/v1/depot/bays', data=create_bay.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_bay_1 = Bay.validate(resp.json()) create_item = ItemInWrite( external_id='item_1', name="Item 1", description="First Item", condition=ItemCondition.Good, condition_comment="Very Good", purchase_date=date.today(), last_service=date.today(), picture_id=None, group_id='item_group_1', tags=['item', 'one'], bay_id=created_bay_1.id, change_comment="Created", ) resp = client.post( '/api/v1/depot/items', data=create_item.json(), auth=MockAuth(sub='admin1', roles=['admin']), ) assert resp.status_code == 201, resp.text created_item = Item.validate(resp.json()) assert created_item.dict(exclude={'id'}) == create_item.dict( exclude={'change_comment'}) resp = client.get('/api/v1/depot/items', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text items = [Item.validate(b) for b in resp.json()] assert len(items) == 1 assert items[0] == created_item resp = client.get(f'/api/v1/depot/items/{created_item.id}', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text assert Item.validate(resp.json()) == created_item update_item = ItemInWrite( external_id='item_1_upd', name="Item 1 Upd", description="First Item Updated", condition=ItemCondition.Bad, condition_comment="Very Bad", purchase_date=date.today() + timedelta(days=2), last_service=date.today() + timedelta(days=2), picture_id=None, group_id=None, tags=['item', 'two'], bay_id=None, change_comment="Updated", ) resp = client.put(f'/api/v1/depot/items/{created_item.id}', data=update_item.json(), auth=MockAuth(sub='admin2', roles=['admin'])) assert resp.status_code == 200, resp.text updated_item = Item.validate(resp.json()) assert updated_item.dict(exclude={'id'}) == update_item.dict( exclude={'change_comment'}) resp = client.get(f'/api/v1/depot/items/{created_item.id}/history', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text states = [ItemState.validate(r) for r in resp.json()] assert len(states) == 2 assert states[0].user_id == 'admin2' assert states[0].item_id == created_item.id assert states[0].comment == 'Updated' assert states[0].changes.external_id.next == update_item.external_id assert states[0].changes.name.next == update_item.name assert states[0].changes.description.next == update_item.description assert states[0].changes.condition.next == update_item.condition assert states[ 0].changes.condition_comment.next == update_item.condition_comment assert states[ 0].changes.purchase_date.next == update_item.purchase_date assert states[0].changes.last_service.next == update_item.last_service assert states[0].changes.group_id.next == update_item.group_id assert states[0].changes.tags.next == update_item.tags assert states[0].changes.bay_id.next == update_item.bay_id assert states[1].user_id == 'admin1' assert states[1].item_id == created_item.id assert states[1].comment == 'Created' assert states[1].changes.external_id.next == create_item.external_id assert states[1].changes.name.next == create_item.name assert states[1].changes.description.next == create_item.description assert states[1].changes.condition.next == create_item.condition assert states[ 1].changes.condition_comment.next == create_item.condition_comment assert states[ 1].changes.purchase_date.next == create_item.purchase_date assert states[1].changes.last_service.next == create_item.last_service assert states[1].changes.group_id.next == create_item.group_id assert states[1].changes.tags.next == create_item.tags assert states[1].changes.bay_id.next == create_item.bay_id resp = client.delete(f'/api/v1/depot/items/{created_item.id}', auth=MockAuth(sub='admin1', roles=['admin'])) assert resp.status_code == 200, resp.text resp = client.get('/api/v1/depot/items', auth=MockAuth(sub='user1')) assert resp.status_code == 200, resp.text assert len(resp.json()) == 0
async def get_bays(_user: UserInfo = Depends(Authentication()), ) -> List[Bay]: return [ Bay.validate(bay) async for bay in collections.bay_collection.find({}) ]