Exemple #1
0
def test_fetch_feed_end():
    source = PhabricatorSource("", "", 100)
    with mock_http(MockResponse(200, "", {
            "result": "10",
            "error_code": None
    })):
        assert source.fetch_feed_end() == 10
Exemple #2
0
def test_phabricator_error_throws_exception():
    source = PhabricatorSource("", "", 100)
    with mock_http(
            MockResponse(200, "", {
                "result": None,
                "error_code": "ERR-OOPS"
            })):
        with pytest.raises(PhabricatorException):
            source.fetch_feed_end()
Exemple #3
0
def test_requests_are_authenticated():
    source = PhabricatorSource("http://phabricator.test", "token", 100)
    response_json = {"result": 0, "error_code": None}
    with mock_http(MockResponse(200, "", response_json)) as mock_post:
        source.fetch_feed_end()
        args = mock_post.call_args.args
        assert args[0] == "http://phabricator.test/api/feed.for_email.status"
        kwargs = mock_post.call_args.kwargs
        assert kwargs["data"] == {
            "params": '{"__conduit__": {"token": "token"}}'
        }
Exemple #4
0
def test_fetch_next():
    source = PhabricatorSource("", "", 100)
    with mock_http(
            MockResponse(200, "", {
                "result": '{"data": [], "cursor": {}}',
                "error_code": None
            })) as mock_post:
        assert source.fetch_next(5) == {"data": [], "cursor": {}}
        kwargs = mock_post.call_args.kwargs
        assert (
            kwargs["data"]["params"] ==
            '{"__conduit__": {"token": ""}, "storyLimit": 100, "after": 5}')
Exemple #5
0
def _parse_pipeline(config: ConfigParser, logger: Logger):
    """Provides data-fetching implementations according to our configuration.

    To facilitate easier local development, we have a few different combinations of
    ways to get data (Phabricator, or just a local file) as well as how we want
    to perform at runtime (fetch-and-send emails once, or on a continuous loop).
    Returns a "data source" and "worker" implementation
    """
    dev_source_file = config.get("dev", "file", fallback=None)
    if dev_source_file:
        # Give RunOnceWorker a garbage feed position, since the position doesn't
        # matter when reading from a file.
        return FileSource(
            pathlib.Path(dev_source_file).resolve()), RunOnceWorker(0)

    host = _parse_host(config.get("phabricator", "host"))
    token = config.get("phabricator", "token")
    story_limit = int(config.get("dev", "story_limit", fallback="100"))
    source = PhabricatorSource(host, token, story_limit)

    override_since_key = config.get("dev", "since_key", fallback=None)
    if override_since_key:
        return source, RunOnceWorker(int(override_since_key))

    poll_gap_seconds = int(
        config.get("phabricator", "poll_gap_seconds", fallback="60"))
    return source, PhabricatorWorker(logger, poll_gap_seconds)
Exemple #6
0
def test_phabricator_bad_status_code_throws_exception():
    source = PhabricatorSource("", "", 100)
    with mock_http(MockResponse(500, "", {})):
        with pytest.raises(PhabricatorException):
            source.fetch_feed_end()
Exemple #7
0
def test_http_error_throws_exception():
    source = PhabricatorSource("", "", 100)
    with mock.patch("phabricatoremails.source.requests.post") as mock_post:
        mock_post.side_effect = RequestException()
        with pytest.raises(PhabricatorException):
            source.fetch_feed_end()