Skip to content

Commit

Permalink
Add tests for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sukrit007 committed May 3, 2015
1 parent 25c8230 commit a85cc8f
Showing 1 changed file with 131 additions and 1 deletion.
132 changes: 131 additions & 1 deletion tests/unit/orchestrator/views/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from flask import Flask
from mock import patch
from nose.tools import eq_
from conf.appconfig import MIME_JSON, MIME_FORM_URL_ENC
from conf.appconfig import MIME_JSON, MIME_FORM_URL_ENC, MIME_GENERIC_HOOK_V1, \
MIME_JOB_V1
from orchestrator.server import app
from orchestrator.views.hooks import authorize

Expand Down Expand Up @@ -257,3 +258,132 @@ def test_post_when_unauthorized(self, mock_callback):
logger.info('Response: %s', resp.data)
eq_(resp.status_code, 401)
_assert_unauthorized(resp)


class TestGenericInternalPostHookApi:

def setup(self):
self.client = app.test_client()

@staticmethod
def _create_mock_payload():
return {
'git': {
'owner': 'totem',
'repo': 'totem-demo',
'ref': 'master',
'commit': '87c4419d3e278036055ca2cd022583e0397d3a5d'
},
'type': 'scm-create',
'name': 'github-create',
'status': 'success',
'force-deploy': False
}

@patch('orchestrator.views.hooks.handle_callback_hook')
def test_post(self, m_handle_callback_hook):
"""
Should return accepted response when a valid github hook is posted.
"""
# Given: Valid JSON Payload for posting to the hook
mock_payload = self._create_mock_payload()

# And: Mock Callback hook handler
m_handle_callback_hook.delay.return_value = 'mock_task_id'

# When I post to generic interal hook
resp = self.client.post(
'/hooks/generic',
data=json.dumps(mock_payload),
headers={
'Content-Type': MIME_GENERIC_HOOK_V1
}
)

# Then: Expected response is returned
eq_(resp.status_code, 202)
data = json.loads(resp.data.decode('UTF-8'))
dict_compare(data, {
'task_id': 'mock_task_id'
})

@patch('orchestrator.views.hooks.handle_callback_hook')
@patch('orchestrator.views.hooks.task_client')
def test_post_synchronous(self, m_task_client, m_handle_callback_hook):
"""
Should return accepted response when a valid github hook is posted.
"""
# Given: Valid JSON Payload for posting to the hook
mock_payload = self._create_mock_payload()

# And: Mock Callback hook handler
m_handle_callback_hook.delay.return_value.id = 'mock_task_id'
mock_job = {
'meta-info': {
'job-id': 'mock_job_id'
}

}
m_task_client.ready.return_value = {
'output': mock_job
}

# When I post to generic interal hook
resp = self.client.post(
'/hooks/generic',
data=json.dumps(mock_payload),
headers={
'Content-Type': MIME_GENERIC_HOOK_V1,
'Accept': MIME_JOB_V1
}
)

# Then: Created job is returned
eq_(resp.status_code, 201)
data = json.loads(resp.data.decode('UTF-8'))
dict_compare(data, mock_job)
eq_(resp.headers['Location'], 'http://localhost/jobs/mock_job_id')

@patch('orchestrator.views.hooks.undeploy')
def test_delete(self, m_undeploy):
"""
Should return accepted response when a valid github hook is posted.
"""

# Given: Mock undeploy handler
m_undeploy.si.return_value.delay.return_value = 'mock_task_id'

# And: Existing Repository and job for given branch
owner = 'totem'
repo = 'totem-demo'
ref = 'master'

# When I fire delete request
resp = self.client.delete(
'/hooks/generic?owner={0}&repo={1}&ref={2}'.format(
owner, repo, ref),
)

# Then: Created job is returned
eq_(resp.status_code, 202)
data = json.loads(resp.data.decode('UTF-8'))
dict_compare(data, {
'task_id': 'mock_task_id'
})

@patch('orchestrator.views.hooks.undeploy')
def test_delete_with_no_query_params(self, m_undeploy):
"""
Should return accepted response when a valid github hook is posted.
"""

# Given: Mock undeploy handler
m_undeploy.si.return_value.delay.return_value = 'mock_task_id'

# When I fire delete request
resp = self.client.delete('/hooks/generic')

# Then: Created job is returned
eq_(resp.status_code, 422)
data = json.loads(resp.data.decode('UTF-8'))
eq_(data['code'], 'BUSINESS_RULE_VIOLATION')

0 comments on commit a85cc8f

Please sign in to comment.