Updated target for debugger

 Tests now use the new flags required for /channels/create
 Doubled size for channel descriptions
This commit is contained in:
shockrah 2021-03-07 17:46:17 -08:00
parent fadc7d6dc1
commit 7c3537e4f6
4 changed files with 37 additions and 39 deletions

View File

@ -15,7 +15,7 @@
"type": "lldb",
"request": "launch",
"name": "(Linux) Debug",
"program": "${workspaceFolder}/target/debug/freechat-server",
"program": "${workspaceFolder}/target/debug/json-api",
"args": [
"-s"
],

View File

@ -145,9 +145,9 @@ def run(worker: Worker):
{'init': ['get', '/channels/list', {}], 'auth': None, 'hope': 401},
{'init': ['post', '/channels/list', {}], 'auth': jwt, 'hope': 404},
{'init': ['post', '/channels/create', {'name': str(new_channel_name), 'kind': TEXT_CHAN, 'description': 'asdf'}], 'auth': jwt, 'hope': 200},
{'init': ['post', '/channels/create', {'name': str(new_channel_name), 'type': TEXT_CHAN, 'description': 'asdf'}], 'auth': jwt, 'hope': 200},
# Just a regular test no saving for this one
{'init': ['post', '/channels/create', {'name': str(new_channel_name+1), 'kind': TEXT_CHAN,}], 'auth': jwt, 'hope': 200},
{'init': ['post', '/channels/create', {'name': str(new_channel_name+1), 'type': TEXT_CHAN,}], 'auth': jwt, 'hope': 200},
{'init': ['post', '/channels/create', {}], 'auth': jwt, 'hope': 400},
{'init': ['post', '/channels/create', {'name': 123, 'kind': 'adsf'}], 'auth': jwt, 'hope': 400},
@ -163,13 +163,15 @@ def run(worker: Worker):
msg_chan_name = time.time()
_id = worker.request('post', '/channels/create', jwt, {
'name': str(msg_chan_name),
'kind': TEXT_CHAN,
'type': TEXT_CHAN,
'description': 'asdf'
}, 200)
chan_d = worker.responses[_id].json()
message_tests = [
# bs message spam
{'init': ['post', '/message/send', {'channel_id': chan_d['id'], 'content': 'bs content'}], 'auth': jwt, 'hope': 200},
{'init': ['post', '/message/send', {'channel_id': chan_d['id'], 'content': 'bs content'}], 'auth': jwt, 'hope': 200, 'body': True},
{'init': ['post', '/message/send', {'channel_id': chan_d['id'], 'content': 'bs content'}], 'auth': jwt, 'hope': 200},
{'init': ['post', '/message/send', {'channel_id': chan_d['id'], 'content': 'bs content'}], 'auth': jwt, 'hope': 200},
{'init': ['post', '/message/send', {'channel_id': chan_d['id'], 'content': 'bs content'}], 'auth': jwt, 'hope': 200},

View File

@ -10,6 +10,8 @@ use crate::{sql_err, no_conn, Response};
pub const VOICE_CHANNEL: Integer = 1;
pub const TEXT_CHANNEL: Integer = 2;
pub const MAX_NAME_LEN: usize = 256;
pub const MAX_DESCRIPTION_LEN: usize = 2048;
impl crate::Channel {
pub async fn filter(p: &Pool, kind: Integer) -> Result<Response<Self>, SqlError> {
@ -40,46 +42,40 @@ impl crate::Channel {
}
pub async fn new(p: &Pool, name: &str, desc: &str, kind: Integer) -> Response<Self> {
pub async fn add(p: &Pool, name: &str, description: &str, kind: Integer)
-> Result<Response<Self>, SqlError> {
//! @returns on success -> Response::Row<Channel>
//! @returns on partial success -> Response::Empty
//! @returns on failure -> Response::Other
//! @returns on user failure -> Response::RestrictedInput(msg)
if let Ok(conn) = p.get_conn().await {
let q = "INSERT INTO channels (name, description, kind) VALUES (:n, :d, :k)";
let insert_result = conn.drop_exec(q, params!{
"n" => name,
"d" => desc,
"k" => kind
}).await;
if let Ok(conn) = insert_result {
// This is only kosher because names are enforced as unique by sql
let q = "SELECT id FROM channels WHERE name = :name";
let fetch_result : Result<(Conn, Option<UBigInt>), SqlError> =
conn.first_exec(q, params!{"name" => name}).await;
return match fetch_result {
Ok((_, id_opt)) => {
if let Some(id) = id_opt {
Response::Row(Self {
// bounds are literally [1, 2]
if kind == TEXT_CHANNEL || kind == VOICE_CHANNEL {
let conn = p.get_conn().await?;
let q = "INSERT INTO channels (name, description, kind) VALUES (:n, :d, :k)";
let conn = conn.drop_exec(q, params!{
"n" => name,
"d" => description,
"k" => kind
}).await?;
let q = "SELECT id FROM channels WHERE name = :name";
let (_, id): (_, Option<UBigInt>) =
conn.first_exec(q, params!{"name" => name}).await?;
// probably safe but honestly could fail if the net goes down right here
let id = id.unwrap();
Ok(Response::Row(Self {
id,
name: name.into(),
description: Some(desc.into()),
name: name.to_string(),
description: Some(description.to_string()),
kind
})
}
else { Response::Empty }
},
Err(_) => Response::Empty
};
}))
}
else {
return Response::RestrictedInput(
"Could not add channel, make sure the name is unique and not longer than 256 bytes".into());
return Ok(Response::RestrictedInput(String::from("Invalid channel type")));
}
}
return Response::Other(no_conn!("db::channels::add"))
}
pub async fn delete(p: &Pool, id: UBigInt) -> Response<Self> {
//! Deletes channel given UBigInt as the row key

View File

@ -2,7 +2,7 @@
CREATE TABLE IF NOT EXISTS `channels` (
`id` BIGINT UNSIGNED NOT NULL auto_increment,
`name` VARCHAR(255) NOT NULL,
`description` VARCHAR(1024),
`description` VARCHAR(2048),
`kind` INTEGER NOT NULL,
PRIMARY KEY(`id`), UNIQUE KEY(`name`)
);