299 lines
13 KiB
Python
299 lines
13 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
|
import pytest
|
|
import os
|
|
import pickle
|
|
import re
|
|
import time
|
|
import datetime
|
|
|
|
from quixote import cleanup
|
|
|
|
import wcs.api # workaround against circular dependencies :/
|
|
from wcs.qommon.form import FileSizeWidget
|
|
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration
|
|
from wcs.qommon.misc import (simplify, json_loads, parse_isotime, format_time,
|
|
date_format, get_as_datetime)
|
|
from wcs.admin.settings import FileTypesDirectory
|
|
from wcs.scripts import Script
|
|
from wcs.qommon import evalutils
|
|
from wcs.qommon.http_request import HTTPRequest
|
|
from wcs.qommon.backoffice.listing import pagination_links
|
|
from wcs.qommon.emails import email as send_email, docutils
|
|
|
|
from utilities import get_app, create_temporary_pub, clean_temporary_pub
|
|
|
|
def setup_module(module):
|
|
cleanup()
|
|
|
|
def teardown_module(module):
|
|
clean_temporary_pub()
|
|
|
|
def test_parse_file_size():
|
|
assert FileSizeWidget.parse_file_size('17') == 17
|
|
assert FileSizeWidget.parse_file_size('17o') == 17
|
|
assert FileSizeWidget.parse_file_size('17 K') == 17*10**3
|
|
assert FileSizeWidget.parse_file_size('17 M') == 17*10**6
|
|
assert FileSizeWidget.parse_file_size('17 Mo') == 17*10**6
|
|
assert FileSizeWidget.parse_file_size('17 MB') == 17*10**6
|
|
assert FileSizeWidget.parse_file_size('17 Kio') == 17*2**10
|
|
assert FileSizeWidget.parse_file_size('17 Mio') == 17*2**20
|
|
assert FileSizeWidget.parse_file_size('17K') == 17*10**3
|
|
assert FileSizeWidget.parse_file_size('17 K') == 17*10**3
|
|
assert FileSizeWidget.parse_file_size(' 17 K ') == 17*10**3
|
|
|
|
def test_parse_invalid_file_size():
|
|
for test_value in ('17i', 'hello', '0.4K', '2G'):
|
|
with pytest.raises(ValueError):
|
|
FileSizeWidget.parse_file_size(test_value)
|
|
|
|
def test_humantime():
|
|
for x in range(3, 100000, 13):
|
|
assert humanduration2seconds(seconds2humanduration(x)) == x
|
|
|
|
def test_parse_mimetypes():
|
|
assert FileTypesDirectory.parse_mimetypes('application/pdf') == ['application/pdf']
|
|
assert FileTypesDirectory.parse_mimetypes('.pdf') == ['application/pdf']
|
|
assert FileTypesDirectory.parse_mimetypes('.pdf, .odt') == [
|
|
'application/pdf', 'application/vnd.oasis.opendocument.text']
|
|
|
|
def test_format_mimetypes():
|
|
assert FileTypesDirectory.format_mimetypes(['application/pdf']) == \
|
|
'application/pdf (.pdf)'
|
|
assert FileTypesDirectory.format_mimetypes(['application/pdf', 'text/rtf']) == \
|
|
'application/pdf (.pdf), text/rtf'
|
|
assert FileTypesDirectory.format_mimetypes(['application/pdf', 'application/msword']) == \
|
|
'application/pdf (.pdf), application/msword (.doc)'
|
|
assert FileTypesDirectory.format_mimetypes(['application/pdf',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'application/msword']) == \
|
|
'application/pdf (.pdf), '\
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document (.docx)'\
|
|
'...'
|
|
|
|
def test_simplify_unchanged():
|
|
assert simplify('test') == 'test'
|
|
assert simplify('another-test') == 'another-test'
|
|
assert simplify('another_test', '_') == 'another_test'
|
|
|
|
def test_simplify_space():
|
|
assert simplify('test again') == 'test-again'
|
|
assert simplify(' test again ') == 'test-again'
|
|
assert simplify('test again', '_') == 'test_again'
|
|
assert simplify(' test again ', '_') == 'test_again'
|
|
|
|
def test_simplify_apostrophes():
|
|
assert simplify('test\'again') == 'test-again'
|
|
assert simplify('test\'\'\'again') == 'test-again'
|
|
|
|
def test_simplify_accented():
|
|
assert simplify(u'cliché') == 'cliche'
|
|
assert simplify(u'cliché'.encode('iso-8859-1')) == 'cliche'
|
|
|
|
def test_simplify_remove():
|
|
assert simplify('this is: (a) "test"') == 'this-is-a-test'
|
|
assert simplify('a test; again?') == 'a-test-again'
|
|
|
|
def test_simplify_mix():
|
|
assert simplify(u' this is: (a) "cliché" ') == 'this-is-a-cliche'
|
|
assert simplify(u' À "cliché"; again? ') == 'a-cliche-again'
|
|
|
|
def test_json_str_decoder():
|
|
json_str = json.dumps({
|
|
'lst': [{'a': 'b'}, 1, 2],
|
|
'bla': u'éléphant'
|
|
})
|
|
assert type(json.loads(json_str).keys()[0]) is unicode
|
|
assert type(json.loads(json_str)['lst'][0]['a']) is unicode
|
|
|
|
assert type(json_loads(json_str).keys()[0]) is str
|
|
assert type(json_loads(json_str)['lst'][0]['a']) is str
|
|
assert type(json_loads(json_str)['bla']) is str
|
|
assert json_loads(json_str)['bla'] == u'éléphant'.encode('utf-8')
|
|
|
|
def test_format_time():
|
|
assert format_time(None, '%(month_name)s') == '?'
|
|
assert format_time(1500000000, '%(month_name)s') == 'July'
|
|
assert format_time(1500000000, '%(month_name)s', gmtime=True) == 'July'
|
|
assert format_time(1500000000, '%(hour)s') == '4'
|
|
assert format_time(1500000000, '%(hour)s', gmtime=True) == '2'
|
|
assert format_time((2016, 8), '%(month)s') == '8'
|
|
assert format_time((2016, 8, 2), '%(month)s') == '8'
|
|
assert format_time(time.localtime(1500000000,), '%(month)s') == '7'
|
|
assert format_time(time.localtime(1500000000,), '%(weekday_name)s') == 'Friday'
|
|
|
|
def test_parse_isotime():
|
|
assert 1420107019 == parse_isotime('2015-01-01T10:10:19Z')
|
|
with pytest.raises(ValueError):
|
|
parse_isotime('2015-01-01T10:10:19')
|
|
with pytest.raises(ValueError):
|
|
parse_isotime('2015-01-0110:10:19Z')
|
|
|
|
def test_script_substitution_variable():
|
|
pub = create_temporary_pub()
|
|
pub.substitutions.feed(pub)
|
|
pub.substitutions.feed(Script)
|
|
variables = pub.substitutions.get_context_variables()
|
|
with pytest.raises(AttributeError):
|
|
assert variables['script'].hello_world()
|
|
|
|
os.mkdir(os.path.join(pub.app_dir, 'scripts'))
|
|
fd = open(os.path.join(pub.app_dir, 'scripts', 'hello_world.py'), 'w')
|
|
fd.write('"""docstring"""\nresult = "hello world"')
|
|
fd.close()
|
|
assert variables['script'].hello_world() == 'hello world'
|
|
|
|
assert Script('hello_world').__doc__ == 'docstring'
|
|
|
|
os.mkdir(os.path.join(pub.APP_DIR, 'scripts'))
|
|
fd = open(os.path.join(pub.APP_DIR, 'scripts', 'hello_world.py'), 'w')
|
|
fd.write('result = "hello global world"')
|
|
fd.close()
|
|
assert variables['script'].hello_world() == 'hello world'
|
|
|
|
os.unlink(os.path.join(pub.app_dir, 'scripts', 'hello_world.py'))
|
|
assert variables['script'].hello_world() == 'hello global world'
|
|
|
|
fd = open(os.path.join(pub.app_dir, 'scripts', 'hello_world.py'), 'w')
|
|
fd.write('result = site_url')
|
|
fd.close()
|
|
assert variables['script'].hello_world() == 'http://example.net'
|
|
|
|
def test_default_charset():
|
|
pub = create_temporary_pub()
|
|
resp = get_app(pub).get('/')
|
|
assert 'utf-8' in resp.headers['Content-Type']
|
|
path = os.path.join(pub.app_dir, 'config.pck')
|
|
with open(path, 'w') as f:
|
|
pickle.dump({}, f)
|
|
resp = get_app(pub).get('/')
|
|
assert 'iso-8859-15' in resp.headers['Content-Type']
|
|
os.unlink(path)
|
|
resp = get_app(pub).get('/')
|
|
assert 'utf-8' in resp.headers['Content-Type']
|
|
|
|
|
|
def test_age_in_years():
|
|
create_temporary_pub()
|
|
assert evalutils.age_in_years('2000-01-01', '2016-05-26') == 16
|
|
assert evalutils.age_in_years(datetime.date(2000, 1, 1), '2016-05-26') == 16
|
|
assert evalutils.age_in_years(time.struct_time((2000, 1, 1, 0, 0, 0, 0, 0, 0)),
|
|
'2016-05-26') == 16
|
|
assert evalutils.age_in_years('2000-06-01', '2016-05-26') == 15
|
|
assert evalutils.age_in_years('2000-02-29', '2016-02-29') == 16
|
|
assert evalutils.age_in_years('2000-02-28', '2016-02-29') == 16
|
|
assert evalutils.age_in_years('2000-03-01', '2016-02-29') == 15
|
|
assert evalutils.age_in_years('2000-01-01') >= 16
|
|
|
|
|
|
def test_age_in_years_and_months():
|
|
create_temporary_pub()
|
|
assert evalutils.age_in_years_and_months('2000-01-01', '2016-05-26') == (16, 4)
|
|
assert evalutils.age_in_years_and_months('2000-01-01', datetime.date(2016, 5, 26)) == (16, 4)
|
|
assert evalutils.age_in_years_and_months(datetime.date(2000, 1, 1), '2016-05-26') == (16, 4)
|
|
assert evalutils.age_in_years_and_months(time.struct_time((2000, 1, 1, 0, 0, 0, 0, 0, 0)),
|
|
'2016-05-26') == (16, 4)
|
|
assert evalutils.age_in_years_and_months('2000-06-01', '2016-05-26') == (15, 11)
|
|
assert evalutils.age_in_years_and_months('2000-02-29', '2016-02-29') == (16, 0)
|
|
assert evalutils.age_in_years_and_months('2000-02-28', '2016-02-29') == (16, 0)
|
|
assert evalutils.age_in_years_and_months('2000-03-01', '2016-02-29') == (15, 11)
|
|
assert evalutils.age_in_years_and_months('2000-01-01') >= (16, 0)
|
|
|
|
|
|
def test_age_in_days():
|
|
assert evalutils.age_in_days('2000-01-01', '2001-01-01') == 366
|
|
assert evalutils.age_in_days(datetime.date(2000, 1, 1), '2001-01-01') == 366
|
|
assert (evalutils.age_in_days(time.struct_time((2000, 1, 1, 0, 0, 0, 0, 0, 0)), '2001-01-01') ==
|
|
366)
|
|
assert evalutils.age_in_days('2001-01-01', '2002-01-01') == 365
|
|
|
|
|
|
def test_age_in_seconds():
|
|
assert evalutils.age_in_seconds('2000-01-01 00:00', '2000-01-01 01:00') == 3600
|
|
assert evalutils.age_in_seconds('2000-01-01', '2000-01-01 01:00') == 3600
|
|
assert evalutils.age_in_seconds(datetime.date(2000, 1, 1), '2000-01-01 01:00') == 3600
|
|
assert evalutils.age_in_seconds(time.struct_time((2000, 1, 1, 0, 0, 0, 0, 0, 0)),
|
|
'2000-01-01 01:00') == 3600
|
|
|
|
def test_date_format():
|
|
pub = create_temporary_pub()
|
|
pub.cfg['language'] = {}
|
|
pub.write_cfg()
|
|
orig_environ = os.environ.copy()
|
|
try:
|
|
if 'LC_TIME' in os.environ:
|
|
del os.environ['LC_TIME']
|
|
if 'LC_ALL' in os.environ:
|
|
del os.environ['LC_ALL']
|
|
assert date_format() == '%Y-%m-%d'
|
|
os.environ['LC_ALL'] = 'nl_BE.UTF-8'
|
|
assert date_format() == '%Y-%m-%d'
|
|
os.environ['LC_ALL'] = 'fr_BE.UTF-8'
|
|
assert date_format() == '%d/%m/%Y'
|
|
os.environ['LC_TIME'] = 'nl_BE.UTF-8'
|
|
assert date_format() == '%Y-%m-%d'
|
|
pub.cfg['language'] = {'language': 'fr'}
|
|
assert date_format() == '%d/%m/%Y'
|
|
finally:
|
|
os.environ = orig_environ
|
|
|
|
def test_get_as_datetime():
|
|
pub = create_temporary_pub()
|
|
datetime_value = datetime.datetime(2017, 4, 25, 12, 0)
|
|
assert get_as_datetime('2017-04-25 12:00') == datetime_value
|
|
assert get_as_datetime('2017-04-25 12:00:00') == datetime_value
|
|
assert get_as_datetime('2017-04-25T12:00:00Z') == datetime_value
|
|
assert get_as_datetime('2017-04-25T12:00:00') == datetime_value
|
|
assert get_as_datetime('25/04/2017 12:00') == datetime_value
|
|
|
|
def test_etld():
|
|
pub = create_temporary_pub()
|
|
pub.load_effective_tld_names()
|
|
assert pub.etld.parse('www.example.net') == ('www.example', 'net')
|
|
assert pub.etld.parse('www.example.rhcloud.com') == ('www.example', 'rhcloud.com')
|
|
|
|
def test_pagination():
|
|
pub = create_temporary_pub()
|
|
req = HTTPRequest(None, {'SERVER_NAME': 'example.net', 'SCRIPT_NAME': ''})
|
|
req.response.filter = {}
|
|
pub.form = {'ajax': 'true'}
|
|
pub._set_request(req)
|
|
|
|
def get_texts(s):
|
|
return [x for x in re.findall(r'>(.*?)<', str(s)) if x.strip()]
|
|
|
|
assert get_texts(pagination_links(0, 10, 0)) == [
|
|
'1', '(0-0/0)', 'Per page: ', '10']
|
|
assert get_texts(pagination_links(0, 10, 10)) == [
|
|
'1', '(1-10/10)', 'Per page: ', '10']
|
|
assert get_texts(pagination_links(0, 10, 20)) == [
|
|
'1', '2', '(1-10/20)', 'Per page: ', '10', '20']
|
|
assert get_texts(pagination_links(10, 10, 20)) == [
|
|
'1', '2', '(11-20/20)', 'Per page: ', '10', '20']
|
|
assert get_texts(pagination_links(10, 10, 50)) == [
|
|
'1', '2', '3', '4', '5', '(11-20/50)', 'Per page: ', '10', '20', '50']
|
|
assert get_texts(pagination_links(10, 10, 500)) == [
|
|
'1', '2', '3', '4', '5', '6', '7', '…', '50', '(11-20/500)', 'Per page: ', '10', '20', '50', '100']
|
|
assert get_texts(pagination_links(100, 10, 500)) == [
|
|
'1', '…', '8', '9', '10', '11', '12', '13', '14', '…', '50', '(101-110/500)', 'Per page: ', '10', '20', '50', '100']
|
|
assert get_texts(pagination_links(100, 20, 500)) == [
|
|
'1', '…', '3', '4', '5', '6', '7', '8', '9', '…', '25', '(101-120/500)', 'Per page: ', '10', '20', '50', '100']
|
|
|
|
def test_email_signature_plain(emails):
|
|
pub = create_temporary_pub()
|
|
pub.cfg['emails'] = {'footer': 'Footer\nText'}
|
|
send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
|
|
assert not emails.emails['test']['msg'].is_multipart()
|
|
assert 'Footer\nText' in emails.emails['test']['msg'].get_payload()
|
|
|
|
@pytest.mark.skipif('docutils is None')
|
|
def test_email_signature_rst(emails):
|
|
pub = create_temporary_pub()
|
|
pub.cfg['emails'] = {'footer': 'Footer\nText'}
|
|
send_email('test', mail_body='Hello', email_rcpt='test@localhost')
|
|
assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
|
|
assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
|
|
assert 'Footer\nText' in emails.emails['test']['msg'].get_payload()[0].get_payload()
|
|
assert '>Footer</div>' in emails.emails['test']['msg'].get_payload()[1].get_payload()
|