def test_last_forum_posts_with_permissions(self): register_user('granted_user', '*****@*****.**', '111111') register_user('wrong_user', '*****@*****.**', '111111') granted_account = AccountPrototype.get_by_nick('granted_user') wrong_account = AccountPrototype.get_by_nick('wrong_user') restricted_subcategory = SubCategoryPrototype.create( category=self.category, caption='subcat2-caption', order=1, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) restricted_thread = ThreadPrototype.create( restricted_subcategory, 'thread-restricted-caption', self.account, 'thread-text') self.assertEqual( set(t.id for t in ThreadPrototype.get_last_threads(account=None, limit=3)), set([self.thread.id])) self.assertEqual( set(t.id for t in ThreadPrototype.get_last_threads( account=granted_account, limit=3)), set([self.thread.id, restricted_thread.id])) self.assertEqual( set(t.id for t in ThreadPrototype.get_last_threads( account=wrong_account, limit=3)), set([self.thread.id]))
def create(cls, account, clan, role): model = cls._model_class.objects.create(clan=clan._model, account=account._model, role=role) ForumPermissionPrototype.create( account, SubCategoryPrototype.get_by_id(clan.forum_subcategory_id)) return cls(model)
def test_last_forum_posts_with_permissions(self): granted_account = self.accounts_factory.create_account() wrong_account = self.accounts_factory.create_account() restricted_subcategory = SubCategoryPrototype.create( category=self.category, caption='subcat2-caption', order=1, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) restricted_thread = ThreadPrototype.create( restricted_subcategory, 'thread-restricted-caption', self.account, 'thread-text') self.assertEqual( set(t.id for t in ThreadPrototype.get_last_threads(account=None, limit=3)), set([self.thread.id])) self.assertEqual( set(t.id for t in ThreadPrototype.get_last_threads( account=granted_account, limit=3)), set([self.thread.id, restricted_thread.id])) self.assertEqual( set(t.id for t in ThreadPrototype.get_last_threads( account=wrong_account, limit=3)), set([self.thread.id]))
def test_create(self): self.assertEqual(self.clan.name, 'clan-name') self.assertEqual(self.clan.abbr, 'abbr') self.assertEqual(self.clan.motto, 'clan-motto') self.assertEqual(self.clan.description, 'clan-description') self.assertEqual(self.clan.members_number, 1) membership = MembershipPrototype.get_by_account_id(self.account.id) self.assertEqual(membership.clan_id, self.clan.id) self.assertEqual(membership.account_id, self.account.id) self.assertTrue(membership.role.is_LEADER) self.account.reload() self.assertEqual(self.account.clan_id, self.clan.id) self.assertEqual(SubCategoryPrototype._db_count(), 1) self.assertTrue( SubCategoryPrototype.get_by_id( self.clan.forum_subcategory_id).restricted) self.assertEqual(ForumPermissionPrototype._db_count(), 1) self.assertNotEqual( ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None)
def test_remove(self): self.clan.remove() self.assertEqual(ClanPrototype._db_count(), 0) self.assertEqual(MembershipPrototype._db_count(), 0) self.assertEqual(SubCategoryPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 0) self.assertEqual(ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None)
def test_subcategories_visible_to_account_with_permissions(self): register_user('test_user', '*****@*****.**', '111111') register_user('granted_user', '*****@*****.**', '111111') register_user('wrong_user', '*****@*****.**', '111111') granted_account = AccountPrototype.get_by_nick('granted_user') wrong_account = AccountPrototype.get_by_nick('wrong_user') category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) subcategory_1 = SubCategoryPrototype.create(category=category, caption='subcat-1-caption', order=2) SubCategoryPrototype.create(category=category, caption='subcat-2-caption', order=1, restricted=True) restricted_subcategory = SubCategoryPrototype.create(category=category, caption='subcat-restricted-caption', order=0, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) self.assertEqual([s.id for s in SubCategoryPrototype.subcategories_visible_to_account(account=None)], [subcategory_1.id]) self.assertEqual([s.id for s in SubCategoryPrototype.subcategories_visible_to_account(account=granted_account)], [restricted_subcategory.id, subcategory_1.id]) self.assertEqual([s.id for s in SubCategoryPrototype.subcategories_visible_to_account(account=wrong_account)], [subcategory_1.id])
def test_remove(self): MembershipPrototype.create(account=self.account_2, clan=self.clan, role=MEMBER_ROLE.MEMBER).remove() self.assertEqual(ForumPermissionPrototype._db_count(), 1) permission = ForumPermissionPrototype._db_get_object(0) self.assertEqual(permission.account_id, self.account.id) self.assertEqual(permission.subcategory_id, self.clan.forum_subcategory_id)
def test_remove_member_member__not_member(self): account_2 = self.accounts_factory.create_account() self.assertRaises(exceptions.RemoveNotMemberFromClanError, self.clan.remove_member, account_2) self.assertEqual(self.clan.members_number, 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_add_member__already_member(self): account_2 = self.accounts_factory.create_account() self.create_clan(account_2, 0) self.assertRaises(exceptions.AddMemberFromClanError, self.clan.add_member, account_2) self.assertEqual(self.clan.members_number, 1) self.assertEqual(MembershipPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype.get_for(account_2.id, self.clan.forum_subcategory_id), None)
def test_remove_member_member__not_member(self): result, account_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') account_2 = AccountPrototype.get_by_id(account_id) self.assertRaises(exceptions.RemoveNotMemberFromClanError, self.clan.remove_member, account_2) self.assertEqual(self.clan.members_number, 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_remove(self): permission = PermissionPrototype.create(self.account, self.subcategory) with mock.patch('the_tale.forum.prototypes.SubscriptionPrototype.remove_all_in_subcategory') as remove_all_in_subcategory: permission.remove() self.assertEqual(remove_all_in_subcategory.call_count, 1) self.assertEqual(remove_all_in_subcategory.call_args, mock.call(account_id=self.account.id, subcategory_id=self.subcategory.id))
def test_subcategories_visible_to_account_with_permissions(self): register_user('test_user', '*****@*****.**', '111111') register_user('granted_user', '*****@*****.**', '111111') register_user('wrong_user', '*****@*****.**', '111111') granted_account = AccountPrototype.get_by_nick('granted_user') wrong_account = AccountPrototype.get_by_nick('wrong_user') category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) subcategory_1 = SubCategoryPrototype.create(category=category, caption='subcat-1-caption', order=2) SubCategoryPrototype.create(category=category, caption='subcat-2-caption', order=1, restricted=True) restricted_subcategory = SubCategoryPrototype.create( category=category, caption='subcat-restricted-caption', order=0, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) self.assertEqual([ s.id for s in SubCategoryPrototype.subcategories_visible_to_account( account=None) ], [subcategory_1.id]) self.assertEqual([ s.id for s in SubCategoryPrototype.subcategories_visible_to_account( account=granted_account) ], [restricted_subcategory.id, subcategory_1.id]) self.assertEqual([ s.id for s in SubCategoryPrototype.subcategories_visible_to_account( account=wrong_account) ], [subcategory_1.id])
def test_add_member__success(self): account_2 = self.accounts_factory.create_account() self.clan.add_member(account_2) self.assertEqual(self.clan.members_number, 2) self.assertEqual(MembershipPrototype._db_count(), 2) account_2.reload() self.assertEqual(account_2.clan_id, self.clan.id) self.assertNotEqual(ForumPermissionPrototype.get_for(account_2.id, self.clan.forum_subcategory_id), None)
def test_add_member__already_member(self): result, account_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') account_2 = AccountPrototype.get_by_id(account_id) self.create_clan(account_2, 0) self.assertRaises(exceptions.AddMemberFromClanError, self.clan.add_member, account_2) self.assertEqual(self.clan.members_number, 1) self.assertEqual(MembershipPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype.get_for(account_2.id, self.clan.forum_subcategory_id), None)
def test_last_forum_posts_with_permissions(self): granted_account = self.accounts_factory.create_account() wrong_account = self.accounts_factory.create_account() restricted_subcategory = SubCategoryPrototype.create(category=self.category, caption='subcat2-caption', order=1, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) restricted_thread = ThreadPrototype.create(restricted_subcategory, 'thread-restricted-caption', self.account, 'thread-text') self.assertEqual(set(t.id for t in ThreadPrototype.get_last_threads(account=None, limit=3)), set([self.thread.id])) self.assertEqual(set(t.id for t in ThreadPrototype.get_last_threads(account=granted_account, limit=3)), set([self.thread.id, restricted_thread.id])) self.assertEqual(set(t.id for t in ThreadPrototype.get_last_threads(account=wrong_account, limit=3)), set([self.thread.id]))
def test_add_member__success(self): result, account_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') account_2 = AccountPrototype.get_by_id(account_id) self.clan.add_member(account_2) self.assertEqual(self.clan.members_number, 2) self.assertEqual(MembershipPrototype._db_count(), 2) account_2.reload() self.assertEqual(account_2.clan_id, self.clan.id) self.assertNotEqual(ForumPermissionPrototype.get_for(account_2.id, self.clan.forum_subcategory_id), None)
def test_create(self): self.assertEqual(self.clan.name, 'clan-name') self.assertEqual(self.clan.abbr, 'abbr') self.assertEqual(self.clan.motto, 'clan-motto') self.assertEqual(self.clan.description, 'clan-description') self.assertEqual(self.clan.members_number, 1) membership = MembershipPrototype.get_by_account_id(self.account.id) self.assertEqual(membership.clan_id, self.clan.id) self.assertEqual(membership.account_id, self.account.id) self.assertTrue(membership.role.is_LEADER) self.account.reload() self.assertEqual(self.account.clan_id, self.clan.id) self.assertEqual(SubCategoryPrototype._db_count(), 1) self.assertTrue(SubCategoryPrototype.get_by_id(self.clan.forum_subcategory_id).restricted) self.assertEqual(ForumPermissionPrototype._db_count(), 1) self.assertNotEqual(ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None)
def test_unique_abbr(self): self.assertRaises(IntegrityError, ClanPrototype.create, self.accounts_factory.create_account(), abbr=self.clan.abbr, name='bla-name', motto='bla-motto', description='bla-description') self.assertEqual(ClanPrototype._db_count(), 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_unique_owner(self): self.assertRaises(IntegrityError, ClanPrototype.create, self.account, abbr='abr2', name='bla-name', motto='bla-motto', description=u'bla-description') self.assertEqual(ClanPrototype._db_count(), 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_unique_owner(self): self.assertRaises(IntegrityError, ClanPrototype.create, self.account, abbr='abr2', name='bla-name', motto='bla-motto', description='bla-description') self.assertEqual(ClanPrototype._db_count(), 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_subcategories_visible_to_account_with_permissions(self): granted_account = self.accounts_factory.create_account() wrong_account = self.accounts_factory.create_account() category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) subcategory_1 = SubCategoryPrototype.create(category=category, caption='subcat-1-caption', order=2) SubCategoryPrototype.create(category=category, caption='subcat-2-caption', order=1, restricted=True) restricted_subcategory = SubCategoryPrototype.create( category=category, caption='subcat-restricted-caption', order=0, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) self.assertEqual([ s.id for s in SubCategoryPrototype.subcategories_visible_to_account( account=None) ], [subcategory_1.id]) self.assertEqual([ s.id for s in SubCategoryPrototype.subcategories_visible_to_account( account=granted_account) ], [restricted_subcategory.id, subcategory_1.id]) self.assertEqual([ s.id for s in SubCategoryPrototype.subcategories_visible_to_account( account=wrong_account) ], [subcategory_1.id])
def test_unique_abbr(self): result, account_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') self.assertRaises(IntegrityError, ClanPrototype.create, AccountPrototype.get_by_id(account_id), abbr=self.clan.abbr, name='bla-name', motto='bla-motto', description=u'bla-description') self.assertEqual(ClanPrototype._db_count(), 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_unique_name(self): self.assertRaises(IntegrityError, ClanPrototype.create, self.accounts_factory.create_account(), abbr='abr2', name=self.clan.name, motto='bla-motto', description='bla-description') self.assertEqual(ClanPrototype._db_count(), 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def test_subcategories_visible_to_account_with_permissions(self): granted_account = self.accounts_factory.create_account() wrong_account = self.accounts_factory.create_account() category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) subcategory_1 = SubCategoryPrototype.create(category=category, caption='subcat-1-caption', order=2) SubCategoryPrototype.create(category=category, caption='subcat-2-caption', order=1, restricted=True) restricted_subcategory = SubCategoryPrototype.create(category=category, caption='subcat-restricted-caption', order=0, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) self.assertEqual([s.id for s in SubCategoryPrototype.subcategories_visible_to_account(account=None)], [subcategory_1.id]) self.assertEqual([s.id for s in SubCategoryPrototype.subcategories_visible_to_account(account=granted_account)], [restricted_subcategory.id, subcategory_1.id]) self.assertEqual([s.id for s in SubCategoryPrototype.subcategories_visible_to_account(account=wrong_account)], [subcategory_1.id])
def test_create_remove_multiple_clans(self): account_2 = self.accounts_factory.create_account() clan_2 = ClanPrototype.create(account_2, abbr='abbr2', name='clan-name-2', motto='clan-2-motto', description='clan-2-description') self.assertEqual(SubCategoryPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype._db_count(), 2) self.assertNotEqual( ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None) self.assertNotEqual( ForumPermissionPrototype.get_for(account_2.id, clan_2.forum_subcategory_id), None) clan_2.remove() self.assertEqual(SubCategoryPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype._db_count(), 1) self.assertNotEqual( ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None) self.assertEqual( ForumPermissionPrototype.get_for(account_2.id, clan_2.forum_subcategory_id), None)
def test_last_forum_posts_with_permissions(self): register_user('granted_user', '*****@*****.**', '111111') register_user('wrong_user', '*****@*****.**', '111111') granted_account = AccountPrototype.get_by_nick('granted_user') wrong_account = AccountPrototype.get_by_nick('wrong_user') restricted_subcategory = SubCategoryPrototype.create(category=self.category, caption='subcat2-caption', order=1, restricted=True) PermissionPrototype.create(granted_account, restricted_subcategory) restricted_thread = ThreadPrototype.create(restricted_subcategory, 'thread-restricted-caption', self.account, 'thread-text') self.assertEqual(set(t.id for t in ThreadPrototype.get_last_threads(account=None, limit=3)), set([self.thread.id])) self.assertEqual(set(t.id for t in ThreadPrototype.get_last_threads(account=granted_account, limit=3)), set([self.thread.id, restricted_thread.id])) self.assertEqual(set(t.id for t in ThreadPrototype.get_last_threads(account=wrong_account, limit=3)), set([self.thread.id]))
def test_create_remove_multiple_clans(self): account_2 = self.accounts_factory.create_account() clan_2 = ClanPrototype.create(account_2, abbr='abbr2', name='clan-name-2', motto='clan-2-motto', description='clan-2-description') self.assertEqual(SubCategoryPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype._db_count(), 2) self.assertNotEqual(ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None) self.assertNotEqual(ForumPermissionPrototype.get_for(account_2.id, clan_2.forum_subcategory_id), None) clan_2.remove() self.assertEqual(SubCategoryPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype._db_count(), 1) self.assertNotEqual(ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None) self.assertEqual(ForumPermissionPrototype.get_for(account_2.id, clan_2.forum_subcategory_id), None)
def test_create_remove_multiple_clans(self): result, account_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111') account_2 = AccountPrototype.get_by_id(account_id) clan_2 = ClanPrototype.create(account_2, abbr=u'abbr2', name=u'clan-name-2', motto='clan-2-motto', description=u'clan-2-description') self.assertEqual(SubCategoryPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype._db_count(), 2) self.assertNotEqual(ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None) self.assertNotEqual(ForumPermissionPrototype.get_for(account_2.id, clan_2.forum_subcategory_id), None) clan_2.remove() self.assertEqual(SubCategoryPrototype._db_count(), 2) self.assertEqual(ForumPermissionPrototype._db_count(), 1) self.assertNotEqual(ForumPermissionPrototype.get_for(self.account.id, self.clan.forum_subcategory_id), None) self.assertEqual(ForumPermissionPrototype.get_for(account_2.id, clan_2.forum_subcategory_id), None)
def test_remove_member_member__leader(self): self.assertRaises(exceptions.RemoveLeaderFromClanError, self.clan.remove_member, self.account) self.assertEqual(self.clan.members_number, 1) self.assertEqual(MembershipPrototype._db_count(), 1) self.assertEqual(ForumPermissionPrototype._db_count(), 1)
def remove(self): ForumPermissionPrototype.get_for(account_id=self.account_id, subcategory_id=ClanPrototype.get_by_id(self.clan_id).forum_subcategory_id).remove() self._model.delete()
def create(cls, account, clan, role): model = cls._model_class.objects.create(clan=clan._model, account=account._model, role=role) ForumPermissionPrototype.create(account, SubCategoryPrototype.get_by_id(clan.forum_subcategory_id)) return cls(model)
def test_is_restricted_for__no_permission(self): self.subcategory._model.restricted = True self.subcategory.save() PermissionPrototype.create(self.account_2, self.subcategory) self.assertTrue(self.subcategory.is_restricted_for(self.account))