Ejemplo n.º 1
0
async def test_send_request_changes_request_status(contractor_representative_out):
    await RequestsDAL.add_worker_to_request(contractor_representative_out, 1, 1)
    res = await RequestsDAL.send_request(contractor_representative_out, 1)
    assert res.status == RequestStatus.WAITING_FOR_VERIFICATION
    with create_session() as session:
        request = session.query(Request).filter(Request.id == 1).one()
        assert request.status == RequestStatus.WAITING_FOR_VERIFICATION
Ejemplo n.º 2
0
async def test_reset_worker_in_request_status(contractor_representative_out, ):
    await RequestsDAL.accept_worker(1, 1)
    res = await RequestsDAL.reset_worker_in_request_status(1, 1)
    assert res.status == WorkerInRequestStatus.WAITING_FOR_VERIFICATION
    with create_session() as session:
        worker = session.query(WorkerInRequest).filter(WorkerInRequest.id == 1).one()
        assert worker.status == WorkerInRequestStatus.WAITING_FOR_VERIFICATION
Ejemplo n.º 3
0
 async def add_worker(contractor_representative: OutUser, last_name: str,
                      first_name: str, patronymic: str, birthday: date,
                      profession: str, **kwargs) -> WorkerWithProfessionOut:
     with create_session() as session:
         try:
             profession = (session.query(Profession).filter(
                 Profession.data == profession).one())
         except NoResultFound:
             raise DALError(
                 HTTPStatus.BAD_REQUEST.value,
                 Message.PROFESSION_DOES_NOT_EXITS.value,
             )
         contractor_representative_from_db = await RepresentativesDAL._get_contractor_representative(
             contractor_representative, session)
         worker = Worker(
             patronymic=patronymic,
             last_name=last_name,
             first_name=first_name,
             birth_date=birthday,
             profession=profession,
             contractor_id=contractor_representative_from_db.contractor_id,
         )
         for key, value in kwargs.items():
             if hasattr(Worker, key) and value:
                 await DocumentsDAL.add_worker_document(
                     session, worker, key, SimpleDocumentIn(file=value))
         session.flush()
         worker_out = WorkerOut.from_orm(worker)
         return WorkerWithProfessionOut(**worker_out.dict(),
                                        profession=worker.profession.data)
Ejemplo n.º 4
0
    def get_worker_from_requests(representative_id: int, request_id: int,
                                 substring: str, page: int, size: int):
        with create_session() as session:
            request = RequestsDAL._get_request(request_id, session)
            try:
                representative = (
                    session.query(ContractorRepresentative).filter(
                        ContractorRepresentative.id ==
                        representative_id).one())

            except NoResultFound:
                raise DALError(HTTPStatus.NOT_FOUND.value)
            if request not in representative.contractor.requests:
                raise DALError(HTTPStatus.FORBIDDEN.value)
            res = []
            for worker_in_request in request.workers_in_request:
                worker = worker_in_request.worker
                if (substring and substring not in
                        f'{worker.last_name} {worker.first_name} {worker.patronymic}'
                    ):
                    continue
                res.append(
                    RequestsDAL.serialize_worker_with_request_info(
                        worker, worker_in_request))
            return get_pagination(res, page, size)
Ejemplo n.º 5
0
async def test_refresh_tokens_saves_new_tokens_pair_to_db(
        username, password, tokens):
    new_tokens = await refresh_tokens(tokens.refresh_token.decode())
    with create_session() as session:
        user = session.query(User).filter(User.username == username).first()
        assert user.refresh_token == new_tokens.refresh_token.decode()
        assert user.access_token == new_tokens.access_token.decode()
Ejemplo n.º 6
0
 def get_representative_request_result(request_id: int,
                                       representative_id: int,
                                       substring: str, page: int,
                                       size: int):
     with create_session() as session:
         try:
             representative = (
                 session.query(ContractorRepresentative).filter(
                     ContractorRepresentative.id ==
                     representative_id).one())
         except NoResultFound:
             raise DALError(HTTPStatus.NOT_FOUND.value)
         request = RequestsDAL._get_request(request_id, session)
         if request not in representative.contractor.requests:
             raise DALError(HTTPStatus.FORBIDDEN.value)
         if request.status != RequestStatus.WAITING_FOR_READINESS:
             raise DALError(HTTPStatus.NOT_FOUND.value)
         workers = map(
             lambda worker_in_request: worker_in_request.worker,
             request.workers_in_request,
         )
         res = [
             RequestsDAL.serialize_worker(worker) for worker in workers
             if substring and substring in f''
             f'{worker.last_name} {worker.first_name} {worker.patronymic}'
             or not substring
         ]
         return get_pagination(res, page, size)
Ejemplo n.º 7
0
 def get_representative_requests(
     representative_id: int,
     substring: str,
     page: int,
     size: int,
     solved: bool = False,
 ) -> Awaitable[ListWithPagination[RequestForTemplateOut]]:
     with create_session() as session:
         try:
             representative: ContractorRepresentative = session.query(
                 ContractorRepresentative).filter(
                     ContractorRepresentative.id ==
                     representative_id).one()
         except NoResultFound:
             raise DALError(HTTPStatus.NOT_FOUND.value)
         res = []
         for request in representative.contractor.requests:
             if substring and substring not in request.object_of_work.data:
                 continue
             if solved and request.status != RequestStatus.CLOSED:
                 continue
             if (not solved and request.status !=
                     RequestStatus.WAITING_FOR_VERIFICATION):
                 continue
             res.append(RequestsDAL._serialize_request(request))
         return get_pagination(res, page, size)
Ejemplo n.º 8
0
async def test_close_request(contractor_representative_out):
    await RequestsDAL.accept_worker(1, 1)
    res = await RequestsDAL.close(1)
    assert res.status == RequestStatus.CLOSED
    with create_session() as session:
        request = session.query(Request).filter(Request.id == 1).one()
        assert request.status == RequestStatus.CLOSED
Ejemplo n.º 9
0
 def _add_db_obj_to_register_to_db(
         self, obj: T,
         out_model: Type[TAddingUserOut]) -> Awaitable[TAddingUserOut]:
     with create_session() as session:
         session.add(obj)
         session.flush()
         return out_model.from_orm(obj)  # type: ignore
Ejemplo n.º 10
0
async def test_add_request(request_in, contractor_representative_out):
    res = await RequestsDAL.add(contractor_representative_out, request_in)
    with create_session() as session:
        request = session.query(Request).filter(Request.id == 1).one()
        assert request.contractor_id == res.contractor_id == 1
        assert request.object_of_work_id == res.object_of_work_id == 1
        assert request.contract_id == res.contract_id == 3
        assert request.status == RequestStatus.WAITING_FOR_READINESS
Ejemplo n.º 11
0
def get_user(req: Request) -> Awaitable[OutUser]:
    tokens = get_tokens(req)
    with create_session() as session:
        user = (session.query(User).filter(
            User.access_token == tokens.access_token).first())
        if user:
            return OutUser.from_orm(user)  # type: ignore
        raise DALError(HTTPStatus.UNAUTHORIZED.value)
Ejemplo n.º 12
0
async def test_deny_worker(deny_worker_in):
    res = await RequestsDAL.deny_worker(1, 1, deny_worker_in)
    assert res.status == WorkerInRequestStatus.CANCELLED
    with create_session() as session:
        worker_in_request = (
            session.query(WorkerInRequest).filter(WorkerInRequest.id == 1).one()
        )
        assert worker_in_request.status == WorkerInRequestStatus.CANCELLED
Ejemplo n.º 13
0
 def remove_user_to_register(uuid: str) -> None:
     with create_session() as session:
         user = (session.query(UserToRegister).filter(
             UserToRegister.uuid == uuid).first())
         if not user:
             raise DALError(HTTPStatus.BAD_REQUEST.value,
                            Message.USER_DOES_NOT_EXISTS.value)
         session.delete(user)
Ejemplo n.º 14
0
 def accept_worker(request_id: int,
                   worker_id: int) -> Awaitable[WorkerInRequestOut]:
     with create_session() as session:
         request = RequestsDAL._get_request(request_id, session)
         worker = RequestsDAL._get_worker(session, worker_id)
         RequestsDAL._validate_data(request, worker)
         worker.status = WorkerInRequestStatus.ACCEPTED
         return WorkerInRequestOut.from_orm(worker)
Ejemplo n.º 15
0
async def test_add_with_real_profession_will_raise_error(
        out_user, worker_fields, profession):
    await RepresentativesDAL.add_worker(**worker_fields,
                                        profession=profession,
                                        contractor_representative=out_user)
    with create_session() as session:
        worker = session.query(Worker).filter(Worker.id == 1).first()
        assert worker.profession.data == profession
Ejemplo n.º 16
0
async def test_accept_worker(contractor_representative_out, ):
    res = await RequestsDAL.accept_worker(1, 1)
    assert res.status == WorkerInRequestStatus.ACCEPTED
    with create_session() as session:
        worker_in_request: WorkerInRequest = session.query(WorkerInRequest).filter(
            WorkerInRequest.id == 1
        ).one()
        assert worker_in_request.status == WorkerInRequestStatus.ACCEPTED
Ejemplo n.º 17
0
def _save_tokens_to_db(
    user: OutUser, access_token: bytes, refresh_token: bytes
) -> None:
    with create_session() as session:
        user_from_db = session.query(UserDB).filter(UserDB.id == user.id).one()
        user_from_db.refresh_token = refresh_token.decode()
        user_from_db.access_token = access_token.decode()
        session.add(user_from_db)
Ejemplo n.º 18
0
async def test_add_same_request_second_time_will_delete_first_and_delete_workers_in_request_connected_with_request(
        request_in, contractor_representative_out
):
    await RequestsDAL.add_worker_to_request(contractor_representative_out, 1, 1)
    await RequestsDAL.add(contractor_representative_out, request_in)
    with create_session() as session:
        assert not session.query(Request).filter(Request.id == 1).first()
        request = session.query(Request).filter(Request.id == 2).one()
        assert len(request.workers_in_request) == 0
Ejemplo n.º 19
0
async def test_delete_worker_from_request(contractor_representative_out):
    await RequestsDAL.add_worker_to_request(contractor_representative_out, 1, 1)
    await RequestsDAL.delete_worker_from_request(
        contractor_representative_out, request_id=1, worker_id=1
    )
    with create_session() as session:
        assert (
            not session.query(WorkerInRequest).filter(WorkerInRequest.id == 1).first()
        )
Ejemplo n.º 20
0
 def get(self, uuid: str) -> Awaitable[TOutModel]:
     with create_session() as session:
         try:
             document = session.query(Document).filter(
                 Document.uuid == uuid).one()
             out_model = get_document_out_model(document.type)
             return out_model.from_orm(document)  # type: ignore
         except NoResultFound:
             raise DALError(HTTPStatus.NOT_FOUND.value)
Ejemplo n.º 21
0
 def register(self, params: SimpleRegistrationParams) -> Awaitable[OutUser]:
     with create_session() as session:
         user = self._add_user(
             session,
             username=params.username,
             password=params.password,
             db_obj=get_db_obj(params.type),
         )
         return OutUser.from_orm(user)  # type: ignore
Ejemplo n.º 22
0
 def delete_catalog(catalog_id: int) -> Awaitable[None]:  # type: ignore
     with create_session() as session:
         try:
             catalog = session.query(Catalog).filter(
                 Catalog.id == catalog_id).one()
             session.delete(catalog)
         except NoResultFound:
             raise DALError(HTTPStatus.NOT_FOUND.value,
                            Message.CATALOG_DOES_NOT_EXISTS.value)
Ejemplo n.º 23
0
async def test_worker_document_adding(worker_document, upload_file, uuid4,
                                      path):
    WorkerDocument.save_file.return_value = DocumentParams(path, uuid4)
    with create_session() as session:
        worker = Worker(last_name='1',
                        first_name='2',
                        patronymic='3',
                        birth_date=datetime.utcnow())
        await worker_document.add(session, worker, 'identification',
                                  SimpleDocumentIn(file=upload_file))
    with create_session() as session:
        identification = (session.query(Identification).filter(
            Identification.id == 1).one())
        worker = session.query(Worker).filter(Worker.id == 1).one()
        assert worker.identification == identification
        assert identification.path_to_document == str(path)
        assert identification.uuid == uuid4
        assert identification.worker == worker
Ejemplo n.º 24
0
 def get_worker_violations(worker_id: int) -> List[WorkerPenaltyOut]:
     with create_session() as session:
         worker = SecuritiesDAL._get_worker(session, worker_id)
         return [
             WorkerPenaltyOut(created_at=penalty.created_at,
                              data=penalty.violation.data,
                              value=penalty.violation.value,
                              object=penalty.object_of_work.data)
             for penalty in worker.penalties
         ]
Ejemplo n.º 25
0
 def close(request_id: int) -> Awaitable[RequestOut]:
     with create_session() as session:
         request = RequestsDAL._get_request(request_id, session)
         if request.status != RequestStatus.WAITING_FOR_VERIFICATION:
             raise DALError(HTTPStatus.BAD_REQUEST.value)
         if WorkerInRequestStatus.WAITING_FOR_VERIFICATION in map(
                 lambda worker: worker.status, request.workers_in_request):
             raise DALError(HTTPStatus.BAD_REQUEST.value)
         request.status = RequestStatus.CLOSED
         return RequestOut.from_orm(request)  # type: ignore
Ejemplo n.º 26
0
async def test_add_same_request_second_time_will_delete_first(
        request_in, contractor_representative_out
):
    res = await RequestsDAL.add(contractor_representative_out, request_in)
    assert res.id == 2
    assert res.contract_id == 3
    assert res.contractor_id == 1
    with create_session() as session:
        assert not session.query(Request).filter(Request.id == 1).first()
        assert session.query(Request).filter(Request.id == 2).one()
Ejemplo n.º 27
0
async def test_add_worker_to_request(contractor_representative_out):
    res = await RequestsDAL.add_worker_to_request(contractor_representative_out, 1, 1)
    with create_session() as session:
        worker_in_request: WorkerInRequest = session.query(WorkerInRequest).filter(
            WorkerInRequest.id == 1
        ).one()
        worker = session.query(Worker).filter(Worker.id == 1).one()
        assert len(worker.worker_requests) == 1
        assert worker_in_request.request_id == 1
        assert worker_in_request.status == WorkerInRequestStatus.WAITING_FOR_READINESS
Ejemplo n.º 28
0
 def add_penalty(worker_id: int, params: PenaltyIn):
     with create_session() as session:
         worker = SecuritiesDAL._get_worker(session, worker_id)
         penalty = Penalty(violation_id=params.violation_id,
                           worker_id=worker_id,
                           object_of_work_id=params.object_of_work_id,
                           created_at=date.today())
         session.add(penalty)
         session.flush()
         worker.penalty_points = worker.penalty_points + penalty.violation.value
Ejemplo n.º 29
0
def _authenticate_user(username: str, password: str) -> Awaitable[OutUser]:
    message = Message.INCORRECT_USERNAME_OR_PASSWORD.value
    with create_session() as session:
        user: Optional[UserDB] = session.query(UserDB).filter(
            UserDB.username == username
        ).first()
        if user is None:
            raise DALError(HTTPStatus.NOT_FOUND.value, message)
        if _is_password_correct(password, user.password_hash):
            return OutUser.from_orm(user)  # type: ignore
        raise DALError(HTTPStatus.NOT_FOUND.value, message)
Ejemplo n.º 30
0
async def test_reset_worker_in_request_status_if_worker_was_cancelled(
        contractor_representative_out, deny_worker_in
):
    await RequestsDAL.deny_worker(1, 1, deny_worker_in)
    res = await RequestsDAL.reset_worker_in_request_status(1, 1)
    assert res.status == WorkerInRequestStatus.WAITING_FOR_VERIFICATION
    with create_session() as session:
        worker = session.query(WorkerInRequest).filter(WorkerInRequest.id == 1).one()
        assert worker.status == WorkerInRequestStatus.WAITING_FOR_VERIFICATION
        assert not worker.reason_for_rejection
        assert not worker.comment