openlp/tests/functional/openlp_core/api/http/test_wsgiapp.py

89 lines
3.2 KiB
Python
Raw Normal View History

2016-07-19 20:47:55 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers #
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
2016-07-19 20:47:55 +00:00
"""
Functional test the routing code.
"""
2016-07-20 19:59:02 +00:00
import os
2016-07-19 20:47:55 +00:00
from unittest import TestCase
2017-08-12 20:08:12 +00:00
from unittest.mock import MagicMock
2018-10-27 01:53:43 +00:00
from openlp.core.api.http import register_endpoint, application
2017-12-28 08:27:44 +00:00
from openlp.core.api.http.endpoint import Endpoint
2018-10-27 01:53:43 +00:00
from openlp.core.api.http.errors import NotFound
2016-07-19 20:47:55 +00:00
2018-10-02 04:39:42 +00:00
2016-07-20 19:59:02 +00:00
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
test_endpoint = Endpoint('test', template_dir=ROOT_DIR, static_dir=ROOT_DIR)
2016-07-19 20:47:55 +00:00
class TestRouting(TestCase):
"""
2016-07-20 19:59:02 +00:00
Test the HTTP routing
2016-07-19 20:47:55 +00:00
"""
def setUp(self):
"""
Convert the application to a test application
:return:
"""
for route, views in application.route_map.items():
application.route_map[route]['GET'] = MagicMock()
def test_routing(self):
"""
2016-07-20 19:59:02 +00:00
Test the Routing in the new application via dispatch
2016-07-19 20:47:55 +00:00
:return:
"""
2016-07-20 19:59:02 +00:00
# GIVE: I try to request and
2016-07-19 20:47:55 +00:00
# WHEN: when the URL is not correct and dispatch called
rqst = MagicMock()
2016-07-20 19:59:02 +00:00
rqst.path = '/test/api'
2016-07-19 20:47:55 +00:00
rqst.method = 'GET'
with self.assertRaises(NotFound) as context:
application.dispatch(rqst)
# THEN: the not found returned
2017-12-09 15:21:59 +00:00
assert context.exception.args[0] == 'Not Found', 'URL not found in dispatcher'
2016-07-19 20:47:55 +00:00
# WHEN: when the URL is correct and dispatch called
rqst = MagicMock()
2016-07-20 19:59:02 +00:00
rqst.path = '/test/image'
2016-07-19 20:47:55 +00:00
rqst.method = 'GET'
application.dispatch(rqst)
# THEN: the not found id called
2018-10-28 23:24:55 +00:00
route_key = next(iter(application.route_map))
assert '/image' in route_key
assert 1 == application.route_map[route_key]['GET'].call_count, \
2017-12-09 15:21:59 +00:00
'main_index function should have been called'
2016-07-20 19:59:02 +00:00
@test_endpoint.route('image')
def image(request):
pass
@test_endpoint.route('')
def index(request):
pass
register_endpoint(test_endpoint)