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,24 +726,52 @@ async fn handle_incoming_message(
}, },
ClientMessage::Whois { target, nick } => { ClientMessage::Whois { target, nick } => {
// todo: finish replpies from the server to the command // todo: finish replpies from the server to the command
match (target, nick) {
match target { (Some(target), Some(nick)) => {
Some(target) => {} ServerMessage {
None => {} 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?
}
(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?
}
} }
ServerMessage {
tags: vec![],
sender: Some(config.server_name.clone()),
body: ServerMessageBody::N318EndOfWhois {
client: user.nickname.clone(),
nick,
msg: "End of /WHOIS list".into(),
},
}
.write_async(writer)
.await?;
writer.flush().await?
} }
ClientMessage::Mode { target } => { ClientMessage::Mode { target } => {
match target { match target {

View File

@ -46,7 +46,7 @@ pub enum ClientMessage {
/// WHOIS [<target>] <nick> /// WHOIS [<target>] <nick>
Whois { Whois {
target: Option<Str>, // server_name or nick_name target: Option<Str>, // server_name or nick_name
nick: Str, nick: Option<Str>,
}, },
/// `TOPIC <chan> :<topic>` /// `TOPIC <chan> :<topic>`
Topic { Topic {
@ -192,21 +192,24 @@ fn client_message_whois(input: &str) -> IResult<&str, ClientMessage> {
"", "",
ClientMessage::Whois { ClientMessage::Whois {
target: None, target: None,
nick: nick.into(), nick: Some(nick.into()),
}, },
)), )),
[target, nick, ..] => Ok(( [target, nick, ..] => Ok((
"", "",
ClientMessage::Whois { ClientMessage::Whois {
target: Some(target.into()), target: Some(target.into()),
nick: nick.into(), nick: Some(nick.into()),
}, },
)), )),
// fixme: idk how to deal with this in more elegant way // fixme: idk how to deal with this in more elegant way
[] => Err(nom::Err::Failure(nom::error::Error { [] => Ok((
input: "No args passed", "",
code: ErrorKind::Fail, 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_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_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_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_one_arg = client_message(test_user);
let res_user_user = client_message(test_user_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_users_list = client_message(test_users_list);
let res_server_users_list = client_message(test_server_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_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 { let expected_arg = ClientMessage::Whois {
target: None, target: None,
nick: "val".into(), nick: Some("val".into()),
}; };
let expected_user_user = ClientMessage::Whois { let expected_user_user = ClientMessage::Whois {
target: Some("val".into()), target: Some("val".into()),
nick: "val".into(), nick: Some("val".into()),
}; };
let expected_server_user = ClientMessage::Whois { let expected_server_user = ClientMessage::Whois {
target: Some("com.test.server".into()), target: Some("com.test.server".into()),
nick: "user".into(), nick: Some("user".into()),
}; };
let expected_user_server = ClientMessage::Whois { let expected_user_server = ClientMessage::Whois {
target: Some("user".into()), target: Some("user".into()),
nick: "com.test.server".into(), nick: Some("com.test.server".into()),
}; };
let expected_user_list = ClientMessage::Whois { let expected_user_list = ClientMessage::Whois {
target: None, 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 { let expected_server_user_list = ClientMessage::Whois {
target: Some("com.test.server".into()), 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 { let expected_more_than_two_params = ClientMessage::Whois {
target: Some("test.server".into()), 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)); 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_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_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_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] #[test]
fn test_client_message_user() { fn test_client_message_user() {

View File

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