Esempio n. 1
0
    def test_bind_mix_resources(self):
        data = [{
            'name': 'test_resource1',
            'version': '1.0'
        },
        {
            'name': 'test_resource3',
            'version': '1.0'
        }]
        offering = Offering.objects.get(name='test_offering2')
        provider = User.objects.get(username='******')

        org = Organization.objects.get(name=provider.username)
        resource = Resource.objects.get(name='test_resource1')
        resource.provider = org
        resource.save()

        resource = Resource.objects.get(name='test_resource3')
        resource.provider = org
        resource.save()

        offerings_management.bind_resources(offering, data, provider)
        offering = Offering.objects.get(name='test_offering2')

        self.assertEqual(len(offering.resources), 2)
        resource = Resource.objects.get(pk=offering.resources[0])
        self.assertEqual(resource.name, 'test_resource1')
        self.assertEqual(resource.version, '1.0')
        resource = Resource.objects.get(pk=offering.resources[1])
        self.assertEqual(resource.name, 'test_resource3')
        self.assertEqual(resource.version, '1.0')
        resource = Resource.objects.get(name='test_resource2')
        self.assertEqual(len(resource.offerings), 0)
Esempio n. 2
0
    def test_unbind_resources(self):
        data = []

        offering = Offering.objects.get(name='test_offering2')
        provider = User.objects.get(username='******')
        offerings_management.bind_resources(offering, data, provider)
        offering = Offering.objects.get(name='test_offering2')

        self.assertEqual(len(offering.resources), 0)
Esempio n. 3
0
    def test_bind_not_existing_resource(self):
        data = [{
            'name': 'test_resource4',
            'version': '1.0'
        }]
        offering = Offering.objects.get(name='test_offering1')
        provider = User.objects.get(username='******')
        error = False
        try:
            offerings_management.bind_resources(offering, data, provider)
        except:
            error = True

        self.assertTrue(error)
Esempio n. 4
0
    def test_binding(self, data, offering_name, side_effect=None, err_type=None, err_msg=None):

        if side_effect:
            side_effect(self)

        offering = Offering.objects.get(name=offering_name)
        provider = User.objects.get(username="******")
        org = Organization.objects.get(name=provider.username)

        self._fill_resources_org(data, org)

        error = None
        try:
            offerings_management.bind_resources(offering, data, provider)
        except Exception as e:
            error = e

        if not err_type:
            self.assertEquals(error, None)
            # Refresh offering object
            offering = Offering.objects.get(name=offering_name)
            # Check binding
            self.assertEquals(len(offering.resources), len(data))

            for off_res in offering.resources:
                found = False
                res = Resource.objects.get(pk=off_res)

                for exp_res in data:
                    if res.name == exp_res["name"]:
                        self.assertEquals(res.version, exp_res["version"])
                        found = True
                        break
                self.assertTrue(found)

            # Check repository calls
            if offering.open:
                pass
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)
Esempio n. 5
0
    def test_binding(self, data, offering_name, side_effect=None, err_type=None, err_msg=None):

        if side_effect:
            side_effect(self)

        offering = Offering.objects.get(name=offering_name)
        provider = User.objects.get(username='******')
        org = Organization.objects.get(name=provider.username)

        self._fill_resources_org(data, org)

        error = None
        try:
            offerings_management.bind_resources(offering, data, provider)
        except Exception as e:
            error = e

        if not err_type:
            self.assertEquals(error, None)
            # Refresh offering object
            offering = Offering.objects.get(name=offering_name)
            # Check binding
            self.assertEquals(len(offering.resources), len(data))

            for off_res in offering.resources:
                found = False
                res = Resource.objects.get(pk=off_res)

                for exp_res in data:
                    if res.name == exp_res['name']:
                        self.assertEquals(res.version, exp_res['version'])
                        found = True
                        break
                self.assertTrue(found)

            # Check repository calls
            if offering.open:
                pass
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)
Esempio n. 6
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        content_type = get_content_type(request)[0]
        try:
            org = Organization.objects.get(name=organization)
            offering = Offering.objects.get(name=name, owner_organization=org, version=version)
        except:
            return build_response(request, 404, 'Not found')

        # Check that the user can bind resources to the offering
        if not offering.is_owner(request.user) and request.user.pk not in org.managers:
            return build_response(request, 403, 'Forbidden')

        if content_type == 'application/json':
            try:
                data = json.loads(request.raw_post_data)
                bind_resources(offering, data, request.user)
            except:
                build_response(request, 400, 'Invalid JSON content')

        return build_response(request, 200, 'OK')
Esempio n. 7
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        try:
            offering, org = _get_offering(organization, name, version)
        except ObjectDoesNotExist as e:
            return build_response(request, 404, unicode(e))
        except Exception as e:
            return build_response(request, 400, unicode(e))

        # Check that the user can bind resources to the offering
        if request.user.userprofile.current_organization != org\
        or (not offering.is_owner(request.user) and not request.user.pk in org.managers):
            return build_response(request, 403, 'Forbidden')

        # Bind the resources
        try:
            data = json.loads(request.raw_post_data)
            bind_resources(offering, data, request.user)
        except Exception as e:
            return build_response(request, 400, unicode(e))

        return build_response(request, 200, 'OK')
Esempio n. 8
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        try:
            offering, org = _get_offering(organization, name, version)
        except ObjectDoesNotExist as e:
            return build_response(request, 404, unicode(e))
        except Exception as e:
            return build_response(request, 400, unicode(e))

        # Check that the user can bind resources to the offering
        if request.user.userprofile.current_organization != org\
        or (not offering.is_owner(request.user) and not request.user.pk in org.managers):
            return build_response(request, 403, 'Forbidden')

        # Bind the resources
        try:
            data = json.loads(request.raw_post_data)
            bind_resources(offering, data, request.user)
        except Exception as e:
            return build_response(request, 400, unicode(e))

        return build_response(request, 200, 'OK')