/
info_test.py
94 lines (69 loc) · 2.58 KB
/
info_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import pytest
from django.test import RequestFactory
from django.urls import reverse
from mock import MagicMock
from nav.models.profiles import Account
from nav.web.info.images.utils import save_thumbnail
from nav.web.info.room.views import create_csv
from nav.web.info.searchproviders import SearchProvider
#########
# Tests #
#########
def test_search_for_nonascii_characters_should_not_crash(client):
url = reverse('info-search') + '?query=æøå'
response = client.get(url)
assert response.status_code == 200
def test_failing_searchprovider_should_not_crash_search_page(
client, failing_searchprovider
):
url = reverse('info-search') + '?query=Da%20Truf'
response = client.get(url)
assert response.status_code == 200
def test_failures_should_be_mentioned_in_search_page(client, failing_searchprovider):
url = reverse('info-search') + '?query=Da%20Truf'
response = client.get(url)
assert failing_searchprovider in response.content.decode('utf-8')
def test_room_csv_download_should_not_produce_bytestring_representations(admin_account):
factory = RequestFactory()
request = factory.post(
reverse("room-csv"), data={"roomid": "myroom", "rows": "one;two;three\n"}
)
request.account = admin_account
request.session = MagicMock()
response = create_csv(request) # type: django.http.response.HttpResponse
assert not response.content.startswith(b"b'")
def test_save_thumbnail_should_produce_a_file(tmpdir):
"""This is more or less a regression test for the third party library Pillow"""
image = "closet.jpg"
save_thumbnail(
imagename=image,
imagedirectory="tests/functional",
thumb_dir=tmpdir,
)
assert os.path.exists(os.path.join(tmpdir, image))
############
# Fixtures #
############
@pytest.fixture
def failing_searchprovider():
"""
Inserts (into NAV's list of search providers to use) a provider that
raises an exception.
"""
from django.conf import settings
provider = '{module}.{klass}'.format(
module=__name__, klass=FailingSearchProvider.__name__
)
if provider not in settings.SEARCHPROVIDERS:
settings.SEARCHPROVIDERS.append(provider)
yield provider
if provider in settings.SEARCHPROVIDERS:
index = settings.SEARCHPROVIDERS.index(provider)
del settings.SEARCHPROVIDERS[index]
class FailingSearchProvider(SearchProvider):
"""A search provider that only raises exceptions"""
def fetch_results(self):
raise Exception("Riddikulus")