167 lines
3.3 KiB
Python
167 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import pytest
|
|
|
|
from django.conf import settings
|
|
from django.urls import reverse
|
|
|
|
from rest_framework.test import APIClient
|
|
|
|
from utils import create_user, create_credentials
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
if settings.HOBO:
|
|
from hobo import signature
|
|
|
|
|
|
@pytest.fixture
|
|
def admin(db):
|
|
return create_user(
|
|
username='admin',
|
|
first_name='admin',
|
|
last_name='admin',
|
|
email='admin@example.net',
|
|
is_superuser=True,
|
|
is_staff=True,
|
|
is_active=True
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def user_john(db):
|
|
return create_user(username='john')
|
|
|
|
|
|
@pytest.fixture
|
|
def cred_john(db, user_john):
|
|
return create_credentials(user_john, {
|
|
"login": u"john",
|
|
"password": u"jôhn password"})
|
|
|
|
|
|
@pytest.fixture(params=['cred_john'])
|
|
def credentials(request, cred_john):
|
|
return locals().get(request.param)
|
|
|
|
|
|
# MIGRATION COMMAND
|
|
def cmd(*args, **kwargs):
|
|
cmd = type(
|
|
'Cmd',
|
|
(object,),
|
|
{
|
|
'name': 'migrate-users',
|
|
'args': args,
|
|
'opts': kwargs
|
|
}
|
|
)
|
|
return cmd
|
|
|
|
|
|
@pytest.fixture
|
|
def command_ldap():
|
|
return cmd('tests/ldap_users.txt', ldap=True)
|
|
|
|
|
|
@pytest.fixture
|
|
def command_csv():
|
|
return cmd('tests/csv_users.csv', csv=True)
|
|
|
|
|
|
@pytest.fixture(params=['command_ldap', 'command_csv'])
|
|
def command(request, command_ldap, command_csv):
|
|
return locals().get(request.param)
|
|
|
|
|
|
@pytest.fixture
|
|
def url():
|
|
return reverse('api')
|
|
|
|
|
|
def create_signed_url(secret, orig):
|
|
url = signature.sign_url(reverse('api'), secret)
|
|
url += '&orig={}'.format(orig)
|
|
return type(
|
|
'signed_url',
|
|
(object,),
|
|
{'url': url, 'orig': orig}
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def url_signed_testserver_service():
|
|
return create_signed_url(
|
|
settings.SECRET_KEY,
|
|
'testserver'
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def url_signed_unknown_service():
|
|
return create_signed_url(
|
|
settings.SECRET_KEY,
|
|
'lol'
|
|
)
|
|
|
|
|
|
@pytest.fixture(params=['url_signed_testserver_service', 'url_signed_unknown_service'])
|
|
def url_signed(request, url_signed_testserver_service, url_signed_unknown_service):
|
|
return locals().get(request.param)
|
|
|
|
|
|
@pytest.fixture
|
|
def client_service():
|
|
return APIClient()
|
|
|
|
|
|
@pytest.fixture
|
|
def client_anonymous():
|
|
return APIClient()
|
|
|
|
|
|
@pytest.fixture
|
|
def client_logged(admin):
|
|
client = APIClient()
|
|
client.login(username='admin', password='admin')
|
|
return client
|
|
|
|
|
|
@pytest.fixture(params=['client_anonymous', 'client_logged'])
|
|
def client(request, client_anonymous, client_logged):
|
|
return locals().get(request.param)
|
|
|
|
|
|
@pytest.fixture
|
|
def kevin_payload():
|
|
return {
|
|
'name_id_content': '12345',
|
|
'email': 'kevin@fake.com',
|
|
'first_name': 'kevin',
|
|
'last_name': 'fake',
|
|
'locators': {
|
|
'login': 'fake',
|
|
'password': 'fake'
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def josh_payload():
|
|
return {
|
|
'name_id_content': '77777',
|
|
'email': 'josh@loking.com',
|
|
'first_name': 'josh',
|
|
'last_name': 'loking',
|
|
'locators': {
|
|
'login': 'josh',
|
|
'password': 'josh password'
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.fixture(params=['kevin_payload', 'josh_payload'])
|
|
def payload(request, kevin_payload, josh_payload):
|
|
return locals().get(request.param)
|