# Path: pushmanager/testing/mocksettings.py

# 
# Path: pushmanager/servlets/testtag.py
# class TestTagServlet(RequestHandler):
# 
#     def _arg(self, key):
#         return pushmanager.core.util.get_str_arg(self.request, key, '')
# 
#     @tornado.web.asynchronous
#     def get(self):
#         request_id = pushmanager.core.util.get_int_arg(self.request, 'id')
#         resp = {}
#         if not request_id:
#             self.set_status(404)
#         else:
#             request = GitQueue._get_request(request_id)
#             resp = self._gen_test_tag_resp(request)
#         self.finish(resp)
# 
#     @classmethod
#     def _gen_test_tag_resp(cls, request):
#         response = {}
# 
#         if 'tests_tag' in Settings and Settings['tests_tag']['tag'] in request['tags']:
#             response['tag'] = Settings['tests_tag']['tag']
#             try:
#                 api_url = Settings['tests_tag']['tag_api_endpoint'].replace('%SHA%', request['revision'])
#                 api_body = Settings['tests_tag']['tag_api_body'].replace('%SHA%', request['revision'])
#                 api_resp = urllib2.urlopen(api_url, api_body)
#                 response['tag'] = xhtml_escape(json.loads(api_resp.read())['tag'])
#             except Exception as e:
#                 response['tag'] += ": ERROR connecting to server"
#                 logging.error(e)
# 
#             response['url'] = ''
#             if 'url_api_endpoint' in Settings['tests_tag']:
#                 try:
#                     result_api_url = Settings['tests_tag']['url_api_endpoint'].replace('%SHA%', request['revision'])
#                     result_api_url = result_api_url.replace('%BRANCH%', request['branch'])
#                     result_api_body = Settings['tests_tag']['url_api_body'].replace('%SHA%', request['revision'])
#                     result_api_body = result_api_body.replace('%BRANCH%', request['branch'])
#                     resp = urllib2.urlopen(result_api_url, result_api_body)
#                     result_id = url_escape(json.loads(resp.read())['id'])
#                     if result_id != '':
#                         response['url'] = Settings['tests_tag']['url_tmpl'].replace(
#                             '%ID%',
#                             result_id
#                         ).replace(
#                             '%SHA%',
#                             request['revision']
#                         )
#                         response['url'] = response['url'].replace('%BRANCH%', request['branch'])
#                 except Exception as e:
#                     logging.warning(e)
#                     logging.warning(
#                         "Couldn't load results for results test URL from %s with body %s" % (
#                             Settings['tests_tag']['url_api_endpoint'].replace('%SHA%', request['revision']),
#                             Settings['tests_tag']['url_api_body'].replace('%SHA%', request['revision'])
#                         )
#                     )
#         return response


import mock
import testify as T
from pushmanager.testing.mocksettings import MockedSettings
from pushmanager.servlets.testtag import TestTagServlet
from pushmanager.core.settings import Settings
        MockedSettings['tests_tag']['url_tmpl'] = 'www.example.com/%ID%'

        request_info = {'tags': 'test', 'branch': 'test', 'revision': 'abc123'}
        with mock.patch.dict(Settings, MockedSettings):
            gen_tags = TestTagServlet._gen_test_tag_resp(request_info)
            T.assert_equals({'tag': 'tag 0 fails', 'url': "www.example.com/123"}, gen_tags)

    @mock.patch('pushmanager.servlets.testtag.urllib2.urlopen')
    def test_generate_test_tag_no_url(self, mock_urlopen):
        m = mock.Mock()
        m.read.side_effect = ['{"tag" : "tag 0 fails"}', '{"url" : ""}']
        mock_urlopen.return_value = m

        MockedSettings['tests_tag'] = {}
        MockedSettings['tests_tag']['tag'] = 'test'
        MockedSettings['tests_tag']['tag_api_endpoint'] = 'example.com'
        MockedSettings['tests_tag']['tag_api_body'] = '{ "sha" : "%SHA%" }'
        MockedSettings['tests_tag']['url_api_endpoint'] = "http://example.com/api/v1/test_results_url"
        MockedSettings['tests_tag']['url_api_body'] = '{ "sha" : "%SHA%" }'
        MockedSettings['tests_tag']['url_tmpl'] = 'www.example.com/%ID%'

        request_info = {'tags': 'test', 'branch': 'test', 'revision': 'abc123'}
        with mock.patch.dict(Settings, MockedSettings):
            gen_tags = TestTagServlet._gen_test_tag_resp(request_info)
            T.assert_equals({'tag': 'tag 0 fails', 'url': ""}, gen_tags)

    def test_generate_test_tag_none(self):
        del MockedSettings['tests_tag']

        request_info = {'tags': 'test', 'branch': 'test', 'revision': 'abc123'}

# Path: pushmanager/testing/mocksettings.py

# 
# Path: pushmanager/servlets/testtag.py
# class TestTagServlet(RequestHandler):
# 
#     def _arg(self, key):
#         return pushmanager.core.util.get_str_arg(self.request, key, '')
# 
#     @tornado.web.asynchronous
#     def get(self):
#         request_id = pushmanager.core.util.get_int_arg(self.request, 'id')
#         resp = {}
#         if not request_id:
#             self.set_status(404)
#         else:
#             request = GitQueue._get_request(request_id)
#             resp = self._gen_test_tag_resp(request)
#         self.finish(resp)
# 
#     @classmethod
#     def _gen_test_tag_resp(cls, request):
#         response = {}
# 
#         if 'tests_tag' in Settings and Settings['tests_tag']['tag'] in request['tags']:
#             response['tag'] = Settings['tests_tag']['tag']
#             try:
#                 api_url = Settings['tests_tag']['tag_api_endpoint'].replace('%SHA%', request['revision'])
#                 api_body = Settings['tests_tag']['tag_api_body'].replace('%SHA%', request['revision'])
#                 api_resp = urllib2.urlopen(api_url, api_body)
#                 response['tag'] = xhtml_escape(json.loads(api_resp.read())['tag'])
#             except Exception as e:
#                 response['tag'] += ": ERROR connecting to server"
#                 logging.error(e)
# 
#             response['url'] = ''
#             if 'url_api_endpoint' in Settings['tests_tag']:
#                 try:
#                     result_api_url = Settings['tests_tag']['url_api_endpoint'].replace('%SHA%', request['revision'])
#                     result_api_url = result_api_url.replace('%BRANCH%', request['branch'])
#                     result_api_body = Settings['tests_tag']['url_api_body'].replace('%SHA%', request['revision'])
#                     result_api_body = result_api_body.replace('%BRANCH%', request['branch'])
#                     resp = urllib2.urlopen(result_api_url, result_api_body)
#                     result_id = url_escape(json.loads(resp.read())['id'])
#                     if result_id != '':
#                         response['url'] = Settings['tests_tag']['url_tmpl'].replace(
#                             '%ID%',
#                             result_id
#                         ).replace(
#                             '%SHA%',
#                             request['revision']
#                         )
#                         response['url'] = response['url'].replace('%BRANCH%', request['branch'])
#                 except Exception as e:
#                     logging.warning(e)
#                     logging.warning(
#                         "Couldn't load results for results test URL from %s with body %s" % (
#                             Settings['tests_tag']['url_api_endpoint'].replace('%SHA%', request['revision']),
#                             Settings['tests_tag']['url_api_body'].replace('%SHA%', request['revision'])
#                         )
#                     )
#         return response


# Path: pushmanager/tests/test_servlet_testtag.py
import mock
import testify as T
from pushmanager.testing.mocksettings import MockedSettings
from pushmanager.servlets.testtag import TestTagServlet
from pushmanager.core.settings import Settings

        MockedSettings['tests_tag']['url_tmpl'] = 'www.example.com/%ID%'

        request_info = {'tags': 'test', 'branch': 'test', 'revision': 'abc123'}
        with mock.patch.dict(Settings, MockedSettings):
            gen_tags = TestTagServlet._gen_test_tag_resp(request_info)
            T.assert_equals({'tag': 'tag 0 fails', 'url': "www.example.com/123"}, gen_tags)

    @mock.patch('pushmanager.servlets.testtag.urllib2.urlopen')
    def test_generate_test_tag_no_url(self, mock_urlopen):
        m = mock.Mock()
        m.read.side_effect = ['{"tag" : "tag 0 fails"}', '{"url" : ""}']
        mock_urlopen.return_value = m

        MockedSettings['tests_tag'] = {}
        MockedSettings['tests_tag']['tag'] = 'test'
        MockedSettings['tests_tag']['tag_api_endpoint'] = 'example.com'
        MockedSettings['tests_tag']['tag_api_body'] = '{ "sha" : "%SHA%" }'
        MockedSettings['tests_tag']['url_api_endpoint'] = "http://example.com/api/v1/test_results_url"
        MockedSettings['tests_tag']['url_api_body'] = '{ "sha" : "%SHA%" }'
        MockedSettings['tests_tag']['url_tmpl'] = 'www.example.com/%ID%'

        request_info = {'tags': 'test', 'branch': 'test', 'revision': 'abc123'}
        with mock.patch.dict(Settings, MockedSettings):
            gen_tags = TestTagServlet._gen_test_tag_resp(request_info)
            T.assert_equals({'tag': 'tag 0 fails', 'url': ""}, gen_tags)

    def test_generate_test_tag_none(self):
        del MockedSettings['tests_tag']

        request_info = {'tags': 'test', 'branch': 'test', 'revision': 'abc123'}

        with mock.patch.dict(Settings, MockedSettings):