def testGetByClientRef(self):
     """Checks the class method, get_by_clientref"""
     sess = db.GqlQuery('SELECT * FROM SyncSession WHERE client_ref = :1', 'C').get()
     sess.payload_size = 9
     sess.put()
     sess2 = SyncSession.get_by_clientref('C')
     self.assertEqual(sess.payload_size, sess2.payload_size)
Exemplo n.º 2
0
 def post(self):
     
     logging.debug('Running File Upload Handler')
     
     bconv = BaseConverter('0123456789abcdef')
     
     dieid = self.request.get('kiosk-id')
     if not dieid:
         self.error(403)
         self.response.write('No device id provided.\n')
         logger.warn('Device attempted contact without die id from ip %s.' % self.request.remote_addr)
         return
         
     logging.info("File upload incoming from kiosk : %s" % dieid)
     
     kiosk = Kiosk.kiosk_from_dieid(dieid)
     
     if not kiosk:
         self.error(400)
         self.response.write('Kiosk is unregistered on system.\n')
         logging.warn('Unregistered kiosk on ip %s with dieid %s.' % (self.request.remote_addr, dieid))
         return
         
     logging.debug("Encoded sig: %s." % self.request.headers['X-eko-signature'])
     
     # look for the signature
     try:
         signature = bconv.to_decimal(self.request.headers['X-eko-signature'])
         logging.debug("Decoded sig: %s." % str(signature))
         challenge = self.request.headers['X-eko-challenge']
         logging.debug("Challenge: %s." % challenge)
         # signature should be uuid we sent kiosk signed with the public key
         verify = self._verify_client(kiosk, signature, challenge)
     except:
         logging.exception("Authentication signature not found.\n")
         verify = False
     
     # auth failed
     if not verify:
         logging.error("Kiosk id %s did not pass id check." % dieid)
         self.response.write('Unable to verify identity of kiosk.\n')
         return
     
     # a uuid that identifies this data packet on the remote device
     client_ref       = self.request.headers['X-eko-challenge']
     if not client_ref:
         logging.error("No client reference provided.")
         self.response.write('Client Error: No reference provided.')
         return
     
     # the type of data being sent (logs, readings, etc...)
     type             = self.request.get('type')
     # version of the client
     software_version = self.request.get('software_version')
     # manifest
     try:
         manifest = self.get_uploads(field_name='manifest')[0]
         logging.debug("Manifest uploaded, size: %s" % str(manifest.size))
     except:
         logging.exception("Manifest missing from upload data.")
         self.response.write('Manifest Not Found\n')
     
     # payload
     try:
         payload = self.get_uploads(field_name='payload')[0]
     except:
         logging.exception("Payload missing from upload data.")
         self.response.write('Payload Not Found\n')
     
     sync = SyncSession.get_by_clientref(client_ref)
     if not sync:
         logging.error("Upload attempt without requesting sync session.")
         self.response.write('Upload request improperly handled.')
         return
     sync.kiosk = kiosk
     sync.data_type = type
     sync.payload_size = 0
     
     # payload size + manifest size
     if payload:
         sync.payload_size += payload.size
     if manifest:
         sync.payload_size += manifest.size
     
     sync.payload = payload
     sync.manifest = manifest
     
     sync.software_version = software_version
     
     #sync.client_ip = self.request.remote_addr
     sync.end_date = datetime.utcnow()
     
     try:
         sync.put()
         logging.debug("Sync packet succesfully added to datastore")
         self.response.write('Success\n')
     except:
         logging.exception("Adding sync packet failed.")
         self.response.write('Failure\n')
     return