async def get_item( item_id: UUID, _user: UserInfo = Depends(Authentication()), ) -> Item: item_data = await collections.item_collection.find_one({'_id': item_id}) if item_data is None: raise HTTPException(404, f"Item {item_id} not found") return Item.validate(item_data)
async def get_items( all: bool = Query(False), _user: UserInfo = Depends(Authentication()), ) -> List[Item]: return [ Item.validate(item) async for item in collections.item_collection.find( {} if all else {'condition': { '$ne': 'gone' }}) ]
async def create_item( item: ItemInWrite = Body(...), _user: UserInfo = Depends(Authentication(require_manager=True)), ) -> Item: change_comment = item.change_comment db_item = DbItem( id=uuid4(), **item.dict(exclude_none=True, exclude={'change_comment'}), ) await collections.item_collection.insert_one(db_item) await _save_state(DbItem(id=db_item.id, name=""), db_item, change_comment, _user['sub']) return Item.validate(db_item)
async def update_item( item_id: UUID, item: ItemInWrite = Body(...), _user: UserInfo = Depends(Authentication(require_manager=True)), ) -> Item: item_data = await collections.item_collection.find_one({'_id': item_id}) if item_data is None: raise HTTPException(404, f"Item {item_id} not found") change_comment = item.change_comment if item.bay_id is not None: if not await collections.bay_collection.exists({'_id': item.bay_id}): raise HTTPException(404, f"Bay {item.bay_id} not found") db_item = DbItem(id=item_id, **item.dict(exclude_none=True, exclude={'change_comment'})) await _save_state(item_data, db_item, change_comment, _user['sub']) if not await collections.item_collection.replace_one(db_item): raise HTTPException(404, f"Item {item_id} not found") return Item.validate(db_item)
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