Projet

Général

Profil

0001-oauth2-add-command-oauth2-put-document-fixes-22948.patch

Benjamin Dauvergne, 03 avril 2018 10:57

Télécharger (9,92 ko)

Voir les différences:

Subject: [PATCH] oauth2: add command oauth2-put-document (fixes #22948)

 fargo/oauth2/management/__init__.py                |   0
 fargo/oauth2/management/commands/__init__.py       |   0
 .../management/commands/oauth2-put-document.py     |  33 +++++++++++++++++++++
 tests/pdf-sample.pdf                               | Bin 0 -> 7945 bytes
 tests/test_oauth2.py                               |  25 ++++++++++++++++
 5 files changed, 58 insertions(+)
 create mode 100644 fargo/oauth2/management/__init__.py
 create mode 100644 fargo/oauth2/management/commands/__init__.py
 create mode 100644 fargo/oauth2/management/commands/oauth2-put-document.py
 create mode 100644 tests/pdf-sample.pdf
fargo/oauth2/management/commands/oauth2-put-document.py
1
import os
2

  
3
from django.core.management.base import BaseCommand
4
from django.core.files.base import ContentFile
5
from django.core.urlresolvers import reverse
6

  
7
from fargo.utils import make_url
8
from fargo.fargo.models import Document
9
from fargo.oauth2.models import OAuth2TempFile, OAuth2Client
10
from fargo.fargo.utils import cleanup
11

  
12

  
13
class Command(BaseCommand):
14
    help = 'Push documents inside fargo, returns URLs'
15

  
16
    def add_arguments(self, parser):
17
        parser.add_argument('--client-id', type=int)
18
        parser.add_argument('redirect_uri')
19
        parser.add_argument('paths', nargs='+')
20

  
21
    def handle(self, redirect_uri, paths, client_id, **options):
22
        client = OAuth2Client.objects.get(id=client_id)
23
        for path in paths:
24
            with open(path) as file_object:
25
                filename = os.path.basename(path)
26
                f = ContentFile(file_object.read(), name=filename)
27
                document = Document.objects.get_by_file(f)
28
                oauth2_document = OAuth2TempFile.objects.create(
29
                    client=client,
30
                    document=document,
31
                    filename=filename)
32
                uri = reverse('oauth2-put-document-authorize', args=[oauth2_document.pk])
33
                print 'https://localhost:8000' + make_url(uri, redirect_uri=redirect_uri)
tests/test_oauth2.py
1
import os
1 2
import json
2 3
import mock
3 4
import pytest
......
228 229
    assert client.redirect_uris == 'https://example.com/'
229 230
    assert client.client_id == 'wtf'
230 231
    assert client.client_secret == 'whocares'
232

  
233

  
234
def test_command_put_document(db, capsys, app, john_doe):
235
    call_command('oauth2-create-client', 'test', 'https://example.com/')
236
    client = OAuth2Client.objects.get()
237
    path = os.path.join(os.path.dirname(__file__),  'pdf-sample.pdf')
238
    redirect_uri = 'https://example.com/'
239
    r = call_command('oauth2-put-document', '--client-id=%s' % client.pk, redirect_uri, path)
240
    out, err = capsys.readouterr()
241
    assert err == ''
242
    url = out.strip()
243
    response = app.get(url).follow()
244
    response.form.set('username', john_doe.username)
245
    response.form.set('password', john_doe.username)
246
    response = response.form.submit().follow()
247
    assert 'pdf-sample.pdf' in response
248
    import pdb
249
    pdb.set_trace()
250
    temp_file = OAuth2TempFile.objects.get()
251
    assert temp_file.uuid in response
252
    response = response.form.submit('accept')
253
    assert response['Location'] == redirect_uri
254
    assert UserDocument.objects.filter(user=john_doe, document=temp_file.document).exists()
255
    assert OAuth2TempFile.objects.count() == 0
231
-