oauth2: add command oauth2-put-document (fixes #22948)

This commit is contained in:
Benjamin Dauvergne 2018-03-31 11:42:36 +02:00
parent 76532ef64a
commit a465f2c176
3 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import os
from django.core.management.base import BaseCommand
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from fargo.utils import make_url
from fargo.fargo.models import Document
from fargo.oauth2.models import OAuth2TempFile, OAuth2Client
class Command(BaseCommand):
help = 'Push documents inside fargo, returns URLs'
def add_arguments(self, parser):
parser.add_argument('--client-id', type=int)
parser.add_argument('redirect_uri')
parser.add_argument('paths', nargs='+')
def handle(self, redirect_uri, paths, client_id, **options):
client = OAuth2Client.objects.get(id=client_id)
for path in paths:
with open(path) as file_object:
filename = os.path.basename(path)
f = ContentFile(file_object.read(), name=filename)
document = Document.objects.get_by_file(f)
oauth2_document = OAuth2TempFile.objects.create(
client=client,
document=document,
filename=filename)
uri = reverse('oauth2-put-document-authorize', args=[oauth2_document.pk])
self.stdout.write('https://localhost:8000' + make_url(uri, redirect_uri=redirect_uri))

BIN
tests/pdf-sample.pdf Normal file

Binary file not shown.

View File

@ -1,3 +1,4 @@
import os
import json
import mock
import pytest
@ -228,3 +229,25 @@ def test_command_create_client(db):
assert client.redirect_uris == 'https://example.com/'
assert client.client_id == 'wtf'
assert client.client_secret == 'whocares'
def test_command_put_document(db, capsys, app, john_doe):
call_command('oauth2-create-client', 'test', 'https://example.com/')
client = OAuth2Client.objects.get()
path = os.path.join(os.path.dirname(__file__), 'pdf-sample.pdf')
redirect_uri = 'https://example.com/'
r = call_command('oauth2-put-document', '--client-id=%s' % client.pk, redirect_uri, path)
out, err = capsys.readouterr()
assert err == ''
url = out.strip()
response = app.get(url).follow()
response.form.set('username', john_doe.username)
response.form.set('password', john_doe.username)
response = response.form.submit().follow()
assert 'pdf-sample.pdf' in response
temp_file = OAuth2TempFile.objects.get()
assert temp_file.uuid in response
response = response.form.submit('accept')
assert response['Location'] == redirect_uri
assert UserDocument.objects.filter(user=john_doe, document=temp_file.document).exists()
assert OAuth2TempFile.objects.count() == 0