102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
import pytest
|
|
|
|
from wcs import data_sources
|
|
from wcs.data_sources import NamedDataSource
|
|
from wcs.qommon.http_request import HTTPRequest
|
|
|
|
from .utilities import clean_temporary_pub, create_temporary_pub
|
|
|
|
|
|
@pytest.fixture
|
|
def pub():
|
|
pub = create_temporary_pub(sql_mode=True)
|
|
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
|
pub.set_app_dir(req)
|
|
return pub
|
|
|
|
|
|
def teardown_module(module):
|
|
clean_temporary_pub()
|
|
|
|
|
|
def test_datasource_users(pub):
|
|
pub.role_class.wipe()
|
|
role1 = pub.role_class(name='role')
|
|
role1.store()
|
|
role2 = pub.role_class(name='role2')
|
|
role2.store()
|
|
|
|
pub.user_class.wipe()
|
|
|
|
NamedDataSource.wipe()
|
|
datasource = NamedDataSource(name='foo')
|
|
datasource.data_source = {'type': 'wcs:users'}
|
|
datasource.store()
|
|
|
|
assert data_sources.get_items({'type': datasource.slug}) == []
|
|
assert data_sources.get_items(datasource.extended_data_source) == []
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == []
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == []
|
|
|
|
users = []
|
|
for i in range(2):
|
|
user = pub.user_class(name='John Doe %s' % i)
|
|
user.roles = [role1.id]
|
|
user.store()
|
|
users.append(user)
|
|
|
|
assert data_sources.get_items({'type': datasource.slug}) == [
|
|
('1', 'John Doe 0', '1', {'id': 1, 'text': 'John Doe 0'}),
|
|
('2', 'John Doe 1', '2', {'id': 2, 'text': 'John Doe 1'}),
|
|
]
|
|
assert data_sources.get_items(datasource.extended_data_source) == [
|
|
('1', 'John Doe 0', '1', {'id': 1, 'text': 'John Doe 0'}),
|
|
('2', 'John Doe 1', '2', {'id': 2, 'text': 'John Doe 1'}),
|
|
]
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == [
|
|
{'id': 1, 'text': 'John Doe 0'},
|
|
{'id': 2, 'text': 'John Doe 1'},
|
|
]
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == [
|
|
{'id': 1, 'text': 'John Doe 0'},
|
|
{'id': 2, 'text': 'John Doe 1'},
|
|
]
|
|
|
|
datasource.users_included_roles = [role1.id]
|
|
datasource.store()
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == [
|
|
{'id': 1, 'text': 'John Doe 0'},
|
|
{'id': 2, 'text': 'John Doe 1'},
|
|
]
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == [
|
|
{'id': 1, 'text': 'John Doe 0'},
|
|
{'id': 2, 'text': 'John Doe 1'},
|
|
]
|
|
|
|
datasource.users_included_roles = [role1.id, role2.id]
|
|
datasource.store()
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == []
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == []
|
|
|
|
users[0].roles = [role1.id, role2.id]
|
|
users[0].store()
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == [{'id': 1, 'text': 'John Doe 0'}]
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == [
|
|
{'id': 1, 'text': 'John Doe 0'}
|
|
]
|
|
|
|
users[0].roles = [role2.id]
|
|
users[0].store()
|
|
datasource.users_included_roles = []
|
|
datasource.users_excluded_roles = [role1.id]
|
|
datasource.store()
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == [{'id': 1, 'text': 'John Doe 0'}]
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == [
|
|
{'id': 1, 'text': 'John Doe 0'}
|
|
]
|
|
|
|
datasource.users_excluded_roles = [role1.id, role2.id]
|
|
datasource.store()
|
|
assert data_sources.get_structured_items({'type': datasource.slug}) == []
|
|
assert data_sources.get_structured_items(datasource.extended_data_source) == []
|