97 lines
2.3 KiB
Python
97 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
import base64
|
|
import uuid
|
|
from mock import MagicMock
|
|
|
|
import pytest
|
|
import pathlib2
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
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 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):
|
|
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,
|
|
}
|
|
|
|
|
|
def test_validation_jpg(glc_app, john, jpeg_file):
|
|
helper_test_validation_image(glc_app, john, jpeg_file)
|
|
|
|
|
|
def test_validation_png(glc_app, john, png_file):
|
|
helper_test_validation_image(glc_app, john, png_file)
|
|
|
|
|
|
def test_validation_pdf(glc_app, john, pdf_file):
|
|
helper_test_validation_image(glc_app, john, pdf_file)
|