2017-03-06 15:09:35 +01:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-05-15 18:23:48 +02:00
|
|
|
|
# fargo - document box
|
|
|
|
|
# Copyright (C) 2016-2019 Entr'ouvert
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2017-03-06 15:09:35 +01:00
|
|
|
|
|
2017-11-06 11:55:08 +01:00
|
|
|
|
from webtest import Upload
|
2015-08-18 17:27:19 +02:00
|
|
|
|
import pytest
|
|
|
|
|
|
2018-03-29 17:00:50 +02:00
|
|
|
|
from django.core.urlresolvers import reverse
|
2018-04-05 22:21:04 +02:00
|
|
|
|
from django.utils.six.moves.urllib import parse as urlparse
|
2018-03-29 17:00:50 +02:00
|
|
|
|
|
2017-04-12 11:47:08 +02:00
|
|
|
|
try:
|
|
|
|
|
import magic
|
|
|
|
|
except ImportError:
|
|
|
|
|
magic = None
|
|
|
|
|
|
|
|
|
|
from fargo.fargo.models import UserDocument
|
2015-08-18 17:27:19 +02:00
|
|
|
|
|
2016-03-28 03:06:59 +02:00
|
|
|
|
from test_manager import login
|
|
|
|
|
|
2019-01-30 12:09:12 +01:00
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
2015-08-18 17:27:19 +02:00
|
|
|
|
|
2017-11-06 11:55:08 +01:00
|
|
|
|
def test_unlogged(app):
|
2016-01-05 18:02:48 +01:00
|
|
|
|
assert urlparse.urlparse(app.get('/', status=302).location).path == '/login/'
|
2016-03-28 03:06:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_upload(app, john_doe):
|
|
|
|
|
login(app, user=john_doe)
|
|
|
|
|
response1 = app.get('/')
|
|
|
|
|
form = response1.form
|
2018-04-05 22:21:04 +02:00
|
|
|
|
form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf')
|
2019-01-30 12:14:53 +01:00
|
|
|
|
response2 = form.submit()
|
|
|
|
|
assert response2['Location'] in ['/', 'http://testserver/']
|
|
|
|
|
response2 = response2.follow()
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert 'monfichier.pdf' in response2.text
|
2017-04-12 11:47:08 +02:00
|
|
|
|
if magic is not None:
|
|
|
|
|
assert UserDocument.objects.get(filename='monfichier.pdf').document.mime_type == 'text/plain'
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert ' mime-text ' in response2.text
|
|
|
|
|
assert ' mime-text-plain' in response2.text
|
2017-04-12 11:47:08 +02:00
|
|
|
|
|
|
|
|
|
UserDocument.objects.all().delete()
|
|
|
|
|
|
|
|
|
|
response1 = app.get('/')
|
|
|
|
|
form = response1.form
|
2018-04-05 22:21:04 +02:00
|
|
|
|
form['content'] = Upload('monfichier.pdf', b'%PDF-1.4 ...', 'application/pdf')
|
2017-04-12 11:47:08 +02:00
|
|
|
|
response2 = form.submit().follow()
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert 'monfichier.pdf' in response2.text
|
|
|
|
|
assert u'12 bytes' in response2.text
|
2017-04-12 11:47:08 +02:00
|
|
|
|
if magic is not None:
|
|
|
|
|
assert UserDocument.objects.get(filename='monfichier.pdf').document.mime_type == 'application/pdf'
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert ' mime-application ' in response2.text
|
|
|
|
|
assert ' mime-application-pdf' in response2.text
|
2016-03-28 03:28:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_upload_max_size(app, private_settings, john_doe):
|
|
|
|
|
private_settings.FARGO_MAX_DOCUMENT_SIZE = 1
|
|
|
|
|
login(app, user=john_doe)
|
|
|
|
|
response1 = app.get('/')
|
|
|
|
|
form = response1.form
|
2018-04-05 22:21:04 +02:00
|
|
|
|
form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf')
|
2016-03-28 03:28:22 +02:00
|
|
|
|
response2 = form.submit()
|
|
|
|
|
assert response2.status_code == 200
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert 'Uploaded file is too big' in response2.text
|
2016-03-22 16:06:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_upload_max_document_box_size(app, private_settings, john_doe):
|
|
|
|
|
private_settings.FARGO_MAX_DOCUMENT_BOX_SIZE = 4
|
|
|
|
|
login(app, user=john_doe)
|
|
|
|
|
response1 = app.get('/')
|
|
|
|
|
form = response1.form
|
2018-04-05 22:21:04 +02:00
|
|
|
|
form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf')
|
2016-03-22 16:06:44 +01:00
|
|
|
|
response2 = form.submit().follow()
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert 'monfichier.pdf' in response2.text
|
2016-03-22 16:06:44 +01:00
|
|
|
|
response1 = app.get('/')
|
|
|
|
|
form = response1.forms['send-file']
|
2018-04-05 22:21:04 +02:00
|
|
|
|
form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf')
|
2016-03-22 16:06:44 +01:00
|
|
|
|
response2 = form.submit()
|
|
|
|
|
assert response2.status_code == 200
|
2018-04-05 22:21:04 +02:00
|
|
|
|
assert 'Your document box is full (limit is 4)' in response2.text
|
2018-03-29 17:00:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pick(app, private_settings, john_doe, user_doc):
|
|
|
|
|
login(app, user=john_doe)
|
|
|
|
|
|
|
|
|
|
return_url = 'http://client.org/callback/'
|
|
|
|
|
response = app.get(reverse('list_to_pick') + '?pick=' + return_url)
|
|
|
|
|
response = response.forms[0].submit('Pick')
|
|
|
|
|
assert response['Location'].startswith(return_url)
|
|
|
|
|
assert '?url=' in response['Location']
|