# -*- coding: utf-8 -*- # 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 . from webtest import Upload import pytest from django.core.urlresolvers import reverse from django.utils.six.moves.urllib import parse as urlparse try: import magic except ImportError: magic = None from fargo.fargo.models import UserDocument from test_manager import login pytestmark = pytest.mark.django_db def test_unlogged(app): assert urlparse.urlparse(app.get('/', status=302).location).path == '/login/' def test_upload(app, john_doe): login(app, user=john_doe) response1 = app.get('/') form = response1.form form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf') response2 = form.submit() assert response2['Location'] in ['/', 'http://testserver/'] response2 = response2.follow() assert 'monfichier.pdf' in response2.text if magic is not None: assert UserDocument.objects.get(filename='monfichier.pdf').document.mime_type == 'text/plain' assert ' mime-text ' in response2.text assert ' mime-text-plain' in response2.text UserDocument.objects.all().delete() response1 = app.get('/') form = response1.form form['content'] = Upload('monfichier.pdf', b'%PDF-1.4 ...', 'application/pdf') response2 = form.submit().follow() assert 'monfichier.pdf' in response2.text assert u'12 bytes' in response2.text if magic is not None: assert UserDocument.objects.get(filename='monfichier.pdf').document.mime_type == 'application/pdf' assert ' mime-application ' in response2.text assert ' mime-application-pdf' in response2.text 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 form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf') response2 = form.submit() assert response2.status_code == 200 assert 'Uploaded file is too big' in response2.text 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 form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf') response2 = form.submit().follow() assert 'monfichier.pdf' in response2.text response1 = app.get('/') form = response1.forms['send-file'] form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf') response2 = form.submit() assert response2.status_code == 200 assert 'Your document box is full (limit is 4)' in response2.text 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']