|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# This file is part of Invenio. |
| 4 | +# Copyright (C) 2016-2018 CERN. |
| 5 | +# |
| 6 | +# Invenio is free software; you can redistribute it and/or modify it |
| 7 | +# under the terms of the MIT License; see LICENSE file for more details. |
| 8 | +"""Tests for patching records.""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +from flask import Flask, url_for |
| 12 | +from flask_login import AnonymousUserMixin |
| 13 | +import flask_security |
| 14 | +from invenio_records_rest.views import RecordsListResource |
| 15 | +from werkzeug.test import EnvironBuilder |
| 16 | +from werkzeug.wrappers import Request |
| 17 | + |
| 18 | +class DummyUser(AnonymousUserMixin): |
| 19 | + is_authenticated = False |
| 20 | + id = None |
| 21 | + |
| 22 | +class DummySearch: |
| 23 | + def with_preference_param(self): |
| 24 | + return self |
| 25 | + def params(self, **kwargs): |
| 26 | + return self |
| 27 | + def to_dict(self): |
| 28 | + return {"sort": [{"control_number": {"order": "desc"}}]} |
| 29 | + def __getitem__(self, key): |
| 30 | + return self |
| 31 | + def execute(self): |
| 32 | + class Result: |
| 33 | + hits = type('hits', (), {'total': 0})() |
| 34 | + def to_dict(self): |
| 35 | + return {} |
| 36 | + return Result() |
| 37 | + |
| 38 | +def make_request(app, query_string=None): |
| 39 | + builder = EnvironBuilder(method='GET', query_string=query_string) |
| 40 | + env = builder.get_environ() |
| 41 | + req = Request(env) |
| 42 | + return req |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def resource_with_dummy_search(app): |
| 46 | + def dummy_search_factory(self, search): |
| 47 | + return search, {} |
| 48 | + |
| 49 | + return RecordsListResource( |
| 50 | + minter_name="recid", |
| 51 | + pid_type="recid", |
| 52 | + pid_fetcher="recid", |
| 53 | + read_permission_factory=None, |
| 54 | + create_permission_factory=None, |
| 55 | + list_permission_factory=None, |
| 56 | + search_class=DummySearch, |
| 57 | + record_serializers={}, |
| 58 | + record_loaders=None, |
| 59 | + search_serializers={'application/json': lambda *a, **k: {}}, |
| 60 | + default_media_type='application/json', |
| 61 | + max_result_window=10000, |
| 62 | + search_factory=dummy_search_factory, |
| 63 | + item_links_factory=None, |
| 64 | + record_class=None, |
| 65 | + indexer_class=None |
| 66 | + ) |
| 67 | + |
| 68 | +def test_format_html_redirect(monkeypatch, app, resource_with_dummy_search): |
| 69 | + # Should redirect when format=html is specified |
| 70 | + with app.test_request_context('/?format=html'): |
| 71 | + monkeypatch.setattr(flask_security, "current_user", DummyUser()) |
| 72 | + monkeypatch.setattr('flask.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 73 | + monkeypatch.setattr('invenio_records_rest.views.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 74 | + monkeypatch.setattr('invenio_accounts.models.User', type('User', (), {'query': type('query', (), {'get': staticmethod(lambda x: type('U', (), {'email': 'dummy@example.com'})())})()}) ) |
| 75 | + resp = resource_with_dummy_search.get() |
| 76 | + assert resp.status_code == 302 |
| 77 | + assert 'search' in resp.location |
| 78 | + |
| 79 | +def test_format_rss(monkeypatch, app, resource_with_dummy_search): |
| 80 | + # Should NOT redirect when format=rss is specified |
| 81 | + with app.test_request_context('/?format=rss&q=test'): |
| 82 | + monkeypatch.setattr(flask_security, "current_user", DummyUser()) |
| 83 | + monkeypatch.setattr('flask.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 84 | + monkeypatch.setattr('invenio_records_rest.views.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 85 | + monkeypatch.setattr('invenio_accounts.models.User', type('User', (), {'query': type('query', (), {'get': staticmethod(lambda x: type('U', (), {'email': 'dummy@example.com'})())})()}) ) |
| 86 | + resp = resource_with_dummy_search.get() |
| 87 | + # Should return a dict as search result, not a redirect |
| 88 | + assert isinstance(resp, dict) |
| 89 | + |
| 90 | +def test_format_multiple(monkeypatch, app, resource_with_dummy_search): |
| 91 | + # Should prioritize html when both format=html&format=rss are specified |
| 92 | + with app.test_request_context('/?format=html&format=rss'): |
| 93 | + monkeypatch.setattr('flask_security.current_user', DummyUser()) |
| 94 | + monkeypatch.setattr('flask.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 95 | + monkeypatch.setattr('invenio_records_rest.views.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 96 | + monkeypatch.setattr('invenio_accounts.models.User', type('User', (), {'query': type('query', (), {'get': staticmethod(lambda x: type('U', (), {'email': 'dummy@example.com'})())})()}) ) |
| 97 | + resp = resource_with_dummy_search.get() |
| 98 | + assert resp.status_code == 302 |
| 99 | + assert 'search' in resp.location |
| 100 | + |
| 101 | +def test_no_format_no_query(monkeypatch, app, resource_with_dummy_search): |
| 102 | + # Should redirect when neither format nor q is specified |
| 103 | + with app.test_request_context('/'): |
| 104 | + monkeypatch.setattr('flask_security.current_user', DummyUser()) |
| 105 | + monkeypatch.setattr('flask.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 106 | + monkeypatch.setattr('invenio_records_rest.views.url_for', lambda endpoint, **kwargs: '/dummy_url') |
| 107 | + monkeypatch.setattr('invenio_accounts.models.User', type('User', (), {'query': type('query', (), {'get': staticmethod(lambda x: type('U', (), {'email': 'dummy@example.com'})())})()}) ) |
| 108 | + resp = resource_with_dummy_search.get() |
| 109 | + assert resp.status_code == 302 |
| 110 | + assert 'search' in resp.location |
| 111 | + |
| 112 | +# def test_redirect_to_search_sets_format_html(app): |
| 113 | + # Should always set format=html in redirect URL, even if other formats are specified |
| 114 | + from invenio_records_rest.views import redirect_to_search |
| 115 | + with app.test_request_context('/?format=rss&q=test&foo=bar'): |
| 116 | + resp = redirect_to_search(page=2, size=50) |
| 117 | + assert resp.status_code == 302 |
| 118 | + location = resp.location |
| 119 | + assert 'format=html' in location |
| 120 | + assert 'page=2' in location |
| 121 | + assert 'size=50' in location |
| 122 | + assert 'foo=bar' in location |
| 123 | + assert 'q=test' in location |
| 124 | + assert location.startswith('http') |
| 125 | + |
| 126 | + |
| 127 | +def test_redirect_to_search_no_format(app): |
| 128 | + # Should add format=html if no format is specified, and preserve other parameters |
| 129 | + from invenio_records_rest.views import redirect_to_search |
| 130 | + with app.test_request_context('/?q=xyz'): |
| 131 | + resp = redirect_to_search(page=3, size=75) |
| 132 | + assert resp.status_code == 302 |
| 133 | + location = resp.location |
| 134 | + assert 'format=html' in location |
| 135 | + assert 'q=xyz' in location |
| 136 | + assert 'page=3' in location |
| 137 | + assert 'size=75' in location |
| 138 | + |
| 139 | +def test_redirect_to_search_page_size_falsy(app): |
| 140 | + # Should not include page or size in URL if they are None, but should include format=html and other params |
| 141 | + from invenio_records_rest.views import redirect_to_search |
| 142 | + with app.test_request_context('/?format=xml&q=abc'): |
| 143 | + resp = redirect_to_search(page=None, size=None) |
| 144 | + location = resp.location |
| 145 | + assert 'page=None' not in location |
| 146 | + assert 'size=None' not in location |
| 147 | + assert 'format=html' in location |
| 148 | + assert 'q=abc' in location |
| 149 | + |
| 150 | +def test_redirect_to_search_exclude_keys(app): |
| 151 | + # Should exclude specific keys (page_no, list_view_num, log_term, lang) from the redirect URL, but include others |
| 152 | + from invenio_records_rest.views import redirect_to_search |
| 153 | + with app.test_request_context('/?page_no=5&list_view_num=99&log_term=foo&lang=ja&q=zzz'): |
| 154 | + resp = redirect_to_search(page=1, size=20) |
| 155 | + location = resp.location |
| 156 | + # Excluded keys should not be in the URL |
| 157 | + assert 'page_no=' not in location |
| 158 | + assert 'list_view_num=' not in location |
| 159 | + assert 'log_term=' not in location |
| 160 | + assert 'lang=' not in location |
| 161 | + # Other parameters should be present |
| 162 | + assert 'q=zzz' in location |
| 163 | + assert 'page=1' in location |
| 164 | + assert 'size=20' in location |
| 165 | + assert 'format=html' in location |
| 166 | + |
0 commit comments