def test_Plant_importExport_EmptyPlant(self): emptyPlantNS = ns.loads("""\ name: alcolea codename: SCSOM04 description: la bonica planta """) expectedPlantNS = ns.loads("""\ codename: SCSOM04 description: la bonica planta inverters: [] irradiationSensors: [] meters: [] name: alcolea temperatureAmbientSensors: [] temperatureModuleSensors: [] """) empty = self.pony.db.Plant(name=emptyPlantNS.name, codename=emptyPlantNS.codename) empty = empty.importPlant(emptyPlantNS) #TODO test the whole fixture, not just the plant data plantns = empty.exportPlant() self.assertNsEqual(plantns, expectedPlantNS)
def test_iter(self): s = peerinfo.PeerInfo(self.peerdatadir) self.assertEqual(list(s),[ ns.loads(somacmeyaml), ns.loads(sombogusyaml), ] )
def test__activateService_post__ok(self): data = ns.loads(self.yaml) package = packaging.generate(self.key, data) r = self.client.post('/activateService', data=package) data = ns.loads(r.data) self.assertEqual(r.status_code, 200) self.assertRegex(data.uuid, '^[0-9a-f\-]+$')
def test_peers(self): self.write("sombogus.yaml",sombogusyaml) self.write("somacme.yaml",somacmeyaml) p = self.setupPortal() self.assertMultiLineEqual(ns(data=list(p)).dump(),ns(data=[ ns.loads(somacmeyaml), ns.loads(sombogusyaml), ]).dump() )
def test_peers(self): self.write("sombogus.yaml", sombogusyaml) self.write("somacme.yaml", somacmeyaml) p = self.setupPortal() self.assertMultiLineEqual( ns(data=list(p)).dump(), ns(data=[ ns.loads(somacmeyaml), ns.loads(sombogusyaml), ]).dump())
def test__activateService_get(self): values = ns.loads(self.yaml) uuid = self.storage.store(values) data = ns(uuid=uuid) r = self.client.get('/activateService/{}'.format(uuid)) self.assertEqual(ns.loads(r.data), values) self.assertEqual(r.status_code, 200)
def test__activateService_post__missingField(self): data = ns.loads(self.yaml) del data.originpeer package = packaging.generate(self.key, data) r = self.client.post('/activateService', data=package) self.assertEqual( ns.loads(r.data), ns(error='MissingField', message="Required field 'originpeer' missing on the payload", arguments=['originpeer'])) self.assertEqual(r.status_code, 400)
def test__activateService_post__withContinuationUrl(self): data = ns.loads(self.yaml) package = packaging.generate(self.key, data) # TODO: Use the constructor param instead self.api.continuationUrlTmpl = '/activateService?uuid={uuid}' r = self.client.post('/activateService', data=package) data = ns.loads(r.data) self.assertEqual(r.status_code, 200) self.assertRegex(data.uuid, '^[0-9a-f\-]+$') self.assertEqual(data.continuationUrl, '/activateService?uuid=' + data.uuid)
def test__activateService_post__missingField(self): data = ns.loads(self.yaml) del data.originpeer package = packaging.generate(self.key, data) r = self.client.post('/activateService', data=package) self.assertEqual(ns.loads(r.data), ns( error = 'MissingField', message = "Required field 'originpeer' missing on the payload", arguments = ['originpeer'] )) self.assertEqual(r.status_code, 400)
def test__activateService_post__withContinuationUrl(self): data = ns.loads(self.yaml) package = packaging.generate(self.key, data) # TODO: Use the constructor param instead self.api.continuationUrlTmpl='/activateService?uuid={uuid}' r = self.client.post('/activateService', data=package) data = ns.loads(r.data) self.assertEqual(r.status_code, 200) self.assertRegex(data.uuid, '^[0-9a-f\-]+$') self.assertEqual(data.continuationUrl, '/activateService?uuid='+data.uuid)
def test__activateService_post__badPeer(self): data = ns.loads(self.yaml) data.originpeer = 'badpeer' package = packaging.generate(self.key, data) r = self.client.post('activateService', data=package) self.assertEqual(r.status_code, 403) self.assertEqual(r.headers.get('Content-Type'), 'application/yaml') self.assertEqual(ns.loads(r.data), ns( error = 'BadPeer', message = "The entity 'badpeer' is not a recognized one", arguments = ['badpeer'], ))
def test__activateService_post__badSignature(self): data = ns.loads(self.yaml) package = ns.loads(packaging.generate(self.key, data)) package.payload = crypto.encode(self.yaml+'\n') package = package.dump() r = self.client.post('/activateService', data=package) self.assertEqual(ns.loads(r.data), ns( error = 'BadSignature', message = "Signature verification failed, untrusted content", arguments = [], )) self.assertEqual(r.status_code, 403)
def activateService(self, service, personalData): package = packaging.generate(self.key, personalData) response = requests.post(self.apiurl + '/activateService', data=package) mime = response.headers.get('content-type', None) or 'Unspecified' if mime != 'application/yaml': raise BackendError("Wrong mime type received: {}".format(mime)) try: r = ns.loads(response.text) except Exception as e: raise BackendError("Bad yaml response\n{}".format(e)) if type(r) != ns: raise BackendError("Wrong content format") if response.status_code == 200: try: return r.continuationUrl except AttributeError: raise BackendError("Wrong content format") print("API call failed\n{}".format(response.text)) if 'error' in r and 'arguments' in r: raise packaging.error(r.error, *r.arguments) raise BackendError(response.text)
def activateService(self, service, personalData): package = packaging.generate(self.key,personalData) response = requests.post(self.apiurl+'/activateService', data=package) mime = response.headers.get('content-type', None) or 'Unspecified' if mime != 'application/yaml': raise BackendError("Wrong mime type received: {}".format(mime)) try: r = ns.loads(response.text) except Exception as e: raise BackendError("Bad yaml response\n{}".format(e)) if type(r) != ns: raise BackendError("Wrong content format") if response.status_code == 200: try: return r.continuationUrl except AttributeError: raise BackendError("Wrong content format") print ("API call failed\n{}".format(response.text)) if 'error' in r and 'arguments' in r: raise packaging.error(r.error, *r.arguments) raise BackendError(response.text)
def test__activateService_post__badPeer(self): data = ns.loads(self.yaml) data.originpeer = 'badpeer' package = packaging.generate(self.key, data) r = self.client.post('activateService', data=package) self.assertEqual(r.status_code, 403) self.assertEqual(r.headers.get('Content-Type'), 'application/yaml') self.assertEqual( ns.loads(r.data), ns( error='BadPeer', message="The entity 'badpeer' is not a recognized one", arguments=['badpeer'], ))
def test_load_recursiveArray(self): ns = namespace.loads( "list:\n" " - key1: value1\n" ) self.assertEqual(ns, namespace(list=[namespace(key1='value1')]))
def test_load_date(self): yamlcontent = ( "adate: 2000-02-28\n" ) ns = namespace.loads(yamlcontent) self.assertEqual(type(ns.adate), dateutils.Date)
def test__activateService_post__badSignature(self): data = ns.loads(self.yaml) package = ns.loads(packaging.generate(self.key, data)) package.payload = crypto.encode(self.yaml + '\n') package = package.dump() r = self.client.post('/activateService', data=package) self.assertEqual( ns.loads(r.data), ns( error='BadSignature', message="Signature verification failed, untrusted content", arguments=[], )) self.assertEqual(r.status_code, 403)
def test_fixtureCreation(self): with orm.db_session: plantsNS = ns.loads("""\ plants: - plant: name: alcolea codename: SCSOM04 description: la bonica planta meters: - meter: name: '1234578' inverters: - inverter: name: '5555' - inverter: name: '6666' irradiationSensors: - irradiationSensor: name: alberto temperatureModuleSensors: - temperatureModuleSensor: name: pol temperatureAmbientSensors: - temperatureAmbientSensor: name: joana """) importPlants(self.pony.db, plantsNS) #TODO test the whole fixture, not just the plant data expectedPlantns = exportPlants(self.pony.db) self.assertNsEqual(expectedPlantns, plantsNS)
def inverterDeviceWithStringsNS(self): devicesns = ns.loads(""" name: inversor1 type: inverter description: inversor1 model: aros-solar enabled: True modmap: - type: holding_registers registers: 0 : 1HR0 1 : 1HR1 2 : 1HR2 900 : string1 901 : string2 902 : string3 - scan: start: 0 range: 3 - scan: start: 900 range: 3 protocol: type: TCP ip: localhost port: 502 slave: 1 timeout: 10 """) return devicesns
def __getattr__(self, name): if name not in self.cache.keys(): extractor = self.factory_data_extractor(name) data = extractor.get_data(self.cursor, self.uid, self.object, self.extra_text, self.context) self.cache[name] = ns.loads(ns(data).dump()) return self.cache[name]
def test_reload_inverseOrder(self): yamlcontent = ( "lola: foo\n" "lala: boo\n" ) ns = namespace.loads(yamlcontent) self.assertEqual(ns.dump(), yamlcontent)
def parse(keyring, message): def packageField(field): if field in package: return package[field] raise BadMessage("missing {}".format(field)) try: package = ns.loads(message) except Exception as e: raise BadMessage("Bad message YAML format\n{}".format(e)) version = packageField('intercoopVersion') if version != protocolVersion: raise WrongVersion(protocolVersion, version) payload = packageField('payload') signature = packageField('signature') try: valuesYaml = decode(payload) except UnicodeError: raise BadMessage("Payload is not base64 coded UTF8") except Exception as e: raise BadMessage( "Payload is invalid Base64: {}".format(e)) try: values = ns.loads(valuesYaml) except Exception as e: raise BadFormat(e) try: peer = values.originpeer except AttributeError: raise MissingField('originpeer') try: pubkey = keyring.get(peer) except KeyError: raise BadPeer(peer) if not isAuthentic(pubkey, valuesYaml, signature): raise BadSignature() return values
def test_reload_subnamespaces(self) : yamlcontent = ( "sub:\n" " foo: value1\n" " bar: value2\n" ) ns = namespace.loads(yamlcontent) self.assertEqual(ns.dump(), yamlcontent)
def test_load_decimal(self) : yamlcontent = ( "decimal: 3.41" ) ns = namespace.loads(yamlcontent) self.assertEqual(type(ns.decimal), decimal.Decimal) self.assertEqual(ns.decimal, decimal.Decimal('3.41'))
def test_generate(self): message = packaging.generate(self.key, self.values) self.assertEqual( dict(ns.loads(message)), dict( intercoopVersion='1.0', signature=self.signedPayload1, payload=self.encodedPayload1, ))
def test_generate(self): message = packaging.generate(self.key, self.values) self.assertEqual( dict(ns.loads(message)), dict( intercoopVersion = '1.0', signature = self.signedPayload1, payload = self.encodedPayload1, ))
def parse(keyring, message): def packageField(field): if field in package: return package[field] raise BadMessage("missing {}".format(field)) try: package = ns.loads(message) except Exception as e: raise BadMessage("Bad message YAML format\n{}".format(e)) version = packageField('intercoopVersion') if version != protocolVersion: raise WrongVersion(protocolVersion, version) payload = packageField('payload') signature = packageField('signature') try: valuesYaml = decode(payload) except UnicodeError: raise BadMessage("Payload is not base64 coded UTF8") except Exception as e: raise BadMessage("Payload is invalid Base64: {}".format(e)) try: values = ns.loads(valuesYaml) except Exception as e: raise BadFormat(e) try: peer = values.originpeer except AttributeError: raise MissingField('originpeer') try: pubkey = keyring.get(peer) except KeyError: raise BadPeer(peer) if not isAuthentic(pubkey, valuesYaml, signature): raise BadSignature() return values
def test_requiredFields_noFields(self): bogus = ns.loads(sombogusyaml) del bogus.services.contract.fields self.write("sombogus.yaml",bogus.dump()) p = self.setupPortal() with self.assertRaises(Exception) as ctx: p.requiredFields("sombogus","contract") self.assertEqual(format(ctx.exception), "Peer 'sombogus' does not specify fields for service 'contract'")
def samplePlantsNS(self, altMeter="12345678"): alcoleaPlantsNS = ns.loads("""\ municipalities: - municipality: name: Figueres ineCode: '17066' countryCode: ES country: Spain regionCode: '09' region: Catalonia provinceCode: '17' province: Girona - municipality: name: Girona ineCode: '17079' plants: - plant: name: alcolea codename: SCSOM04 description: la bonica planta municipality: '17066' meters: - meter: name: '{}' inverters: - inverter: name: '5555' - plant: name: figuerea codename: Som_figuerea description: la bonica planta municipality: '17079' meters: - meter: name: '5432' - meter: name: '9876' inverters: - inverter: name: '4444' - inverter: name: '2222' irradiationSensors: - irradiationSensor: name: oriol temperatureModuleSensors: - temperatureModuleSensor: name: joan temperatureAmbientSensors: - temperatureAmbientSensor: name: benjami integratedSensors: - integratedSensor: name: david""".format(altMeter)) return alcoleaPlantsNS
def test_requiredFields_noFields(self): bogus = ns.loads(sombogusyaml) del bogus.services.contract.fields self.write("sombogus.yaml", bogus.dump()) p = self.setupPortal() with self.assertRaises(Exception) as ctx: p.requiredFields("sombogus", "contract") self.assertEqual( format(ctx.exception), "Peer 'sombogus' does not specify fields for service 'contract'")
def load_invoice_data(fact_id): step("Carregant dades de la factura...") error = False data = "" data_yaml = rprt_obj.get_components_data_yaml([fact_id]) try: data = ns.loads(data_yaml) except Exception as e: error = True step("Error localitzat ...") data = data_yaml.replace("\n", '\n') return error, data
def samplePlantNS(self): plantNS = ns.loads("""\ name: alibaba codename: SCSOM04 description: la bonica planta inverters: - inverter: name: '5555' temperatureAmbientSensors: - temperatureAmbientSensor: name: benjami""") return plantNS
def rtve(id, title=''): thumb = 'https://img2.rtve.es/css/rtve.commons/rtve.header.footer/i/logoRTVEes.png' link = f'https://www.rtve.es/v/{id}/' embeded = f"https://secure-embed.rtve.es/drmn/embed/video/{id}/" response = requests.get(f'http://api.rtve.es/api/videos/{id}.json', headers={'Cache-Control': 'no-cache'}) if response.ok: metadata = ns.loads(response.text) thumb = metadata.page['items'][0].thumbnail title = html.escape(metadata.page['items'][0].longTitle) link = html.escape(metadata.page['items'][0].htmlUrl) return f"""\
def samplePlantNS(self): # Copied from test_models.py:samplePlantNS alcoleaPlantNS = ns.loads("""\ name: myplant codename: PLANTCODE description: Plant Description meters: - meter: name: '1234578' irradiationSensors: - irradiationSensor: name: irradiationSensor1 """) return alcoleaPlantNS
def setUpPlant(self): plantsns = ns.loads("""\ plants: - plant: name: Alcolea codename: SCSOM04 description: la bonica planta inverters: - inverter: name: 'inversor1' """) importPlants(self.pony.db, plantsns)
def test__activateService_get__notFound(self): uuid = '01020304-0506-0708-090a-0b0c0d0e0f10' data = ns(uuid=uuid) r = self.client.get('/activateService/{}'.format(uuid)) self.assertEqual(ns.loads(r.data), ns( error = 'NoSuchUuid', message = "No personal data available for uuid " "'01020304-0506-0708-090a-0b0c0d0e0f10'", arguments=[uuid], )) self.assertEqual(r.status_code, 404)
def test_getFields_allFields(self): storage = userinfo.UserInfo(self.datadir) data = storage.getFields('myuser', [ 'name', 'nif', 'lang', ]) self.assertEqual(data, ns.loads("""\ name: de los Palotes, Perico nif: 12345678Z lang: ca """ ))
def setUp(self): self.keyfile = 'testkey.pem' self.key = crypto.loadKey(self.keyfile) self.personalData = ns.loads(self.yaml) self.apiurl = "https://api.somacme.coop/intercoop" self.service = "contract" self.uuid = '01020304-0506-0708-090a-0b0c0d0e0f10' self.continuationUrl = 'https://somacme.coop/contract?token={}'.format( self.uuid) self.client = apiclient.ApiClient( apiurl=self.apiurl, key=self.key, )
def setUp(self): self.maxDiff = None self.keyfile = 'testkey.pem' self.pubfile = 'testkey-public.pem' if not os.access(self.keyfile, os.F_OK): crypto.generateKey(self.keyfile, self.pubfile) self.key = crypto.loadKey(self.keyfile) self.public = crypto.loadKey(self.pubfile) self.values = ns.loads(self.yaml) self.encodedPayload1 = crypto.encode(self.yaml) self.signedPayload1 = crypto.sign(self.key, self.yaml) self.keyring = KeyRingMock(dict(testpeer=self.public, ))
def samplePlantNSWithStrings(self): alcoleaPlantNS = ns.loads("""\ name: alcolea codename: SCSOM04 description: la bonica planta inverters: - inverter: name: '5555' strings: - string1 - string2 - inverter: name: '6666' """) return alcoleaPlantNS
def test__activateService_get__notFound(self): uuid = '01020304-0506-0708-090a-0b0c0d0e0f10' data = ns(uuid=uuid) r = self.client.get('/activateService/{}'.format(uuid)) self.assertEqual( ns.loads(r.data), ns( error='NoSuchUuid', message="No personal data available for uuid " "'01020304-0506-0708-090a-0b0c0d0e0f10'", arguments=[uuid], )) self.assertEqual(r.status_code, 404)
def test_getFields_allFields(self): storage = userinfo.UserInfo(self.datadir) data = storage.getFields('myuser', [ 'name', 'nif', 'lang', ]) self.assertEqual( data, ns.loads("""\ name: de los Palotes, Perico nif: 12345678Z lang: ca """))
def fieldLabels(self, fields, lang=None): if lang: _ = translation.Translator(lang) return _(self.fieldLabels(fields)) labels = ns.loads(labelsyaml) for field in fields: if field not in labels: raise BadField(field) return ns([(key, value) for key, value in labels.items() if key in fields]) return self.users.fieldLabels(fields)
def setUp(self): self.maxDiff = None self.keyfile = 'testkey.pem' self.pubfile = 'testkey-public.pem' if not os.access(self.keyfile, os.F_OK): crypto.generateKey(self.keyfile, self.pubfile) self.key = crypto.loadKey(self.keyfile) self.public = crypto.loadKey(self.pubfile) self.values = ns.loads(self.yaml) self.encodedPayload1 = crypto.encode(self.yaml) self.signedPayload1 = crypto.sign(self.key, self.yaml) self.keyring = KeyRingMock(dict( testpeer=self.public, ))
def fieldLabels(self, fields, lang=None): if lang: _ = translation.Translator(lang) return _(self.fieldLabels(fields)) labels = ns.loads(labelsyaml) for field in fields: if field not in labels: raise BadField(field) return ns([ (key, value) for key,value in labels.items() if key in fields ]) return self.users.fieldLabels(fields)
def test_namespace2response_json(self): namespace = ns.loads("""\ url: http://mysite.com/path/page status_code: 200 headers: Content-Type: application/json json: data: value """) response = Fetcher._namespace2response(namespace) self.assertResponseEqual( response, """\ url: http://mysite.com/path/page status_code: 200 headers: Content-Type: application/json json: data: value """)
def test_namespace2response_text(self): namespace = ns.loads("""\ url: http://mysite.com/path/page status_code: 200 headers: Content-Type: text/plain text: hello world encoding: ISO-8859-1 """) response = Fetcher._namespace2response(namespace) self.assertResponseEqual( response, """\ url: http://mysite.com/path/page status_code: 200 headers: Content-Type: text/plain text: hello world encoding: ISO-8859-1 """)
def test__protocolVersion_get(self): r = self.client.get('/protocolVersion') self.assertEqual( ns.loads(r.data), ns(protocolVersion=packaging.protocolVersion), )
def parseIfString(nsOrString): if isinstance(nsOrString, dict): return nsOrString return ns.loads(nsOrString)
def __init__(self, config): self.config = config self.supportedFields = ns.loads(labelsyaml).keys()
def ns(self, content): return ns.loads(content)
def test_load_empty(self): self.assertEqual(namespace.loads("{}"), namespace())
def test_get(self): self.write('somacme.yaml', somacmeyaml) s = peerinfo.PeerInfo(self.peerdatadir) peerData = s.get("somacme") self.assertEqual(peerData,ns.loads(somacmeyaml))
def test_peerDescription_manyServices(self): peer = ns.loads(somacmeyaml) p = self.setupPortal() self.assertMultiLineEqual( p.peerDescription(peer), acmePeerHeader + acmeService + acmeService2 + peerFooter)
def test_load_None(self): self.assertEqual(namespace.loads(""), None)