forked from lavina/lavina
1
0
Fork 0

- extended tests

- added 431 err msg
This commit is contained in:
homycdev 2024-04-07 21:55:49 +03:00
parent 9cf4176a83
commit 5b30312238
3 changed files with 81 additions and 32 deletions

View File

@ -726,18 +726,14 @@ async fn handle_incoming_message(
},
ClientMessage::Whois { target, nick } => {
// todo: finish replpies from the server to the command
match target {
Some(target) => {}
None => {}
}
match (target, nick) {
(Some(target), Some(nick)) => {
ServerMessage {
tags: vec![],
sender: Some(config.server_name.clone()),
body: ServerMessageBody::N318EndOfWhois {
client: user.nickname.clone(),
nick,
nick: nick,
msg: "End of /WHOIS list".into(),
},
}
@ -745,6 +741,38 @@ async fn handle_incoming_message(
.await?;
writer.flush().await?
}
(Some(target), None) => {
todo!()
}
(None, Some(nick)) => {
ServerMessage {
tags: vec![],
sender: Some(config.server_name.clone()),
body: ServerMessageBody::N318EndOfWhois {
client: user.nickname.clone(),
nick: nick,
msg: "End of /WHOIS list".into(),
},
}
.write_async(writer)
.await?;
writer.flush().await?
}
(None, None) => {
ServerMessage {
tags: vec![],
sender: Some(config.server_name.clone()),
body: ServerMessageBody::N431ErrNoNicknameGiven {
client: user.nickname.clone(),
message: "No nickname given".into(),
},
}
.write_async(writer)
.await?;
writer.flush().await?
}
}
}
ClientMessage::Mode { target } => {
match target {
Recipient::Nick(nickname) => {

View File

@ -46,7 +46,7 @@ pub enum ClientMessage {
/// WHOIS [<target>] <nick>
Whois {
target: Option<Str>, // server_name or nick_name
nick: Str,
nick: Option<Str>,
},
/// `TOPIC <chan> :<topic>`
Topic {
@ -192,21 +192,24 @@ fn client_message_whois(input: &str) -> IResult<&str, ClientMessage> {
"",
ClientMessage::Whois {
target: None,
nick: nick.into(),
nick: Some(nick.into()),
},
)),
[target, nick, ..] => Ok((
"",
ClientMessage::Whois {
target: Some(target.into()),
nick: nick.into(),
nick: Some(nick.into()),
},
)),
// fixme: idk how to deal with this in more elegant way
[] => Err(nom::Err::Failure(nom::error::Error {
input: "No args passed",
code: ErrorKind::Fail,
})),
[] => Ok((
"",
ClientMessage::Whois {
target: None,
nick: None,
},
)),
}
}
@ -396,6 +399,7 @@ mod test {
let test_users_list = "WHOIS user_1,user_2,user_3";
let test_server_users_list = "WHOIS com.test.server user_1,user_2,user_3";
let test_more_than_two_params = "WHOIS test.server user_1,user_2,user_3 whatever spam";
let test_none_none_params = "WHOIS ";
let res_one_arg = client_message(test_user);
let res_user_user = client_message(test_user_user);
@ -405,38 +409,44 @@ mod test {
let res_users_list = client_message(test_users_list);
let res_server_users_list = client_message(test_server_users_list);
let res_more_than_two_params = client_message(test_more_than_two_params);
let res_none_none_params = client_message(test_none_none_params);
let expected_arg = ClientMessage::Whois {
target: None,
nick: "val".into(),
nick: Some("val".into()),
};
let expected_user_user = ClientMessage::Whois {
target: Some("val".into()),
nick: "val".into(),
nick: Some("val".into()),
};
let expected_server_user = ClientMessage::Whois {
target: Some("com.test.server".into()),
nick: "user".into(),
nick: Some("user".into()),
};
let expected_user_server = ClientMessage::Whois {
target: Some("user".into()),
nick: "com.test.server".into(),
nick: Some("com.test.server".into()),
};
let expected_user_list = ClientMessage::Whois {
target: None,
nick: "user_1,user_2,user_3".into(),
nick: Some("user_1,user_2,user_3".into()),
};
let expected_server_user_list = ClientMessage::Whois {
target: Some("com.test.server".into()),
nick: "user_1,user_2,user_3".into(),
nick: Some("user_1,user_2,user_3".into()),
};
let expected_more_than_two_params = ClientMessage::Whois {
target: Some("test.server".into()),
nick: "user_1,user_2,user_3".into(),
nick: Some("user_1,user_2,user_3".into()),
};
let expected_none_none_params = ClientMessage::Whois {
target: None,
nick: None,
};
assert_matches!(res_one_arg, Ok(result) => assert_eq!(expected_arg, result));
@ -445,7 +455,8 @@ mod test {
assert_matches!(res_user_server, Ok(result) => assert_eq!(expected_user_server, result));
assert_matches!(res_users_list, Ok(result) => assert_eq!(expected_user_list, result));
assert_matches!(res_server_users_list, Ok(result) => assert_eq!(expected_server_user_list, result));
assert_matches!(res_more_than_two_params, Ok(result) => assert_eq!(expected_more_than_two_params, result))
assert_matches!(res_more_than_two_params, Ok(result) => assert_eq!(expected_more_than_two_params, result));
assert_matches!(res_none_none_params, Ok(result) => assert_eq!(expected_none_none_params, result))
}
#[test]
fn test_client_message_user() {

View File

@ -141,6 +141,10 @@ pub enum ServerMessageBody {
client: Str,
chan: Chan,
},
N431ErrNoNicknameGiven {
client: Str,
message: Str,
},
N474BannedFromChan {
client: Str,
chan: Chan,
@ -343,6 +347,12 @@ impl ServerMessageBody {
chan.write_async(writer).await?;
writer.write_all(b" :End of /NAMES list").await?;
}
ServerMessageBody::N431ErrNoNicknameGiven { client, message } => {
writer.write_all(b"431").await?;
writer.write_all(client.as_bytes()).await?;
writer.write_all(b" :").await?;
writer.write_all(message.as_bytes()).await?;
}
ServerMessageBody::N474BannedFromChan { client, chan, message } => {
writer.write_all(b"474 ").await?;
writer.write_all(client.as_bytes()).await?;