def test403(self):
     'Should return a 403 response if the principal is not authorized'
     self.principal['roles'].clear()
     uuid = 'dario'
     event = {
         'httpMethod': 'GET',
         'path': '/thing/{0}'.format(uuid),
         'headers': {
             'Host': 'localhost',
             'X-Forwarded-Proto': 'http',
             'X-Forwarded-Port': '80'
         },
         'requestContext': {
             'authorizer': {
                 'principalId': json.dumps(self.principal)
             }
         },
         'pathParameters': {
             'uuid': uuid
         }
     }
     response = handler(event, None)
     self.assertEqual(response['statusCode'], 403)
     self.assertEqual(response['headers'],
                      {'Access-Control-Allow-Origin': '*'})
     body = json.loads(response['body'])
     self.assertEqual(body['statusCode'], 403)
     self.assertEqual(body['statusReason'], 'Forbidden')
     self.assertEqual(body['message'],
                      'Principal is not authorized to get things')
     self.assertEqual(body['causes'], [])
     self.assertEqual(body['method'], 'GET')
     self.assertEqual(body['resource'], 'http://localhost/thing/dario')
     self.assertRegex(body['timestamp'], ISO_DATETIME_Z_REGEX)
 def test404NotOwned(self):
     'Should return a 404 response if the thing is not owned'
     self.principal['organizationId'] = 'ANOTHER'
     uuid = '002'
     event = {
         'httpMethod': 'GET',
         'path': '/thing/{0}'.format(uuid),
         'headers': {
             'Host': 'localhost',
             'X-Forwarded-Proto': 'http',
             'X-Forwarded-Port': '80'
         },
         'requestContext': {
             'authorizer': {
                 'principalId': json.dumps(self.principal)
             }
         },
         'pathParameters': {
             'uuid': uuid
         }
     }
     response = handler(event, None)
     self.assertEqual(response['statusCode'], 404)
     self.assertEqual(response['headers'],
                      {'Access-Control-Allow-Origin': '*'})
     body = json.loads(response['body'])
     self.assertEqual(body['statusCode'], 404)
     self.assertEqual(body['statusReason'], 'Not found')
     self.assertEqual(body['message'], 'Thing "{0}" not found'.format(uuid))
     self.assertEqual(body['causes'], [])
     self.assertEqual(body['method'], 'GET')
     self.assertEqual(body['resource'],
                      'http://localhost/thing/{0}'.format(uuid))
     self.assertRegex(body['timestamp'], ISO_DATETIME_Z_REGEX)
 def test200(self):
     'Should return a 200 response with the requested thing'
     uuid = '001'
     event = {
         'httpMethod': 'GET',
         'path': '/thing/{0}'.format(uuid),
         'headers': {
             'Host': 'localhost',
             'X-Forwarded-Proto': 'http',
             'X-Forwarded-Port': '80'
         },
         'requestContext': {
             'authorizer': {
                 'principalId': json.dumps(self.principal)
             }
         },
         'pathParameters': {
             'uuid': uuid
         }
     }
     response = handler(event, None)
     self.assertEqual(response['statusCode'], 200)
     body = json.loads(response['body'])
     self.assertEqual(
         response['headers'], {
             'Access-Control-Allow-Origin':
             '*',
             'Last-Modified':
             email.utils.formatdate(timeval=json2datetime(
                 body['lastModified']).timestamp(),
                                    localtime=False,
                                    usegmt=True)
         })
     self.assertEqual(body['uuid'], uuid)
     self.assertEqual(body['owner'], self.principal['organizationId'])
     self.assertIsInstance(body['name'], str)
     self.assertRegex(body['created'], ISO_DATETIME_Z_REGEX)