2016-11-18 19:26:04 +01:00
|
|
|
import mock
|
2018-11-04 19:44:17 +01:00
|
|
|
from twisted.internet import reactor
|
2016-11-18 19:26:04 +01:00
|
|
|
from twisted.trial import unittest
|
2018-11-04 21:10:49 +01:00
|
|
|
from lbrynet.extras.daemon import conf
|
2018-11-04 19:44:17 +01:00
|
|
|
from lbrynet.extras.daemon.auth import server
|
2016-11-18 19:26:04 +01:00
|
|
|
|
2018-07-06 22:16:58 +02:00
|
|
|
from tests.mocks import mock_conf_settings
|
|
|
|
|
2016-11-18 19:26:04 +01:00
|
|
|
|
|
|
|
class AuthJSONRPCServerTest(unittest.TestCase):
|
|
|
|
# TODO: move to using a base class for tests
|
|
|
|
# and add useful general utilities like this
|
|
|
|
# onto it.
|
|
|
|
def setUp(self):
|
2018-05-02 20:45:01 +02:00
|
|
|
conf.initialize_settings(False)
|
2018-07-20 22:46:15 +02:00
|
|
|
self.server = server.AuthJSONRPCServer(True, use_authentication=False)
|
2016-11-18 19:26:04 +01:00
|
|
|
|
2018-09-21 18:38:57 +02:00
|
|
|
def test_listen_auth_https(self):
|
|
|
|
self.server._use_https = True
|
|
|
|
self.server._use_authentication = True
|
|
|
|
factory = self.server.get_server_factory()
|
|
|
|
listening_port = reactor.listenSSL(
|
|
|
|
conf.settings['api_port'], factory, factory.options, interface="localhost"
|
|
|
|
)
|
|
|
|
listening_port.stopListening()
|
|
|
|
|
|
|
|
def test_listen_no_auth_https(self):
|
|
|
|
self.server._use_https = True
|
|
|
|
self.server._use_authentication = False
|
|
|
|
factory = self.server.get_server_factory()
|
|
|
|
listening_port = reactor.listenSSL(
|
|
|
|
conf.settings['api_port'], factory, factory.options, interface="localhost"
|
|
|
|
)
|
|
|
|
listening_port.stopListening()
|
|
|
|
|
|
|
|
def test_listen_auth_http(self):
|
|
|
|
self.server._use_https = False
|
|
|
|
self.server._use_authentication = True
|
|
|
|
factory = self.server.get_server_factory()
|
|
|
|
listening_port = reactor.listenTCP(
|
|
|
|
conf.settings['api_port'], factory, interface="localhost"
|
|
|
|
)
|
|
|
|
listening_port.stopListening()
|
|
|
|
|
|
|
|
def test_listen_no_auth_http(self):
|
|
|
|
self.server._use_https = False
|
|
|
|
self.server._use_authentication = False
|
|
|
|
factory = self.server.get_server_factory()
|
|
|
|
listening_port = reactor.listenTCP(
|
|
|
|
conf.settings['api_port'], factory, interface="localhost"
|
|
|
|
)
|
|
|
|
listening_port.stopListening()
|
|
|
|
|
2016-11-18 19:26:04 +01:00
|
|
|
def test_get_server_port(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
('example.com', 80), self.server.get_server_port('http://example.com'))
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
('example.com', 1234), self.server.get_server_port('http://example.com:1234'))
|
|
|
|
|
|
|
|
def test_foreign_origin_is_rejected(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
mock_conf_settings(self) # have to call this to generate Config mock
|
2016-11-18 19:26:04 +01:00
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://example.com')
|
|
|
|
self.assertFalse(self.server._check_header_source(request, 'Origin'))
|
|
|
|
|
|
|
|
def test_wrong_port_is_rejected(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
mock_conf_settings(self, {'api_port': 1234})
|
2016-11-18 19:26:04 +01:00
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://localhost:9999')
|
|
|
|
self.assertFalse(self.server._check_header_source(request, 'Origin'))
|
|
|
|
|
|
|
|
def test_matching_origin_is_allowed(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
mock_conf_settings(self, {'api_host': 'example.com', 'api_port': 1234})
|
2016-11-18 19:26:04 +01:00
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://example.com:1234')
|
|
|
|
self.assertTrue(self.server._check_header_source(request, 'Origin'))
|
|
|
|
|
|
|
|
def test_any_origin_is_allowed(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
mock_conf_settings(self, {'api_host': '0.0.0.0', 'api_port': 80})
|
2016-11-18 19:26:04 +01:00
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://example.com')
|
|
|
|
self.assertTrue(self.server._check_header_source(request, 'Origin'))
|
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://another-example.com')
|
|
|
|
self.assertTrue(self.server._check_header_source(request, 'Origin'))
|
|
|
|
|
|
|
|
def test_matching_referer_is_allowed(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
mock_conf_settings(self, {'api_host': 'the_api', 'api_port': 1111})
|
2016-11-18 19:26:04 +01:00
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://the_api:1111?settings')
|
|
|
|
self.assertTrue(self.server._check_header_source(request, 'Referer'))
|
|
|
|
request.getHeader.assert_called_with('Referer')
|
2017-01-12 16:49:57 +01:00
|
|
|
|
|
|
|
def test_request_is_allowed_when_matching_allowed_origin_setting(self):
|
|
|
|
mock_conf_settings(self, {'allowed_origin': 'http://example.com:1234'})
|
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
request.getHeader = mock.Mock(return_value='http://example.com:1234')
|
|
|
|
self.assertTrue(self.server._check_header_source(request, 'Origin'))
|
|
|
|
|
|
|
|
def test_request_is_rejected_when_not_matching_allowed_origin_setting(self):
|
|
|
|
mock_conf_settings(self, {'allowed_origin': 'http://example.com:1234'})
|
|
|
|
request = mock.Mock(['getHeader'])
|
|
|
|
# note the ports don't match
|
|
|
|
request.getHeader = mock.Mock(return_value='http://example.com:1235')
|
|
|
|
self.assertFalse(self.server._check_header_source(request, 'Origin'))
|