Ejemplo n.º 1
0
async def test_synse_led_write_valid_6(mock_validate_device_type, mock_write,
                                       no_pretty_json):
    """Test writing LED state with a valid state specified."""

    r = utils.make_request('/synse/led?color=ffffff&state=on')

    result = await led_route(r, 'rack-1', 'vec', '123456')

    expected = [{
        'context': {
            'action': 'state',
            'data': b'on'
        },
        'transaction': 'rack-1-vec-123456'
    }, {
        'context': {
            'action': 'color',
            'data': b'ffffff'
        },
        'transaction': 'rack-1-vec-123456'
    }]

    expected_json = ujson.dumps(expected)

    assert isinstance(result, HTTPResponse)
    assert result.body == expected_json.encode('ascii')
    assert result.status == 200
Ejemplo n.º 2
0
async def test_synse_scan_route_bad_param(mock_scan, no_pretty_json):
    """Test scanning, passing an unsupported query param."""

    r = utils.make_request('/synse/scan?unsupported=true')

    with pytest.raises(errors.InvalidArgumentsError):
        await scan_route(r)
Ejemplo n.º 3
0
async def test_synse_power_route_bad_param(mock_validate_device_type, mock_write, no_pretty_json):
    """Test setting power, passing an unsupported query param."""

    r = utils.make_request('/synse/power?unsupported=true')

    with pytest.raises(errors.InvalidArgumentsError):
        await power_route(r, 'rack-1', 'vec', '123456')
Ejemplo n.º 4
0
    def test_federation_client_unsafe_ip(self, resolver):
        self.sydent.run()

        request, channel = make_request(
            self.sydent.reactor,
            "POST",
            "/_matrix/identity/v2/account/register",
            {
                "access_token": "foo",
                "expires_in": 300,
                "matrix_server_name": "example.com",
                "token_type": "Bearer",
            },
        )

        resolver.return_value = [
            Server(
                host=self.unsafe_domain,
                port=443,
                priority=1,
                weight=1,
                expires=100,
            )
        ]

        request.render(self.sydent.servlets.registerServlet)

        self.assertNot(self.reactor.tcpClients)

        self.assertEqual(channel.code, 500)
Ejemplo n.º 5
0
async def test_synse_config_route(mock_plugins, no_pretty_json):
    """Test successfully getting the plugins."""

    result = await plugins_route(utils.make_request('/synse/plugins'))

    assert isinstance(result, HTTPResponse)
    assert result.body == b'[]'
    assert result.status == 200
Ejemplo n.º 6
0
async def test_synse_info_route_no_optional(mock_info, no_pretty_json):
    """Test successfully getting the info without optional params specified."""

    result = await info_route(utils.make_request('/synse/info'), 'rack1')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"r":"rack1","b":null,"d":null}'
    assert result.status == 200
Ejemplo n.º 7
0
async def test_synse_config_route(mock_config, no_pretty_json):
    """Test successfully getting the config."""

    result = await config_route(utils.make_request('/synse/config'))

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"test":"config"}'
    assert result.status == 200
Ejemplo n.º 8
0
async def test_synse_info_route(mock_info, no_pretty_json):
    """Test successfully getting the info."""

    result = await info_route(utils.make_request('/synse/info'), 'rack1',
                              'board1', 'device1')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"r":"rack1","b":"board1","d":"device1"}'
    assert result.status == 200
Ejemplo n.º 9
0
async def test_synse_read_route(mock_read, no_pretty_json):
    """Test a successful read."""

    result = await read_route(utils.make_request('/synse/read'), 'rack-1',
                              'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"value":1}'
    assert result.status == 200
Ejemplo n.º 10
0
async def test_synse_write_route_invalid_json(mock_write, no_pretty_json):
    """Write when 'raw' and 'action' are not in the given request body."""

    data = {'key1': 'color', 'key2': [b'00ff55']}

    r = utils.make_request('/synse/write', data)

    with pytest.raises(SynseError):
        await write_route(r, 'rack-1', 'vec', '123456')
Ejemplo n.º 11
0
async def test_synse_fan_route_bad_params(mock_validate_device_type,
                                          mock_write, no_pretty_json):
    """Test setting fan, passing both 'speed' and 'speed_percent' params,
    which is not supported."""

    r = utils.make_request('/synse/fan?speed=100&speed_percent=50')

    with pytest.raises(errors.InvalidArgumentsError):
        await fan_route(r, 'rack-1', 'vec', '123456')
Ejemplo n.º 12
0
async def test_synse_power_read(mock_validate_device_type, mock_read, no_pretty_json):
    """Test a successful read."""

    r = utils.make_request('/synse/power')

    result = await power_route(r, 'rack-1', 'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"value":1}'
    assert result.status == 200
Ejemplo n.º 13
0
async def test_synse_scan_route_with_rack_and_board(mock_scan, no_pretty_json):
    """Test performing a scan with a rack and board specified."""

    r = utils.make_request('/synse/scan')

    result = await scan_route(r, 'rack-1', 'vec')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"r":"rack-1","b":"vec","forced":false}'
    assert result.status == 200
Ejemplo n.º 14
0
async def test_synse_led_write_invalid_6(mock_validate_device_type, mock_write, no_pretty_json):
    """Test writing LED state with an invalid state specified."""

    r = utils.make_request('/synse/led?color=-FF')

    try:
        await led_route(r, 'rack-1', 'vec', '123456')
    except errors.SynseError as e:
        assert e.error_id == errors.INVALID_ARGUMENTS
        assert '-FF' in e.args[0]
Ejemplo n.º 15
0
async def test_synse_scan_route(mock_scan, no_pretty_json):
    """Test a successful scan."""

    r = utils.make_request('/synse/scan')

    result = await scan_route(r)

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"r":null,"b":null,"forced":false}'
    assert result.status == 200
Ejemplo n.º 16
0
async def test_synse_scan_route_forced(mock_scan, no_pretty_json):
    """Test forcing a rescan successfully."""

    r = utils.make_request('/synse/scan?force=true')

    result = await scan_route(r)

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"r":null,"b":null,"forced":true}'
    assert result.status == 200
Ejemplo n.º 17
0
async def test_validate_no_query_params():
    """Test validating that an incoming request has no query params, when there
    are no query params.
    """
    @validate.no_query_params()
    async def test_fn(request, *args, **kwargs):
        """Dummy function for testing the decorator."""
        return request

    await test_fn(utils.make_request('/synse/endpoint'))
Ejemplo n.º 18
0
async def test_synse_write_route_bad_json(mock_write, no_pretty_json):
    """Write when invalid JSON is posted."""

    data = '{{/.'

    r = utils.make_request('/synse/write')
    r.body = data

    with pytest.raises(InvalidJsonError):
        await write_route(r, 'rack-1', 'vec', '123456')
Ejemplo n.º 19
0
async def test_synse_led_write_valid_5(mock_validate_device_type, mock_write, no_pretty_json):
    """Test writing LED state with a valid state specified."""

    r = utils.make_request('/synse/led?color=ffffff')

    result = await led_route(r, 'rack-1', 'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'[{"context":{"action":"color","raw":["ffffff"]},"transaction":"rack-1-vec-123456"}]'
    assert result.status == 200
Ejemplo n.º 20
0
async def test_synse_power_write_invalid(mock_validate_device_type, mock_write, no_pretty_json):
    """Test writing power state with an invalid state specified."""

    r = utils.make_request('/synse/power?state=test')

    try:
        await power_route(r, 'rack-1', 'vec', '123456')
    except errors.SynseError as e:
        assert e.error_id == errors.INVALID_ARGUMENTS
        assert 'test' in e.args[0]
Ejemplo n.º 21
0
async def test_synse_power_write_valid_2(mock_validate_device_type, mock_write, no_pretty_json):
    """Test writing power state with a valid state specified."""

    r = utils.make_request('/synse/power?state=off')

    result = await power_route(r, 'rack-1', 'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"data":{"action":"state","raw":"off"}}'
    assert result.status == 200
Ejemplo n.º 22
0
async def test_synse_scan_route_forced_2(mock_scan, no_pretty_json):
    """Test forcing a rescan, but using an unrecognized value."""

    r = utils.make_request('/synse/scan?force=yes')

    result = await scan_route(r)

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"r":null,"b":null,"forced":false}'
    assert result.status == 200
Ejemplo n.º 23
0
    def test_incoming_replication(self):
        """Impersonate a peer that sends a replication push to Sydent, then checks that it
        accepts the payload and saves it correctly.
        """
        self.sydent.run()

        # Configure the Sydent to impersonate. We need to use "fake.server" as the
        # server's name because that's the name the recipient Sydent has for it. On top
        # of that, the replication servlet expects a TLS certificate in the request so it
        # can extract a common name and figure out which peer sent it from its common
        # name. The common name of the certificate we use for tests is fake.server.
        config = {
            "general": {
                "server.name": "fake.server"
            },
            "crypto": {
                "ed25519.signingkey":
                "ed25519 0 b29eXMMAYCFvFEtq9mLI42aivMtcg4Hl0wK89a+Vb6c"
            },
        }

        fake_sender_sydent = make_sydent(config)
        signer = Signer(fake_sender_sydent)

        # Sign the associations with the Sydent to impersonate so the recipient Sydent
        # can verify the signatures on them.
        signed_assocs = {}
        for assoc_id, assoc in enumerate(self.assocs):
            signed_assoc = signer.signedThreePidAssociation(assoc)
            signed_assocs[assoc_id] = signed_assoc

        # Send the replication push.
        body = json.dumps({"sgAssocs": signed_assocs})
        request, channel = make_request(self.sydent.reactor, "POST",
                                        "/_matrix/identity/replicate/v1/push",
                                        body)
        request.render(self.sydent.servlets.replicationPush)

        self.assertEqual(channel.code, 200)

        # Check that the recipient Sydent has correctly saved the associations in the
        # push.
        cur = self.sydent.db.cursor()
        res = cur.execute(
            "SELECT originId, sgAssoc FROM global_threepid_associations")

        res_assocs = {}
        for row in res.fetchall():
            originId = row[0]
            signed_assoc = json.loads(row[1])

            res_assocs[originId] = signed_assoc

        for assoc_id, signed_assoc in signed_assocs.items():
            self.assertDictEqual(signed_assoc, res_assocs[assoc_id])
Ejemplo n.º 24
0
 def test_request_code_via_url_query_params(self) -> None:
     self.sydent.run()
     url = ("/_matrix/identity/api/v1/validate/msisdn/requestToken?"
            "phone_number=447700900750"
            "&country=GB"
            "&client_secret=oursecret"
            "&send_attempt=0")
     request, channel = make_request(self.sydent.reactor, "POST", url)
     sendSMS_mock = self._render_request(request)
     sendSMS_mock.assert_called_once()
     self.assertEqual(channel.code, 200)
Ejemplo n.º 25
0
async def test_synse_fan_write_valid(mock_validate_device_type, mock_write,
                                     no_pretty_json):
    """Test writing fan speed (rpm) with a valid target specified."""

    r = utils.make_request('/synse/fan?speed=500')

    result = await fan_route(r, 'rack-1', 'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"data":{"action":"speed","raw":"500"}}'
    assert result.status == 200
Ejemplo n.º 26
0
async def test_synse_lock_write_valid_1(mock_validate_device_type, mock_write,
                                        no_pretty_json):
    """Test writing lock state with a valid state specified."""

    r = utils.make_request('/synse/lock?action=unlock')

    result = await lock_route(r, 'rack-1', 'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"data":{"action":"unlock"}}'
    assert result.status == 200
Ejemplo n.º 27
0
async def test_synse_boot_target_write_valid_1(mock_validate_device_type,
                                               mock_write, no_pretty_json):
    """Test writing boot target with a valid target specified."""

    r = utils.make_request('/synse/boot_target?target=pxe')

    result = await boot_target_route(r, 'rack-1', 'vec', '123456')

    assert isinstance(result, HTTPResponse)
    assert result.body == b'{"data":{"action":"target","raw":"pxe"}}'
    assert result.status == 200
Ejemplo n.º 28
0
async def test_synse_boot_target_write_invalid(mock_validate_device_type,
                                               mock_write, no_pretty_json):
    """Test writing boot target with an invalid target specified."""

    r = utils.make_request('/synse/boot_target?target=test')

    try:
        await boot_target_route(r, 'rack-1', 'vec', '123456')
    except errors.SynseError as e:
        assert e.error_id == errors.INVALID_ARGUMENTS
        assert 'test' in e.args[0]
Ejemplo n.º 29
0
async def test_validate_no_query_params2():
    """Test validating that an incoming request has no query params, when there
    are query params. In this case, we expect an error.
    """
    @validate.no_query_params()
    async def test_fn(request, *args, **kwargs):
        """Dummy function for testing the decorator."""
        return request

    with pytest.raises(errors.InvalidArgumentsError):
        await test_fn(utils.make_request('/synse/endpoint?test=param'))
Ejemplo n.º 30
0
async def test_synse_test_route():
    """Test successfully hitting the test route."""

    result = await synse_test_route(utils.make_request('/synse/test'))

    assert isinstance(result, HTTPResponse)
    assert result.status == 200

    body = ujson.loads(result.body)
    assert 'status' in body
    assert 'timestamp' in body
    assert body['status'] == 'ok'