Fix broken get user query, because it's broken with query_as! macro
continuous-integration/drone/push Build is failing Details

This commit is contained in:
projectmoon 2021-05-25 16:06:26 +00:00
parent 5362488645
commit 4ada1697ee
2 changed files with 36 additions and 45 deletions

View File

@ -18,42 +18,6 @@
] ]
} }
}, },
"2eadaecbb9b743592cd8498ece3aeea97560fe8721ba085407456c99cb99b02d": {
"query": "SELECT\n a.user_id as \"username\", a.password,\n s.active_room as \"active_room: _\",\n s.account_status as \"account_status: _\"\n FROM accounts a\n JOIN user_state s on a.user_id = s.user_id\n WHERE a.user_id = ?",
"describe": {
"columns": [
{
"name": "username",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "password",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "active_room: _",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "account_status: _",
"ordinal": 3,
"type_info": "Null"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false,
true,
true,
false
]
}
},
"59313c67900a1a9399389720b522e572f181ae503559cd2b49d6305acb9e2207": { "59313c67900a1a9399389720b522e572f181ae503559cd2b49d6305acb9e2207": {
"query": "SELECT key, value as \"value: i32\" FROM user_variables\n WHERE room_id = ? AND user_id = ?", "query": "SELECT key, value as \"value: i32\" FROM user_variables\n WHERE room_id = ? AND user_id = ?",
"describe": { "describe": {

View File

@ -1,8 +1,28 @@
use super::Database; use super::Database;
use crate::db::{errors::DataError, Users}; use crate::db::{errors::DataError, Users};
use crate::error::BotError; use crate::error::BotError;
use crate::models::User; use crate::models::{AccountStatus, User};
use async_trait::async_trait; use async_trait::async_trait;
use std::convert::From;
#[derive(Eq, PartialEq, Debug, Default, sqlx::FromRow)]
struct UserRow {
pub username: String,
pub password: Option<String>,
pub active_room: Option<String>,
pub account_status: Option<AccountStatus>,
}
impl From<UserRow> for User {
fn from(row: UserRow) -> Self {
User {
username: row.username,
password: row.password,
active_room: row.active_room,
account_status: row.account_status.unwrap_or_default(),
}
}
}
#[async_trait] #[async_trait]
impl Users for Database { impl Users for Database {
@ -30,21 +50,23 @@ impl Users for Database {
} }
async fn get_user(&self, username: &str) -> Result<Option<User>, DataError> { async fn get_user(&self, username: &str) -> Result<Option<User>, DataError> {
let user_row = sqlx::query_as!( // Should be query_as! macro, but the left join breaks it with a
User, // non existing error message.
let user_row: Option<UserRow> = sqlx::query_as(
r#"SELECT r#"SELECT
a.user_id as "username", a.password, a.user_id as "username",
s.active_room as "active_room: _", a.password,
s.account_status as "account_status: _" s.active_room,
s.account_status
FROM accounts a FROM accounts a
JOIN user_state s on a.user_id = s.user_id LEFT JOIN user_state s on a.user_id = s.user_id
WHERE a.user_id = ?"#, WHERE a.user_id = ?"#,
username
) )
.bind(username)
.fetch_optional(&self.conn) .fetch_optional(&self.conn)
.await?; .await?;
Ok(user_row) Ok(user_row.map(|r| r.into()))
} }
async fn authenticate_user( async fn authenticate_user(
@ -53,6 +75,10 @@ impl Users for Database {
raw_password: &str, raw_password: &str,
) -> Result<Option<User>, BotError> { ) -> Result<Option<User>, BotError> {
let user = self.get_user(username).await?; let user = self.get_user(username).await?;
println!(
"user pw is {:?}",
user.as_ref().map(|u| u.password.as_ref())
);
Ok(user.filter(|u| u.verify_password(raw_password))) Ok(user.filter(|u| u.verify_password(raw_password)))
} }
} }
@ -63,6 +89,7 @@ mod tests {
use crate::db::sqlite::Database; use crate::db::sqlite::Database;
use crate::db::Users; use crate::db::Users;
//TODO test selecting user when state doesn't exist.
async fn create_db() -> Database { async fn create_db() -> Database {
let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap()) crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())