Пример #1
0
    def test_resource_registering_download_multipart(self):

        f = open(settings.BASEDIR + '/wstore/test/test_usdl.rdf')
        f1 = StringIO(f.read())
        f.close()

        f1.name = 'test_usdl.rdf'

        data = {
            'name': 'Download',
            'version': '1.0',
            'description': 'This service is in charge of maintaining historical info for Smart Cities',
            'type': 'download',
            'content_type': 'application/rdf+xml'
        }

        provider = User.objects.get(username='******')
        register_resource(provider, data, f1)

        res = Resource.objects.get(name='Download')
        self.assertEqual(res.version, '1.0')
        self.assertEqual(res.resource_path, '/media/resources/test_user__Download__1.0__test_usdl.rdf')

        res_path = settings.BASEDIR + res.resource_path
        os.remove(res_path)
Пример #2
0
    def create(self, request):

        user = request.user
        profile = user.userprofile
        content_type = get_content_type(request)[0]

        if 'provider' in profile.get_current_roles():

            try:
                if content_type == 'application/json':
                    data = json.loads(request.raw_post_data)
                    register_resource(user, data)
                else:
                    data = json.loads(request.POST['json'])
                    f = request.FILES['file']
                    register_resource(user, data, file_=f)

            except ConflictError as e:
                return build_response(request, 409, unicode(e))
            except Exception as e:
                return build_response(request, 400, unicode(e))
        else:
            return build_response(request, 403, "You don't have the provider role")

        return build_response(request, 201, 'Created')
Пример #3
0
    def test_resource_registering_plugin(self, name, encoder=None, err_type=None, err_msg=None):

        data = {
            'name': 'Download',
            'version': '1.0',
            'description': 'This service is in charge of maintaining historical info for Smart Cities',
            'content': {
                'name': 'test_usdl.rdf',
                'data': ''
            },
            'resource_type': 'test_plugin',
            'content_type': 'application/x-widget'
        }

        # Create plugin module mocks
        plugin_mock = MagicMock(name="test_plugin")
        plugin_mock.on_pre_create_validation.return_value = data
        wstore.offerings.resource_plugins.decorators.load_plugin_module = MagicMock(name="load_plugin_module")
        wstore.offerings.resource_plugins.decorators.load_plugin_module.return_value = plugin_mock
        reload(resources_management)

        self.setUp()

        if encoder is not None:
            encoder(self, data)

        provider = User.objects.get(username='******')

        error = None
        try:
            resources_management.register_resource(provider, data)
        except Exception as e:
            error = e

        if err_type is None:
            self.assertEquals(error, None)
            # Check event calls
            expected_data = {
                'name': 'Download',
                'metadata': {},
                'content_path': '/media/resources/test_user__Download__1.0__test_usdl.rdf',
                'version': '1.0',
                'link': '',
                'content_type': 'application/x-widget',
                'open': False,
                'resource_type': 'test_plugin',
                'description': 'This service is in charge of maintaining historical info for Smart Cities'
            }
            plugin_mock.on_pre_create.assert_called_once_with(provider.userprofile.current_organization, expected_data)

            res = Resource.objects.get(name=data['name'], version=data['version'])
            plugin_mock.on_post_create.assert_called_once_with(res)

        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(error), err_msg)
Пример #4
0
    def test_resource_registering(self, data, encoder=None, is_file=False, uploaded=True, err_type=None, err_msg=None):

        # Call the encoder for the data if needed
        if encoder:
            encoder(self, data)

        f1 = None
        if is_file:
            f = open(settings.BASEDIR + '/wstore/test/test_usdl.rdf')
            f1 = StringIO(f.read())
            f.close()
            f1.name = 'test_usdl.rdf'

        # Build the provider
        provider = User.objects.get(username='******')

        # Call the method
        error = None
        try:
            resources_management.register_resource(provider, data, file_=f1)
        except Exception as e:
            error = e

        # Check result
        if not err_type:
            self.assertEquals(error, None)
            res = Resource.objects.get(name=data['name'], version=data['version'])
            self.assertEquals(res.version, data['version'])
            self.assertEquals(res.content_type, data['content_type'])
            self.assertEquals(res.resource_uri, "http://localhost/api/offering/resources/test_user/" + data['name'].replace(' ', '%20') + "/" + data["version"])

            if uploaded:
                self.assertEquals(res.resource_usdl, "http://testrepo.com/resource1")
            else:
                self.assertEquals(res.resource_usdl, "")

            if 'content' in data or is_file:
                if is_file:
                    f_name = f1.name
                else:
                    f_name = data['content']['name']

                self.assertEquals(res.resource_path, '/media/resources/' + 'test_user__' + data['name'] + '__' + data['version'] + '__' + f_name)
                res_path = settings.BASEDIR + res.resource_path
                os.remove(res_path)
            elif 'link' in data:
                self.assertEquals(res.download_link, data['link'])

        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)
Пример #5
0
    def test_resource_registering(self, data, encoder=None, is_file=False, err_type=None, err_msg=None):

        # Call the encoder for the data if needed
        if encoder:
            encoder(self, data)

        f1 = None
        if is_file:
            f = open(settings.BASEDIR + '/wstore/test/test_usdl.rdf')
            f1 = StringIO(f.read())
            f.close()
            f1.name = 'test_usdl.rdf'

        # Build the provider
        provider = User.objects.get(username='******')

        # Call the method
        error = None
        try:
            resources_management.register_resource(provider, data, file_=f1)
        except Exception as e:
            error = e

        # Check result
        if not err_type:
            self.assertEquals(error, None)
            res = Resource.objects.get(name=data['name'], version=data['version'])
            self.assertEquals(res.version, data['version'])
            self.assertEquals(res.content_type, data['content_type'])

            if 'content' in data or is_file:
                if is_file:
                    f_name = f1.name
                else:
                    f_name = data['content']['name']

                self.assertEquals(res.resource_path, settings.BASE_URL + 'media/resources/' + 'test_user__' + data['name'] + '__' + data['version'] + '__' + f_name)
                pathSuffix = res.resource_path.replace(settings.BASE_URL, '/', 1)
                res_path = settings.DATADIR + pathSuffix
                os.remove(res_path)
            elif 'link' in data:
                self.assertEquals(res.download_link, data['link'])
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)
Пример #6
0
    def test_resource_registering_download_link(self):

        data = {
            'name': 'History Mod',
            'version': '1.0',
            'description': 'This service is in charge of maintaining historical info for Smart Cities',
            'type': 'download',
            'link': 'https://historymod.com/download',
            'content_type': 'text/plain'
        }

        provider = User.objects.get(username='******')
        register_resource(provider, data)

        res = Resource.objects.get(name='History Mod')

        self.assertEqual(res.version, '1.0')
        self.assertEqual(res.download_link, 'https://historymod.com/download')
        self.assertEqual(res.content_type, 'text/plain')
Пример #7
0
    def test_resource_registering_invalid_version(self):

        data = {
            'name': 'Invalid',
            'version': '1.0a',
            'description': '',
            'type': 'download',
            'link': 'https://existing.com/download'
        }

        provider = User.objects.get(username='******')

        error = False
        msg = None
        try:
            register_resource(provider, data)
        except Exception, e:
            error = True
            msg = e.message
Пример #8
0
    def create(self, request):

        user = request.user
        profile = user.userprofile
        content_type = get_content_type(request)[0]

        if 'provider' in profile.get_current_roles():

            if content_type == 'application/json':
                try:
                    data = json.loads(request.raw_post_data)
                    register_resource(user, data)
                except Exception, e:
                    return build_response(request, 400, e.message)
            else:
                try:
                    data = json.loads(request.POST['json'])
                    f = request.FILES['file']
                    register_resource(user, data, file_=f)
                except Exception, e:
                    return build_response(request, 400, e.message)
Пример #9
0
    def test_resource_registering_already_existing(self):

        data = {
            'name': 'Existing',
            'version': '1.0',
            'description': '',
            'type': 'download',
            'link': 'https://existing.com/download'
        }

        provider = User.objects.get(username='******')
        org = Organization.objects.get(name=provider.username)
        resource = Resource.objects.get(name='Existing')
        resource.provider = org
        resource.save()

        error = False
        msg = None
        try:
            register_resource(provider, data)
        except Exception, e:
            error = True
            msg = e.message
Пример #10
0
    def test_resource_registering_plugin(self,
                                         name,
                                         encoder=None,
                                         err_type=None,
                                         err_msg=None):

        data = {
            'name': 'Download',
            'version': '1.0',
            'description':
            'This service is in charge of maintaining historical info for Smart Cities',
            'content': {
                'name': 'test_usdl.rdf',
                'data': ''
            },
            'resource_type': 'test_plugin',
            'content_type': 'application/x-widget'
        }

        # Create plugin module mocks
        plugin_mock = MagicMock(name="test_plugin")
        plugin_mock.on_pre_create_validation.return_value = data
        wstore.offerings.resource_plugins.decorators.load_plugin_module = MagicMock(
            name="load_plugin_module")
        wstore.offerings.resource_plugins.decorators.load_plugin_module.return_value = plugin_mock
        reload(resources_management)

        self.setUp()

        if encoder is not None:
            encoder(self, data)

        provider = User.objects.get(username='******')

        error = None
        try:
            resources_management.register_resource(provider, data)
        except Exception as e:
            error = e

        if err_type is None:
            self.assertEquals(error, None)
            # Check event calls
            expected_data = {
                'name':
                'Download',
                'metadata': {},
                'content_path':
                '/media/resources/test_user__Download__1.0__test_usdl.rdf',
                'version':
                '1.0',
                'link':
                '',
                'content_type':
                'application/x-widget',
                'open':
                False,
                'resource_type':
                'test_plugin',
                'description':
                'This service is in charge of maintaining historical info for Smart Cities'
            }
            plugin_mock.on_pre_create.assert_called_once_with(
                provider.userprofile.current_organization, expected_data)

            res = Resource.objects.get(name=data['name'],
                                       version=data['version'])
            plugin_mock.on_post_create.assert_called_once_with(res)

        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(error), err_msg)
Пример #11
0
    def test_resource_registering(self,
                                  data,
                                  encoder=None,
                                  is_file=False,
                                  uploaded=True,
                                  err_type=None,
                                  err_msg=None):

        # Call the encoder for the data if needed
        if encoder:
            encoder(self, data)

        f1 = None
        if is_file:
            f = open(settings.BASEDIR + '/wstore/test/test_usdl.rdf')
            f1 = StringIO(f.read())
            f.close()
            f1.name = 'test_usdl.rdf'

        # Build the provider
        provider = User.objects.get(username='******')

        # Call the method
        error = None
        try:
            resources_management.register_resource(provider, data, file_=f1)
        except Exception as e:
            error = e

        # Check result
        if not err_type:
            self.assertEquals(error, None)
            res = Resource.objects.get(name=data['name'],
                                       version=data['version'])
            self.assertEquals(res.version, data['version'])
            self.assertEquals(res.content_type, data['content_type'])
            self.assertEquals(
                res.resource_uri,
                "http://localhost/api/offering/resources/test_user/" +
                data['name'].replace(' ', '%20') + "/" + data["version"])

            if uploaded:
                self.assertEquals(res.resource_usdl,
                                  "http://testrepo.com/resource1")
            else:
                self.assertEquals(res.resource_usdl, "")

            if 'content' in data or is_file:
                if is_file:
                    f_name = f1.name
                else:
                    f_name = data['content']['name']

                self.assertEquals(
                    res.resource_path, '/media/resources/' + 'test_user__' +
                    data['name'] + '__' + data['version'] + '__' + f_name)
                res_path = settings.BASEDIR + res.resource_path
                os.remove(res_path)
            elif 'link' in data:
                self.assertEquals(res.download_link, data['link'])

        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)