<commit_before># pylint: disable=missing-docstring,no-member
from resdk.exceptions import ResolweServerError

from ..base import USER_USERNAME, BaseResdkFunctionalTest


class TestPermissions(BaseResdkFunctionalTest):

    def test_permissions(self):
        collection_admin = self.res.collection.create(name='Test collection')

        # User doesn't have the permission to view the collection.
        with self.assertRaises(LookupError):
            self.user_res.collection.get(collection_admin.id)

        collection_admin.permissions.add_user(USER_USERNAME, 'view')

        # User can see the collection, but cannot edit it.
        collection_user = self.user_res.collection.get(collection_admin.id)
        collection_user.name = 'New test collection'
        with self.assertRaises(ResolweServerError):
            collection_user.save()

        collection_admin.permissions.add_user(USER_USERNAME, 'edit')

        # User can edit the collection.
        collection_user = self.user_res.collection.get(collection_admin.id)
        collection_user.name = 'New test collection'
        collection_user.save()

        collection_admin.permissions.remove_user(USER_USERNAME, 'edit')

        # Edit permission is removed again.
        collection_user = self.user_res.collection.get(collection_admin.id)
        collection_user.name = 'Another collection'
        with self.assertRaises(ResolweServerError):
            collection_user.save()

    def test_get_holders_with_perm(self):
        collection = self.res.collection.create(name='Test collection')
        collection.permissions.add_user(USER_USERNAME, ['edit', 'view'])
        collection.permissions.add_public('view')

        self.assertEqual(len(collection.permissions.owners), 1)
        self.assertEqual(collection.permissions.owners[0].get_name(), "admin")

        self.assertEqual(len(collection.permissions.editors), 2)
        self.assertEqual(collection.permissions.editors[0].get_name(), "admin")
        self.assertEqual(collection.permissions.editors[1].get_name(), "user")


        self.assertEqual(len(collection.permissions.viewers), 3)
        self.assertEqual(collection.permissions.viewers[0].first_name, "admin")
        self.assertEqual(collection.permissions.viewers[1].first_name, "user")
        self.assertEqual(collection.permissions.viewers[2].username, "public")<commit_msg>Clean up test data in e2e_permissions.py tests module<commit_after># pylint: disable=missing-docstring,no-member
from resdk.exceptions import ResolweServerError

from ..base import USER_USERNAME, BaseResdkFunctionalTest


class TestPermissions(BaseResdkFunctionalTest):

    def setUp(self):
        super().setUp()

        self.test_collection = self.res.collection.create(name='Test collection')

    def tearDown(self):
        super().tearDown()

        self.test_collection.delete(force=True)

    def test_permissions(self):
        # User doesn't have the permission to view the collection.
        with self.assertRaises(LookupError):
            self.user_res.collection.get(self.test_collection.id)

        self.test_collection.permissions.add_user(USER_USERNAME, 'view')

        # User can see the collection, but cannot edit it.
        user_collection = self.user_res.collection.get(self.test_collection.id)
        user_collection.name = 'Different name'
        with self.assertRaises(ResolweServerError):
            user_collection.save()

        self.test_collection.permissions.add_user(USER_USERNAME, 'edit')

        # User can edit the collection.
        user_collection.name = 'Different name'
        user_collection.save()

        self.test_collection.permissions.remove_user(USER_USERNAME, 'edit')

        # Edit permission is removed again.
        user_collection.name = 'Different name 2'
        with self.assertRaises(ResolweServerError):
            user_collection.save()

    def test_get_holders_with_perm(self):
        self.test_collection.permissions.add_user(USER_USERNAME, ['edit', 'view'])
        self.test_collection.permissions.add_public('view')

        self.assertEqual(len(self.test_collection.permissions.owners), 1)
        self.assertEqual(self.test_collection.permissions.owners[0].get_name(), "admin")

        self.assertEqual(len(self.test_collection.permissions.editors), 2)
        self.assertEqual(self.test_collection.permissions.editors[0].get_name(), "admin")
        self.assertEqual(self.test_collection.permissions.editors[1].get_name(), "user")

        self.assertEqual(len(self.test_collection.permissions.viewers), 3)
        self.assertEqual(self.test_collection.permissions.viewers[0].first_name, "admin")
        self.assertEqual(self.test_collection.permissions.viewers[1].first_name, "user")
        self.assertEqual(self.test_collection.permissions.viewers[2].username, "public")
{'fraction_non_alphanumeric': 0.10458867300537412, 'fraction_numerical': 0.00413393964448119, 'mean_word_length': 3.711781888997079, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7387080', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1349, 'n_words': 221}