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)
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()
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}
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}
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()
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}
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}
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}
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}
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}
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}
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}
def test_badge_raises_if_no_uri(): with pytest.raises(httpexceptions.HTTPBadRequest): badge(mock.Mock(params={}))