Exemplo n.º 1
0
    def test_check_get_user_role(
        self, mock_get_user_public_keys, mock_is_authenticated, mock_get_test_meta_key, mock_pecan_abort
    ):
        # Check user level
        mock_get_test_meta_key.return_value = None
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role("fake_test"))
        api_utils.enforce_permissions("fake_test", const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_OWNER)

        mock_get_test_meta_key.side_effect = {
            ("fake_test", const.PUBLIC_KEY): "fake key",
            ("fake_test", const.SHARED_TEST_RUN): "true",
        }.get
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role("fake_test"))
        api_utils.enforce_permissions("fake_test", const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_OWNER)

        mock_is_authenticated.return_value = True
        mock_get_user_public_keys.return_value = [{"format": "fake", "pubkey": "key"}]
        mock_get_test_meta_key.side_effect = {
            ("fake_test", const.PUBLIC_KEY): "fake key",
            ("fake_test", const.SHARED_TEST_RUN): "true",
        }.get
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role("fake_test"))
        api_utils.enforce_permissions("fake_test", const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_OWNER)

        # Check owner level
        mock_is_authenticated.return_value = True
        mock_get_user_public_keys.return_value = [{"format": "fake", "pubkey": "key"}]
        mock_get_test_meta_key.side_effect = lambda *args: {
            ("fake_test", const.PUBLIC_KEY): "fake key",
            ("fake_test", const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertEqual(const.ROLE_OWNER, api_utils.get_user_role("fake_test"))
        api_utils.enforce_permissions("fake_test", const.ROLE_USER)
        api_utils.enforce_permissions("fake_test", const.ROLE_OWNER)

        # Check negative cases
        mock_is_authenticated.return_value = False
        mock_get_test_meta_key.side_effect = lambda *args: {
            ("fake_test", const.PUBLIC_KEY): "fake key",
            ("fake_test", const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_OWNER)

        mock_is_authenticated.return_value = True
        mock_get_user_public_keys.return_value = [{"format": "fake", "pubkey": "key"}]
        mock_get_test_meta_key.side_effect = lambda *args: {
            ("fake_test", const.PUBLIC_KEY): "fake other_key",
            ("fake_test", const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertEqual(None, api_utils.get_user_role("fake_test"))
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions, "fake_test", const.ROLE_OWNER)
Exemplo n.º 2
0
 def get_one(self, test_id):
     """Handler for getting item."""
     if api_utils.get_user_role(test_id) == const.ROLE_OWNER:
         test_info = db.get_test(test_id, allowed_keys=["id", "cpid", "created_at", "duration_seconds", "meta"])
     else:
         test_info = db.get_test(test_id)
     test_list = db.get_test_results(test_id)
     test_name_list = [test_dict["name"] for test_dict in test_list]
     test_info.update({"results": test_name_list, "user_role": api_utils.get_user_role(test_id)})
     return test_info
Exemplo n.º 3
0
 def get_one(self, test_id):
     """Handler for getting item."""
     if api_utils.get_user_role(test_id) == const.ROLE_OWNER:
         test_info = db.get_test(
             test_id, allowed_keys=['id', 'cpid', 'created_at',
                                    'duration_seconds', 'meta']
         )
     else:
         test_info = db.get_test(test_id)
     test_list = db.get_test_results(test_id)
     test_name_list = [test_dict['name'] for test_dict in test_list]
     test_info.update({'results': test_name_list,
                       'user_role': api_utils.get_user_role(test_id)})
     return test_info
Exemplo n.º 4
0
    def get_one(self, test_id):
        """Handler for getting item."""
        user_role = api_utils.get_user_role(test_id)
        if user_role in (const.ROLE_FOUNDATION, const.ROLE_OWNER):
            test_info = db.get_test(test_id,
                                    allowed_keys=[
                                        'id', 'cpid', 'created_at',
                                        'duration_seconds', 'meta',
                                        'product_version',
                                        'verification_status'
                                    ])
        else:
            test_info = db.get_test(test_id)
        test_list = db.get_test_results(test_id)
        test_name_list = [test_dict['name'] for test_dict in test_list]
        test_info.update({'results': test_name_list, 'user_role': user_role})

        if user_role not in (const.ROLE_FOUNDATION, const.ROLE_OWNER):
            # Don't expose product information if product is not public.
            if (test_info.get('product_version') and
                    not test_info['product_version']['product_info']['public']
                ):

                test_info['product_version'] = None

            test_info['meta'] = {
                k: v
                for k, v in test_info['meta'].items()
                if k in MetadataController.rw_access_keys
            }
        return test_info
Exemplo n.º 5
0
 def get_one(self, test_id, key):
     """Get value for key from test run metadata."""
     role = api_utils.get_user_role(test_id)
     if role in (const.ROLE_FOUNDATION, const.ROLE_OWNER):
         return db.get_test_meta_key(test_id, key)
     elif role in (const.ROLE_USER) and key in self.rw_access_keys:
         return db.get_test_meta_key(test_id, key)
     pecan.abort(403)
Exemplo n.º 6
0
 def get(self, test_id):
     """Get test run metadata."""
     test_info = db.get_test_result(test_id)
     role = api_utils.get_user_role(test_id)
     if role in (const.ROLE_FOUNDATION, const.ROLE_OWNER):
         return test_info['meta']
     elif role in (const.ROLE_USER):
         return {k: v for k, v in test_info['meta'].items()
                 if k in self.rw_access_keys}
     pecan.abort(403)
Exemplo n.º 7
0
    def get_one(self, test_id):
        """Handler for getting item."""
        if api_utils.get_user_role(test_id) == const.ROLE_OWNER:
            test_info = db.get_test(
                test_id, allowed_keys=['id', 'cpid', 'created_at',
                                       'duration_seconds', 'meta']
            )
        else:
            test_info = db.get_test(test_id)
        test_list = db.get_test_results(test_id)
        test_name_list = [test_dict['name'] for test_dict in test_list]
        test_info.update({'results': test_name_list,
                          'user_role': api_utils.get_user_role(test_id)})

        cloud_id = test_info['cpid']
        cloud = db.get_cloud(cloud_id)
        if cloud:
            test_info.update({'cloud_name': cloud['name'],
                              'cloud_description': cloud['description'],
                              'cloud_shared': cloud['shared']})

        return test_info
Exemplo n.º 8
0
    def test_check_get_user_role(self, mock_get_user_id, mock_is_authenticated,
                                 mock_get_test, mock_get_test_meta_key,
                                 mock_pecan_abort, mock_check_foundation):
        # Check user level
        mock_check_foundation.return_value = False
        mock_get_test_meta_key.return_value = None
        mock_get_test.return_value = {}
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        mock_get_test_meta_key.side_effect = {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): 'true',
        }.get
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        mock_is_authenticated.return_value = True
        mock_get_user_id.return_value = 'fake_openid'
        mock_get_test_meta_key.side_effect = {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): 'true',
        }.get
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        # Check owner level
        mock_is_authenticated.return_value = True
        mock_get_user_id.return_value = 'fake_openid'
        mock_get_test_meta_key.side_effect = lambda *args: {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertEqual(const.ROLE_OWNER,
                         api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        api_utils.enforce_permissions('fake_test', const.ROLE_OWNER)

        # Check negative cases
        mock_is_authenticated.return_value = False
        mock_get_test_meta_key.side_effect = lambda *args: {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        mock_is_authenticated.return_value = True
        mock_get_user_id.return_value = 'fake_openid'
        mock_get_test_meta_key.side_effect = lambda *args: {
            ('fake_test', const.USER): 'some_other_user',
            ('fake_test', const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertIsNone(api_utils.get_user_role('fake_test'))
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)
Exemplo n.º 9
0
    def test_check_get_user_role(self, mock_get_user_id,
                                 mock_is_authenticated,
                                 mock_get_test_result,
                                 mock_get_test_result_meta_key,
                                 mock_pecan_abort,
                                 mock_check_foundation):
        # Check user level
        mock_check_foundation.return_value = False
        mock_get_test_result_meta_key.return_value = None
        mock_get_test_result.return_value = {}
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        mock_get_test_result_meta_key.side_effect = {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): 'true',
        }.get
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        mock_is_authenticated.return_value = True
        mock_get_user_id.return_value = 'fake_openid'
        mock_get_test_result_meta_key.side_effect = {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): 'true',
        }.get
        self.assertEqual(const.ROLE_USER, api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        # Check owner level
        mock_is_authenticated.return_value = True
        mock_get_user_id.return_value = 'fake_openid'
        mock_get_test_result_meta_key.side_effect = lambda *args: {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertEqual(const.ROLE_OWNER,
                         api_utils.get_user_role('fake_test'))
        api_utils.enforce_permissions('fake_test', const.ROLE_USER)
        api_utils.enforce_permissions('fake_test', const.ROLE_OWNER)

        # Check negative cases
        mock_is_authenticated.return_value = False
        mock_get_test_result_meta_key.side_effect = lambda *args: {
            ('fake_test', const.USER): 'fake_openid',
            ('fake_test', const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)

        mock_is_authenticated.return_value = True
        mock_get_user_id.return_value = 'fake_openid'
        mock_get_test_result_meta_key.side_effect = lambda *args: {
            ('fake_test', const.USER): 'some_other_user',
            ('fake_test', const.SHARED_TEST_RUN): None,
        }.get(args)
        self.assertIsNone(api_utils.get_user_role('fake_test'))
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_USER)
        self.assertRaises(exc.HTTPError, api_utils.enforce_permissions,
                          'fake_test', const.ROLE_OWNER)