示例#1
0
    def test_sync_server_side_client_ip(self):
        service_config = get_service_config([
            Service("ownCloud", "http", "_http._tcp", 80, url="owncloud"),
            Service("SSH", "https", "_http._tcp", 81, url=None)
        ])
        port_config = get_port_config([Port(80, 80), Port(81, 81)])
        upnpc = get_upnpc(external_ip='10.1.1.1')
        upnpc.mapped_external_ports = MagicMock(side_effect=[[], [80]])

        port_mapper = get_port_mapper(port_config, upnpc)
        domain_config = get_domain_config(Domain('boris', 'some_update_token'))

        responses.add(responses.POST,
                      "http://api.domain.com/domain/update",
                      status=200,
                      body="{'message': 'Domain was updated'}",
                      content_type="application/json")

        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config, port_mapper, '127.0.0.1')
        dns.sync()

        expected_request = '''
{
    "services": [
        {"name": "ownCloud", "protocol": "http", "type": "_http._tcp", "port": 80, "local_port": 80, "url": "owncloud"},
        {"name": "SSH", "protocol": "https", "type": "_http._tcp", "port": 81, "local_port": 81, "url": null}
    ],
    "token": "some_update_token",
    "local_ip": "127.0.0.1"
}
'''
        self.assertSingleRequest(expected_request)
示例#2
0
    def test_get_not_existing_service(self):
        service_config = get_service_config([])
        port_config = get_port_config([])
        upnpc = get_upnpc(external_ip='192.167.44.52')
        port_mapper = get_port_mapper(port_config, upnpc)

        domain_config = get_domain_config(None)
        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config, port_mapper, '127.0.0.1')

        service = dns.get_service("ownCloud")

        self.assertIsNone(service)
示例#3
0
    def test_get_service(self):
        service_config = get_service_config([])
        port_config = get_port_config([])
        upnpc = get_upnpc(external_ip='192.167.44.52')
        port_mapper = get_port_mapper(port_config, upnpc)

        domain_config = get_domain_config(None)

        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config, port_mapper, '127.0.0.1')
        dns.add_service("ownCloud", "http", "_http._tcp", 80, url="owncloud")

        service = dns.get_service("ownCloud")

        self.assertIsNotNone(service)
        self.assertEquals("ownCloud", service.name)
        self.assertEquals("_http._tcp", service.type)
        self.assertEquals(80, service.port)
        self.assertEquals("owncloud", service.url)
示例#4
0
    def test_endpoints(self):
        service_config = get_service_config([
            Service("ownCloud", "http", "_http._tcp", 80, url="owncloud"),
            Service("SSH", "https", "_http._tcp", 81, url=None)
        ])
        port_config = get_port_config([Port(80, 8080), Port(81, 8181)])
        upnpc = get_upnpc(external_ip='10.1.1.1')
        # upnpc.mapped_external_ports = MagicMock(side_effect=[[], [80]])

        port_mapper = get_port_mapper(port_config, upnpc)
        domain_config = get_domain_config(Domain('boris', 'some_update_token'))
        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config, port_mapper, '127.0.0.1')

        endpoints = dns.endpoints()
        assert len(endpoints) == 2
        assert endpoints[0].service.name == 'ownCloud'
        assert endpoints[0].external_port == 8080
        assert endpoints[1].service.name == 'SSH'
        assert endpoints[1].external_port == 8181
示例#5
0
    def test_sync_server_error(self):
        service_config = get_service_config([Service("ownCloud", "http", "_http._tcp", 80, url="owncloud")])
        port_config = get_port_config([Port(80, 10000)])
        upnpc = get_upnpc(external_ip='192.167.44.52')
        port_mapper = get_port_mapper(port_config, upnpc)
        domain_config = get_domain_config(Domain('boris', 'some_update_token'))

        responses.add(responses.POST,
                      "http://api.domain.com/domain/update",
                      status=400,
                      body='{"message": "Unknown update token"}',
                      content_type="application/json")

        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config, port_mapper, '127.0.0.1')

        with self.assertRaises(Exception) as context:
            dns.sync()

        self.assertEquals(context.exception.message, "Unknown update token")
示例#6
0
    def test_link_server_error(self):
        config.footprints.append(('my-PC', footprint.footprint()))
        config.titles['my-PC'] = 'My PC'

        domain_config = get_domain_config(None)

        responses.add(responses.POST,
                      "http://api.domain.com/domain/acquire",
                      status=403,
                      body='{"message": "Authentication failed"}',
                      content_type="application/json")

        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config=None, port_mapper=None, local_ip='127.0.0.1')
        with self.assertRaises(Exception) as context:
            result = dns.acquire('*****@*****.**', 'pass1234', 'boris')

        self.assertEquals(context.exception.message, "Authentication failed")

        domain = domain_config.load()
        self.assertIsNone(domain)
示例#7
0
    def test_add_service(self):
        service_config = get_service_config([])
        port_config = get_port_config([])
        upnpc = get_upnpc(external_ip='192.167.44.52')
        port_mapper = get_port_mapper(port_config, upnpc)

        domain_config = get_domain_config(None)

        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config, port_mapper, '127.0.0.1')
        dns.add_service("ownCloud", "http", "_http._tcp", 80, url="owncloud")

        services = service_config.load()
        self.assertEquals(1, len(services))
        service = services[0]
        self.assertEquals("ownCloud", service.name)
        self.assertEquals("_http._tcp", service.type)
        self.assertEquals(80, service.port)
        self.assertEquals("owncloud", service.url)

        mappings = port_config.load()
        self.assertEquals(1, len(mappings))
        mapping = mappings[0]
        self.assertEquals(80, mapping.local_port)
示例#8
0
    def test_link_success(self):
        config.footprints.append(('my-PC', footprint.footprint()))
        config.titles['my-PC'] = 'My PC'
        device_id = id.id()

        domain_config = get_domain_config(None)

        responses.add(responses.POST,
                      "http://api.domain.com/domain/acquire",
                      status=200,
                      body='{"user_domain": "boris", "update_token": "some_update_token"}',
                      content_type="application/json")

        insider_config = get_insider_config('domain.com', 'http://api.domain.com')
        dns = Dns(insider_config, domain_config, service_config=None, port_mapper=None, local_ip='127.0.0.1')
        result = dns.acquire('*****@*****.**', 'pass1234', 'boris')

        self.assertIsNotNone(result)
        self.assertEquals(result.user_domain, "boris")
        self.assertEquals(result.update_token, "some_update_token")

        expected_request_data = {
            "password": "******",
            "email": "*****@*****.**",
            "user_domain": "boris",
            'device_mac_address': device_id.mac_address,
            'device_name': device_id.name,
            'device_title': device_id.title,
        }
        # Need to assert all passed POST parameters
        # self.assertSingleRequest(convertible.to_json(expected_request_data))

        domain = domain_config.load()
        self.assertIsNotNone(domain)
        self.assertEquals(domain.user_domain, "boris")
        self.assertEquals(domain.update_token, "some_update_token")