def test_run_with_invalid_db_secret(self): """Run a method that tries to use an invalid secret for an Nflex DB """ handler = 'module_with_db.test' expected = { 'exc_message': 'The mydb secret is not a valid MongoDB connection string', # noqa 'exc_type': 'Exception', } context = FlexerContext() secrets = [ "test", "mongodb://", "mongodb://a", "mongodb://*****:*****@", "mongodb://*****:*****@c", "mongodb://*****:*****@c/", ] for s in secrets: context.secrets = {"_nflexdb_mydb": s} result = self.runner.run(event={}, context=context, handler=handler) actual = json.loads(result) self.assertEqual(None, actual['value']) error = actual['error'] self.assertEqual(expected['exc_message'], error['exc_message']) self.assertEqual(expected['exc_type'], error['exc_type']) self.assertIn('Exception', error['stack_trace'])
def run(handler, event, config, secrets, cmp_client): event = json.loads(event) handler = "main.%s" % handler context = FlexerContext(cmp_client=cmp_client) if config is not None: context.config = json.loads(config) if secrets is not None: context.secrets = json.loads(secrets) result = Flexer().run(event=event, context=context, handler=handler, debug=True) return json.loads(result)
def test_run_with_valid_db_secret(self): """Run a method that tries to use a valid secret for an Nflex DB """ handler = 'module_with_db.test' context = FlexerContext() context.secrets = {"_nflexdb_mydb": "mongodb://*****:*****@c/mydb"} with mock.patch('pymongo.MongoClient', return_value=mock.MagicMock()): result = self.runner.run(event={}, context=context, handler=handler) actual = json.loads(result) self.assertTrue('error' in actual) self.assertTrue('value' in actual) self.assertEqual(None, actual['error']) self.assertTrue('result' in actual['value'])