def process_user_data(self, data): id = data[self.user_data_id_field] email = data.get(self.user_data_email_field) user = ( User.get_instance(**{self.user_identifier: id}) or ( email and User.get_instance(email = email) ) ) if user is None: user = User() if not user.get(self.user_identifier): user.set(self.user_identifier, id) if not user.email: user.email = email first_login = True else: first_login = False self.user_authenticated( user = user, data = data, first_login = first_login ) user.insert() return user
def test_delete(self): from datetime import datetime from woost.models import (Item, User, ChangeSet, changeset_context) author = User() author.insert() item = Item() item.insert() with changeset_context(author) as changeset: item.delete() assert list(ChangeSet.select()) == [changeset] assert changeset.author is author assert isinstance(changeset.date, datetime) assert changeset.changes.keys() == [item.id] change = changeset.changes[item.id] assert change.target is item assert change.action == "delete" assert change.changeset is changeset assert item.changes == [change] assert not item.id in Item.index
class BaseTestCase(TempStorageMixin, TestCase): def setUp(self): from woost import app from woost.models import Configuration, User, Role from woost.models.trigger import get_triggers_enabled, set_triggers_enabled from woost.controllers.installer import Installer self.__prev_installation_id = app.installation_id self.__prev_triggers_enabled = get_triggers_enabled() app.installation_id = "TEST" set_triggers_enabled(False) TempStorageMixin.setUp(self) app.root = os.path.join(self._temp_dir, "test_project") installer = Installer() installer.create_project({"project_path": app.root}) # Configuration self.config = Configuration(qname = "woost.configuration") self.config.insert() # Roles and users self.anonymous_role = Role(qname = "woost.anonymous") self.anonymous_role.insert() self.anonymous_user = User(qname = "woost.anonymous_user") self.anonymous_user.roles.append(self.anonymous_role) self.anonymous_user.insert() self.everybody_role = Role(qname = "woost.everybody") self.everybody_role.insert() self.authenticated_role = Role(qname = "woost.authenticated") self.authenticated_role.insert() set_triggers_enabled(True) def tearDown(self): from woost import app from woost.models import staticpublication from woost.models.trigger import set_triggers_enabled app.installation_id = self.__prev_installation_id set_triggers_enabled(self.__prev_triggers_enabled) TempStorageMixin.tearDown(self)
def test_modify(self): from time import sleep from datetime import datetime from woost.models import (Document, User, ChangeSet, changeset_context) author = User() author.insert() with changeset_context(author) as creation: item = Document() item.set("title", u"Foo!", "en") item.resource_type = u"text/foo" item.hidden = True item.insert() # Make sure creation_time and last_update_time don't match sleep(0.1) with changeset_context(author) as modification: item.set("title", u"Bar!", "en") item.resource_type = u"text/bar" item.hidden = True assert list(ChangeSet.select()) == [creation, modification] assert modification.author is author assert isinstance(modification.date, datetime) assert modification.changes.keys() == [item.id] change = modification.changes[item.id] assert change.target is item assert change.action == "modify" assert change.changeset is modification assert change.changed_members == set(["title", "resource_type"]) assert item.changes == [creation.changes[item.id], change] for key in "id", "changes", "creation_time", "last_update_time": assert not key in change.item_state assert change.item_state["title"] == {"en": u"Bar!"} assert change.item_state["resource_type"] == u"text/bar" assert change.item_state["hidden"] == True assert change.item_state["translation_enabled"] == \ {"en": item.get("translation_enabled", "en")} assert item.author is author assert item.creation_time assert item.last_update_time assert not item.creation_time == item.last_update_time
def test_insert(self): from datetime import datetime from woost.models import (Document, User, ChangeSet, changeset_context) author = User() author.insert() with changeset_context(author) as changeset: item = Document() item.set("title", u"Foo!", "en") item.resource_type = u"text/foo" item.hidden = True assert not changeset.changes item.insert() assert list(ChangeSet.select()) == [changeset] assert changeset.author is author assert isinstance(changeset.date, datetime) assert changeset.changes.keys() == [item.id] change = changeset.changes[item.id] assert change.target is item assert change.action == "create" assert change.changeset is changeset assert item.changes == [change] for key in "id", "changes", "creation_time", "last_update_time": assert not key in change.item_state print change.item_state assert change.item_state["title"] == {"en": u"Foo!"} assert change.item_state["resource_type"] == u"text/foo" assert change.item_state["hidden"] == True assert change.item_state["translation_enabled"] == \ {"en": item.get("translation_enabled", "en")} assert item.author is author assert item.creation_time assert item.last_update_time assert item.creation_time == item.last_update_time
class IsAccessibleExpressionTestCase(BaseTestCase): def setUp(self): from woost.models import User BaseTestCase.setUp(self) set_language("en") self.user = User() self.user.insert() def accessible_items(self): from woost.models import Publishable, IsAccessibleExpression return set(Publishable.select(IsAccessibleExpression(self.user))) def test_enabled(self): from woost.models import Publishable, ReadPermission self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" })) a = Publishable() a.enabled = True a.insert() b = Publishable() b.enabled = False b.insert() c = Publishable() c.enabled = True c.insert() d = Publishable() d.enabled = False d.insert() assert self.accessible_items() == set([a, c]) def test_translation_enabled(self): from cocktail.translations import language_context from woost.models import (Publishable, ReadPermission, ReadTranslationPermission) self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" })) self.everybody_role.permissions.append(ReadTranslationPermission()) self.config.languages = ["en"] self.config.published_languages = [] with language_context("en"): a = Publishable() a.per_language_publication = True a.translation_enabled = True a.insert() b = Publishable() b.per_language_publication = True b.translation_enabled = False b.insert() c = Publishable() c.per_language_publication = True c.translation_enabled = True c.insert() d = Publishable() d.per_language_publication = True d.set("translation_enabled", True, "de") d.insert() e = Publishable() e.per_language_publication = False e.enabled = True e.insert() accessible = self.accessible_items() print a in accessible print b in accessible print c in accessible print d in accessible print e in accessible assert self.accessible_items() == set([a, c, e]) self.config.published_languages = ["en"] assert self.accessible_items() == set([a, c, e]) with language_context("de"): self.config.published_languages = ["de"] assert self.accessible_items() == set([d, e]) def test_translation_permitted(self): from cocktail.translations import language_context from woost.models import (Publishable, ReadPermission, ReadTranslationPermission, set_current_user) set_current_user(self.user) self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" })) self.everybody_role.permissions.append( ReadTranslationPermission(matching_languages=["ca", "es"])) self.everybody_role.permissions.append( ReadTranslationPermission(matching_languages=["en"], authorized=False)) self.config.languages = ["ca", "es", "en"] self.config.published_languages = [] a = Publishable() a.per_language_publication = True a.insert() b = Publishable() b.per_language_publication = False b.insert() with language_context("ca"): a.translation_enabled = True assert a.is_accessible() assert b.is_accessible() assert self.accessible_items() == set([a, b]) for language in "es", "en", "de": with language_context(language): assert not a.is_accessible() assert b.is_accessible() assert self.accessible_items() == set([b]) def test_current(self): from woost.models import Publishable, ReadPermission from datetime import datetime, timedelta self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" })) now = datetime.now() a = Publishable() a.enabled = True a.insert() b = Publishable() b.enabled = True b.start_date = now b.end_date = now + timedelta(days=1) b.insert() c = Publishable() c.enabled = True c.start_date = now + timedelta(days=1) c.insert() d = Publishable() d.enabled = True d.end_date = now - timedelta(days=1) d.insert() assert self.accessible_items() == set([a, b]) def test_allowed(self): from woost.models import Publishable, ReadPermission a = Publishable() a.enabled = True a.insert() b = Publishable() b.enabled = True b.insert() self.everybody_role.permissions.append( ReadPermission( matching_items={ "type": "woost.models.publishable.Publishable", "filter": "member-id", "filter_operator0": "ne", "filter_value0": str(b.id) })) assert self.accessible_items() == set([a])