Beispiel #1
0
        def caller(uri, annotated=True, blocked=False):
            if annotated:
                factories.DocumentURI(uri=uri)
                pyramid_request.db.flush()

            Blocklist.is_blocked.return_value = blocked

            pyramid_request.params["uri"] = uri
            return badge(pyramid_request)
Beispiel #2
0
def test_badge_does_not_search_if_uri_never_annotated(models, pyramid_request, search_run):
    pyramid_request.params['uri'] = 'http://example.com'
    models.Blocklist.is_blocked.return_value = False

    result = badge(pyramid_request)

    assert result == {'total': 0}
    models.Blocklist.is_blocked.assert_not_called()
    search_run.assert_not_called()
Beispiel #3
0
def test_badge_returns_0_if_blocked(models, search_run):
    request = mock.Mock(params={'uri': 'test_uri'})
    models.Blocklist.is_blocked.return_value = True
    search_run.return_value = {'total': 29}

    result = badge(request)

    assert not search_run.called
    assert result == {'total': 0}
Beispiel #4
0
def test_badge_returns_number_from_search(models, search_run):
    request = mock.Mock(params={'uri': 'test_uri'})
    models.Blocklist.is_blocked.return_value = False
    search_run.return_value = mock.Mock(total=29)

    result = badge(request)

    search_run.assert_called_once_with({'uri': 'test_uri', 'limit': 0})
    assert result == {'total': 29}
Beispiel #5
0
def test_badge_returns_0_if_blocked(models, search_run):
    request = mock.Mock(params={'uri': 'test_uri'})
    models.Blocklist.is_blocked.return_value = True
    search_run.return_value = {'total': 29}

    result = badge(request)

    assert not search_run.called
    assert result == {'total': 0}
Beispiel #6
0
def test_badge_returns_number_from_search(models, search_run):
    request = mock.Mock(params={'uri': 'test_uri'})
    models.Blocklist.is_blocked.return_value = False
    search_run.return_value = mock.Mock(total=29)

    result = badge(request)

    search_run.assert_called_once_with({'uri': 'test_uri', 'limit': 0})
    assert result == {'total': 29}
Beispiel #7
0
def test_badge_does_not_search_if_uri_never_annotated(models, pyramid_request,
                                                      search_run):
    pyramid_request.params["uri"] = "http://example.com"
    models.Blocklist.is_blocked.return_value = False

    result = badge(pyramid_request)

    assert result == {"total": 0}
    models.Blocklist.is_blocked.assert_not_called()
    search_run.assert_not_called()
Beispiel #8
0
def test_badge_returns_number_from_search(models, pyramid_request, search_run, mark_uri_as_annotated):
    mark_uri_as_annotated('http://example.com')

    pyramid_request.params['uri'] = 'http://example.com'
    models.Blocklist.is_blocked.return_value = False
    search_run.return_value = mock.Mock(total=29)

    result = badge(pyramid_request)

    search_run.assert_called_once_with({'uri': 'http://example.com', 'limit': 0})
    assert result == {'total': 29}
Beispiel #9
0
def test_badge_returns_0_if_blocked(models, pyramid_request, search_run, mark_uri_as_annotated):
    mark_uri_as_annotated('http://blocked-domain.com')

    pyramid_request.params['uri'] = 'http://blocked-domain.com'
    models.Blocklist.is_blocked.return_value = True
    search_run.return_value = {'total': 29}

    result = badge(pyramid_request)

    models.Blocklist.is_blocked.assert_called_with(mock.ANY, 'http://blocked-domain.com')
    assert not search_run.called
    assert result == {'total': 0}
Beispiel #10
0
def test_badge_returns_0_if_blocked(models, pyramid_request, search_run,
                                    mark_uri_as_annotated):
    mark_uri_as_annotated("http://blocked-domain.com")

    pyramid_request.params["uri"] = "http://blocked-domain.com"
    models.Blocklist.is_blocked.return_value = True
    search_run.return_value = {"total": 29}

    result = badge(pyramid_request)

    models.Blocklist.is_blocked.assert_called_with(
        mock.ANY, "http://blocked-domain.com")
    assert not search_run.called
    assert result == {"total": 0}
Beispiel #11
0
def test_badge_returns_0_if_blocked(models, pyramid_request, search_run,
                                    mark_uri_as_annotated):
    mark_uri_as_annotated('http://blocked-domain.com')

    pyramid_request.params['uri'] = 'http://blocked-domain.com'
    models.Blocklist.is_blocked.return_value = True
    search_run.return_value = {'total': 29}

    result = badge(pyramid_request)

    models.Blocklist.is_blocked.assert_called_with(
        mock.ANY, 'http://blocked-domain.com')
    assert not search_run.called
    assert result == {'total': 0}
Beispiel #12
0
def test_badge_returns_number_from_search(models, pyramid_request, search_run,
                                          mark_uri_as_annotated):
    mark_uri_as_annotated('http://example.com')

    pyramid_request.params['uri'] = 'http://example.com'
    models.Blocklist.is_blocked.return_value = False
    search_run.return_value = mock.Mock(total=29)

    result = badge(pyramid_request)

    search_run.assert_called_once_with({
        'uri': 'http://example.com',
        'limit': 0
    })
    assert result == {'total': 29}
Beispiel #13
0
def test_badge_returns_number_from_search(
    models, pyramid_request, search_run, mark_uri_as_annotated
):
    mark_uri_as_annotated("http://example.com")

    pyramid_request.params["uri"] = "http://example.com"
    models.Blocklist.is_blocked.return_value = False
    search_run.return_value = mock.Mock(total=29)

    result = badge(pyramid_request)

    search_run.assert_called_once_with(
        MultiDict({"uri": "http://example.com", "limit": 0})
    )
    assert result == {"total": 29}
Beispiel #14
0
def test_badge_returns_0_if_blocked(
    models, pyramid_request, search_run, mark_uri_as_annotated
):
    mark_uri_as_annotated("http://blocked-domain.com")

    pyramid_request.params["uri"] = "http://blocked-domain.com"
    models.Blocklist.is_blocked.return_value = True
    search_run.return_value = {"total": 29}

    result = badge(pyramid_request)

    models.Blocklist.is_blocked.assert_called_with(
        mock.ANY, "http://blocked-domain.com"
    )
    assert not search_run.called
    assert result == {"total": 0}
Beispiel #15
0
def test_badge_raises_if_no_uri():
    with pytest.raises(httpexceptions.HTTPBadRequest):
        badge(mock.Mock(params={}))
Beispiel #16
0
def test_badge_raises_if_no_uri():
    with pytest.raises(httpexceptions.HTTPBadRequest):
        badge(mock.Mock(params={}))