summaryrefslogtreecommitdiffstats
path: root/tests/test_api.py
blob: 1e87f5a951a9da260c3a48a01012f0635fdae555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
import base64
import uuid

import pytest
import pathlib2

from django.contrib.auth import get_user_model
from authentic2_cut import models

from utils import login

User = get_user_model()

JOHN = u'Jôhn'
DOE = u'Dôe'
EMAIL = 'john.doe@example.com'

TEST_DIR = pathlib2.Path(__file__).parent


@pytest.fixture
def admin(db):
    user = User(
        username='admin',
        email='admin@example.net',
        is_superuser=True,
        is_staff=True)
    user.set_password('admin')
    user.save()
    return user


@pytest.fixture
def john(glc_app):
    response = glc_app.post_json('/api/users/', params={
        'first_name': JOHN,
        'last_name': DOE,
        'email': EMAIL,
    })
    user = User.objects.get(first_name=JOHN)
    assert response.json['sub'] != user.uuid
    assert response.json['first_name'] == JOHN
    assert response.json['last_name'] == DOE
    assert response.json['email'] == EMAIL
    assert user.first_name == JOHN
    assert user.last_name == DOE
    assert user.email == EMAIL
    assert user.ou.slug == 'usagers'
    user._oidc_sub = response.json['sub']
    return user


def test_no_email(glc_app):
    app = glc_app

    response = app.post_json('/api/users/', params={}, status=400)
    assert set(response.json['errors']) == set(['first_name', 'last_name', 'email'])
    assert response.json['result'] == 0


def test_create_user(john):
    assert john


@pytest.fixture
def jpeg_file():
    with (TEST_DIR / 'minimal.jpg').open('rb') as fd:
        yield fd.read()


@pytest.fixture
def png_file():
    with (TEST_DIR / 'minimal.png').open('rb') as fd:
        yield fd.read()


@pytest.fixture
def pdf_file():
    with (TEST_DIR / 'minimal.pdf').open('rb') as fd:
        yield fd.read()


def helper_test_validation_image(glc_app, john, image_file, extension):
    external_id = uuid.uuid4().hex
    response = glc_app.post_json('/api/users/%s/validate/' % john._oidc_sub, params={
        'external_id': external_id,
        'justificatifs': [{
            'b64_content': base64.b64encode(image_file),
        }],
    }, status=201)
    assert response.json == {
        'status': 'received',
        'id': response.json['id'],
        'result': 1,
        'external_id': external_id,
        'sub': john._oidc_sub,
    }
    validation_request = models.ValidationRequest.objects.get(id=response.json['id'])
    attachment = models.ValidationRequestAttachment.objects.get(validation_request=validation_request)
    assert attachment.image.name.endswith(extension)
    return response.json['id']


def test_validation_jpg(app, admin, glc_app, john, jpeg_file):
    validation_id = helper_test_validation_image(glc_app, john, jpeg_file, 'jpeg')

    response = login(app, admin, 'cut-manager-user-validation')
    response = response.click(str(validation_id))
    assert response.pyquery('.popup-image')


def test_validation_png(app, admin, glc_app, john, png_file):
    validation_id = helper_test_validation_image(glc_app, john, png_file, 'png')
    response = login(app, admin, 'cut-manager-user-validation')
    response = response.click(str(validation_id))
    assert response.pyquery('.popup-image')


def test_validation_pdf(app, admin, glc_app, john, pdf_file):
    validation_id = helper_test_validation_image(glc_app, john, pdf_file, 'pdf')
    response = login(app, admin, 'cut-manager-user-validation')
    response = response.click(str(validation_id))
    assert not response.pyquery('.popup-image')


def test_many_attachments(app, admin, glc_app, john, png_file, jpeg_file, pdf_file):
    external_id = uuid.uuid4().hex
    response = glc_app.post_json('/api/users/%s/validate/' % john._oidc_sub, params={
        'external_id': external_id,
        'justificatifs': [
            {
                'b64_content': base64.b64encode(png_file),
            },
            {
                'b64_content': base64.b64encode(jpeg_file),
            },
            {
                'b64_content': base64.b64encode(pdf_file),
            },
        ],
    }, status=201)
    assert response.json == {
        'status': 'received',
        'id': response.json['id'],
        'result': 1,
        'external_id': external_id,
        'sub': john._oidc_sub,
    }
    validation_request = models.ValidationRequest.objects.get(id=response.json['id'])
    assert validation_request.attachments.count() == 3
    validation_id = str(response.json['id'])
    response = login(app, admin, 'cut-manager-user-validation')
    response = response.click(validation_id)
    assert len(response.pyquery('#attachments p a')) == 3
    attachments_elts = response.pyquery('#attachments p a')
    assert [elt.attrib.get('class', '') for elt in attachments_elts] == ['popup-image', 'popup-image', '']
    assert app.get(attachments_elts[0].attrib['href']).content == png_file
    assert app.get(attachments_elts[1].attrib['href']).content == jpeg_file
    assert app.get(attachments_elts[2].attrib['href']).content == pdf_file