Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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()
Beispiel #4
0
def test_present_with_url_already_registered():
    response = Response(error=UrlAlreadyRegistered())
    presenter = HttpPresenter()
    with pytest.raises(HTTPException) as error:
        presenter.present(response)
        assert error.status_code == status.HTTP_400_BAD_REQUEST
        assert error.detail == response.error.dict()
Beispiel #5
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())
Beispiel #6
0
def test_download_list_with_success():
    downloads = []
    projection, presenter = Mock(**{"list.return_value": downloads}), 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(
        body=DownloadListBodyResponse(downloads=downloads))
Beispiel #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)
Beispiel #8
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()))
Beispiel #9
0
def test_update_file_infos_with_unknown_id():
    id_ = uuid4()
    manager, presenter, command = (
        Mock(**{"load.side_effect": StreamNotFoundError(id_)}),
        Mock(),
        UpdateFileInfosCommand(id_=id_, size=1000, filename="foo.avi"),
    )

    UpdateFileInfos(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=UnknownDownload()))
Beispiel #10
0
def test_set_download_offline_success():
    id_ = uuid4()
    download = Mock()
    manager, presenter, command = (
        Mock(**{"load.return_value": download}),
        Mock(),
        SetDownloadOfflineCommand(id_=id_, size=1000, filename="foo.avi"),
    )

    SetDownloadOffline(manager).execute(command, presenter)

    manager.load.assert_called_once_with(id_, Download)
    download.set_offline.assert_called_once()
    manager.save_and_dispatch.assert_called_once_with([download])
    presenter.present.assert_called_once_with(Response())
Beispiel #11
0
def test_update_file_infos_success():
    id_ = uuid4()
    download = Mock()
    manager, presenter, command = (
        Mock(**{"load.return_value": download}),
        Mock(),
        UpdateFileInfosCommand(id_=id_, size=1000, filename="foo.avi"),
    )

    UpdateFileInfos(manager).execute(command, presenter)

    manager.load.assert_called_once_with(id_, Download)
    download.set_infos.assert_called_once_with(1000, "foo.avi")
    download.set_online.assert_called_once()
    manager.save_and_dispatch.assert_called_once_with([download])
    presenter.present.assert_called_once_with(Response())
Beispiel #12
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())
Beispiel #13
0
def test_add_new_url_when_lock_fail():
    error = AlreadyLockedError(URL)
    manager, presenter, locker, command = (
        Mock(),
        Mock(),
        Mock(**{"lock.side_effect": error}),
        AddNewUrlCommand(url=URL),
    )

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

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

    assert presenter.present.call_args.args[0] == Response(
        error=UrlAlreadyRegistered())
Beispiel #14
0
def test_add_new_url_success():
    manager, presenter, locker, command = (
        Mock(),
        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()

    [download] = manager.save_and_dispatch.call_args.args[0]
    assert download.url == command.url
    assert download.status == DownloadStatus.NEW
    assert presenter.present.call_args.args[0] == Response(
        body=AddNewUrlBodyResponse(download_id=download.get_id()))
Beispiel #15
0
def test_present_with_success_response():
    expected_result = Sample(foo_="bar")
    response = Response(body=expected_result)
    presenter = HttpPresenter()
    presenter.present(response)
    assert presenter.result == expected_result