Enable authentication in dbtest tests

next
Sayan Nandan 3 years ago
parent 4a075422de
commit ade5b2a105
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -1,3 +1,11 @@
# although this is exported by cargo, we'll export it again to use it in the Makefile
export ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
SKYTEST_TCP_PORT := 2003
SKYTEST_TLS_PORT := 2004
SKYTEST_TCP_PORT2 := 2005
SKYTEST_TLS_PORT2 := 2006
SKYTEST_TLS_CERT := $(ROOT_DIR)/cert.pem
SKYTEST_TLS_KEY := $(ROOT_DIR)/key.pem
# no additional software note
NO_ADDITIONAL_SOFTWARE := echo "No additional software required for this target"
# target argument
@ -37,7 +45,7 @@ BINARY_SKYMIGRATE := $(TARGET_FOLDER)sky-migrate
# archive command
ARCHIVE :=
# start background server command
START_SERVER := cargo run $(TARGET_ARG) -p skyd -- --noart --sslchain cert.pem --sslkey key.pem
START_SERVER := cargo run $(TARGET_ARG) -p skyd -- --noart --sslchain ${SKYTEST_TLS_CERT} --sslkey ${SKYTEST_TLS_KEY} --port ${SKYTEST_TCP_PORT} --sslport ${SKYTEST_TLS_PORT}
STOP_SERVER :=
ifeq ($(OS),Windows_NT)
@ -61,6 +69,9 @@ else
STOP_SERVER := pkill skyd
endif
START_SERVER2 := $(subst $(SKYTEST_TCP_PORT),$(SKYTEST_TCP_PORT2),$(START_SERVER))
START_SERVER2 := $(subst $(SKYTEST_TLS_PORT),$(SKYTEST_TLS_PORT2),$(START_SERVER2))
# update the archive command if we have a version and artifact name
RENAME_ARTIFACT :=
ifneq ($(origin ARTIFACT),undefined)
@ -120,7 +131,8 @@ test: .pre
@echo "Building and starting server in debug mode ..."
@${CBUILD} -p skyd
@chmod +x ci/ssl.sh && bash ci/ssl.sh
@${START_SERVER}
@mkdir -p server1 && cd server1 && ${START_SERVER}
@mkdir -p server2 && cd server2 && ${START_SERVER2}
@echo "Sleeping for 10 seconds to let the server start up ..."
@sleep 10
@echo "Finished sleeping"

@ -0,0 +1,29 @@
/*
* Created on Fri Mar 11 2022
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2022, Sayan Nandan <ohsayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#[sky_macros::dbtest_func(username = "abcd", password = "1234")]
#[should_panic]
async fn test_auth_fail() {}

@ -28,6 +28,7 @@
#[macro_use]
mod macros;
mod auth;
mod ddl_tests;
mod inspect_tests;
mod kvengine;

@ -34,6 +34,8 @@ pub struct DBTestFunctionConfig {
port: u16,
host: String,
tls_cert: Option<String>,
username: Option<String>,
password: Option<String>,
}
impl DBTestFunctionConfig {
@ -43,6 +45,8 @@ impl DBTestFunctionConfig {
port: 2003,
host: "127.0.0.1".to_owned(),
tls_cert: None,
username: None,
password: None,
}
}
pub fn get_connection_tokens(&self) -> impl quote::ToTokens {
@ -80,6 +84,22 @@ impl DBTestFunctionConfig {
).await.unwrap()
}
}
pub fn get_login_tokens(&self) -> Option<impl quote::ToTokens> {
let Self {
username, password, ..
} = self;
match (username, password) {
(Some(username), Some(password)) => Some(quote! {
let query = ::skytable::query!("auth", "login", #username, #password);
assert_eq!(
con.run_simple_query(&query).await.unwrap(),
::skytable::Element::RespCode(::skytable::RespCode::Okay)
);
}),
(None, None) => None,
_ => panic!("Expected both `username` and `password`"),
}
}
}
pub fn parse_dbtest_func_args(
@ -104,6 +124,14 @@ pub fn parse_dbtest_func_args(
"tls_cert" => {
fcfg.tls_cert = Some(util::parse_string(lit, span, "host").expect("Expected a string"));
}
"username" => {
fcfg.username =
Some(util::parse_string(lit, span, "username").expect("Expected a string"))
}
"password" => {
fcfg.password =
Some(util::parse_string(lit, span, "password").expect("Expected a string"))
}
x => panic!("unknown attribute {x} specified"),
}
}
@ -139,6 +167,15 @@ fn generate_dbtest(
#body
#connection_tokens
};
// check if we need to log in
if let Some(login_tokens) = fcfg.get_login_tokens() {
body = quote! {
#body
#login_tokens
};
}
// now create keyspace
body = quote! {
#body

Loading…
Cancel
Save