def test_basic_offering_update(self): data = { 'offering_description': { 'content_type': 'application/rdf+xml', 'data': self._usdl } } offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") offerings_management.update_offering(offering, data) offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") usdl = offering.offering_description parser = USDLParser(json.dumps(usdl), 'application/json') usdl_content = parser.parse() self.assertEqual(len(usdl_content['services_included']), 1) service = usdl_content['services_included'][0] self.assertEqual(service['name'], 'Map viewer') self.assertEqual(service['vendor'], 'CoNWeT') self.assertEqual(usdl_content['pricing']['title'], 'Map viewer free use') self.assertEqual(len(usdl_content['pricing']['price_plans']), 1) plan = usdl_content['pricing']['price_plans'][0] self.assertEqual(plan['title'], 'Free use')
def test_offering_update_from_url(self): data = { 'description_url': "http://examplerep/v1/test_usdl" } offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") offerings_management.update_offering(offering, data) offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") usdl = offering.offering_description parser = USDLParser(json.dumps(usdl), 'application/json') usdl_content = parser.parse() self.assertEqual(len(usdl_content['services_included']), 1) service = usdl_content['services_included'][0] self.assertEqual(service['name'], 'Map viewer') self.assertEqual(service['vendor'], 'CoNWeT') self.assertEqual(usdl_content['pricing']['title'], 'Map viewer free use') self.assertEqual(len(usdl_content['pricing']['price_plans']), 1) plan = usdl_content['pricing']['price_plans'][0] self.assertEqual(plan['title'], 'Free use')
def update(self, request, organization, name, version): user = request.user # Get the offering 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)) # Update the offering try: # Check if the user is the owner of the offering or if is a manager of the # owner organization if user.userprofile.current_organization != org \ or (not offering.is_owner(user) and user.pk not in org.managers): return build_response(request, 403, 'You are not allowed to edit the current offering') data = json.loads(request.raw_post_data) update_offering(user, offering, data) except Exception, e: return build_response(request, 400, e.message)
def update(self, request, organization, name, version): logger.debug("OfferingEntry.update()") user = request.user # Get the offering 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)) # Update the offering try: # Check if the user is the owner of the offering or if is a manager of the # owner organization if user.userprofile.current_organization != org\ or (not offering.is_owner(user) and not user.pk in org.managers): return build_response(request, 403, 'You are not allowed to edit the current offering') data = json.loads(request.raw_post_data) update_offering(offering, data) except Exception, e: import traceback import StringIO buff = StringIO.StringIO() traceback.print_exc(file=buff) print("EXCEPTION: %s %s" % (str(e), buff.getvalue())) return build_response(request, 400, e.message)
def test_offering_update(self, initial_data, data_filler=None, err_type=None, err_msg=None): if data_filler: data = data_filler(self, initial_data) else: data = initial_data offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") error = None try: offerings_management.update_offering(self._user, offering, data) except Exception as e: error = e if not err_type: self.assertEquals(error, None) new_offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") self.se_object.update_index.assert_called_with(offering) if 'offering_description' in data: usdl = new_offering.offering_description self.assertEquals(usdl['pricing'], { 'price_plans': [] }) self.assertEquals(usdl['legal'], { 'title': 'legal title', 'text': 'legal text' }) self.assertEquals(usdl['description'], 'Test offering') self.assertEquals(usdl['abstract'], 'A test offering') if 'image' in data: # Check deletion of old image offerings_management.os.remove.assertCall(os.path.join(settings.BASEDIR, 'wstore/test/test_organization__test_offering2__1.0/image.png')) f = open(os.path.join(self.test_dir, data['image']['name']), "rb") content = f.read() self.assertEquals(content, 'decoded data') self.assertEquals(new_offering.image_url, '/media/test_organization__test_offering2__1.0/test_logo.png') if 'related_images' in data: os.remove.assertCall(os.path.join(settings.BASEDIR, 'wstore/test/test_organization__test_offering2__1.0/screen1.png')) for img in data['related_images']: f = open(os.path.join(self.test_dir, img['name']), "rb") content = f.read() self.assertEquals(content, 'decoded data') if offering.open and offering.state == 'published': # Check repository_call offerings_management.unreg_repository_adaptor_factory.assert_called_once_with(offering.description_url) self._repo_mock.set_credentials.assert_called_once_with(self._user.userprofile.access_token) self._repo_mock.upload.assert_called_once_with('application/rdf+xml', 'USDL offering') else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(e), err_msg)
def test_offering_update(self, initial_data, data_filler=None, err_type=None, err_msg=None): if data_filler: data = data_filler(self, initial_data) else: data = initial_data offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") error = None try: offerings_management.update_offering(self._user, offering, data) except Exception as e: error = e if not err_type: self.assertEquals(error, None) new_offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") self.se_object.update_index.assert_called_with(offering) if "offering_description" in data: usdl = new_offering.offering_description self.assertEquals(usdl["pricing"], {"price_plans": []}) self.assertEquals(usdl["legal"], {"title": "legal title", "text": "legal text"}) self.assertEquals(usdl["description"], "Test offering") self.assertEquals(usdl["abstract"], "A test offering") if "image" in data: # Check deletion of old image offerings_management.os.remove.assertCall( os.path.join(settings.BASEDIR, "wstore/test/test_organization__test_offering2__1.0/image.png") ) f = open(os.path.join(self.test_dir, data["image"]["name"]), "rb") content = f.read() self.assertEquals(content, "decoded data") self.assertEquals(new_offering.image_url, "/media/test_organization__test_offering2__1.0/test_logo.png") if "related_images" in data: os.remove.assertCall( os.path.join(settings.BASEDIR, "wstore/test/test_organization__test_offering2__1.0/screen1.png") ) for img in data["related_images"]: f = open(os.path.join(self.test_dir, img["name"]), "rb") content = f.read() self.assertEquals(content, "decoded data") if offering.open and offering.state == "published": # Check repository_call offerings_management.unreg_repository_adaptor_factory.assert_called_once_with(offering.description_url) self._repo_mock.set_credentials.assert_called_once_with(self._user.userprofile.access_token) self._repo_mock.upload.assert_called_once_with("application/rdf+xml", "USDL offering") else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(e), err_msg)
def test_offering_update_published(self): offering = Offering.objects.get(pk="61000aba8e05ac2115f022f9") error = False msg = None try: offerings_management.update_offering(offering, {}) except Exception, e: error = True msg = e.message
def test_offering_update_invalid_link(self): data = { 'description_url': { 'content_type': 'application/rdf+xml', 'link': 'http://examplerep/v1/invalid' } } offering = Offering.objects.get(pk="61000aba8e15ac2115f022f9") error = False msg = None try: offerings_management.update_offering(offering, data) except Exception, e: error = True msg = e.message
def update(self, request, organization, name, version): user = request.user # Get the offering try: org = Organization.objects.get(name=organization) offering = Offering.objects.get(owner_organization=org, name=name, version=version) except: return build_response(request, 404, 'Not found') # Update the offering try: # Check if the user is the owner of the offering or if is a manager of the # owner organization if not offering.is_owner(user) and user.pk not in org.managers: return build_response(request, 403, 'Forbidden') data = json.loads(request.raw_post_data) update_offering(offering, data) except Exception, e: return build_response(request, 400, e.message)