"""Tests for blacklist words handlers.""" from unittest.mock import MagicMock from owsrequest import flask_request from owsresponse import response import pytest from blacklist_manager.constants import header from blacklist_manager.logic import blacklist_words from blacklist_manager.utils import pagination @pytest.mark.parametrize('term', ['', 'Word']) def test_get_blacklist_words_success( monkeypatch, valid_blacklist_words_response_handler, fixture_pagination, fixture_grass_account, fixture_ok_response, client, term): """Test routes that get list of blacklist words.""" url = '/blacklist-words?q={}'.format(term) fast_patch(monkeypatch, { pagination: dict(get_pagination=fixture_pagination), flask_request: dict( get_grass_headers=fixture_grass_account, verify_grass_access=fixture_ok_response ), blacklist_words: dict( get_blacklist_words=valid_blacklist_words_response_handler) }) result = client.get(url) blacklist_words.get_blacklist_words.assert_called_with( page_offset=fixture_pagination.offset, page_limit=fixture_pagination.limit, term=term) assert result.status_code == 200 @pytest.mark.parametrize('term', ['', 'Word']) def test_get_blacklist_words_failure(monkeypatch, client, term): """GET /blacklist-words handler failure.""" monkeypatch.setattr( blacklist_words, 'get_blacklist_words', value=MagicMock(return_value=response.create_fatal_response())) url = '/blacklist-words?q={}'.format(term) result = client.get(url) assert result.status_code == 500 assert blacklist_words.get_blacklist_words.called @pytest.mark.parametrize('term', ['', 'Word']) def test_get_blacklist_words_incomplete_grass_header( client, grass_headers, term): """POST /blacklist-words handler grass access failure.""" del grass_headers[header.GRASS_ACCOUNT_TYPE] url = '/blacklist-words?q={}'.format(term) result = client.get(url, headers=grass_headers) assert result.status_code == 400 def fast_patch(monkeypatch, options): """Multiple method monkeypatching helper. Args: monkeypatch (pytest.monkeypatch.monkeypatch): monkeypatch object options (dict): describing the module, attributes, return values Usage: fast_patch(monkeypatch, { module1: dict(attribute1=value1, attribute2=value2), module2: dict(attribute3=value3, attribute4=value4), }) """ for module, attributes in options.items(): for attribute, fixture in attributes.items(): monkeypatch.setattr( module, attribute, MagicMock(return_value=fixture))