Python ddt.data() Examples

The following are 30 code examples of ddt.data(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ddt , or try the search function .
Example #1
Source File: test_api.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_no_organization(self, mock_consul):
        """
        GET+POST - An instance manager without an organization can't see/update any PR.
        """
        self.api_client.login(username='user5', password='pass')
        watched_pr = make_watched_pr_and_instance(branch_name='api-test-branch')

        response = self.api_client.get('/api/v1/pr_watch/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, [])

        response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=watched_pr.pk))
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=watched_pr.pk))
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) 
Example #2
Source File: test_gceservice.py    From cloudbase-init with Apache License 2.0 6 votes vote down vote up
def test_get_user_data_b64(self, mock_get_cache_data):
        user_data = b'fake userdata'
        user_data_b64 = 'ZmFrZSB1c2VyZGF0YQ=='
        userdata_key = "%s/user-data" % self._module.MD_INSTANCE_ATTR
        userdata_enc_key = (
            "%s/user-data-encoding" % self._module.MD_INSTANCE_ATTR)

        def _get_cache_data_side_effect(*args, **kwargs):
            if args[0] == ("%s/user-data" % self._module.MD_INSTANCE_ATTR):
                return user_data_b64
            return 'base64'
        mock_get_cache_data.side_effect = _get_cache_data_side_effect

        response = self._service.get_user_data()

        mock_calls = [mock.call(userdata_key),
                      mock.call(userdata_enc_key, decode=True)]
        mock_get_cache_data.assert_has_calls(mock_calls)
        self.assertEqual(response, user_data) 
Example #3
Source File: test_openedx_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_instance_list_admin(self, mock_consul):
        """
        Instance list should return all instances when queried from an admin
        user.
        """
        instance = OpenEdXInstanceFactory(sub_domain='test.com')
        # User 3 belongs to organization 2
        instance.ref.owner = self.organization2
        instance.save()
        instance2 = OpenEdXInstanceFactory(sub_domain='test2.com')
        # ... not to organization 1
        instance2.ref.owner = self.organization
        instance2.save()
        self.api_client.login(username='user3', password='pass')
        response = self.api_client.get('/api/v1/instance/')
        self.assertEqual(len(response.data), 2) 
Example #4
Source File: test_openedx_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_authenticated(self, username, mock_consul):
        """
        GET - Authenticated - instance manager user (superuser or not) is allowed access
        """
        self.api_client.login(username=username, password='pass')
        response = self.api_client.get('/api/v1/instance/')
        self.assertEqual(response.data, [])

        # Both user3 (superuser) and user4 (non-superuser) will see user4's instance
        instance = OpenEdXInstanceFactory(sub_domain='domain.api')
        instance.ref.creator = self.user4.profile
        instance.ref.owner = self.user4.profile.organization
        instance.save()

        response = self.api_client.get('/api/v1/instance/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        instance_data = response.data[0].items()
        self.assertIn(('domain', 'domain.api.example.com'), instance_data)
        self.assertIn(('is_archived', False), instance_data)
        self.assertIn(('appserver_count', 0), instance_data)
        self.assertIn(('active_appservers', []), instance_data)
        self.assertIn(('is_healthy', None), instance_data)
        self.assertIn(('is_steady', None), instance_data)
        self.assertIn(('status_description', ''), instance_data)
        self.assertIn(('newest_appserver', None), instance_data) 
Example #5
Source File: test_forms.py    From manila-ui with Apache License 2.0 6 votes vote down vote up
def test_create_share_type(self, mock_horizon_messages_success):
        form = self._get_form()
        data = {
            'extra_specs': '',
            'is_public': False,
            'spec_driver_handles_share_servers': 'True',
            'name': 'share',
        }

        result = form.handle(self.request, data)

        self.assertTrue(result)
        self.manilaclient.share_types.create.assert_called_once_with(
            name=data['name'],
            spec_driver_handles_share_servers='true',
            is_public=data["is_public"])
        mock_horizon_messages_success.assert_called_once_with(
            self.request, mock.ANY) 
Example #6
Source File: test_jinja2_template.py    From cloudbase-init with Apache License 2.0 6 votes vote down vote up
def test_jinja_render_template_multiple_variables(self):
        fake_instance_data = {
            'v1': {
                'localhostname': 'fake_hostname'
            },
            'ds': {
                'meta_data': {
                    'hostname': 'fake_hostname'
                },
                'meta-data': {
                    'hostname': 'fake_hostname'
                }
            }
        }
        fake_template = b'{{ds.meta_data.hostname}}'
        expected_result = b'fake_hostname'
        self._test_jinja_render_template(
            fake_instance_data=fake_instance_data,
            expected_result=expected_result,
            fake_template=fake_template) 
Example #7
Source File: test_openedx_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_newest_appserver(self, mock_consul):
        """
        GET - instance list - is 'newest_appserver' in fact the newest one?
        """
        instance, dummy = self.add_active_appserver()

        mid_app_server = make_test_appserver(instance)
        mid_app_server.is_active = True
        mid_app_server.save()  # Outside of tests, use app_server.make_active() instead

        newest_appserver = make_test_appserver(instance)

        self.api_client.login(username='user3', password='pass')
        response = self.api_client.get('/api/v1/instance/')

        self.assertEqual(response.data[0]['newest_appserver']['id'], newest_appserver.id) 
Example #8
Source File: test_openedx_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_details_unsteady(self, mock_consul):
        """
        GET - Detailed attributes
        """
        # Make app_server unsteady
        instance, app_server = self.add_active_appserver()
        app_server._status_to_waiting_for_server()
        app_server.save()

        response = self.assert_active_appserver(instance.ref.id, app_server.pk)
        instance_data = response.data.items()
        self.assertIn(('name', instance.name), instance_data)
        self.assertIn(('is_healthy', True), instance_data)
        self.assertIn(('is_steady', False), instance_data)
        self.assertIn(('status_description', 'VM not yet accessible'), instance_data)
        self.assertEqual(response.data['active_appservers'][0]['status'], 'waiting') 
Example #9
Source File: test_openedx_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_details_unhealthy(self, mock_consul):
        """
        GET - Detailed attributes
        """
        # Make app_server unhealthy
        instance, app_server = self.add_active_appserver()
        app_server._status_to_waiting_for_server()
        app_server._status_to_error()
        app_server.save()

        response = self.assert_active_appserver(instance.ref.id, app_server.pk)
        instance_data = response.data.items()
        self.assertIn(('name', instance.name), instance_data)
        self.assertIn(('is_healthy', False), instance_data)
        self.assertIn(('is_steady', True), instance_data)
        self.assertIn(('status_description', 'App server never got up and running '
                       '(something went wrong when trying to build new VM)'), instance_data)
        self.assertEqual(response.data['active_appservers'][0]['status'], 'error') 
Example #10
Source File: test_vmwareguestinfoservice.py    From cloudbase-init with Apache License 2.0 6 votes vote down vote up
def test_get_guest_data(self, test_data, expected_encoding,
                            mock_get_guestinfo_value,
                            mock_decode_data):

        (data_key, encoding_ret) = test_data
        (is_base64, is_gzip) = expected_encoding
        data_key_ret = 'fake_data'
        decoded_data = 'fake_decoded_data'

        def guest_info_side_effect(*args, **kwargs):
            if args[0] == data_key:
                return data_key_ret
            return encoding_ret

        mock_get_guestinfo_value.side_effect = guest_info_side_effect
        mock_decode_data.return_value = decoded_data

        data = self._service._get_guest_data(data_key)

        self.assertEqual(data, decoded_data)
        mock_decode_data.assert_called_once_with(data_key_ret,
                                                 is_base64, is_gzip) 
Example #11
Source File: test_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_set_notes_instance_do_nothing_if_notes_not_in_payload(self, mock_consul):
        """
        POST - Update instance notes does not change if not 'notes' field is provided
        """
        self.api_client.login(username='user3', password='pass')
        instance = OpenEdXInstanceFactory(name='Test!')

        self.assertEqual(instance.ref.notes, '')

        old_instance_ref_dict = instance.ref.__dict__.copy()
        response = self.api_client.post('/api/v1/instance/{pk}/set_notes/'.format(pk=instance.ref.pk))
        instance.ref.refresh_from_db()
        current_instance_ref_dict = {}
        for k, _ in old_instance_ref_dict.items():
            current_instance_ref_dict[k] = instance.ref.__dict__[k]

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual({'status': 'No notes value provided.'}, response.data)
        self.assertEqual(current_instance_ref_dict, old_instance_ref_dict) 
Example #12
Source File: test_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_log_entries(self, mock_consul):
        """
        GET - Log entries
        """
        self.api_client.login(username='user3', password='pass')
        instance = OpenEdXInstanceFactory(name="Test!")
        instance.logger.info("info")
        instance.logger.error("error")

        response = self.api_client.get('/api/v1/instance/{pk}/logs/'.format(pk=instance.ref.pk))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        expected_list = [
            {'level': 'INFO', 'text': 'instance.models.instance  | instance={inst_id} (Test!) | info'},
            {'level': 'ERROR', 'text': 'instance.models.instance  | instance={inst_id} (Test!) | error'},
        ]
        log_entries = response.data['log_entries']
        self.assertEqual(len(expected_list), len(log_entries))

        for expected_entry, log_entry in zip(expected_list, log_entries):
            self.assertEqual(expected_entry['level'], log_entry['level'])
            self.assertEqual(expected_entry['text'].format(inst_id=instance.ref.pk), log_entry['text'])
            self.assertEqual(expected_entry['text'].format(inst_id=instance.ref.pk), log_entry['text']) 
Example #13
Source File: test_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_app_servers_list(self, mock_consul):
        """
        GET - App Servers
        """
        self.api_client.login(username='user3', password='pass')
        instance = OpenEdXInstanceFactory(name="Test!")

        for _ in range(10):
            make_test_appserver(instance)

        response = self.api_client.get('/api/v1/instance/{pk}/app_servers/'.format(pk=instance.ref.pk))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertTrue('app_servers' in response.data)

        # Verify that all of them are returned, not only NUM_INITIAL_APPSERVERS_SHOWN
        self.assertEqual(len(response.data['app_servers']), 10)
        self.assertTrue('name' in response.data['app_servers'][0])
        self.assertTrue('name' in response.data['app_servers'][9]) 
Example #14
Source File: tests.py    From manila-ui with Apache License 2.0 6 votes vote down vote up
def test_share_group_snapshot_delete_post(self):
        data = {'action': 'share_group_snapshots__delete__%s' % self.sgs.id}
        self.mock_object(api_manila, "share_group_snapshot_delete")
        self.mock_object(
            api_manila, "share_group_snapshot_list",
            mock.Mock(return_value=[self.sgs]))

        res = self.client.post(INDEX_URL, data)

        self.assertStatusCode(res, 302)
        self.assertMessageCount(success=1)
        api_manila.share_group_snapshot_delete.assert_called_once_with(
            mock.ANY, self.sgs.id)
        api_manila.share_group_snapshot_list.assert_called_once_with(
            mock.ANY, search_opts={'all_tenants': True})
        self.assertRedirectsNoFollow(res, INDEX_URL) 
Example #15
Source File: test_api.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_update_instance_branch_delete(self, mock_get_pr_by_number, mock_get_commit_id_from_ref, mock_consul):
        """
        Test what happens when we try to update an instance for a PR whose branch has been
        deleted.

        Note: Once WatchedPullRequest.update_instance_from_pr() has been refactored so that it
        first queries GitHub for PR details (rather than accepting a PR parameter), it can get
        the commit ID from the PR details response, rather than using get_branch_tip(), and then
        this test won't be necessary since the PR API always contains the commit information
        (in ["head"]["sha"]) even if the branch has been deleted.
        """
        self.api_client.login(username='user3', password='pass')

        watched_pr = make_watched_pr_and_instance()
        mock_get_pr_by_number.return_value = PRFactory(number=watched_pr.github_pr_number)
        response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=watched_pr.pk))
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, {'error': 'Could not fetch updated details from GitHub.'}) 
Example #16
Source File: tests.py    From manila-ui with Apache License 2.0 6 votes vote down vote up
def test_update_share_metadata_post(self):
        share = test_data.share_with_metadata
        data = {
            'metadata': 'aaa=ccc',
        }
        form_data = {
            'metadata': {'aaa': 'ccc'},
        }
        url = reverse(
            'horizon:project:shares:update_metadata', args=[share.id])
        self.mock_object(
            api_manila, "share_get", mock.Mock(return_value=share))
        self.mock_object(api_manila, "share_set_metadata")
        self.mock_object(
            neutron, "is_service_enabled", mock.Mock(return_value=[True]))

        res = self.client.post(url, data)

        api_manila.share_set_metadata.assert_called_once_with(
            mock.ANY, share, form_data['metadata'])
        self.assertRedirectsNoFollow(res, INDEX_URL) 
Example #17
Source File: tests.py    From manila-ui with Apache License 2.0 6 votes vote down vote up
def test_share_group_snapshot_delete_post(self):
        data = {'action': 'share_group_snapshots__delete__%s' % self.sgs.id}
        self.mock_object(api_manila, "share_group_snapshot_delete")
        self.mock_object(
            api_manila, "share_group_snapshot_list",
            mock.Mock(return_value=[self.sgs]))

        res = self.client.post(INDEX_URL, data)

        self.assertStatusCode(res, 302)
        self.assertMessageCount(success=1)
        api_manila.share_group_snapshot_delete.assert_called_once_with(
            mock.ANY, self.sgs.id)
        api_manila.share_group_snapshot_list.assert_called_once_with(
            mock.ANY, search_opts={'all_tenants': True})
        self.assertRedirectsNoFollow(res, INDEX_URL) 
Example #18
Source File: test_forms.py    From manila-ui with Apache License 2.0 6 votes vote down vote up
def test_handle_success_only_unset(self, mock_horizon_messages_success):
        initial = {
            'id': 'fake_id',
            'name': 'fake_name',
            'extra_specs': {'foo': 'bar'}
        }
        form = self._get_form(initial)
        data = {'extra_specs': 'foo\r\n'}
        share_types_get = self.manilaclient.share_types.get
        share_types_get.return_value.get_keys.return_value = {
            'foo': 'bar', 'quuz': 'zaab'}

        result = form.handle(self.request, data)

        self.assertTrue(result)
        mock_horizon_messages_success.assert_called_once_with(
            mock.ANY, mock.ANY)
        self.manilaclient.share_types.get.assert_has_calls([
            mock.call(initial['id'])])
        share_types_get.return_value.get_keys.assert_called_once_with()
        self.assertFalse(share_types_get.return_value.set_keys.called)
        share_types_get.return_value.unset_keys.assert_called_once_with(
            {'foo'}) 
Example #19
Source File: test_forms.py    From manila-ui with Apache License 2.0 6 votes vote down vote up
def test_handle_success_set_and_unset(self, mock_horizon_messages_success):
        initial = {
            'id': 'fake_id',
            'name': 'fake_name',
            'extra_specs': {'foo': 'bar'}
        }
        form = self._get_form(initial)
        data = {'extra_specs': 'foo\r\nquuz=zaab'}
        share_types_get = self.manilaclient.share_types.get
        share_types_get.return_value.get_keys.return_value = {'foo': 'bar'}

        result = form.handle(self.request, data)

        self.assertTrue(result)
        mock_horizon_messages_success.assert_called_once_with(
            mock.ANY, mock.ANY)
        self.manilaclient.share_types.get.assert_has_calls([
            mock.call(initial['id'])])
        share_types_get.return_value.get_keys.assert_called_once_with()
        share_types_get.return_value.set_keys.assert_called_once_with(
            {'quuz': 'zaab'})
        share_types_get.return_value.unset_keys.assert_called_once_with(
            {'foo'}) 
Example #20
Source File: test_instance.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_not_all_appservers_are_loaded_by_default(self, mock_consul):
        """
        Tries to add e.g. 7 appservers and then verifies that only 5 are returned initially.
        That is, the results are filtered by the NUM_INITIAL_APPSERVERS_SHOWN setting.
        """
        self.api_client.login(username='user3', password='pass')
        instance = OpenEdXInstanceFactory(sub_domain='domain.api')
        for _ in range(settings.NUM_INITIAL_APPSERVERS_SHOWN + 2):
            make_test_appserver(instance)

        response = self.api_client.get('/api/v1/instance/{pk}/'.format(pk=instance.ref.pk))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # After creating e.g. 7, we check that 7 exist but only 5 are loaded
        self.assertEqual(response.data['appserver_count'], settings.NUM_INITIAL_APPSERVERS_SHOWN + 2)
        self.assertTrue(
            len(response.data['appservers']) <= settings.NUM_INITIAL_APPSERVERS_SHOWN,
            "Too many initial app servers for instance detail"
        ) 
Example #21
Source File: test_volumeops.py    From compute-hyperv with Apache License 2.0 6 votes vote down vote up
def _test_get_disk_resource_path_by_conn_info(self,
                                                  mock_get_disk_res_path,
                                                  disk_found=True):
        conn_info = get_fake_connection_info()
        mock_vol_paths = [mock.sentinel.disk_path] if disk_found else []
        self._conn.get_volume_paths.return_value = mock_vol_paths

        if disk_found:
            disk_res_path = self._base_vol_driver.get_disk_resource_path(
                conn_info)

            self._conn.get_volume_paths.assert_called_once_with(
                conn_info['data'])
            self.assertEqual(mock_get_disk_res_path.return_value,
                             disk_res_path)
            mock_get_disk_res_path.assert_called_once_with(
                mock.sentinel.disk_path)
        else:
            self.assertRaises(exception.DiskNotFound,
                              self._base_vol_driver.get_disk_resource_path,
                              conn_info) 
Example #22
Source File: test_volumeops.py    From compute-hyperv with Apache License 2.0 6 votes vote down vote up
def test_detach_volume(self, update_device_metadata,
                           mock_update_dev_meta,
                           mock_get_volume_driver):
        mock_instance = fake_instance.fake_instance_obj()
        fake_volume_driver = mock_get_volume_driver.return_value
        fake_conn_info = {'data': 'fake_conn_info_data'}

        self._volumeops.detach_volume(mock.sentinel.context,
                                      fake_conn_info,
                                      mock_instance,
                                      update_device_metadata)

        mock_get_volume_driver.assert_called_once_with(
            fake_conn_info)
        fake_volume_driver.detach_volume.assert_called_once_with(
            fake_conn_info, mock_instance.name)
        fake_volume_driver.disconnect_volume.assert_called_once_with(
            fake_conn_info)

        if update_device_metadata:
            mock_update_dev_meta.assert_called_once_with(
                mock.sentinel.context, mock_instance)
        else:
            mock_update_dev_meta.assert_not_called() 
Example #23
Source File: test_gceservice.py    From cloudbase-init with Apache License 2.0 5 votes vote down vote up
def test_get_user_data(self, mock_get_cache_data):
        response = self._service.get_user_data()
        userdata_key = "%s/user-data" % self._module.MD_INSTANCE_ATTR
        userdata_enc_key = (
            "%s/user-data-encoding" % self._module.MD_INSTANCE_ATTR)
        mock_calls = [mock.call(userdata_key),
                      mock.call(userdata_enc_key, decode=True)]
        mock_get_cache_data.assert_has_calls(mock_calls)
        self.assertEqual(mock_get_cache_data.return_value,
                         response) 
Example #24
Source File: test_lib_reporter.py    From OpenDoor with GNU General Public License v3.0 5 votes vote down vote up
def test_load(self, value):
        """ Reporter.load() test """

        expected = Reporter.load(value, 'test.local', {})
        self.assertIsInstance(expected, PluginProvider)
        
    # @data('std', 'txt', 'json', 'html')
    # def test_process(self, ext):
    #     """ Reporter.load().process() test """
    #
    #     report = Reporter.load(ext, 'test.local', self.mockdata)
    #     self.assertIsNone(report.process())
    #     if ext in ['html','json']:
    #         self.assertTrue(filesystem.is_exist('tests/reports/test.local', 'test.local.{0}'.format(ext)))
    #     if ext in ['txt']:
    #         self.assertTrue(filesystem.is_exist('tests/reports/test.local', 'success.{0}'.format(ext)))
    #     shutil.rmtree('tests/reports')
        
    # @data('std', 'txt', 'json', 'html')
    # def test_load_plugin_exception(self, ext):
    #     """ Reporter.load() exception test """
    #
    #     if ext in ['html','json', 'txt']:
    #
    #         PluginProvider.CONFIG_FILE = 'wrong.cfg'
    #         with self.assertRaises(Exception) as context:
    #             Reporter.load(ext, 'test.local', self.mockdata)
    #             self.assertTrue(Exception == context.expected) 
Example #25
Source File: test_api.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_filtered_by_organization(self, mock_get_pr_by_number, mock_get_commit_id_from_ref, mock_consul):
        """
        GET+POST - A user (instance manager) can only manage PRs from WF which belong to the user's organization.
        """
        wpr1 = make_watched_pr_and_instance(username='user1', organization=self.organization)
        wpr2 = make_watched_pr_and_instance(username='user4', organization=self.organization2)
        self.assertEqual(WatchedPullRequest.objects.count(), 2)

        # We'll log in with user4, and we should only see pr2, but not pr1
        self.api_client.login(username='user4', password='pass')

        # Check the PR list
        response = self.api_client.get('/api/v1/pr_watch/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertNotIn(('id', wpr1.pk), response.data[0].items())
        self.assertIn(('id', wpr2.pk), response.data[0].items())

        # Also check the detailed view
        response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=wpr1.pk))
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=wpr2.pk))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # Also check update_instance
        mock_get_pr_by_number.return_value = PRFactory(number=wpr1.github_pr_number)
        response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=wpr1.pk))
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        mock_get_pr_by_number.return_value = PRFactory(number=wpr2.github_pr_number)
        response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=wpr2.pk))
        self.assertEqual(response.status_code, status.HTTP_200_OK) 
Example #26
Source File: test_lib_browser.py    From OpenDoor with GNU General Public License v3.0 5 votes vote down vote up
def __configuration(self):
        test_config = filesystem.getabsname(os.path.join('tests', 'data', 'setup-scan.cfg'))
        config = RawConfigParser()
        config.read(test_config)
        return config 
Example #27
Source File: test_vmwareguestinfoservice.py    From cloudbase-init with Apache License 2.0 5 votes vote down vote up
def test_get_public_keys(self, keys_data, expected_keys):
        self._service._meta_data = {
            "public-keys-data": keys_data
        }
        public_keys = self._service.get_public_keys()
        public_keys.sort()
        expected_keys.sort()
        self.assertEqual(public_keys, expected_keys) 
Example #28
Source File: test_api.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_permission_denied(self, username, mock_consul):
        """
        GET - basic and staff users denied access
        """
        forbidden_message = {"detail": "You do not have permission to perform this action."}

        self.api_client.login(username=username, password='pass')
        response = self.api_client.get('/api/v1/pr_watch/')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(response.data, forbidden_message)

        watched_pr = make_watched_pr_and_instance()
        response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=watched_pr.pk))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(response.data, forbidden_message) 
Example #29
Source File: test_api.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_unauthenticated(self, mock_consul):
        """
        GET - Require to be authenticated
        """
        forbidden_message = {"detail": "Authentication credentials were not provided."}

        response = self.api_client.get('/api/v1/pr_watch/')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(response.data, forbidden_message)

        watched_pr = make_watched_pr_and_instance()
        response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=watched_pr.pk))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(response.data, forbidden_message) 
Example #30
Source File: test_api.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_authenticated(self, username, mock_consul):
        """
        GET - Authenticated - instance manager users (superuser or not) allowed access
        """
        self.api_client.login(username=username, password='pass')
        response = self.api_client.get('/api/v1/pr_watch/')
        self.assertEqual(response.data, [])

        # This uses user4's organization. Both user3 and user4 will be able to see it later
        watched_pr = make_watched_pr_and_instance(
            branch_name='api-test-branch',
            username='user4',
            organization=self.organization2
        )

        def check_output(data):
            """ Check that the data object passed matches expectations for 'watched_pr' """
            data = data.items()
            self.assertIn(('id', watched_pr.pk), data)
            self.assertIn(('fork_name', 'fork/repo'), data)
            self.assertIn(('target_fork_name', 'source/repo'), data)
            self.assertIn(('branch_name', 'api-test-branch'), data)
            self.assertIn(('github_pr_number', watched_pr.github_pr_number), data)
            self.assertIn(('github_pr_url', watched_pr.github_pr_url), data)
            self.assertIn(('instance_id', watched_pr.instance.ref.id), data)

        response = self.api_client.get('/api/v1/pr_watch/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        check_output(response.data[0])

        # And check the details view:
        response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=watched_pr.pk))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        check_output(response.data)