示例#1
0
    def test_add_remove_user(self):
        from pillar.api.projects import utils as proj_utils
        from pillar.api.utils import dumps

        project_mng_user_url = '/api/p/users'

        # Use our API to add user to group
        payload = {
            'project_id': self.project_id,
            'user_id': self.other_user_id,
            'action': 'add'
        }

        resp = self.client.post(project_mng_user_url,
                                data=dumps(payload),
                                content_type='application/json',
                                headers={
                                    'Authorization': self.make_header('token'),
                                    'If-Match': self.project['_etag']
                                })
        self.assertEqual(200, resp.status_code, resp.data)

        # Check if the user is now actually member of the group.
        with self.app.test_request_context():
            users = self.app.data.driver.db['users']

            db_user = users.find_one(self.other_user_id)
            admin_group = proj_utils.get_admin_group(self.project)

            self.assertIn(admin_group['_id'], db_user['groups'])

        # Update payload to remove the user we just added
        payload['action'] = 'remove'

        resp = self.client.post(project_mng_user_url,
                                data=dumps(payload),
                                content_type='application/json',
                                headers={
                                    'Authorization': self.make_header('token'),
                                    'If-Match': self.project['_etag']
                                })
        self.assertEqual(200, resp.status_code, resp.data)

        # Check if the user is now actually removed from the group.
        with self.app.test_request_context():
            users = self.app.data.driver.db['users']

            db_user = users.find_one(self.other_user_id)
            self.assertNotIn(admin_group['_id'], db_user['groups'])
示例#2
0
    def test_remove_self(self):
        """Every user should be able to remove themselves from a project,
         regardless of permissions.
         """

        from pillar.api.projects import utils as proj_utils
        from pillar.api.utils import dumps

        project_mng_user_url = '/api/p/users'

        # Use our API to add user to group
        payload = {
            'project_id': self.project_id,
            'user_id': self.other_user_id,
            'action': 'add'
        }

        resp = self.client.post(
            project_mng_user_url,
            data=dumps(payload),
            content_type='application/json',
            headers={'Authorization': self.make_header('token')})
        self.assertEqual(200, resp.status_code, resp.data)

        # Update payload to remove the user we just added, and call it as that user.
        payload['action'] = 'remove'

        resp = self.client.post(
            project_mng_user_url,
            data=dumps(payload),
            content_type='application/json',
            headers={'Authorization': self.make_header('other-token')})
        self.assertEqual(200, resp.status_code, resp.data)

        # Check if the user is now actually removed from the group.
        with self.app.test_request_context():
            users = self.app.data.driver.db['users']

            db_user = users.find_one(self.other_user_id)
            admin_group = proj_utils.get_admin_group(self.project)
            self.assertNotIn(admin_group['_id'], db_user['groups'])