From 7c3537e4f66473fdae58d176bbe800a6906b4919 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 7 Mar 2021 17:46:17 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Updated=20target=20for=20debugger?= =?UTF-8?q?=20=E2=9C=A8=20Tests=20now=20use=20the=20new=20flags=20required?= =?UTF-8?q?=20for=20/channels/create=20=E2=9D=97=20Doubled=20size=20for=20?= =?UTF-8?q?channel=20descriptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json-api/.vscode/launch.json | 2 +- json-api/client-tests/client.py | 10 +-- json-api/db/src/channels.rs | 62 +++++++++---------- .../2020-03-11-005217_channels/up.sql | 2 +- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/json-api/.vscode/launch.json b/json-api/.vscode/launch.json index 321e59e..ebf226d 100644 --- a/json-api/.vscode/launch.json +++ b/json-api/.vscode/launch.json @@ -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" ], diff --git a/json-api/client-tests/client.py b/json-api/client-tests/client.py index 90fd9c2..44257e2 100644 --- a/json-api/client-tests/client.py +++ b/json-api/client-tests/client.py @@ -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}, diff --git a/json-api/db/src/channels.rs b/json-api/db/src/channels.rs index fea7d46..fbc26e0 100644 --- a/json-api/db/src/channels.rs +++ b/json-api/db/src/channels.rs @@ -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, SqlError> { @@ -40,45 +42,39 @@ impl crate::Channel { } - pub async fn new(p: &Pool, name: &str, desc: &str, kind: Integer) -> Response { + pub async fn add(p: &Pool, name: &str, description: &str, kind: Integer) + -> Result, SqlError> { //! @returns on success -> Response::Row //! @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), 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 { - id, - name: name.into(), - description: Some(desc.into()), - 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()); - } + // 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) = + 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.to_string(), + description: Some(description.to_string()), + kind + })) + } + else { + 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 { diff --git a/json-api/migrations/2020-03-11-005217_channels/up.sql b/json-api/migrations/2020-03-11-005217_channels/up.sql index de4b94c..fffcabf 100644 --- a/json-api/migrations/2020-03-11-005217_channels/up.sql +++ b/json-api/migrations/2020-03-11-005217_channels/up.sql @@ -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`) );