Esempio n. 1
0
def test_send_200_when_receive_auth_request(sut_with_client, client):
    sut_with_client.on_auth(Authenticate("username", "password"), client)

    assert client.name == "username"
    client.msg_sender.send.assert_called_once_with(
        Response(HTTPStatus.OK, "Login successful")
    )
Esempio n. 2
0
def test_on_get_contacts_401_when_not_authed(sut_with_client, client):
    msg = GetContacts()
    sut_with_client.on_get_contacts(msg, client)

    client.msg_sender.send.assert_called_once_with(
        Response(HTTPStatus.UNAUTHORIZED, "Allowed only for authed users")
    )
    client.disconnector.disconnect.assert_not_called()
Esempio n. 3
0
def test_required_disconnect_client_when_not_authed(client_with_name):
    mock = MagicMock()
    sut = Sut(mock)
    sut.method(MagicMock(), client_with_name)
    mock.assert_not_called()
    client_with_name.msg_sender.send.assert_called_once_with(
        Response(HTTPStatus.UNAUTHORIZED, "Allowed only for authed users")
    )
    client_with_name.disconnector.disconnect.assert_not_called()
Esempio n. 4
0
def test_send_400_when_receive_auth_request_with_invalid_name(
    sut_with_client, chat_room_manager, client
):
    chat_room_manager.is_valid_name.return_value = True
    sut_with_client.on_auth(Authenticate("#username", "password"), client)

    chat_room_manager.is_valid_name.assert_called_once_with("#username")
    client.msg_sender.send.assert_called_once_with(
        Response(HTTPStatus.BAD_REQUEST, "Invalid name")
    )
Esempio n. 5
0
        {"action": "leave", "room": "#room_name", "time": TIME_FACTORY_TIMESTAMP},
    ),
    (
        AddContact("user_name"),
        {"action": "add_contact", "user": "******", "time": TIME_FACTORY_TIMESTAMP},
    ),
    (
        RemoveContact("user_name"),
        {"action": "del_contact", "user": "******", "time": TIME_FACTORY_TIMESTAMP},
    ),
    (GetContacts(), {"action": "get_contacts", "time": TIME_FACTORY_TIMESTAMP}),
]

test_data_server_to_client = [
    (
        Response(HTTPStatus.OK, "message text"),
        {"response": 200, "message": "message text", "time": TIME_FACTORY_TIMESTAMP},
    ),
    (Probe(), {"action": "probe", "time": TIME_FACTORY_TIMESTAMP}),
    (
        ChatToClient("sender", "message text"),
        {
            "action": "msg",
            "time": TIME_FACTORY_TIMESTAMP,
            "from": "sender",
            "message": "message text",
        },
    ),
    (
        ChatToClient("sender", "message text", "#room"),
        {
Esempio n. 6
0
def test_on_chat_401_when_not_authed(sut_with_client, client):
    sut_with_client.on_chat(ChatFromClient("username", "msg"), client)
    client.msg_sender.send.assert_called_once_with(
        Response(HTTPStatus.UNAUTHORIZED, "Allowed only for authed users")
    )
    client.disconnector.disconnect.assert_not_called()
Esempio n. 7
0
def test_on_leave_401_when_not_auth(sut_with_client, client):
    sut_with_client.on_leave(Join("#room"), client)
    client.msg_sender.send.assert_called_once_with(
        Response(HTTPStatus.UNAUTHORIZED, "Allowed only for authed users")
    )
    client.disconnector.disconnect.assert_not_called()
Esempio n. 8
0
def sut_logged_in(sut_login_sent, msg_sender):
    sut_login_sent.on_response(Response(HTTPStatus.OK, "OK"))
    msg_sender.send.reset_mock()
    return sut_login_sent
Esempio n. 9
0
def test_disconnect_when_login_failed(code, sut_login_sent, msg_sender,
                                      disconnector):
    sut_login_sent.on_response(Response(code, "error"))
    disconnector.disconnect.assert_called_once()
    msg_sender.send.assert_not_called()
Esempio n. 10
0
def test_send_presence_when_success_login(sut_login_sent, msg_sender):
    sut_login_sent.on_response(Response(HTTPStatus.OK, "OK"))
    msg_sender.send.assert_called_once_with(Presence(Status.ONLINE))