def test_full_list_with_unautorized_user(self):
     """
     It should 403 "You do not have permission to perform this action."
     """
     UserFactory.create_batch(2)
     response = self.request()
     assert response.status_code == 403
     assert response.content['detail'] == PermissionDenied.default_detail
示例#2
0
 def test_full_list(self):
     """
     It should return all users.
     """
     UserFactory.create_batch(2)
     response = self.request(user=self.user)
     assert response.status_code == 200
     assert len(response.content['results']) == 3
 def test_full_list(self):
     """
     It should return all users.
     """
     UserFactory.create_batch(2)
     response = self.request(user=self.user)
     assert response.status_code == 200
     assert len(response.content) == 3
示例#4
0
 def test_full_list_with_unautorized_user(self):
     """
     It should 403 "You do not have permission to perform this action."
     """
     UserFactory.create_batch(2)
     response = self.request()
     assert response.status_code == 403
     assert response.content['detail'] == PermissionDenied.default_detail
示例#5
0
 def test_get_carousel_resources(self):
     ResourceFactory.create(
         title='first', likes=UserFactory.create_batch(size=10), status=RESOURCE_APPROVED
     )
     second = ResourceFactory.create(
         title='second',
         tried=UserFactory.create_batch(size=5),
         hits=20,
         status=RESOURCE_APPROVED,
     )
     resources = Resource.get_carousel_resources(limit=1)
     self.assertIn(second, resources)
    def test_list_to_representation(self):
        users = UserFactory.create_batch(2)
        prj_admin = UserFactory.create()
        org_admin = UserFactory.create()
        public_user = UserFactory.create()

        superuser = UserFactory.create(is_superuser=True)

        org = OrganizationFactory.create(
            add_users=[superuser, prj_admin, public_user])
        project = ProjectFactory.create(add_users=users, organization=org)

        OrganizationRole.objects.create(organization=org,
                                        user=org_admin,
                                        admin=True)
        ProjectRole.objects.create(project=project, user=prj_admin, role='PM')

        serializer = serializers.ProjectUserSerializer(
            org.users.all(), many=True, context={'project': project})

        assert len(serializer.data) == 6

        for u in serializer.data:
            if u['username'] == superuser.username:
                assert u['role'] == 'A'
            elif u['username'] == org_admin.username:
                assert u['role'] == 'A'
            elif u['username'] == prj_admin.username:
                assert u['role'] == 'PM'
            elif u['username'] == public_user.username:
                assert u['role'] == 'Pb'
            else:
                assert u['role'] == 'PU'
示例#7
0
    def test_full_list_organizations(self):
        """
        It should return all users with their organizations.
        """
        user1, user2 = UserFactory.create_batch(2)
        o0 = OrganizationFactory.create(add_users=[user1, user2])
        o1 = OrganizationFactory.create(add_users=[user1])
        o2 = OrganizationFactory.create(add_users=[user2])
        response = self.request(user=self.user)
        assert response.status_code == 200
        assert len(response.content['results']) == 3

        assert 'organizations' in response.content['results'][0]
        assert response.content['results'][0]['organizations'] == []
        assert 'organizations' in response.content['results'][1]
        assert ({
            'id': o0.id,
            'name': o0.name
        } in response.content['results'][1]['organizations'])
        assert ({
            'id': o1.id,
            'name': o1.name
        } in response.content['results'][1]['organizations'])
        assert 'organizations' in response.content['results'][2]
        assert ({
            'id': o0.id,
            'name': o0.name
        } in response.content['results'][2]['organizations'])
        assert ({
            'id': o2.id,
            'name': o2.name
        } in response.content['results'][2]['organizations'])
    def users(self, create, extracted, **kwargs):
        if not create:
            return

        if extracted:
            for user in extracted:
                self.users.add(user)
        else:
            self.users = UserFactory.create_batch(10)
示例#9
0
    def users(self, create, extracted, **kwargs):
        if not create:
            return

        if extracted:
            for user in extracted:
                self.users.add(user)
        else:
            self.users = UserFactory.create_batch(10)
 def test_full_list(self):
     """
     It should return all organizations.
     """
     prj_users = UserFactory.create_batch(2)
     other_user = UserFactory.create()
     self.project = ProjectFactory.create(add_users=prj_users)
     response = self.request(user=self.user)
     assert response.status_code == 200
     assert len(response.content) == 2
     assert (other_user.username not in
             [u['username'] for u in response.content])
示例#11
0
 def test_full_list(self):
     """
     It should return all organizations.
     """
     prj_users = UserFactory.create_batch(2)
     other_user = UserFactory.create()
     self.project = ProjectFactory.create(add_users=prj_users)
     response = self.request(user=self.user)
     assert response.status_code == 200
     assert len(response.content) == 2
     assert (other_user.username not in
             [u['username'] for u in response.content])
示例#12
0
    def setup_models(self):
        clauses = {
            'clause': [
                clause('allow', ['org.*']),
                clause('allow', ['org.*', 'org.*.*'], ['organization/*'])
            ]
        }
        policy = Policy.objects.create(name='test-policy',
                                       body=json.dumps(clauses))
        self.user = UserFactory.create()
        assign_user_policies(self.user, policy)

        org_users = UserFactory.create_batch(2)
        self.org = OrganizationFactory.create(slug='org', add_users=org_users)
 def test_visibility_filtering(self):
     users = UserFactory.create_batch(8)
     clause = {
         'clause': [
             {
                 'effect': 'allow',
                 'object': ['organization/*'],
                 'action': ['project.list']
             },
             {
                 'effect': 'allow',
                 'object': ['project/*/*'],
                 'action': ['project.view']
             }
         ]
     }
     policy = Policy.objects.create(
         name='test-policy',
         body=json.dumps(clause)
     )
     for user in users:
         user.assign_policies(policy)
     orgs = [
         OrganizationFactory.create(
             add_users=[users[1], users[4], users[5], users[7]]
         ),
         OrganizationFactory.create(
             add_users=[users[2], users[4], users[6], users[7]]
         ),
         OrganizationFactory.create(
             add_users=[users[3], users[5], users[6], users[7]]
         )
     ]
     prjs = [
         ProjectFactory.create(organization=orgs[0], access='public'),
         ProjectFactory.create(organization=orgs[0], access='private'),
         ProjectFactory.create(organization=orgs[1], access='public'),
         ProjectFactory.create(organization=orgs[1], access='private'),
         ProjectFactory.create(organization=orgs[2], access='public'),
         ProjectFactory.create(organization=orgs[2], access='private')
     ]
     self._check_visible(users, orgs, prjs, users[0], [0,    2,    4])
     self._check_visible(users, orgs, prjs, users[1], [0, 1, 2,    4])
     self._check_visible(users, orgs, prjs, users[2], [0,    2, 3, 4])
     self._check_visible(users, orgs, prjs, users[3], [0,    2,    4, 5])
     self._check_visible(users, orgs, prjs, users[4], [0, 1, 2, 3, 4])
     self._check_visible(users, orgs, prjs, users[5], [0, 1, 2,    4, 5])
     self._check_visible(users, orgs, prjs, users[6], [0,    2, 3, 4, 5])
     self._check_visible(users, orgs, prjs, users[7], [0, 1, 2, 3, 4, 5])
示例#14
0
 def test_visibility_filtering(self):
     users = UserFactory.create_batch(8)
     clause = {
         'clause': [
             {
                 'effect': 'allow',
                 'object': ['organization/*'],
                 'action': ['project.list']
             },
             {
                 'effect': 'allow',
                 'object': ['project/*/*'],
                 'action': ['project.view']
             }
         ]
     }
     policy = Policy.objects.create(
         name='test-policy',
         body=json.dumps(clause)
     )
     for user in users:
         user.assign_policies(policy)
     orgs = [
         OrganizationFactory.create(
             add_users=[users[1], users[4], users[5], users[7]]
         ),
         OrganizationFactory.create(
             add_users=[users[2], users[4], users[6], users[7]]
         ),
         OrganizationFactory.create(
             add_users=[users[3], users[5], users[6], users[7]]
         )
     ]
     prjs = [
         ProjectFactory.create(organization=orgs[0], access='public'),
         ProjectFactory.create(organization=orgs[0], access='private'),
         ProjectFactory.create(organization=orgs[1], access='public'),
         ProjectFactory.create(organization=orgs[1], access='private'),
         ProjectFactory.create(organization=orgs[2], access='public'),
         ProjectFactory.create(organization=orgs[2], access='private')
     ]
     self._check_visible(users, orgs, prjs, users[0], [0,    2,    4])
     self._check_visible(users, orgs, prjs, users[1], [0, 1, 2,    4])
     self._check_visible(users, orgs, prjs, users[2], [0,    2, 3, 4])
     self._check_visible(users, orgs, prjs, users[3], [0,    2,    4, 5])
     self._check_visible(users, orgs, prjs, users[4], [0, 1, 2, 3, 4])
     self._check_visible(users, orgs, prjs, users[5], [0, 1, 2,    4, 5])
     self._check_visible(users, orgs, prjs, users[6], [0,    2, 3, 4, 5])
     self._check_visible(users, orgs, prjs, users[7], [0, 1, 2, 3, 4, 5])
    def test_list_to_representation(self):
        users = UserFactory.create_batch(2)
        prj_admin = UserFactory.create()
        project = ProjectFactory.create(add_users=users)
        ProjectRole.objects.create(user=prj_admin, project=project, role='PM')
        serializer = serializers.ProjectUserSerializer(
            project.users.all(), many=True, context={'project': project})

        assert len(serializer.data) == 3

        for u in serializer.data:
            if u['username'] == prj_admin.username:
                assert u['role'] == 'PM'
            else:
                assert u['role'] == 'PU'
    def setup_models(self):
        clauses = {
            'clause': [
                clause('allow', ['org.*']),
                clause('allow', ['org.*', 'org.*.*'], ['organization/*'])
            ]
        }
        policy = Policy.objects.create(
            name='test-policy',
            body=json.dumps(clauses))
        self.user = UserFactory.create()
        assign_user_policies(self.user, policy)

        org_users = UserFactory.create_batch(2)
        self.org = OrganizationFactory.create(slug='org', add_users=org_users)
    def test_list_to_representation(self):
        users = UserFactory.create_batch(2)
        org_admin = UserFactory.create()
        org = OrganizationFactory.create(add_users=users)
        OrganizationRole.objects.create(user=org_admin,
                                        organization=org,
                                        admin=True)
        serializer = serializers.OrganizationUserSerializer(
            org.users.all(), many=True, context={'organization': org})

        assert len(serializer.data) == 3

        for u in serializer.data:
            if u['username'] == org_admin.username:
                assert u['admin'] is True
            else:
                assert u['admin'] is False
    def test_list_to_representation(self):
        users = UserFactory.create_batch(2)
        org_admin = UserFactory.create()
        org = OrganizationFactory.create(add_users=users)
        OrganizationRole.objects.create(
            user=org_admin, organization=org, admin=True
        )
        serializer = serializers.OrganizationUserSerializer(
            org.users.all(), many=True, context={'organization': org}
        )

        assert len(serializer.data) == 3

        for u in serializer.data:
            if u['username'] == org_admin.username:
                assert u['admin'] is True
            else:
                assert u['admin'] is False
    def test_list_to_representation(self):
        users = UserFactory.create_batch(2)
        prj_admin = UserFactory.create()
        org_admin = UserFactory.create()
        public_user = UserFactory.create()

        superuser = UserFactory.create()
        superuser_role = Role.objects.get(name='superuser')
        superuser.assign_policies(superuser_role)

        org = OrganizationFactory.create(
            add_users=[superuser, prj_admin, public_user])
        project = ProjectFactory.create(add_users=users, organization=org)

        OrganizationRole.objects.create(
            organization=org, user=org_admin, admin=True)
        ProjectRole.objects.create(
            project=project, user=prj_admin, role='PM')

        serializer = serializers.ProjectUserSerializer(
            org.users.all(),
            many=True,
            context={'project': project}
        )

        assert len(serializer.data) == 6

        for u in serializer.data:
            if u['username'] == superuser.username:
                assert u['role'] == 'A'
            elif u['username'] == org_admin.username:
                assert u['role'] == 'A'
            elif u['username'] == prj_admin.username:
                assert u['role'] == 'PM'
            elif u['username'] == public_user.username:
                assert u['role'] == 'Pb'
            else:
                assert u['role'] == 'PU'
    def test_full_list_organizations(self):
        """
        It should return all users with their organizations.
        """
        user1, user2 = UserFactory.create_batch(2)
        o0 = OrganizationFactory.create(add_users=[user1, user2])
        o1 = OrganizationFactory.create(add_users=[user1])
        o2 = OrganizationFactory.create(add_users=[user2])
        response = self.request(user=self.user)
        assert response.status_code == 200
        assert len(response.content) == 3

        assert 'organizations' in response.content[0]
        assert response.content[0]['organizations'] == []
        assert 'organizations' in response.content[1]
        assert ({'id': o0.id, 'name': o0.name}
                in response.content[1]['organizations'])
        assert ({'id': o1.id, 'name': o1.name}
                in response.content[1]['organizations'])
        assert 'organizations' in response.content[2]
        assert ({'id': o0.id, 'name': o0.name}
                in response.content[2]['organizations'])
        assert ({'id': o2.id, 'name': o2.name}
                in response.content[2]['organizations'])
示例#21
0
 def create_normal_org(self):
     org_users = UserFactory.create_batch(2)
     return OrganizationFactory.create(add_users=org_users)
 def setup_models(self):
     self.users = UserFactory.create_batch(2)
     self.org = OrganizationFactory.create(add_users=self.users)
    def test_users_are_not_serialized(self):
        users = UserFactory.create_batch(2)
        org = OrganizationFactory.create(add_users=users)

        serializer = serializers.OrganizationSerializer([org], many=True)
        assert 'users' not in serializer.data[0]
    def test_users_are_serialized_detail_view(self):
        users = UserFactory.create_batch(2)
        org = OrganizationFactory.create(add_users=users)

        serializer = serializers.OrganizationSerializer(org, detail=True)
        assert 'users' in serializer.data
    def test_users_are_not_serialized(self):
        users = UserFactory.create_batch(2)
        org = OrganizationFactory.create(add_users=users)

        serializer = serializers.OrganizationSerializer([org], many=True)
        assert 'users' not in serializer.data[0]
 def create_normal_org(self):
     org_users = UserFactory.create_batch(2)
     return OrganizationFactory.create(add_users=org_users)
    def test_users_are_serialized_detail_view(self):
        users = UserFactory.create_batch(2)
        org = OrganizationFactory.create(add_users=users)

        serializer = serializers.OrganizationSerializer(org, detail=True)
        assert 'users' in serializer.data
 def setup_models(self):
     self.users = UserFactory.create_batch(2)
     self.org = OrganizationFactory.create(add_users=self.users)