changement vers google drive storage pour les contrats.
CI - Security, Lint & Tests / validate (push) Failing after 1m15s
CI - Security, Lint & Tests / validate (push) Failing after 1m15s
Tentative d'obtenir un token de connection échoue
This commit is contained in:
@@ -14,13 +14,25 @@ import io
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from pypdf import PdfWriter as _Pw
|
||||
|
||||
from app.extensions import db
|
||||
from app.models import Contract
|
||||
|
||||
PDF_BYTES = b'%PDF-1.7\n1 0 obj\n<<>>\nendobj\ntrailer\n%%EOF\n'
|
||||
|
||||
def _valid_pdf_bytes():
|
||||
w = _Pw()
|
||||
w.add_blank_page(612, 792)
|
||||
buf = io.BytesIO()
|
||||
w.write(buf)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
_PDF_BYTES = _valid_pdf_bytes()
|
||||
NOT_PDF_BYTES = b'MZ\x90\x00\x03\x00\x00\x00' # a Windows executable header
|
||||
|
||||
PDF_HEADER_ONLY_BYTES = b'%PDF-1.7\n' + b'MZ' * 100 # PDF header over non-PDF content
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def contract_for(app, tmp_path):
|
||||
@@ -29,7 +41,7 @@ def contract_for(app, tmp_path):
|
||||
def _make(player_id, uploader_id):
|
||||
stored = 'deadbeef.pdf'
|
||||
path = tmp_path / stored
|
||||
path.write_bytes(PDF_BYTES)
|
||||
path.write_bytes(_PDF_BYTES)
|
||||
with app.app_context():
|
||||
contract = Contract(
|
||||
player_id=player_id,
|
||||
@@ -89,7 +101,7 @@ class TestSignedUpload:
|
||||
|
||||
client.post(
|
||||
f'/users/contracts/{contract_id}/upload_signed',
|
||||
data={'signed_file': (io.BytesIO(PDF_BYTES), 'signed.pdf')},
|
||||
data={'signed_file': (io.BytesIO(_PDF_BYTES), 'signed.pdf')},
|
||||
content_type='multipart/form-data',
|
||||
follow_redirects=True,
|
||||
)
|
||||
@@ -110,7 +122,7 @@ class TestSignedUpload:
|
||||
|
||||
client.post(
|
||||
f'/users/contracts/{contract_id}/upload_signed',
|
||||
data={'signed_file': (io.BytesIO(PDF_BYTES), 'signed.pdf')},
|
||||
data={'signed_file': (io.BytesIO(_PDF_BYTES), 'signed.pdf')},
|
||||
content_type='multipart/form-data',
|
||||
follow_redirects=True,
|
||||
)
|
||||
@@ -118,6 +130,31 @@ class TestSignedUpload:
|
||||
with app.app_context():
|
||||
assert db.session.get(Contract, contract_id).status != 'signed'
|
||||
|
||||
def test_signed_upload_with_google_drive(
|
||||
self, app, client, as_role, make_user, contract_for, monkeypatch
|
||||
):
|
||||
"""Signing with Drive backend stores gdrive://<id> in signed_file_path."""
|
||||
monkeypatch.setenv('DOCUMENT_STORAGE_BACKEND', 'google_drive')
|
||||
monkeypatch.setattr(
|
||||
'app.storage.upload_google_drive_file',
|
||||
lambda **_kwargs: 'drive-signed-456',
|
||||
)
|
||||
player_id = as_role('player')
|
||||
admin_id = make_user('admin')
|
||||
contract_id, directory = contract_for(player_id, admin_id)
|
||||
|
||||
client.post(
|
||||
f'/users/contracts/{contract_id}/upload_signed',
|
||||
data={'signed_file': (io.BytesIO(_PDF_BYTES), 'signed.pdf')},
|
||||
content_type='multipart/form-data',
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
contract = db.session.get(Contract, contract_id)
|
||||
assert contract.status == 'signed'
|
||||
assert contract.signed_file_path == 'gdrive://drive-signed-456'
|
||||
|
||||
|
||||
class TestTheHelper:
|
||||
def test_it_reports_a_missing_file(self, app):
|
||||
@@ -133,16 +170,27 @@ class TestTheHelper:
|
||||
|
||||
from app.routes.users import ALLOWED_CONTRACT_EXTENSIONS, pdf_upload_error
|
||||
|
||||
upload = FileStorage(stream=io.BytesIO(PDF_BYTES), filename='c.pdf')
|
||||
upload = FileStorage(stream=io.BytesIO(_PDF_BYTES), filename='c.pdf')
|
||||
with app.test_request_context('/'):
|
||||
assert pdf_upload_error(upload, ALLOWED_CONTRACT_EXTENSIONS) is None
|
||||
assert upload.stream.read() == PDF_BYTES
|
||||
assert upload.stream.read() == _PDF_BYTES
|
||||
|
||||
def test_a_name_without_a_dot_is_refused(self, app):
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from app.routes.users import ALLOWED_CONTRACT_EXTENSIONS, pdf_upload_error
|
||||
|
||||
upload = FileStorage(stream=io.BytesIO(PDF_BYTES), filename='pdf')
|
||||
upload = FileStorage(stream=io.BytesIO(_PDF_BYTES), filename='pdf')
|
||||
with app.test_request_context('/'):
|
||||
assert pdf_upload_error(upload, ALLOWED_CONTRACT_EXTENSIONS)
|
||||
|
||||
def test_a_header_only_file_is_refused(self, app):
|
||||
"""A stream starting with %PDF- but holding non-PDF data is
|
||||
rejected (the header check is not enough)."""
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from app.routes.users import ALLOWED_CONTRACT_EXTENSIONS, pdf_upload_error
|
||||
|
||||
upload = FileStorage(stream=io.BytesIO(PDF_HEADER_ONLY_BYTES), filename='clever.pdf')
|
||||
with app.test_request_context('/'):
|
||||
assert pdf_upload_error(upload, ALLOWED_CONTRACT_EXTENSIONS)
|
||||
|
||||
Reference in New Issue
Block a user