Example #1
0
def test_present_with_unknown_error_message():
    response = Response(error=UnknownErrorMessage())
    presenter = HttpPresenter()
    with pytest.raises(HTTPException) as error:
        presenter.present(response)
        assert error.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        assert error.detail == response.error.dict()
Example #2
0
 def execute(self, _: Message, presenter: Presenter):
     response = Response()
     try:
         response.body = DownloadListBodyResponse(
             downloads=self.projection.list())
     except HasturError as error:
         self.logger.exception(error)
         response.error = UnknownErrorMessage()
     presenter.present(response)
Example #3
0
def test_download_list_with_projection_fail():
    error = HasturError()
    projection, presenter = Mock(**{"list.side_effect": error}), Mock()
    handler = DownloadList(projection)
    handler.execute(Mock(), presenter)

    projection.list.assert_called_once()
    presenter.present.assert_called_once()
    assert presenter.present.call_args.args[0] == Response(
        error=UnknownErrorMessage())
Example #4
0
def test_set_download_offline_with_unknown_error():
    id_ = uuid4()
    manager, presenter, command = (
        Mock(**{"load.side_effect": HasturError()}),
        Mock(),
        SetDownloadOfflineCommand(id_=id_, size=1000, filename="foo.avi"),
    )

    SetDownloadOffline(manager).execute(command, presenter)

    manager.load.assert_called_once_with(id_, Download)
    manager.save_and_dispatch.assert_not_called()
    presenter.present.assert_called_once_with(Response(error=UnknownErrorMessage()))
Example #5
0
 def execute(self, message: Message, presenter: Presenter):
     message = cast(SetDownloadOfflineCommand, message)
     response = Response()
     try:
         download: Download = cast(Download,
                                   self.manager.load(message.id_, Download))
         download.set_offline()
         self.manager.save_and_dispatch([download])
     except StreamNotFoundError:
         response.error = UnknownDownload()
     except HasturError as error:
         self.logger.exception(error)
         response.error = UnknownErrorMessage()
     presenter.present(response)
Example #6
0
def test_add_new_url_when_save_fail():
    error = EventStoreError(URL)
    manager, presenter, locker, command = (
        Mock(**{"save_and_dispatch.side_effect": error}),
        Mock(),
        Mock(),
        AddNewUrlCommand(url=URL),
    )

    AddNewUrl(manager, locker).execute(command, presenter)

    locker.lock.assert_called_once_with(command.url)
    manager.save_and_dispatch.assert_called_once()
    presenter.present.assert_called_once()

    assert presenter.present.call_args.args[0] == Response(
        error=UnknownErrorMessage())
Example #7
0
 def execute(self, message: Message, presenter: Presenter):
     message = cast(AddNewUrlCommand, message)
     response = Response()
     try:
         self.locker.lock(message.url)
         download = Download(
             uuid4(),
             init_payload=DownloadCreatedEvent.Payload(
                 url=message.url, status=DownloadStatus.NEW),
         )
         self.manager.save_and_dispatch([download])
     except AlreadyLockedError:
         response.error = UrlAlreadyRegistered()
     except HasturError as error:
         self.logger.exception(error)
         response.error = UnknownErrorMessage()
     else:
         response.body = AddNewUrlBodyResponse(
             download_id=download.get_id())
     presenter.present(response)