def test_webhook_init(self): kwargs = { 'id': 'WEBHOOK_ID', 'name': 'test-webhook', 'user': '******', 'project': 'test-project', 'domain': 'test-domain', 'created_time': timeutils.utcnow(), 'deleted_time': None, 'credential': self.credential, 'params': self.params } webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action', **kwargs) self.assertEqual('test-obj-id', webhook.obj_id) self.assertEqual('test-obj-type', webhook.obj_type) self.assertEqual('test-action', webhook.action) self.assertEqual(kwargs['id'], webhook.id) self.assertEqual(kwargs['name'], webhook.name) self.assertEqual(kwargs['user'], webhook.user) self.assertEqual(kwargs['project'], webhook.project) self.assertEqual(kwargs['domain'], webhook.domain) self.assertEqual(kwargs['created_time'], webhook.created_time) self.assertEqual(kwargs['deleted_time'], webhook.deleted_time) self.assertEqual(kwargs['credential'], webhook.credential) self.assertEqual(kwargs['params'], webhook.params)
def test_generate_url(self, mock_service_get, mock_endpoint_get, mock_init): mock_init.return_value = None mock_service_get.return_value = { 'id': 'SENLIN_SERVICE_ID' } mock_endpoint_get.return_value = { 'url': 'HTTP://HOST_IP:PORT/V1/$(tenant_id)s' } kwargs = { 'id': 'WEBHOOK_ID', 'name': 'test-webhook', 'user': '******', 'project': 'test-project', 'domain': 'test-domain', 'created_time': timeutils.utcnow(), 'deleted_time': None, 'credential': self.credential, 'params': self.params } webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action', **kwargs) key = 'test-key' res1, res2 = webhook.generate_url(key) expected_url = _('HTTP://HOST_IP:PORT/V1/%(tenant_id)s/webhooks/' '%(webhook_id)s/trigger?key=%(key)s' ) % {'tenant_id': 'test-project', 'webhook_id': webhook.id, 'key': six.text_type(key)} self.assertEqual(expected_url, res1) self.assertEqual(key, res2) # Senlin service not found mock_service_get.return_value = None ex = self.assertRaises(exception.ResourceNotFound, webhook.generate_url, key) resource = _('service:type=clustering,name=senlin') msg = _('The resource (%(resource)s) could not be found.' ) % {'resource': resource} self.assertEqual(msg, six.text_type(ex)) # Senlin endpoint not found mock_service_get.return_value = { 'id': 'SENLIN_SERVICE_ID' } service_id = mock_service_get.return_value['id'] mock_endpoint_get.return_value = None ex = self.assertRaises(exception.ResourceNotFound, webhook.generate_url, key) resource = _('endpoint: service=%(service)s,region=' '%(region)s,visibility=%(interface)s' ) % {'service': service_id, 'region': None, 'interface': 'public'} msg = _('The resource (%(resource)s) could not be found.' ) % {'resource': resource} self.assertEqual(msg, six.text_type(ex))
def test_webhook_store(self): webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action') self.assertIsNotNone(webhook.id) webhook_id = webhook.store(self.context) self.assertIsNotNone(webhook_id) self.assertEqual(webhook_id, webhook.id) result = db_api.webhook_get(self.context, webhook_id) self.assertIsNotNone(result) self.assertEqual(webhook_id, result.id) self.assertEqual(webhook.name, result.name) self.assertEqual(webhook.user, result.user) self.assertEqual(webhook.project, result.project) self.assertEqual(webhook.domain, result.domain) self.assertEqual(webhook.created_time, result.created_time) self.assertEqual(webhook.deleted_time, result.deleted_time) self.assertEqual(webhook.credential, result.credential) self.assertEqual(webhook.params, result.params) self.assertEqual('test-obj-id', result.obj_id) self.assertEqual('test-obj-type', result.obj_type) self.assertEqual('test-action', result.action) self.assertIsNotNone(result.created_time) self.assertIsNone(result.deleted_time)
def test_webhook_init_with_context(self): webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action', context=self.context) self.assertEqual(self.context.user, webhook.user) self.assertEqual(self.context.project, webhook.project) self.assertEqual(self.context.domain, webhook.domain)
def test_webhook_init_default_value(self): webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action') self.assertEqual('test-obj-id', webhook.obj_id) self.assertEqual('test-obj-type', webhook.obj_type) self.assertEqual('test-action', webhook.action) self.assertIsNotNone(webhook.id) self.assertEqual(None, webhook.name) self.assertEqual('', webhook.user) self.assertEqual('', webhook.project) self.assertEqual('', webhook.domain) self.assertEqual(None, webhook.created_time) self.assertEqual(None, webhook.deleted_time) self.assertEqual(None, webhook.credential) self.assertEqual({}, webhook.params)
def test_get_credential_failed(self, mock_enforce): kwargs = { 'id': None, 'name': 'test-webhook', 'created_time': timeutils.utcnow(), 'deleted_time': None, 'credential': jsonutils.dumps(self.credential), 'params': {} } webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action', context=self.ctx, **kwargs) webhook.encrypt_credential() webhook.store(self.ctx) # Credential getting failed for invalid key provided self.assertRaises(exception.Forbidden, self.middleware._get_credential, self.ctx.project, webhook.id, 'fake-key')
def test_encrypt_credential(self): kwargs = { 'id': 'WEBHOOK_ID', 'name': 'test-webhook', 'user': '******', 'project': 'test-project', 'domain': 'test-domain', 'created_time': timeutils.utcnow(), 'deleted_time': None, 'credential': self.credential, 'params': self.params } webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action', **kwargs) key = webhook.encrypt_credential() cdata = encrypt_utils.decrypt(webhook.credential, key) credential = jsonutils.loads(cdata) self.assertEqual('abc', credential['password'])
def test_get_credential_succeed(self, mock_enforce): kwargs = { 'id': None, 'name': 'test-webhook', 'created_time': timeutils.utcnow(), 'deleted_time': None, 'credential': jsonutils.dumps(self.credential), 'params': {} } webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action', context=self.ctx, **kwargs) key = webhook.encrypt_credential() webhook.store(self.ctx) # User credential can be got correctly if valid key is provided res = self.middleware._get_credential(self.ctx.project, webhook.id, key) self.assertEqual('123', res['user_id']) self.assertEqual('abc', res['password']) self.assertEqual('TEST_URL', res['auth_url'])
def test_webhook_store_already_created(self): webhook = webhook_mod.Webhook('test-obj-id', 'test-obj-type', 'test-action') webhook.id = 'FAKE_ID' webhook_id = webhook.store(self.context) self.assertEqual('FAKE_ID', webhook_id)