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_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_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_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_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_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_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_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_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_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_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))
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))