|
| 1 | +# Copyright 2016 Internap. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +from flexmock import flexmock, flexmock_teardown |
| 18 | + |
| 19 | +from . import ContextMatcher |
| 20 | +from ubersmith_remote_module_server import ubersmith_core |
| 21 | +from ubersmith_remote_module_server.objects import RequestContext |
| 22 | +from ubersmith_remote_module_server.router import Router |
| 23 | +from ubersmith_remote_module_server.ubersmith_core import UbersmithCore |
| 24 | + |
| 25 | + |
| 26 | +class FakeModule(object): |
| 27 | + def call_uber_core(self): |
| 28 | + UbersmithCore.test() |
| 29 | + |
| 30 | + |
| 31 | +class RouterUbersmithCoreTest(unittest.TestCase): |
| 32 | + def test_a_module_can_call_ubersmith_core_without_context(self): |
| 33 | + fake_module = FakeModule() |
| 34 | + remote_executor_mock = flexmock() |
| 35 | + |
| 36 | + flexmock(ubersmith_core).should_receive('RemoteExecutor').\ |
| 37 | + with_args(context=RequestContext).and_return(remote_executor_mock) |
| 38 | + |
| 39 | + remote_executor_mock.should_receive('invoke_global').with_args('test', args={}).once() |
| 40 | + |
| 41 | + self.basic_router = Router(env_as_kwarg=False) |
| 42 | + self.basic_router.invoke_method(module=fake_module, method='call_uber_core') |
| 43 | + |
| 44 | + def test_module_context_is_injected_when_service_module(self): |
| 45 | + fake_module = FakeModule() |
| 46 | + remote_executor_mock = flexmock() |
| 47 | + |
| 48 | + example_callback = {'params': {'module_id': '44', 'service_id': '1260'}, |
| 49 | + 'url': 'http://user:[email protected]/' |
| 50 | + 'api/2.0/?method=service.module_call'} |
| 51 | + |
| 52 | + flexmock(ubersmith_core).should_receive('RemoteExecutor'). \ |
| 53 | + with_args(context=ContextMatcher(callback_url=example_callback['url'], |
| 54 | + module_id='44', |
| 55 | + service_id='1260'))\ |
| 56 | + .and_return(remote_executor_mock) |
| 57 | + |
| 58 | + remote_executor_mock.should_receive('invoke_global') |
| 59 | + |
| 60 | + self.basic_router = Router(env_as_kwarg=False) |
| 61 | + self.basic_router.invoke_method(module=fake_module, |
| 62 | + method='call_uber_core', |
| 63 | + callback=example_callback) |
| 64 | + |
| 65 | + def test_module_context_is_injected_when_device_module(self): |
| 66 | + fake_module = FakeModule() |
| 67 | + |
| 68 | + example_callback = {'params': {'module_id': '173', 'device_id': '200025'}, |
| 69 | + 'url': 'http://user:[email protected]/' |
| 70 | + 'api/2.0/?method=device.module_call'} |
| 71 | + |
| 72 | + remote_executor_mock = flexmock() |
| 73 | + flexmock(ubersmith_core).should_receive('RemoteExecutor'). \ |
| 74 | + with_args(context=ContextMatcher(callback_url=example_callback['url'], |
| 75 | + module_id='173', |
| 76 | + device_id='200025'))\ |
| 77 | + .and_return(remote_executor_mock) |
| 78 | + |
| 79 | + remote_executor_mock.should_receive('invoke_global') |
| 80 | + |
| 81 | + self.basic_router = Router(env_as_kwarg=False) |
| 82 | + self.basic_router.invoke_method(module=fake_module, |
| 83 | + method='call_uber_core', |
| 84 | + callback=example_callback) |
| 85 | + |
| 86 | + def tearDown(self): |
| 87 | + flexmock_teardown() |
| 88 | + super(RouterUbersmithCoreTest, self).tearDown() |
0 commit comments