859 lines
27 KiB
Rust
859 lines
27 KiB
Rust
use std::{
|
|
path::Path,
|
|
sync::{Arc, Mutex},
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
};
|
|
|
|
use rand::{RngCore, rngs::OsRng};
|
|
use rusqlite::{Connection, OptionalExtension, params};
|
|
use sha2::{Digest, Sha256};
|
|
use thiserror::Error;
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum DbError {
|
|
#[error("database error: {0}")]
|
|
Message(String),
|
|
#[error("record not found")]
|
|
NotFound,
|
|
#[error("record already exists")]
|
|
Conflict,
|
|
#[error("database worker failed: {0}")]
|
|
Worker(String),
|
|
}
|
|
|
|
pub type DbResult<T> = Result<T, DbError>;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Database {
|
|
connection: Arc<Mutex<Connection>>,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct User {
|
|
pub id: i64,
|
|
pub email: String,
|
|
pub display_name: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct SessionUser {
|
|
pub user: User,
|
|
pub csrf_token: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct GroceryList {
|
|
pub id: i64,
|
|
pub name: String,
|
|
pub revision: i64,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Item {
|
|
pub id: i64,
|
|
pub list_id: i64,
|
|
pub name: String,
|
|
pub quantity: String,
|
|
pub note: String,
|
|
pub category_id: Option<i64>,
|
|
pub checked: bool,
|
|
pub version: i64,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Category {
|
|
pub id: i64,
|
|
pub name: String,
|
|
}
|
|
|
|
impl Database {
|
|
pub fn open(path: impl AsRef<Path>) -> DbResult<Self> {
|
|
let connection = Connection::open(path).map_err(sql_error)?;
|
|
configure(&connection)?;
|
|
migrate(&connection)?;
|
|
|
|
Ok(Self {
|
|
connection: Arc::new(Mutex::new(connection)),
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub fn open_in_memory() -> DbResult<Self> {
|
|
Self::open(":memory:")
|
|
}
|
|
|
|
async fn call<T, F>(&self, operation: F) -> DbResult<T>
|
|
where
|
|
T: Send + 'static,
|
|
F: FnOnce(&mut Connection) -> DbResult<T> + Send + 'static,
|
|
{
|
|
let connection = Arc::clone(&self.connection);
|
|
tokio::task::spawn_blocking(move || {
|
|
let mut connection = connection
|
|
.lock()
|
|
.map_err(|error| DbError::Worker(error.to_string()))?;
|
|
operation(&mut connection)
|
|
})
|
|
.await
|
|
.map_err(|error| DbError::Worker(error.to_string()))?
|
|
}
|
|
|
|
pub async fn create_user(
|
|
&self,
|
|
email: String,
|
|
display_name: String,
|
|
password_hash: String,
|
|
) -> DbResult<User> {
|
|
self.call(move |connection| {
|
|
let result = connection.execute(
|
|
"INSERT INTO users (email, display_name, password_hash, created_at)
|
|
VALUES (?1, ?2, ?3, ?4)",
|
|
params![email, display_name, password_hash, now()],
|
|
);
|
|
|
|
match result {
|
|
Ok(_) => {
|
|
let id = connection.last_insert_rowid();
|
|
Ok(User {
|
|
id,
|
|
email,
|
|
display_name,
|
|
})
|
|
}
|
|
Err(error) if error.to_string().contains("UNIQUE") => Err(DbError::Conflict),
|
|
Err(error) => Err(sql_error(error)),
|
|
}
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn find_user_by_email(&self, email: String) -> DbResult<Option<(User, String)>> {
|
|
self.call(move |connection| {
|
|
connection
|
|
.query_row(
|
|
"SELECT id, email, display_name, password_hash
|
|
FROM users WHERE email = ?1 COLLATE NOCASE",
|
|
params![email],
|
|
|row| {
|
|
Ok((
|
|
User {
|
|
id: row.get(0)?,
|
|
email: row.get(1)?,
|
|
display_name: row.get(2)?,
|
|
},
|
|
row.get(3)?,
|
|
))
|
|
},
|
|
)
|
|
.optional()
|
|
.map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn has_users(&self) -> DbResult<bool> {
|
|
self.call(|connection| {
|
|
connection
|
|
.query_row("SELECT EXISTS(SELECT 1 FROM users)", [], |row| {
|
|
Ok(row.get::<_, i64>(0)? != 0)
|
|
})
|
|
.map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn create_session(&self, user_id: i64) -> DbResult<(String, String)> {
|
|
self.call(move |connection| {
|
|
let session_token = new_secret();
|
|
let csrf_token = new_secret();
|
|
connection
|
|
.execute(
|
|
"INSERT INTO sessions (token_hash, user_id, csrf_token, expires_at)
|
|
VALUES (?1, ?2, ?3, ?4)",
|
|
params![
|
|
hash_secret(&session_token),
|
|
user_id,
|
|
csrf_token,
|
|
now() + 60 * 60 * 24 * 30
|
|
],
|
|
)
|
|
.map_err(sql_error)?;
|
|
Ok((session_token, csrf_token))
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn session_user(&self, session_token: String) -> DbResult<Option<SessionUser>> {
|
|
self.call(move |connection| {
|
|
connection
|
|
.query_row(
|
|
"SELECT u.id, u.email, u.display_name, s.csrf_token
|
|
FROM sessions s
|
|
JOIN users u ON u.id = s.user_id
|
|
WHERE s.token_hash = ?1 AND s.expires_at > ?2",
|
|
params![hash_secret(&session_token), now()],
|
|
|row| {
|
|
Ok(SessionUser {
|
|
user: User {
|
|
id: row.get(0)?,
|
|
email: row.get(1)?,
|
|
display_name: row.get(2)?,
|
|
},
|
|
csrf_token: row.get(3)?,
|
|
})
|
|
},
|
|
)
|
|
.optional()
|
|
.map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn delete_session(&self, session_token: String) -> DbResult<()> {
|
|
self.call(move |connection| {
|
|
connection
|
|
.execute(
|
|
"DELETE FROM sessions WHERE token_hash = ?1",
|
|
params![hash_secret(&session_token)],
|
|
)
|
|
.map_err(sql_error)?;
|
|
Ok(())
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn list_summaries(&self) -> DbResult<Vec<GroceryList>> {
|
|
self.call(move |connection| {
|
|
let mut statement = connection
|
|
.prepare(
|
|
"SELECT l.id, l.name, l.revision
|
|
FROM lists l
|
|
ORDER BY l.created_at DESC",
|
|
)
|
|
.map_err(sql_error)?;
|
|
let rows = statement
|
|
.query_map([], |row| {
|
|
Ok(GroceryList {
|
|
id: row.get(0)?,
|
|
name: row.get(1)?,
|
|
revision: row.get(2)?,
|
|
})
|
|
})
|
|
.map_err(sql_error)?;
|
|
|
|
rows.collect::<Result<Vec<_>, _>>().map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn create_list(&self, name: String) -> DbResult<GroceryList> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
transaction
|
|
.execute(
|
|
"INSERT INTO lists (name, revision, created_at)
|
|
VALUES (?1, 0, ?2)",
|
|
params![name, now()],
|
|
)
|
|
.map_err(sql_error)?;
|
|
let list_id = transaction.last_insert_rowid();
|
|
for (position, category_name) in DEFAULT_CATEGORIES.iter().enumerate() {
|
|
transaction
|
|
.execute(
|
|
"INSERT INTO categories (list_id, name, position, created_at)
|
|
VALUES (?1, ?2, ?3, ?4)",
|
|
params![list_id, category_name, position as i64, now()],
|
|
)
|
|
.map_err(sql_error)?;
|
|
}
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(GroceryList {
|
|
id: list_id,
|
|
name,
|
|
revision: 0,
|
|
})
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn list_access(&self, list_id: i64) -> DbResult<Option<GroceryList>> {
|
|
self.call(move |connection| {
|
|
connection
|
|
.query_row(
|
|
"SELECT l.id, l.name, l.revision
|
|
FROM lists l
|
|
WHERE l.id = ?1",
|
|
params![list_id],
|
|
|row| {
|
|
Ok(GroceryList {
|
|
id: row.get(0)?,
|
|
name: row.get(1)?,
|
|
revision: row.get(2)?,
|
|
})
|
|
},
|
|
)
|
|
.optional()
|
|
.map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn items(&self, list_id: i64) -> DbResult<Vec<Item>> {
|
|
self.call(move |connection| {
|
|
let mut statement = connection
|
|
.prepare(
|
|
"SELECT id, list_id, name, quantity, note, category_id, checked, version
|
|
FROM items
|
|
WHERE list_id = ?1
|
|
ORDER BY position ASC, created_at ASC",
|
|
)
|
|
.map_err(sql_error)?;
|
|
let rows = statement
|
|
.query_map(params![list_id], |row| {
|
|
Ok(Item {
|
|
id: row.get(0)?,
|
|
list_id: row.get(1)?,
|
|
name: row.get(2)?,
|
|
quantity: row.get(3)?,
|
|
note: row.get(4)?,
|
|
category_id: row.get(5)?,
|
|
checked: row.get::<_, i64>(6)? != 0,
|
|
version: row.get(7)?,
|
|
})
|
|
})
|
|
.map_err(sql_error)?;
|
|
rows.collect::<Result<Vec<_>, _>>().map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn categories(&self, list_id: i64) -> DbResult<Vec<Category>> {
|
|
self.call(move |connection| {
|
|
let mut statement = connection
|
|
.prepare(
|
|
"SELECT id, name
|
|
FROM categories
|
|
WHERE list_id = ?1
|
|
ORDER BY position ASC, name COLLATE NOCASE ASC",
|
|
)
|
|
.map_err(sql_error)?;
|
|
let rows = statement
|
|
.query_map(params![list_id], |row| {
|
|
Ok(Category {
|
|
id: row.get(0)?,
|
|
name: row.get(1)?,
|
|
})
|
|
})
|
|
.map_err(sql_error)?;
|
|
rows.collect::<Result<Vec<_>, _>>().map_err(sql_error)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn create_category(&self, list_id: i64, name: String) -> DbResult<i64> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
let position: i64 = transaction
|
|
.query_row(
|
|
"SELECT COALESCE(MAX(position), -1) + 1
|
|
FROM categories WHERE list_id = ?1",
|
|
params![list_id],
|
|
|row| row.get(0),
|
|
)
|
|
.map_err(sql_error)?;
|
|
let result = transaction.execute(
|
|
"INSERT INTO categories (list_id, name, position, created_at)
|
|
VALUES (?1, ?2, ?3, ?4)",
|
|
params![list_id, name, position, now()],
|
|
);
|
|
match result {
|
|
Ok(_) => {}
|
|
Err(error) if error.to_string().contains("UNIQUE") => {
|
|
return Err(DbError::Conflict);
|
|
}
|
|
Err(error) => return Err(sql_error(error)),
|
|
}
|
|
let revision = bump_revision(&transaction, list_id)?;
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(revision)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn add_item(
|
|
&self,
|
|
list_id: i64,
|
|
name: String,
|
|
quantity: String,
|
|
note: String,
|
|
category_id: Option<i64>,
|
|
) -> DbResult<i64> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
ensure_category(&transaction, list_id, category_id)?;
|
|
let position: i64 = transaction
|
|
.query_row(
|
|
"SELECT COALESCE(MAX(position), -1) + 1 FROM items WHERE list_id = ?1",
|
|
params![list_id],
|
|
|row| row.get(0),
|
|
)
|
|
.map_err(sql_error)?;
|
|
transaction
|
|
.execute(
|
|
"INSERT INTO items
|
|
(list_id, name, quantity, note, category_id, checked, version, position, created_at, updated_at)
|
|
VALUES (?1, ?2, ?3, ?4, ?5, 0, 1, ?6, ?7, ?7)",
|
|
params![list_id, name, quantity, note, category_id, position, now()],
|
|
)
|
|
.map_err(sql_error)?;
|
|
let revision = bump_revision(&transaction, list_id)?;
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(revision)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn set_item_checked(
|
|
&self,
|
|
list_id: i64,
|
|
item_id: i64,
|
|
checked: bool,
|
|
) -> DbResult<i64> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
let changed = transaction
|
|
.execute(
|
|
"UPDATE items
|
|
SET checked = ?1, version = version + 1, updated_at = ?2
|
|
WHERE id = ?3 AND list_id = ?4",
|
|
params![checked as i64, now(), item_id, list_id],
|
|
)
|
|
.map_err(sql_error)?;
|
|
if changed == 0 {
|
|
return Err(DbError::NotFound);
|
|
}
|
|
let revision = bump_revision(&transaction, list_id)?;
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(revision)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn update_item(
|
|
&self,
|
|
list_id: i64,
|
|
item_id: i64,
|
|
name: String,
|
|
quantity: String,
|
|
note: String,
|
|
category_id: Option<i64>,
|
|
) -> DbResult<i64> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
ensure_category(&transaction, list_id, category_id)?;
|
|
let changed = transaction
|
|
.execute(
|
|
"UPDATE items
|
|
SET name = ?1, quantity = ?2, note = ?3, category_id = ?4,
|
|
version = version + 1, updated_at = ?5
|
|
WHERE id = ?6 AND list_id = ?7",
|
|
params![name, quantity, note, category_id, now(), item_id, list_id],
|
|
)
|
|
.map_err(sql_error)?;
|
|
if changed == 0 {
|
|
return Err(DbError::NotFound);
|
|
}
|
|
let revision = bump_revision(&transaction, list_id)?;
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(revision)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn delete_item(&self, list_id: i64, item_id: i64) -> DbResult<i64> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
let changed = transaction
|
|
.execute(
|
|
"DELETE FROM items WHERE id = ?1 AND list_id = ?2",
|
|
params![item_id, list_id],
|
|
)
|
|
.map_err(sql_error)?;
|
|
if changed == 0 {
|
|
return Err(DbError::NotFound);
|
|
}
|
|
let revision = bump_revision(&transaction, list_id)?;
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(revision)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn create_invitation(&self, created_by: i64, token: String) -> DbResult<i64> {
|
|
self.call(move |connection| {
|
|
let expires_at = now() + 60 * 60 * 24 * 7;
|
|
connection
|
|
.execute(
|
|
"INSERT INTO invitations (token_hash, created_by, expires_at)
|
|
VALUES (?1, ?2, ?3)",
|
|
params![hash_secret(&token), created_by, expires_at],
|
|
)
|
|
.map_err(sql_error)?;
|
|
Ok(expires_at)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn invitation(&self, token: String) -> DbResult<bool> {
|
|
self.call(move |connection| {
|
|
let valid = connection
|
|
.query_row(
|
|
"SELECT 1 FROM invitations
|
|
WHERE token_hash = ?1 AND expires_at > ?2",
|
|
params![hash_secret(&token), now()],
|
|
|_| Ok(()),
|
|
)
|
|
.optional()
|
|
.map_err(sql_error)?
|
|
.is_some();
|
|
Ok(valid)
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn accept_invitation(&self, token: String) -> DbResult<()> {
|
|
self.call(move |connection| {
|
|
let transaction = connection.transaction().map_err(sql_error)?;
|
|
let valid = transaction
|
|
.query_row(
|
|
"SELECT 1 FROM invitations
|
|
WHERE token_hash = ?1 AND expires_at > ?2",
|
|
params![hash_secret(&token), now()],
|
|
|_| Ok(()),
|
|
)
|
|
.optional()
|
|
.map_err(sql_error)?
|
|
.is_some();
|
|
if !valid {
|
|
return Err(DbError::NotFound);
|
|
}
|
|
transaction
|
|
.execute(
|
|
"DELETE FROM invitations WHERE token_hash = ?1",
|
|
params![hash_secret(&token)],
|
|
)
|
|
.map_err(sql_error)?;
|
|
transaction.commit().map_err(sql_error)?;
|
|
Ok(())
|
|
})
|
|
.await
|
|
}
|
|
}
|
|
|
|
fn configure(connection: &Connection) -> DbResult<()> {
|
|
connection
|
|
.pragma_update(None, "foreign_keys", true)
|
|
.map_err(sql_error)?;
|
|
connection
|
|
.pragma_update(None, "journal_mode", "WAL")
|
|
.map_err(sql_error)?;
|
|
connection
|
|
.busy_timeout(std::time::Duration::from_secs(5))
|
|
.map_err(sql_error)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn migrate(connection: &Connection) -> DbResult<()> {
|
|
connection
|
|
.execute_batch(
|
|
"CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
email TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
|
display_name TEXT NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
created_at INTEGER NOT NULL
|
|
);
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
token_hash TEXT PRIMARY KEY,
|
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
csrf_token TEXT NOT NULL,
|
|
expires_at INTEGER NOT NULL
|
|
);
|
|
CREATE TABLE IF NOT EXISTS lists (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
revision INTEGER NOT NULL DEFAULT 0,
|
|
created_at INTEGER NOT NULL
|
|
);
|
|
CREATE TABLE IF NOT EXISTS categories (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
list_id INTEGER NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
|
name TEXT NOT NULL COLLATE NOCASE,
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
created_at INTEGER NOT NULL,
|
|
UNIQUE (list_id, name)
|
|
);
|
|
CREATE TABLE IF NOT EXISTS invitations (
|
|
token_hash TEXT PRIMARY KEY,
|
|
created_by INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
expires_at INTEGER NOT NULL
|
|
);
|
|
CREATE TABLE IF NOT EXISTS items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
list_id INTEGER NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
|
name TEXT NOT NULL,
|
|
quantity TEXT NOT NULL DEFAULT '',
|
|
note TEXT NOT NULL DEFAULT '',
|
|
category_id INTEGER REFERENCES categories(id) ON DELETE SET NULL,
|
|
checked INTEGER NOT NULL DEFAULT 0,
|
|
version INTEGER NOT NULL DEFAULT 1,
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
created_at INTEGER NOT NULL,
|
|
updated_at INTEGER NOT NULL
|
|
);
|
|
CREATE INDEX IF NOT EXISTS items_list_idx ON items(list_id);
|
|
CREATE INDEX IF NOT EXISTS categories_list_idx ON categories(list_id);
|
|
CREATE INDEX IF NOT EXISTS sessions_user_idx ON sessions(user_id);",
|
|
)
|
|
.map_err(sql_error)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn ensure_category(
|
|
transaction: &rusqlite::Transaction<'_>,
|
|
list_id: i64,
|
|
category_id: Option<i64>,
|
|
) -> DbResult<()> {
|
|
let Some(category_id) = category_id else {
|
|
return Ok(());
|
|
};
|
|
let exists = transaction
|
|
.query_row(
|
|
"SELECT 1 FROM categories WHERE id = ?1 AND list_id = ?2",
|
|
params![category_id, list_id],
|
|
|_| Ok(()),
|
|
)
|
|
.optional()
|
|
.map_err(sql_error)?;
|
|
if exists.is_none() {
|
|
return Err(DbError::NotFound);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
const DEFAULT_CATEGORIES: &[&str] = &[
|
|
"Produce",
|
|
"Meat & seafood",
|
|
"Dairy & eggs",
|
|
"Pantry",
|
|
"Frozen",
|
|
"Household",
|
|
];
|
|
|
|
fn bump_revision(transaction: &rusqlite::Transaction<'_>, list_id: i64) -> DbResult<i64> {
|
|
transaction
|
|
.execute(
|
|
"UPDATE lists SET revision = revision + 1 WHERE id = ?1",
|
|
params![list_id],
|
|
)
|
|
.map_err(sql_error)?;
|
|
transaction
|
|
.query_row(
|
|
"SELECT revision FROM lists WHERE id = ?1",
|
|
params![list_id],
|
|
|row| row.get(0),
|
|
)
|
|
.map_err(sql_error)
|
|
}
|
|
|
|
fn sql_error(error: impl std::fmt::Display) -> DbError {
|
|
DbError::Message(error.to_string())
|
|
}
|
|
|
|
fn now() -> i64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_secs() as i64
|
|
}
|
|
|
|
pub fn new_secret() -> String {
|
|
let mut bytes = [0_u8; 32];
|
|
OsRng.fill_bytes(&mut bytes);
|
|
hex::encode(bytes)
|
|
}
|
|
|
|
pub fn hash_secret(secret: &str) -> String {
|
|
let mut hasher = Sha256::new();
|
|
hasher.update(secret.as_bytes());
|
|
hex::encode(hasher.finalize())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[tokio::test]
|
|
async fn creates_a_list_and_item() {
|
|
let database = Database::open_in_memory().unwrap();
|
|
let list = database
|
|
.create_list("Weekly shop".into())
|
|
.await
|
|
.unwrap();
|
|
database
|
|
.add_item(
|
|
list.id.clone(),
|
|
"Milk".into(),
|
|
"2 litres".into(),
|
|
String::new(),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let items = database.items(list.id).await.unwrap();
|
|
assert_eq!(items.len(), 1);
|
|
assert_eq!(items[0].name, "Milk");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sessions_and_invitations_are_scoped_to_users() {
|
|
let database = Database::open_in_memory().unwrap();
|
|
let owner = database
|
|
.create_user("owner@example.com".into(), "Owner".into(), "hash".into())
|
|
.await
|
|
.unwrap();
|
|
let list = database
|
|
.create_list("Household".into())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(database.categories(list.id.clone()).await.unwrap().len(), 6);
|
|
let (session_token, csrf_token) = database.create_session(owner.id.clone()).await.unwrap();
|
|
|
|
let session = database
|
|
.session_user(session_token.clone())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(session.user.id, owner.id);
|
|
assert_eq!(session.csrf_token, csrf_token);
|
|
|
|
// Any registered account can access every list.
|
|
assert_eq!(
|
|
database
|
|
.list_access(list.id.clone())
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.name,
|
|
"Household"
|
|
);
|
|
|
|
let invitation_token = "test-invitation".to_owned();
|
|
database
|
|
.create_invitation(owner.id.clone(), invitation_token.clone())
|
|
.await
|
|
.unwrap();
|
|
assert!(
|
|
database
|
|
.invitation(invitation_token.clone())
|
|
.await
|
|
.unwrap()
|
|
);
|
|
|
|
database
|
|
.accept_invitation(invitation_token.clone())
|
|
.await
|
|
.unwrap();
|
|
assert!(
|
|
!database
|
|
.invitation(invitation_token)
|
|
.await
|
|
.unwrap()
|
|
);
|
|
|
|
// A list created later is also accessible to every account.
|
|
let future_list = database
|
|
.create_list("Future shop".into())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
database
|
|
.list_access(future_list.id)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.name,
|
|
"Future shop"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn checked_state_is_set_not_toggled() {
|
|
let database = Database::open_in_memory().unwrap();
|
|
let list = database.create_list("List".into()).await.unwrap();
|
|
database
|
|
.add_item(
|
|
list.id.clone(),
|
|
"Coffee".into(),
|
|
String::new(),
|
|
String::new(),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let item = database.items(list.id.clone()).await.unwrap().remove(0);
|
|
|
|
database
|
|
.set_item_checked(list.id.clone(), item.id.clone(), true)
|
|
.await
|
|
.unwrap();
|
|
database
|
|
.set_item_checked(list.id.clone(), item.id.clone(), true)
|
|
.await
|
|
.unwrap();
|
|
|
|
let item = database.items(list.id).await.unwrap().remove(0);
|
|
assert!(item.checked);
|
|
assert_eq!(item.version, 3);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn checking_an_item_does_not_change_list_order() {
|
|
let database = Database::open_in_memory().unwrap();
|
|
let list = database.create_list("List".into()).await.unwrap();
|
|
database
|
|
.add_item(
|
|
list.id.clone(),
|
|
"First".into(),
|
|
String::new(),
|
|
String::new(),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
database
|
|
.add_item(
|
|
list.id.clone(),
|
|
"Second".into(),
|
|
String::new(),
|
|
String::new(),
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let first_item = database.items(list.id.clone()).await.unwrap().remove(0);
|
|
|
|
database
|
|
.set_item_checked(list.id.clone(), first_item.id, true)
|
|
.await
|
|
.unwrap();
|
|
|
|
let items = database.items(list.id).await.unwrap();
|
|
assert_eq!(items[0].name, "First");
|
|
assert!(items[0].checked);
|
|
assert_eq!(items[1].name, "Second");
|
|
}
|
|
}
|