Exemplo n.º 1
0
def test_resolve(valid_action_name, not_valid_action_name, controller,
                 actions):

    res_controllers = [
        resolve(valid_action_name, actions),
        resolve(not_valid_action_name, actions)
    ]

    expected = [controller, None]

    for i, item in enumerate(res_controllers):
        assert item == expected[i]
Exemplo n.º 2
0
def handle_default_request(bytes_request):
    request = json.loads(bytes_request.decode())

    if validate_request(request):
        actions_name = request.get('action')
        controller = resolve(actions_name)
        if controller:
            try:
                logging.info(f'Client send valid request {request}')
                response = controller(request)
            except Exception as err:
                logging.critical(f'Internal server error: {err}')
                response = make_response(request,
                                         500,
                                         data='Internal server error')
        else:
            logging.error(
                f'Controller with action name {actions_name} does not exists')
            response = make_response(request, 404, 'Action not found')
    else:
        logging.error(f'Client send invalid request {request}')
        response = make_response(request, 400, 'Wrong request')

    str_response = json.dumps(response)
    return str_response.encode()
Exemplo n.º 3
0
    def test_tag_is_not_configured(self):
        # given
        event = actions.NfcEvent.DETECT
        tag_id = "666"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_empty()
Exemplo n.º 4
0
    def test_resolve_action_for_simple_config_on_remove(self):
        # given
        event = actions.NfcEvent.REMOVE
        tag_id = "111"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_empty()
Exemplo n.º 5
0
    def test_resolve_action_for_extended_config_on_remove(self):
        # given
        event = actions.NfcEvent.REMOVE
        tag_id = "222"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_length(1)
        assert_that(result).contains_only({"type": "curl", "url": "http://localhost:3000/?cmd=play&name=detect_remove"})
Exemplo n.º 6
0
    def test_resolve_action_for_full_config_on_remove(self):
        # given
        event = actions.NfcEvent.REMOVE
        tag_id = "333"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_length(1)
        assert_that(result).contains_only({"type": "curl", "url": "http://localhost:3000/?cmd=pause&name=all_events"})
Exemplo n.º 7
0
    def test_resolve_action_for_simple_config_on_redetect(self):
        # given
        event = actions.NfcEvent.REDETECT
        tag_id = "111"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_length(1)
        assert_that(result).contains_only({"type": "curl", "url": "http://localhost:3000/?cmd=playplaylist&name=single_event"})
Exemplo n.º 8
0
def check_validate(request, server_actions):
    action_name = request.get('action')

    if validate_request(request):
        controller = resolve(action_name, server_actions)
        if controller:
            try:
                return controller(request)
            except Exception as err:
                logging.critical(err)
                return make_response(request, 500, 'Internal server error')
        else:
            logging.error(f'Action with name { action_name } does not exists')
            return make_404(request)
    else:
        logging.error('Request is not valid')
        return make_400(request)
Exemplo n.º 9
0
    def test_resolve_multiple_actions_on_remove(self):
        # given
        event = actions.NfcEvent.REMOVE
        tag_id = "11"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_length(2)
        assert_that(result).contains_only(
            {
                "type": "curl",
                "url": "http://localhost:3000/?cmd=playplaylist&name=remove"
            }, {
                "type": "curl",
                "url": "http://localhost:3000/?cmd=volume&volume=50"
            })
Exemplo n.º 10
0
    def test_resolve_multiple_actions_on_redetect(self):
        # given
        event = actions.NfcEvent.REDETECT
        tag_id = "11"

        # when
        result = actions.resolve(self.config, event, tag_id)

        # then
        assert_that(result).is_length(3)
        assert_that(result).contains_only(
            {
                "type": "curl",
                "url": "http://localhost:3000/?cmd=playplaylist&name=redetect"
            }, {
                "type": "command",
                "command": "dostuff_onredetect.sh"
            }, {
                "type": "curl",
                "url": "http://localhost:3000/?cmd=volume&volume=100"
            })
Exemplo n.º 11
0
def default_handler(raw_request):

    request = json.loads(raw_request)
    action_name = request.get('action')

    if validate_request(request):
        controller = resolve(action_name)
        if controller:
            try:
                response = controller(request)
            except Exception as err:
                logger.critical(err)
                response = make_response(request, 500, 'Internal server error')
        else:
            logger.error(f'Action with name {action_name} does not exists')
            response = make_404(request)
    else:
        logger.error('request is not valid')
        response = make_400(request)

    return json.dumps(response)
Exemplo n.º 12
0
def handle_default_request(row_request):
    request = json.loads(row_request.decode(ENCODING))

    server_actions = get_server_actions()
    action_name = request.get('action')

    if validate_request(request):
        controller = resolve(action_name, server_actions)
        if controller:
            try:
                response = controller(request)
            except Exception as err:
                logging.critical(err)
                response = make_response(request, 500, 'Internal server error')
        else:
            logging.error(f'Action with name {action_name} does not exist')
            response = make_404(request)
    else:
        logging.error(f'Request is no valid')
        response = make_400(request)

    row_response = json.dumps(response).encode(ENCODING)
    return row_response
Exemplo n.º 13
0
def test_resolve_invalid_action(invalid_action, assert_false):
    controllers = resolve(invalid_action)

    assert assert_false == controllers
Exemplo n.º 14
0
def test_expected_server_resolve(expected_resolves):
    for expected_resolve in expected_resolves:
        assert expected_resolve.get('controller') == resolve(
            expected_resolve.get('action'))
Exemplo n.º 15
0
def test_resolve(actions, controller):
    resolved = resolve('Some text', actions)
    assert resolved == controller
Exemplo n.º 16
0
def execute_action(event: NfcEvent, tag_id: str):

    resolved_actions = resolve(config, event, tag_id)

    for action in resolved_actions:
        ACTION_MAP[action["type"]](action)
Exemplo n.º 17
0
    sock = socket.socket()
    sock.bind((addr, port))
    sock.listen(5)
    print(f'Server started at {addr}:{port}')

    while True:
        client, from_address = sock.accept()
        from_ip, from_port = from_address[0], from_address[1]
        print(f'Get connection from {from_ip}:{from_port}')

        b_request = client.recv(config.get('buffersize'))
        request = json.loads(b_request.decode())

        if validate_request(request):
            actions_name = request.get('action')
            controller = resolve(actions_name)

            if controller:
                try:
                    print(f'Client send valid request: {request}')
                    response = controller(request)
                except Exception as err:
                    print(f'Internal server error: {err}')
                    response = make_response(request, 500,
                                             'Internal server error')
            else:
                print(
                    f'Controller with action name {actions_name} does not exists'
                )
                response = make_response(request, 404, 'Action not found')
Exemplo n.º 18
0
def test_valid_resolve(expected_action):
    controller = resolve(expected_action)
    assert controller
Exemplo n.º 19
0
def test_invalid_resolve(non_expected_action):
    controller = resolve(non_expected_action)
    assert not controller
Exemplo n.º 20
0
def test_resolve_valid_action(valid_action, assert_controllers):
    controllers = resolve(valid_action)

    assert assert_controllers == controllers