|
| 1 | +import unittest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from cterasdk.core.remote import _relay_base |
| 5 | + |
| 6 | + |
| 7 | +class TestRelayBase(unittest.TestCase): |
| 8 | + |
| 9 | + def _make_portal(self, baseurl): |
| 10 | + portal = mock.MagicMock() |
| 11 | + portal.ctera.baseurl = baseurl |
| 12 | + return portal |
| 13 | + |
| 14 | + def _make_device(self, name, device_dns_name=None): |
| 15 | + device = mock.MagicMock(spec=['name', 'deviceDnsName'] if device_dns_name is not None else ['name']) |
| 16 | + device.name = name |
| 17 | + if device_dns_name is not None: |
| 18 | + device.deviceDnsName = device_dns_name |
| 19 | + return device |
| 20 | + |
| 21 | + def test_fallback_when_device_dns_is_none(self): |
| 22 | + portal = self._make_portal('https://portal.ctera.me') |
| 23 | + device = self._make_device('vGateway-7192') |
| 24 | + result = _relay_base(portal, device) |
| 25 | + self.assertEqual(result, 'https://portal.ctera.me/devices/vGateway-7192') |
| 26 | + |
| 27 | + def test_fallback_when_device_dns_does_not_start_with_name(self): |
| 28 | + portal = self._make_portal('https://portal.ctera.me') |
| 29 | + device = self._make_device('vGateway-7192', 'other-device.portal.ctera.me') |
| 30 | + result = _relay_base(portal, device) |
| 31 | + self.assertEqual(result, 'https://portal.ctera.me/devices/vGateway-7192') |
| 32 | + |
| 33 | + def test_hostname_derivation_from_dns_name(self): |
| 34 | + portal = self._make_portal('https://10.0.0.1') |
| 35 | + device = self._make_device('vGateway-7192', 'vGateway-7192.portal.ctera.me') |
| 36 | + result = _relay_base(portal, device) |
| 37 | + self.assertEqual(result, 'https://portal.ctera.me/devices/vGateway-7192') |
| 38 | + |
| 39 | + def test_non_standard_port_preserved(self): |
| 40 | + portal = self._make_portal('https://10.0.0.1:8443') |
| 41 | + device = self._make_device('vGateway-7192', 'vGateway-7192.portal.ctera.me') |
| 42 | + result = _relay_base(portal, device) |
| 43 | + self.assertEqual(result, 'https://portal.ctera.me:8443/devices/vGateway-7192') |
| 44 | + |
| 45 | + def test_standard_https_port_omitted(self): |
| 46 | + portal = self._make_portal('https://10.0.0.1:443') |
| 47 | + device = self._make_device('vGateway-7192', 'vGateway-7192.portal.ctera.me') |
| 48 | + result = _relay_base(portal, device) |
| 49 | + self.assertEqual(result, 'https://portal.ctera.me/devices/vGateway-7192') |
| 50 | + |
| 51 | + def test_standard_http_port_omitted(self): |
| 52 | + portal = self._make_portal('http://10.0.0.1:80') |
| 53 | + device = self._make_device('vGateway-7192', 'vGateway-7192.portal.ctera.me') |
| 54 | + result = _relay_base(portal, device) |
| 55 | + self.assertEqual(result, 'http://portal.ctera.me/devices/vGateway-7192') |
| 56 | + |
| 57 | + def test_trailing_slash_in_baseurl_no_double_slash(self): |
| 58 | + portal = self._make_portal('https://portal.ctera.me/') |
| 59 | + device = self._make_device('vGateway-7192', 'vGateway-7192.portal.ctera.me') |
| 60 | + result = _relay_base(portal, device) |
| 61 | + self.assertNotIn('//', result.split('://')[1]) |
| 62 | + self.assertEqual(result, 'https://portal.ctera.me/devices/vGateway-7192') |
| 63 | + |
| 64 | + def test_baseurl_with_path(self): |
| 65 | + portal = self._make_portal('https://10.0.0.1/api/v1') |
| 66 | + device = self._make_device('vGateway-7192', 'vGateway-7192.portal.ctera.me') |
| 67 | + result = _relay_base(portal, device) |
| 68 | + self.assertEqual(result, 'https://portal.ctera.me/api/v1/devices/vGateway-7192') |
| 69 | + |
| 70 | + def test_substring_device_name_does_not_false_match(self): |
| 71 | + """A device named 'gw' should NOT match dns 'other-gw.portal.ctera.me'.""" |
| 72 | + portal = self._make_portal('https://portal.ctera.me') |
| 73 | + device = self._make_device('gw', 'other-gw.portal.ctera.me') |
| 74 | + result = _relay_base(portal, device) |
| 75 | + self.assertEqual(result, 'https://portal.ctera.me/devices/gw') |
| 76 | + |
| 77 | + def test_fallback_strips_trailing_slash(self): |
| 78 | + portal = self._make_portal('https://portal.ctera.me/') |
| 79 | + device = self._make_device('vGateway-7192') |
| 80 | + result = _relay_base(portal, device) |
| 81 | + self.assertEqual(result, 'https://portal.ctera.me/devices/vGateway-7192') |
0 commit comments