2016-09-18 16:00:31 +02:00
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
from openslides.core.models import Projector
|
|
|
|
from openslides.utils import collection
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetModelFromCollectionString(TestCase):
|
|
|
|
def test_known_app(self):
|
|
|
|
projector_model = collection.get_model_from_collection_string('core/projector')
|
|
|
|
|
|
|
|
self.assertEqual(projector_model, Projector)
|
|
|
|
|
|
|
|
def test_unknown_app(self):
|
2017-04-27 15:16:07 +02:00
|
|
|
with self.assertRaises(ValueError):
|
2016-09-18 16:00:31 +02:00
|
|
|
collection.get_model_from_collection_string('invalid/model')
|
|
|
|
|
|
|
|
|
|
|
|
class TestCollectionElement(TestCase):
|
|
|
|
def test_from_values(self):
|
2017-02-12 14:09:53 +01:00
|
|
|
with patch.object(collection.CollectionElement, 'get_full_data'):
|
|
|
|
collection_element = collection.CollectionElement.from_values('testmodule/model', 42)
|
2016-09-18 16:00:31 +02:00
|
|
|
|
|
|
|
self.assertEqual(collection_element.collection_string, 'testmodule/model')
|
|
|
|
self.assertEqual(collection_element.id, 42)
|
|
|
|
|
2016-10-01 15:26:00 +02:00
|
|
|
def test_channel_message(self):
|
|
|
|
"""
|
2017-09-04 00:25:45 +02:00
|
|
|
Test that to_channel_message works together with from_channel_message.
|
2016-10-01 15:26:00 +02:00
|
|
|
"""
|
|
|
|
collection_element = collection.CollectionElement.from_values(
|
|
|
|
'testmodule/model',
|
|
|
|
42,
|
|
|
|
full_data={'data': 'value'},
|
|
|
|
information={'some': 'information'})
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
created_collection_element = collection.from_channel_message(
|
|
|
|
collection.to_channel_message([collection_element]))[0]
|
2016-10-01 15:26:00 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
collection_element,
|
|
|
|
created_collection_element)
|
|
|
|
self.assertEqual(created_collection_element.full_data, {'data': 'value'})
|
|
|
|
self.assertEqual(created_collection_element.information, {'some': 'information'})
|
|
|
|
|
2016-09-18 16:00:31 +02:00
|
|
|
def test_as_autoupdate_for_user(self):
|
2017-02-12 14:09:53 +01:00
|
|
|
with patch.object(collection.CollectionElement, 'get_full_data'):
|
|
|
|
collection_element = collection.CollectionElement.from_values('testmodule/model', 42)
|
2016-09-18 16:00:31 +02:00
|
|
|
fake_user = MagicMock()
|
|
|
|
collection_element.get_access_permissions = MagicMock()
|
2017-09-04 00:25:45 +02:00
|
|
|
collection_element.get_access_permissions().get_restricted_data.return_value = ['restricted_data']
|
2016-09-18 16:00:31 +02:00
|
|
|
collection_element.get_full_data = MagicMock()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
collection_element.as_autoupdate_for_user(fake_user),
|
|
|
|
{'collection': 'testmodule/model',
|
|
|
|
'id': 42,
|
|
|
|
'action': 'changed',
|
|
|
|
'data': 'restricted_data'})
|
|
|
|
|
|
|
|
def test_as_autoupdate_for_user_no_permission(self):
|
2017-02-12 14:09:53 +01:00
|
|
|
with patch.object(collection.CollectionElement, 'get_full_data'):
|
|
|
|
collection_element = collection.CollectionElement.from_values('testmodule/model', 42)
|
2016-09-18 16:00:31 +02:00
|
|
|
fake_user = MagicMock()
|
|
|
|
collection_element.get_access_permissions = MagicMock()
|
|
|
|
collection_element.get_access_permissions().get_restricted_data.return_value = None
|
|
|
|
collection_element.get_full_data = MagicMock()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
collection_element.as_autoupdate_for_user(fake_user),
|
|
|
|
{'collection': 'testmodule/model',
|
|
|
|
'id': 42,
|
|
|
|
'action': 'deleted'})
|
|
|
|
|
|
|
|
def test_as_autoupdate_for_user_deleted(self):
|
|
|
|
collection_element = collection.CollectionElement.from_values('testmodule/model', 42, deleted=True)
|
|
|
|
fake_user = MagicMock()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
collection_element.as_autoupdate_for_user(fake_user),
|
|
|
|
{'collection': 'testmodule/model',
|
|
|
|
'id': 42,
|
|
|
|
'action': 'deleted'})
|
|
|
|
|
2017-02-12 14:09:53 +01:00
|
|
|
@patch.object(collection.CollectionElement, 'get_full_data')
|
|
|
|
def test_equal(self, mock_get_full_data):
|
2016-09-30 21:43:22 +02:00
|
|
|
self.assertEqual(
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 1),
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 1))
|
|
|
|
self.assertEqual(
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 1),
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 1, deleted=True))
|
|
|
|
self.assertNotEqual(
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 1),
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 2))
|
|
|
|
self.assertNotEqual(
|
|
|
|
collection.CollectionElement.from_values('testmodule/model', 1),
|
|
|
|
collection.CollectionElement.from_values('testmodule/other_model', 1))
|