Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more unit test for User command #31

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/commands/Kick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Kick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:30:47 by gilmar #+# #+# */
/* Updated: 2024/05/30 13:06:19 by yde-goes ### ########.fr */
/* Updated: 2024/05/30 16:15:26 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,19 @@
* Reference: https://datatracker.ietf.org/doc/html/rfc1459#section-4.2.8
*/

/*
* Cenários de Teste:
* 1. O comando KICK é recebido sem parâmetros suficientes.
* 2. O comando KICK é recebido e o canal não existe.
* 3. O comando KICK é recebido e o cliente não está no canal.
* 4. O comando KICK é recebido e o cliente não é um operador do canal.
* 5. O comando KICK é recebido e o cliente alvo não existe.
* 6. O comando KICK é recebido e o cliente alvo não está no canal.
* 7. O comando KICK é recebido e o cliente é expulso do canal com sucesso.
* 8. O comando KICK é recebido e o cliente é expulso do canal com sucesso e um comentário é enviado.
*
*/

/**
* @brief Handles the KICK command received from the client.
*
Expand Down
12 changes: 11 additions & 1 deletion src/commands/Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:30:34 by gilmar #+# #+# */
/* Updated: 2024/05/28 22:01:53 by gilmar ### ########.fr */
/* Updated: 2024/05/30 17:00:18 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,6 +20,16 @@
* Reference: https://datatracker.ietf.org/doc/html/rfc1459#section-4.2.3
*/

/*
* Cenários de Teste:
* 1. O comando MODE é recebido sem parâmetros suficientes.
* 2. O comando MODE é recebido e o canal não existe.
* 3. O comando MODE é recebido e o cliente não tem privilégios de operador no canal.
* 4. O comando MODE é recebido e o cliente tem privilégios de operador no canal.
* 5. O comando MODE é recebido e o modo do canal é alterado com sucesso.
*
*/

/**
* @brief Handles the MODE command received from the client.
*
Expand Down
10 changes: 9 additions & 1 deletion src/commands/Part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:30:20 by gilmar #+# #+# */
/* Updated: 2024/05/26 11:52:13 by gilmar ### ########.fr */
/* Updated: 2024/05/30 17:00:05 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,14 @@
* Link: https://datatracker.ietf.org/doc/html/rfc1459#section-4.2.2
*/

/*
* Test Cases:
* 1. The PART command is received without enough parameters.
* 2. The PART command is received and the channel does not exist.
* 3. The PART command is received and the client is not in the channel.
* 4. The PART command is received and the client leaves the channel successfully.
*/

/**
* @brief Handles the PART command received from the client.
*
Expand Down
5 changes: 1 addition & 4 deletions src/commands/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:32:16 by gilmar #+# #+# */
/* Updated: 2024/05/30 15:19:14 by gilmar ### ########.fr */
/* Updated: 2024/05/30 16:47:25 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

#include "Server.hpp"

#define USER_CMD "USER"

/*
* Command: USER
* Parameters: <username>
* Link: https://datatracker.ietf.org/doc/html/rfc1459#section-4.1.3
*/


/*
* Cenários de Teste:
* 1. O comando USER é recebido sem parâmetros suficientes.
Expand Down
6 changes: 5 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Insert the test rules here
TESTS := user nick invite join kick part topic privmsg quit
TESTS += marvinbot
TESTS += marvinbot mode

CYAN := \33[1;36m
RESET := \033[0m
Expand Down Expand Up @@ -75,6 +75,10 @@ quit: $(OBJS) TestQuit.cpp
marvinbot: $(OBJS) TestMarvinBot.cpp
@$(call run, "$^")

.PHONY: mode
mode: $(OBJS) TestMode.cpp
@$(call run, "$^")

# Insert the test rules here, follwing the example above.

# Makefile rules to clean stdin/stdout/stderr test's files
Expand Down
12 changes: 12 additions & 0 deletions tests/TestMode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* TestMode.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 16:51:31 by gilmar #+# #+# */
/* Updated: 2024/05/30 16:59:55 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

3 changes: 1 addition & 2 deletions tests/TestNick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 18:03:34 by yde-goes #+# #+# */
/* Updated: 2024/05/30 16:06:39 by gilmar ### ########.fr */
/* Updated: 2024/05/30 16:48:25 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -183,7 +183,6 @@ Test(NickCommand, success_readytologin)
Server server;

server._clients.push_back(*client);
server._handler_client_username("Username", 4);
server._handler_client_nickname("Nickname", 4);
cr_assert(eq(int, server._reply_code, 001));
}
87 changes: 50 additions & 37 deletions tests/TestUser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* TestUser.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 18:03:11 by yde-goes #+# #+# */
/* Updated: 2024/05/30 14:55:00 by yde-goes ### ########.fr */
/* Updated: 2024/05/30 16:53:44 by gilmar ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,19 +17,32 @@
#define private public
#include "Server.hpp"

/*
* Cenários de Teste:
* 1. O comando USER é recebido sem parâmetros suficientes.
* 2. O comando USER é recebido e o cliente não está registrado.
* 3. O comando USER é recebido e o cliente já está registrado.
* 4. O comando USER é recebido e o cliente está pronto para fazer login.
* 5. O comando USER é recebido e o cliente não está pronto para fazer login.
*
*/

Client *mockClient()
{
Client *client = new Client();
client->set_fd(4);
client->set_username("username");
client->set_nickname("oldNickname");
client->set_password("password");
client->set_buffer("USER new_username");
client->set_username("Username");
client->set_nickname("Nickname");
client->set_password("Password");
client->set_buffer("USER Username");
client->set_is_logged(true);
client->set_is_authenticated(true);
return client;
}

/*
* 1. O comando USER é recebido sem parâmetros suficientes.
*/
Test(UserCommand, err_needmoreparams)
{
Client* client = mockClient();
Expand All @@ -38,9 +51,12 @@ Test(UserCommand, err_needmoreparams)

server._clients.push_back(*client);
server._handler_client_username("", 4);
cr_assert(eq(int, server._reply_code, 461)); // Assuming 461 is the reply code for ERR_NEEDMOREPARAMS
cr_assert(eq(int, server._reply_code, 461));
}

/*
* 2. O comando USER é recebido e o cliente não está registrado.
*/
Test(UserCommand, err_notregistered)
{
Client* client = mockClient();
Expand All @@ -50,58 +66,55 @@ Test(UserCommand, err_notregistered)
Server server;

server._clients.push_back(*client);
server._handler_client_username("new_username", 4);
cr_assert(eq(int, server._reply_code, 451)); // Assuming 451 is the reply code for ERR_NOTREGISTERED
server._handler_client_username(client->get_username(), 4);
cr_assert(eq(int, server._reply_code, 451));
}

Test(UserCommand, success_setusername_1)
/*
* 3. O comando USER é recebido e o cliente já está registrado.
*/
Test(UserCommand, err_alreadyregistered)
{
Client* client = mockClient();
client->set_username("");

Server server;

server._clients.push_back(*client);
server._handler_client_username("new_username", 4);
cr_assert(eq(int, server._reply_code, 200)); // Assuming 200 is the reply code for a successful username change
server._handler_client_username(client->get_username(), 4);
cr_assert(eq(int, server._reply_code, 462));
}

Test(UserCommand, success_setusername_2)
/*
* 4. O comando USER é recebido e o cliente está pronto para fazer login.
*/
Test(UserCommand, success_readytologin)
{
Client* client = mockClient();
client->set_username("withUsername");
client->set_username("");
client->set_is_authenticated(true);
client->set_is_logged(false);

Server server;

server._clients.push_back(*client);
server._handler_client_username("newUsername", 4);
cr_assert(eq(int, server._reply_code, 462)); // Assuming 200 is the reply code for a successful username change
server._handler_client_username("Username", 4);
cr_assert(eq(int, server._reply_code, 001));
}

Test(UserCommand, err_alreadyregistered)
/*
* 5. O comando USER é recebido e o cliente não está pronto para fazer login.
*/
Test(UserCommand, success_notreadytologin)
{
Client* client = mockClient();
client->set_username("");
client->set_nickname("");
client->set_is_authenticated(true);
client->set_is_logged(false);

Server server;

server._clients.push_back(*client);
server._handler_client_username("username", 4);
cr_assert(eq(int, server._reply_code, 462)); // Assuming 462 is the reply code for ERR_ALREADYREGISTERED
server._handler_client_username("Username", 4);
cr_assert(eq(int, server._reply_code, 200));
}


/*
Using the opened tabs as context, write 8 unit tests using criterion framework, one for each of the following criteria:

The USER command is received without sufficient parameters.
The USER command is received and the client is not registered.
The USER command is received and the client is already registered.
The USER command is received and the client is ready to log in.
The USER command is received and the client is not ready to log in.
The USER command is received and the client is already logged in.
The USER command is received and the client is not authenticated.
The USER command is received and the client is authenticated.
*/



Loading