2022-09-10 19:21:49 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2022-11-27 22:01:36 +05:30
|
|
|
use lazy_static::lazy_static;
|
2022-09-10 20:08:59 +05:30
|
|
|
use serde::{Deserialize, Serialize};
|
2022-09-10 19:21:49 +05:30
|
|
|
use sqlx::postgres::PgPoolOptions;
|
|
|
|
use sqlx::types::time::OffsetDateTime;
|
|
|
|
use sqlx::ConnectOptions;
|
|
|
|
use sqlx::PgPool;
|
2022-11-11 14:56:36 +05:30
|
|
|
use tracing::error;
|
2022-12-20 07:11:21 +05:30
|
|
|
use url::Url;
|
2022-11-15 20:24:13 +05:30
|
|
|
use uuid::Uuid;
|
2022-09-10 19:21:49 +05:30
|
|
|
|
|
|
|
use crate::errors::*;
|
2022-12-20 07:11:21 +05:30
|
|
|
use crate::utils;
|
2022-09-10 19:21:49 +05:30
|
|
|
|
|
|
|
/// Connect to databse
|
|
|
|
pub enum ConnectionOptions {
|
|
|
|
/// fresh connection
|
|
|
|
Fresh(Fresh),
|
|
|
|
/// existing connection
|
|
|
|
Existing(Conn),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use an existing database pool
|
|
|
|
pub struct Conn(pub PgPool);
|
|
|
|
|
|
|
|
pub struct Fresh {
|
|
|
|
pub pool_options: PgPoolOptions,
|
|
|
|
pub disable_logging: bool,
|
|
|
|
pub url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConnectionOptions {
|
|
|
|
async fn connect(self) -> ServiceResult<Database> {
|
|
|
|
let pool = match self {
|
|
|
|
Self::Fresh(fresh) => {
|
2022-12-19 11:49:42 +05:30
|
|
|
tracing::info!("DATABASE URL: {}", fresh.url);
|
2022-09-10 19:21:49 +05:30
|
|
|
let mut connect_options =
|
|
|
|
sqlx::postgres::PgConnectOptions::from_str(&fresh.url).unwrap();
|
|
|
|
if fresh.disable_logging {
|
|
|
|
connect_options.disable_statement_logging();
|
|
|
|
}
|
|
|
|
sqlx::postgres::PgConnectOptions::from_str(&fresh.url)
|
|
|
|
.unwrap()
|
|
|
|
.disable_statement_logging();
|
|
|
|
fresh
|
|
|
|
.pool_options
|
|
|
|
.connect_with(connect_options)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2022-09-10 20:08:59 +05:30
|
|
|
//.map_err(|e| ServiceError::ServiceError(Box::new(e)))?
|
2022-09-10 19:21:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
Self::Existing(conn) => conn.0,
|
|
|
|
};
|
|
|
|
Ok(Database { pool })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Database {
|
|
|
|
pub pool: PgPool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
pub async fn migrate(&self) -> ServiceResult<()> {
|
|
|
|
sqlx::migrate!("./migrations/")
|
|
|
|
.run(&self.pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-10 20:08:59 +05:30
|
|
|
//.map_err(|e| ServiceError::ServiceError(Box::new(e)))?;
|
2022-11-15 20:24:13 +05:30
|
|
|
self.create_event_type().await?;
|
2022-09-10 19:21:49 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ping(&self) -> bool {
|
|
|
|
use sqlx::Connection;
|
|
|
|
|
|
|
|
if let Ok(mut con) = self.pool.acquire().await {
|
|
|
|
con.ping().await.is_ok()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 20:08:59 +05:30
|
|
|
|
|
|
|
/// register a new user
|
|
|
|
pub async fn register(&self, p: &Register<'_>) -> ServiceResult<()> {
|
|
|
|
sqlx::query!(
|
2022-11-09 14:01:15 +05:30
|
|
|
"INSERT INTO librepages_users
|
|
|
|
(name , password, email) VALUES ($1, $2, $3)",
|
2022-09-10 20:08:59 +05:30
|
|
|
&p.username,
|
|
|
|
&p.hash,
|
|
|
|
&p.email,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// delete a user
|
|
|
|
pub async fn delete_user(&self, username: &str) -> ServiceResult<()> {
|
|
|
|
sqlx::query!("DELETE FROM librepages_users WHERE name = ($1)", username)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// check if username exists
|
|
|
|
pub async fn username_exists(&self, username: &str) -> ServiceResult<bool> {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"SELECT EXISTS (SELECT 1 from librepages_users WHERE name = $1)",
|
|
|
|
username,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
|
|
|
|
let mut resp = false;
|
|
|
|
if let Some(x) = res.exists {
|
|
|
|
resp = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get user email
|
|
|
|
pub async fn get_email(&self, username: &str) -> ServiceResult<String> {
|
|
|
|
struct Email {
|
|
|
|
email: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = sqlx::query_as!(
|
|
|
|
Email,
|
|
|
|
"SELECT email FROM librepages_users WHERE name = $1",
|
|
|
|
username
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
Ok(res.email)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// check if email exists
|
|
|
|
pub async fn email_exists(&self, email: &str) -> ServiceResult<bool> {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"SELECT EXISTS (SELECT 1 from librepages_users WHERE email = $1)",
|
|
|
|
email
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
|
|
|
|
let mut resp = false;
|
|
|
|
if let Some(x) = res.exists {
|
|
|
|
resp = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// update a user's email
|
|
|
|
pub async fn update_email(&self, p: &UpdateEmail<'_>) -> ServiceResult<()> {
|
|
|
|
sqlx::query!(
|
|
|
|
"UPDATE librepages_users set email = $1
|
|
|
|
WHERE name = $2",
|
|
|
|
&p.new_email,
|
|
|
|
&p.username,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get a user's password
|
|
|
|
pub async fn get_password(&self, l: &Login<'_>) -> ServiceResult<NameHash> {
|
|
|
|
struct Password {
|
|
|
|
name: String,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let rec = match l {
|
|
|
|
Login::Username(u) => sqlx::query_as!(
|
|
|
|
Password,
|
|
|
|
r#"SELECT name, password FROM librepages_users WHERE name = ($1)"#,
|
|
|
|
u,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?,
|
|
|
|
Login::Email(e) => sqlx::query_as!(
|
|
|
|
Password,
|
|
|
|
r#"SELECT name, password FROM librepages_users WHERE email = ($1)"#,
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = NameHash {
|
|
|
|
hash: rec.password,
|
|
|
|
username: rec.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// update user's password
|
2022-09-12 00:22:56 +05:30
|
|
|
pub async fn update_password(&self, p: &NameHash) -> ServiceResult<()> {
|
2022-09-10 20:08:59 +05:30
|
|
|
sqlx::query!(
|
|
|
|
"UPDATE librepages_users set password = $1
|
|
|
|
WHERE name = $2",
|
|
|
|
&p.hash,
|
|
|
|
&p.username,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// update username
|
2022-09-12 00:22:56 +05:30
|
|
|
pub async fn update_username(&self, current: &str, new: &str) -> ServiceResult<()> {
|
2022-09-10 20:08:59 +05:30
|
|
|
sqlx::query!(
|
|
|
|
"UPDATE librepages_users set name = $1
|
|
|
|
WHERE name = $2",
|
|
|
|
new,
|
|
|
|
current,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-09 14:01:15 +05:30
|
|
|
|
|
|
|
pub async fn add_site(&self, msg: &Site) -> ServiceResult<()> {
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO librepages_sites
|
2022-12-03 16:28:24 +05:30
|
|
|
(site_secret, repo_url, branch, hostname, pub_id, owned_by)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, ( SELECT ID FROM librepages_users WHERE name = $6 ));
|
2022-11-09 14:01:15 +05:30
|
|
|
",
|
|
|
|
msg.site_secret,
|
|
|
|
msg.repo_url,
|
|
|
|
msg.branch,
|
|
|
|
msg.hostname,
|
2022-12-03 16:28:24 +05:30
|
|
|
msg.pub_id,
|
2022-11-09 14:01:15 +05:30
|
|
|
msg.owner,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-27 20:15:15 +05:30
|
|
|
pub async fn get_site_from_repo_url(&self, repo_url: &str) -> ServiceResult<Site> {
|
|
|
|
struct S {
|
|
|
|
repo_url: String,
|
|
|
|
branch: String,
|
|
|
|
hostname: String,
|
|
|
|
owned_by: i32,
|
|
|
|
site_secret: String,
|
|
|
|
pub_id: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
let site = sqlx::query_as!(
|
|
|
|
S,
|
|
|
|
"SELECT repo_url, site_secret, branch, hostname, owned_by, pub_id
|
|
|
|
FROM librepages_sites
|
|
|
|
WHERE repo_url = $1
|
|
|
|
AND deleted = false;
|
|
|
|
",
|
|
|
|
repo_url,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
|
|
|
|
|
|
|
struct Owner {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
let owner = sqlx::query_as!(
|
|
|
|
Owner,
|
|
|
|
"SELECT name FROM librepages_users WHERE ID = $1",
|
|
|
|
site.owned_by
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
|
|
|
|
|
|
|
let site = Site {
|
|
|
|
site_secret: site.site_secret,
|
|
|
|
branch: site.branch,
|
|
|
|
hostname: site.hostname,
|
|
|
|
owner: owner.name,
|
|
|
|
repo_url: site.repo_url,
|
|
|
|
pub_id: site.pub_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(site)
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:45:52 +05:30
|
|
|
pub async fn get_site_from_pub_id(&self, pub_id: Uuid, owner: String) -> ServiceResult<Site> {
|
|
|
|
struct S {
|
|
|
|
repo_url: String,
|
|
|
|
branch: String,
|
|
|
|
hostname: String,
|
|
|
|
owned_by: i32,
|
|
|
|
site_secret: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let site = sqlx::query_as!(
|
|
|
|
S,
|
|
|
|
"SELECT repo_url, branch, hostname, owned_by, site_secret
|
|
|
|
FROM librepages_sites
|
|
|
|
WHERE pub_id = $1
|
|
|
|
AND
|
|
|
|
owned_by = (SELECT ID from librepages_users WHERE name = $2)
|
2022-12-06 06:29:56 +05:30
|
|
|
AND
|
|
|
|
deleted = false;
|
2022-12-05 15:45:52 +05:30
|
|
|
",
|
|
|
|
&pub_id,
|
|
|
|
&owner,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
|
|
|
|
|
|
|
let site = Site {
|
|
|
|
site_secret: site.site_secret,
|
|
|
|
branch: site.branch,
|
|
|
|
hostname: site.hostname,
|
2022-12-05 17:40:38 +05:30
|
|
|
owner,
|
2022-12-05 15:45:52 +05:30
|
|
|
repo_url: site.repo_url,
|
|
|
|
pub_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(site)
|
|
|
|
}
|
|
|
|
|
2022-11-09 14:45:12 +05:30
|
|
|
pub async fn get_site_from_secret(&self, site_secret: &str) -> ServiceResult<Site> {
|
|
|
|
struct S {
|
|
|
|
repo_url: String,
|
|
|
|
branch: String,
|
|
|
|
hostname: String,
|
|
|
|
owned_by: i32,
|
2022-12-03 16:28:24 +05:30
|
|
|
pub_id: Uuid,
|
2022-11-09 14:45:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
let site = sqlx::query_as!(
|
|
|
|
S,
|
2022-12-03 16:28:24 +05:30
|
|
|
"SELECT repo_url, branch, hostname, owned_by, pub_id
|
2022-11-09 14:45:12 +05:30
|
|
|
FROM librepages_sites
|
|
|
|
WHERE site_secret = $1
|
2022-12-06 06:29:56 +05:30
|
|
|
AND deleted = false;
|
2022-11-09 14:45:12 +05:30
|
|
|
",
|
|
|
|
site_secret,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
2022-11-10 15:23:30 +05:30
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
2022-11-09 14:45:12 +05:30
|
|
|
|
|
|
|
struct Owner {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
let owner = sqlx::query_as!(
|
|
|
|
Owner,
|
|
|
|
"SELECT name FROM librepages_users WHERE ID = $1",
|
|
|
|
site.owned_by
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
2022-11-10 15:23:30 +05:30
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
2022-11-09 14:45:12 +05:30
|
|
|
|
|
|
|
let site = Site {
|
|
|
|
site_secret: site_secret.to_owned(),
|
|
|
|
branch: site.branch,
|
|
|
|
hostname: site.hostname,
|
|
|
|
owner: owner.name,
|
|
|
|
repo_url: site.repo_url,
|
2022-12-03 16:28:24 +05:30
|
|
|
pub_id: site.pub_id,
|
2022-11-09 14:45:12 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
Ok(site)
|
|
|
|
}
|
|
|
|
|
2022-11-09 14:01:15 +05:30
|
|
|
pub async fn get_site(&self, owner: &str, hostname: &str) -> ServiceResult<Site> {
|
|
|
|
let site = sqlx::query_as!(
|
|
|
|
InnerSite,
|
2022-12-03 16:28:24 +05:30
|
|
|
"SELECT site_secret, repo_url, branch, hostname, pub_id
|
2022-11-09 14:01:15 +05:30
|
|
|
FROM librepages_sites
|
2022-12-06 06:29:56 +05:30
|
|
|
WHERE deleted = false
|
|
|
|
AND owned_by = (SELECT ID FROM librepages_users WHERE name = $1 )
|
2022-11-09 14:01:15 +05:30
|
|
|
AND hostname = $2;
|
|
|
|
",
|
|
|
|
owner,
|
|
|
|
hostname
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
2022-11-10 15:23:30 +05:30
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
2022-11-09 14:01:15 +05:30
|
|
|
|
|
|
|
let res = site.to_site(owner.into());
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn list_all_sites(&self, owner: &str) -> ServiceResult<Vec<Site>> {
|
|
|
|
let mut sites = sqlx::query_as!(
|
|
|
|
InnerSite,
|
2022-12-03 16:28:24 +05:30
|
|
|
"SELECT site_secret, repo_url, branch, hostname, pub_id
|
2022-11-09 14:01:15 +05:30
|
|
|
FROM librepages_sites
|
2022-12-06 06:29:56 +05:30
|
|
|
WHERE deleted = false
|
|
|
|
AND owned_by = (SELECT ID FROM librepages_users WHERE name = $1 );
|
2022-11-09 14:01:15 +05:30
|
|
|
",
|
|
|
|
owner,
|
|
|
|
)
|
|
|
|
.fetch_all(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
let res = sites.drain(0..).map(|s| s.to_site(owner.into())).collect();
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_site(&self, owner: &str, hostname: &str) -> ServiceResult<()> {
|
|
|
|
sqlx::query!(
|
2022-12-06 06:29:56 +05:30
|
|
|
"UPDATE librepages_sites SET deleted = true
|
2022-11-09 14:01:15 +05:30
|
|
|
WHERE hostname = ($1)
|
|
|
|
AND owned_by = ( SELECT ID FROM librepages_users WHERE name = $2);
|
|
|
|
",
|
|
|
|
hostname,
|
|
|
|
owner
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
2022-11-10 15:23:30 +05:30
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
2022-11-09 14:01:15 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-10 16:19:28 +05:30
|
|
|
|
|
|
|
/// check if hostname exists
|
|
|
|
pub async fn hostname_exists(&self, hostname: &str) -> ServiceResult<bool> {
|
|
|
|
let res = sqlx::query!(
|
2022-12-06 06:29:56 +05:30
|
|
|
"SELECT EXISTS (SELECT 1 from librepages_sites WHERE hostname = $1 AND deleted = false)",
|
2022-11-10 16:19:28 +05:30
|
|
|
hostname,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
|
|
|
|
let mut resp = false;
|
|
|
|
if let Some(x) = res.exists {
|
|
|
|
resp = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(resp)
|
|
|
|
}
|
2022-11-15 20:24:13 +05:30
|
|
|
|
2022-12-27 20:15:15 +05:30
|
|
|
/// check if site with repository exists
|
|
|
|
pub async fn site_with_repository_exists(&self, url: &str) -> ServiceResult<bool> {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"SELECT EXISTS (SELECT 1 from librepages_sites WHERE repo_url = $1)",
|
|
|
|
url,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
|
|
|
|
let mut resp = false;
|
|
|
|
if let Some(x) = res.exists {
|
|
|
|
resp = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
|
2022-11-15 20:24:13 +05:30
|
|
|
/// check if event type exists
|
|
|
|
async fn event_type_exists(&self, event: &Event) -> ServiceResult<bool> {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"SELECT EXISTS (SELECT 1 from librepages_deploy_event_type WHERE name = $1)",
|
|
|
|
event.name,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
|
|
|
|
let mut resp = false;
|
|
|
|
if let Some(x) = res.exists {
|
|
|
|
resp = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_event_type(&self) -> ServiceResult<()> {
|
2022-11-27 22:01:36 +05:30
|
|
|
for e in &*EVENTS {
|
2022-12-03 16:12:19 +05:30
|
|
|
if !self.event_type_exists(e).await? {
|
2022-11-15 20:24:13 +05:30
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO librepages_deploy_event_type
|
2022-11-15 20:51:34 +05:30
|
|
|
(name) VALUES ($1) ON CONFLICT (name) DO NOTHING;",
|
2022-11-15 20:24:13 +05:30
|
|
|
e.name
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn log_event(&self, hostname: &str, event: &Event) -> ServiceResult<Uuid> {
|
|
|
|
let now = now_unix_time_stamp();
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO librepages_site_deploy_events
|
|
|
|
(event_type, time, site, pub_id) VALUES (
|
|
|
|
(SELECT iD from librepages_deploy_event_type WHERE name = $1),
|
|
|
|
$2,
|
|
|
|
(SELECT ID from librepages_sites WHERE hostname = $3),
|
|
|
|
$4
|
|
|
|
);
|
|
|
|
",
|
|
|
|
event.name,
|
|
|
|
&now,
|
|
|
|
hostname,
|
|
|
|
uuid,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(map_register_err)?;
|
|
|
|
Ok(uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_event(
|
|
|
|
&self,
|
|
|
|
hostname: &str,
|
|
|
|
event_id: &Uuid,
|
|
|
|
) -> ServiceResult<LibrePagesEvent> {
|
|
|
|
let event = sqlx::query_as!(
|
|
|
|
InnerLibrepagesEvent,
|
|
|
|
"SELECT
|
|
|
|
librepages_deploy_event_type.name,
|
|
|
|
librepages_site_deploy_events.time,
|
|
|
|
librepages_site_deploy_events.pub_id
|
|
|
|
FROM
|
|
|
|
librepages_site_deploy_events
|
|
|
|
INNER JOIN librepages_deploy_event_type ON
|
|
|
|
librepages_deploy_event_type.ID = librepages_site_deploy_events.event_type
|
|
|
|
WHERE
|
|
|
|
librepages_site_deploy_events.site = (
|
|
|
|
SELECT ID FROM librepages_sites WHERE hostname = $1
|
|
|
|
)
|
|
|
|
AND
|
|
|
|
librepages_site_deploy_events.pub_id = $2
|
|
|
|
",
|
|
|
|
hostname,
|
|
|
|
event_id,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
Ok(LibrePagesEvent {
|
|
|
|
id: event.pub_id,
|
|
|
|
time: event.time,
|
|
|
|
event_type: Event::from_str(&event.name).unwrap(),
|
|
|
|
site: hostname.to_owned(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-03 17:45:06 +05:30
|
|
|
pub async fn get_latest_update_event(
|
|
|
|
&self,
|
|
|
|
hostname: &str,
|
|
|
|
) -> ServiceResult<Option<LibrePagesEvent>> {
|
|
|
|
self.get_latest_event_of_type(hostname, &EVENT_TYPE_UPDATE)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_latest_event_of_type(
|
|
|
|
&self,
|
|
|
|
hostname: &str,
|
|
|
|
event_type: &Event,
|
|
|
|
) -> ServiceResult<Option<LibrePagesEvent>> {
|
|
|
|
struct InnerLibrepagesEventNameless {
|
|
|
|
time: OffsetDateTime,
|
|
|
|
pub_id: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
let event = sqlx::query_as!(
|
|
|
|
InnerLibrepagesEventNameless,
|
|
|
|
"SELECT
|
|
|
|
time,
|
|
|
|
pub_id
|
|
|
|
FROM
|
|
|
|
librepages_site_deploy_events
|
|
|
|
WHERE
|
|
|
|
site = (SELECT ID FROM librepages_sites WHERE hostname = $1)
|
|
|
|
AND
|
|
|
|
event_type = (SELECT ID FROM librepages_deploy_event_type WHERE name = $2)
|
|
|
|
AND
|
|
|
|
time = (
|
|
|
|
SELECT MAX(time)
|
|
|
|
FROM
|
|
|
|
librepages_site_deploy_events
|
|
|
|
WHERE
|
|
|
|
site = (SELECT ID FROM librepages_sites WHERE hostname = $1)
|
|
|
|
)
|
|
|
|
",
|
|
|
|
hostname,
|
|
|
|
event_type.name
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Ok(event) => Ok(Some(LibrePagesEvent {
|
|
|
|
id: event.pub_id,
|
|
|
|
time: event.time,
|
|
|
|
event_type: event_type.clone(),
|
|
|
|
site: hostname.to_owned(),
|
|
|
|
})),
|
|
|
|
|
|
|
|
Err(sqlx::Error::RowNotFound) => Ok(None),
|
|
|
|
Err(e) => Err(map_register_err(e)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
}
|
|
|
|
|
2022-11-15 20:24:13 +05:30
|
|
|
pub async fn list_all_site_events(
|
|
|
|
&self,
|
|
|
|
hostname: &str,
|
|
|
|
) -> ServiceResult<Vec<LibrePagesEvent>> {
|
|
|
|
let mut inner_events = sqlx::query_as!(
|
|
|
|
InnerLibrepagesEvent,
|
|
|
|
"SELECT
|
|
|
|
librepages_deploy_event_type.name,
|
|
|
|
librepages_site_deploy_events.time,
|
|
|
|
librepages_site_deploy_events.pub_id
|
|
|
|
FROM
|
|
|
|
librepages_site_deploy_events
|
|
|
|
INNER JOIN librepages_deploy_event_type ON
|
|
|
|
librepages_deploy_event_type.ID = librepages_site_deploy_events.event_type
|
|
|
|
WHERE
|
|
|
|
librepages_site_deploy_events.site = (
|
|
|
|
SELECT ID FROM librepages_sites WHERE hostname = $1
|
|
|
|
);
|
|
|
|
",
|
|
|
|
hostname,
|
|
|
|
)
|
|
|
|
.fetch_all(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
|
|
|
|
let mut events = Vec::with_capacity(inner_events.len());
|
|
|
|
|
|
|
|
for e in inner_events.drain(0..) {
|
|
|
|
events.push(LibrePagesEvent {
|
|
|
|
id: e.pub_id,
|
|
|
|
time: e.time,
|
|
|
|
event_type: Event::from_str(&e.name).unwrap(),
|
|
|
|
site: hostname.to_owned(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Ok(events)
|
|
|
|
}
|
2022-12-20 07:11:21 +05:30
|
|
|
|
|
|
|
/// register a new webhook
|
|
|
|
pub async fn new_webhook(&self, gitea_url: Url, owner: &str) -> ServiceResult<GiteaWebhook> {
|
|
|
|
let hook = GiteaWebhook::new(gitea_url);
|
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO librepages_gitea_webhooks
|
|
|
|
(gitea_url , auth_token, gitea_webhook_secret, owned_by) VALUES ($1, $2, $3,
|
|
|
|
(SELECT ID FROM librepages_users WHERE name = $4)
|
|
|
|
)",
|
|
|
|
hook.gitea_url.as_str(),
|
|
|
|
&hook.auth_token,
|
|
|
|
&hook.gitea_webhook_secret,
|
|
|
|
owner,
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
|
|
|
|
Ok(hook)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_wenhook(&self, auth_token: &str) -> ServiceResult<GiteaWebhook> {
|
|
|
|
struct H {
|
|
|
|
gitea_url: String,
|
|
|
|
auth_token: String,
|
|
|
|
gitea_webhook_secret: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let h = sqlx::query_as!(
|
|
|
|
H,
|
|
|
|
"SELECT gitea_url, auth_token, gitea_webhook_secret
|
|
|
|
FROM librepages_gitea_webhooks
|
|
|
|
WHERE auth_token = $1
|
|
|
|
",
|
|
|
|
auth_token,
|
|
|
|
)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
|
|
|
|
|
|
|
|
let h = GiteaWebhook {
|
|
|
|
gitea_url: Url::parse(&h.gitea_url).unwrap(),
|
|
|
|
auth_token: h.auth_token,
|
|
|
|
gitea_webhook_secret: h.gitea_webhook_secret,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// register a webhook against a site
|
|
|
|
pub async fn webhoo_link_site(&self, auth_token: &str, repo_url: &Url) -> ServiceResult<()> {
|
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO librepages_gitea_webhook_site_mapping
|
|
|
|
(site_id, gitea_webhook_id) VALUES (
|
|
|
|
(SELECT ID FROM librepages_sites WHERE repo_url = $1),
|
|
|
|
(SELECT ID FROM librepages_gitea_webhooks WHERE auth_token = $2)
|
|
|
|
) ON CONFLICT (site_id, gitea_webhook_id) DO NOTHING;",
|
|
|
|
repo_url.as_str(),
|
|
|
|
auth_token
|
|
|
|
)
|
|
|
|
.execute(&self.pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-09 14:01:15 +05:30
|
|
|
}
|
2022-12-20 07:11:21 +05:30
|
|
|
|
2022-11-09 14:01:15 +05:30
|
|
|
struct InnerSite {
|
|
|
|
site_secret: String,
|
|
|
|
repo_url: String,
|
|
|
|
branch: String,
|
|
|
|
hostname: String,
|
2022-12-03 16:28:24 +05:30
|
|
|
pub_id: Uuid,
|
2022-11-09 14:01:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerSite {
|
|
|
|
fn to_site(self, owner: String) -> Site {
|
|
|
|
Site {
|
|
|
|
site_secret: self.site_secret,
|
|
|
|
repo_url: self.repo_url,
|
|
|
|
branch: self.branch,
|
|
|
|
hostname: self.hostname,
|
2022-12-03 16:28:24 +05:30
|
|
|
pub_id: self.pub_id,
|
2022-11-09 14:01:15 +05:30
|
|
|
owner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
|
|
|
/// Data required to add a new site
|
|
|
|
pub struct Site {
|
|
|
|
pub site_secret: String,
|
|
|
|
pub repo_url: String,
|
2022-12-03 16:28:24 +05:30
|
|
|
pub pub_id: Uuid,
|
2022-11-09 14:01:15 +05:30
|
|
|
pub branch: String,
|
|
|
|
pub hostname: String,
|
|
|
|
pub owner: String,
|
2022-09-10 20:08:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
|
|
|
/// Data required to register a new user
|
|
|
|
pub struct Register<'a> {
|
|
|
|
/// username of new user
|
|
|
|
pub username: &'a str,
|
|
|
|
/// hashed password of new use
|
|
|
|
pub hash: &'a str,
|
|
|
|
/// Optionally, email of new use
|
|
|
|
pub email: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
|
|
|
/// data required to update them email of a user
|
|
|
|
pub struct UpdateEmail<'a> {
|
|
|
|
/// username of the user
|
|
|
|
pub username: &'a str,
|
|
|
|
/// new email address of the user
|
|
|
|
pub new_email: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
|
|
|
/// types of credentials used as identifiers during login
|
|
|
|
pub enum Login<'a> {
|
|
|
|
/// username as login
|
|
|
|
Username(&'a str),
|
|
|
|
/// email as login
|
|
|
|
Email(&'a str),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
|
|
|
/// type encapsulating username and hashed password of a user
|
|
|
|
pub struct NameHash {
|
|
|
|
/// username
|
|
|
|
pub username: String,
|
|
|
|
/// hashed password
|
|
|
|
pub hash: String,
|
2022-09-10 19:21:49 +05:30
|
|
|
}
|
|
|
|
|
2022-11-15 20:24:13 +05:30
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct Event {
|
2022-11-27 22:01:36 +05:30
|
|
|
pub name: String,
|
2022-11-15 20:24:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
impl Event {
|
2022-11-27 22:01:36 +05:30
|
|
|
fn new(name: String) -> Self {
|
2022-11-15 20:24:13 +05:30
|
|
|
Self { name }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_str(name: &str) -> Option<Event> {
|
2022-11-27 22:01:36 +05:30
|
|
|
(*EVENTS).into_iter().find(|e| e.name == name).cloned()
|
2022-11-15 20:24:13 +05:30
|
|
|
}
|
|
|
|
}
|
2022-11-27 22:01:36 +05:30
|
|
|
lazy_static! {
|
|
|
|
pub static ref EVENT_TYPE_CREATE: Event = Event::new("site.event.create".into());
|
|
|
|
pub static ref EVENT_TYPE_UPDATE: Event = Event::new("site.event.update".into());
|
|
|
|
pub static ref EVENT_TYPE_DELETE: Event = Event::new("site.event.delete".into());
|
|
|
|
pub static ref EVENTS: [&'static Event; 3] = [
|
|
|
|
&*EVENT_TYPE_CREATE,
|
|
|
|
&*EVENT_TYPE_DELETE,
|
|
|
|
&*EVENT_TYPE_UPDATE
|
|
|
|
];
|
|
|
|
}
|
2022-11-15 20:24:13 +05:30
|
|
|
|
|
|
|
struct InnerLibrepagesEvent {
|
|
|
|
name: String,
|
|
|
|
time: OffsetDateTime,
|
|
|
|
pub_id: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct LibrePagesEvent {
|
|
|
|
pub event_type: Event,
|
|
|
|
pub time: OffsetDateTime,
|
|
|
|
pub site: String,
|
|
|
|
pub id: Uuid,
|
|
|
|
}
|
|
|
|
|
2022-12-20 07:11:21 +05:30
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct GiteaWebhook {
|
|
|
|
pub gitea_url: Url,
|
|
|
|
pub gitea_webhook_secret: String,
|
|
|
|
pub auth_token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GiteaWebhook {
|
|
|
|
fn new(gitea_url: Url) -> Self {
|
|
|
|
Self {
|
|
|
|
gitea_url,
|
|
|
|
gitea_webhook_secret: utils::get_random(40),
|
|
|
|
auth_token: utils::get_random(40),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 19:21:49 +05:30
|
|
|
fn now_unix_time_stamp() -> OffsetDateTime {
|
|
|
|
OffsetDateTime::now_utc()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_db(settings: &crate::settings::Settings) -> Database {
|
|
|
|
let pool_options = PgPoolOptions::new().max_connections(settings.database.pool);
|
|
|
|
ConnectionOptions::Fresh(Fresh {
|
|
|
|
pool_options,
|
|
|
|
url: settings.database.url.clone(),
|
|
|
|
disable_logging: !settings.debug,
|
|
|
|
})
|
|
|
|
.connect()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:08:59 +05:30
|
|
|
/// map custom row not found error to DB error
|
|
|
|
pub fn map_row_not_found_err(e: sqlx::Error, row_not_found: ServiceError) -> ServiceError {
|
|
|
|
if let sqlx::Error::RowNotFound = e {
|
|
|
|
row_not_found
|
|
|
|
} else {
|
|
|
|
map_register_err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// map postgres errors to [ServiceError](ServiceError) types
|
|
|
|
fn map_register_err(e: sqlx::Error) -> ServiceError {
|
|
|
|
use sqlx::Error;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
if let Error::Database(err) = e {
|
|
|
|
if err.code() == Some(Cow::from("23505")) {
|
|
|
|
let msg = err.message();
|
|
|
|
println!("{}", msg);
|
|
|
|
if msg.contains("librepages_users_name_key") {
|
|
|
|
ServiceError::UsernameTaken
|
|
|
|
} else if msg.contains("librepages_users_email_key") {
|
|
|
|
ServiceError::EmailTaken
|
|
|
|
} else {
|
2022-11-11 14:56:36 +05:30
|
|
|
error!("{}", msg);
|
2022-09-10 20:08:59 +05:30
|
|
|
ServiceError::InternalServerError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ServiceError::InternalServerError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ServiceError::InternalServerError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 19:21:49 +05:30
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-11-15 20:51:34 +05:30
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2022-09-10 19:21:49 +05:30
|
|
|
use super::*;
|
|
|
|
use crate::settings::Settings;
|
|
|
|
|
2022-11-15 20:51:34 +05:30
|
|
|
#[test]
|
|
|
|
fn event_names_are_unique() {
|
|
|
|
let mut uniq = HashSet::new();
|
2022-11-27 22:01:36 +05:30
|
|
|
assert!(EVENTS.into_iter().all(move |x| uniq.insert(x.name.clone())));
|
2022-11-15 20:51:34 +05:30
|
|
|
}
|
|
|
|
|
2022-09-10 19:21:49 +05:30
|
|
|
#[actix_rt::test]
|
|
|
|
async fn db_works() {
|
|
|
|
let settings = Settings::new().unwrap();
|
|
|
|
let pool_options = PgPoolOptions::new().max_connections(1);
|
|
|
|
let db = ConnectionOptions::Fresh(Fresh {
|
|
|
|
pool_options,
|
|
|
|
url: settings.database.url.clone(),
|
|
|
|
disable_logging: !settings.debug,
|
|
|
|
})
|
|
|
|
.connect()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(db.ping().await);
|
2022-09-10 20:08:59 +05:30
|
|
|
|
|
|
|
const EMAIL: &str = "postgresuser@foo.com";
|
|
|
|
const EMAIL2: &str = "postgresuser2@foo.com";
|
|
|
|
const NAME: &str = "postgresuser";
|
|
|
|
const PASSWORD: &str = "pasdfasdfasdfadf";
|
|
|
|
|
|
|
|
db.migrate().await.unwrap();
|
|
|
|
let p = super::Register {
|
|
|
|
username: NAME,
|
|
|
|
email: EMAIL,
|
|
|
|
hash: PASSWORD,
|
|
|
|
};
|
|
|
|
|
|
|
|
if db.username_exists(p.username).await.unwrap() {
|
|
|
|
db.delete_user(p.username).await.unwrap();
|
|
|
|
assert!(
|
|
|
|
!db.username_exists(p.username).await.unwrap(),
|
|
|
|
"user is deleted so username shouldn't exist"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.register(&p).await.unwrap();
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
db.register(&p).await,
|
|
|
|
Err(ServiceError::UsernameTaken)
|
|
|
|
));
|
|
|
|
|
|
|
|
// testing get_password
|
|
|
|
|
|
|
|
// with username
|
|
|
|
let name_hash = db.get_password(&Login::Username(p.username)).await.unwrap();
|
|
|
|
assert_eq!(name_hash.hash, p.hash, "user password matches");
|
|
|
|
|
|
|
|
assert_eq!(name_hash.username, p.username, "username matches");
|
|
|
|
|
|
|
|
// with email
|
|
|
|
let mut name_hash = db.get_password(&Login::Email(p.email)).await.unwrap();
|
|
|
|
assert_eq!(name_hash.hash, p.hash, "user password matches");
|
|
|
|
assert_eq!(name_hash.username, p.username, "username matches");
|
|
|
|
|
|
|
|
// testing get_email
|
|
|
|
assert_eq!(db.get_email(p.username).await.unwrap(), p.email);
|
|
|
|
|
|
|
|
// testing email exists
|
|
|
|
assert!(
|
|
|
|
db.email_exists(p.email).await.unwrap(),
|
|
|
|
"user is registered so email should exist"
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
db.username_exists(p.username).await.unwrap(),
|
|
|
|
"user is registered so username should exist"
|
|
|
|
);
|
|
|
|
|
|
|
|
// update password test. setting password = username
|
|
|
|
name_hash.hash = name_hash.username.clone();
|
|
|
|
db.update_password(&name_hash).await.unwrap();
|
|
|
|
|
|
|
|
let name_hash = db.get_password(&Login::Username(p.username)).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
name_hash.hash, p.username,
|
|
|
|
"user password matches with changed value"
|
|
|
|
);
|
|
|
|
assert_eq!(name_hash.username, p.username, "username matches");
|
|
|
|
|
|
|
|
// update username to p.email
|
|
|
|
assert!(
|
|
|
|
!db.username_exists(p.email).await.unwrap(),
|
|
|
|
"user with p.email doesn't exist. pre-check to update username to p.email"
|
|
|
|
);
|
|
|
|
db.update_username(p.username, p.email).await.unwrap();
|
|
|
|
assert!(
|
|
|
|
db.username_exists(p.email).await.unwrap(),
|
|
|
|
"user with p.email exist post-update"
|
|
|
|
);
|
|
|
|
|
|
|
|
// testing update email
|
|
|
|
let update_email = UpdateEmail {
|
|
|
|
username: p.username,
|
|
|
|
new_email: EMAIL2,
|
|
|
|
};
|
|
|
|
db.update_email(&update_email).await.unwrap();
|
|
|
|
println!(
|
|
|
|
"null user email: {}",
|
|
|
|
db.email_exists(p.email).await.unwrap()
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
db.email_exists(p.email).await.unwrap(),
|
|
|
|
"user was with empty email but email is set; so email should exist"
|
|
|
|
);
|
|
|
|
|
|
|
|
// deleting user
|
|
|
|
db.delete_user(p.email).await.unwrap();
|
|
|
|
assert!(
|
|
|
|
!db.username_exists(p.email).await.unwrap(),
|
|
|
|
"user is deleted so username shouldn't exist"
|
|
|
|
);
|
2022-09-10 19:21:49 +05:30
|
|
|
}
|
2022-11-09 14:01:15 +05:30
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
pub async fn test_db_sites() {
|
|
|
|
let settings = Settings::new().unwrap();
|
|
|
|
let pool_options = PgPoolOptions::new().max_connections(1);
|
|
|
|
let db = ConnectionOptions::Fresh(Fresh {
|
|
|
|
pool_options,
|
|
|
|
url: settings.database.url.clone(),
|
|
|
|
disable_logging: !settings.debug,
|
|
|
|
})
|
|
|
|
.connect()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(db.ping().await);
|
|
|
|
|
|
|
|
const EMAIL: &str = "postgresdbsiteuser@foo.com";
|
|
|
|
const NAME: &str = "postgresdbsiteuser";
|
|
|
|
const PASSWORD: &str = "pasdfasdfasdfadf";
|
|
|
|
|
|
|
|
db.migrate().await.unwrap();
|
2022-11-15 20:24:13 +05:30
|
|
|
|
|
|
|
// check if events are created
|
2022-11-27 22:01:36 +05:30
|
|
|
for e in &*EVENTS {
|
2022-11-15 20:24:13 +05:30
|
|
|
println!("Testing event type exists {}", e.name);
|
2022-12-03 16:12:19 +05:30
|
|
|
assert!(db.event_type_exists(e).await.unwrap());
|
2022-11-15 20:24:13 +05:30
|
|
|
}
|
|
|
|
|
2022-11-09 14:01:15 +05:30
|
|
|
let p = super::Register {
|
|
|
|
username: NAME,
|
|
|
|
email: EMAIL,
|
|
|
|
hash: PASSWORD,
|
|
|
|
};
|
|
|
|
|
|
|
|
if db.username_exists(p.username).await.unwrap() {
|
|
|
|
db.delete_user(p.username).await.unwrap();
|
|
|
|
assert!(
|
|
|
|
!db.username_exists(p.username).await.unwrap(),
|
|
|
|
"user is deleted so username shouldn't exist"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.register(&p).await.unwrap();
|
|
|
|
|
|
|
|
let site = Site {
|
|
|
|
site_secret: "foobar".into(),
|
2022-12-27 20:15:15 +05:30
|
|
|
repo_url: "https://git.test_db_sites.example.org/LibrePages/librepages.git".into(),
|
2022-11-09 14:01:15 +05:30
|
|
|
branch: "librepages".into(),
|
|
|
|
hostname: "db_works.tests.librepages.librepages.org".into(),
|
2022-12-03 16:28:24 +05:30
|
|
|
pub_id: Uuid::new_v4(),
|
2022-11-09 14:01:15 +05:30
|
|
|
owner: p.username.into(),
|
|
|
|
};
|
2022-11-10 16:19:28 +05:30
|
|
|
|
|
|
|
// test if hostname exists. Should be false
|
|
|
|
assert!(!db.hostname_exists(&site.hostname).await.unwrap());
|
2022-12-27 20:15:15 +05:30
|
|
|
assert!(!db
|
|
|
|
.site_with_repository_exists(&site.repo_url)
|
|
|
|
.await
|
|
|
|
.unwrap());
|
2022-11-10 16:19:28 +05:30
|
|
|
|
|
|
|
// testing adding site
|
2022-11-09 14:01:15 +05:30
|
|
|
db.add_site(&site).await.unwrap();
|
|
|
|
|
2022-11-10 16:19:28 +05:30
|
|
|
// test if hostname exists. Should be true
|
|
|
|
assert!(db.hostname_exists(&site.hostname).await.unwrap());
|
2022-12-27 20:15:15 +05:30
|
|
|
assert!(db
|
|
|
|
.site_with_repository_exists(&site.repo_url)
|
|
|
|
.await
|
|
|
|
.unwrap());
|
2022-11-10 16:19:28 +05:30
|
|
|
|
2022-11-09 14:01:15 +05:30
|
|
|
// get site
|
|
|
|
let db_site = db.get_site(p.username, &site.hostname).await.unwrap();
|
|
|
|
assert_eq!(db_site, site);
|
|
|
|
|
2022-11-09 14:45:12 +05:30
|
|
|
// get site by secret
|
|
|
|
assert_eq!(
|
|
|
|
db_site,
|
|
|
|
db.get_site_from_secret(&site.site_secret).await.unwrap()
|
|
|
|
);
|
|
|
|
|
2022-12-27 20:15:15 +05:30
|
|
|
// get site by repo_url
|
|
|
|
assert_eq!(
|
|
|
|
db_site,
|
|
|
|
db.get_site_from_repo_url(&site.repo_url).await.unwrap()
|
|
|
|
);
|
|
|
|
|
2022-11-09 14:01:15 +05:30
|
|
|
// list all sites owned by user
|
|
|
|
let db_sites = db.list_all_sites(p.username).await.unwrap();
|
|
|
|
assert_eq!(db_sites.len(), 1);
|
|
|
|
assert_eq!(db_sites, vec![site.clone()]);
|
|
|
|
|
2022-11-15 20:24:13 +05:30
|
|
|
// add event to site
|
|
|
|
let event_id = db
|
|
|
|
.log_event(&site.hostname, &EVENT_TYPE_CREATE)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let event = db.get_event(&site.hostname, &event_id).await.unwrap();
|
|
|
|
assert_eq!(event.id, event_id);
|
2022-11-27 22:01:36 +05:30
|
|
|
assert_eq!(event.event_type, *EVENT_TYPE_CREATE);
|
2022-11-15 20:24:13 +05:30
|
|
|
assert_eq!(event.site, site.hostname);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
db.list_all_site_events(&site.hostname).await.unwrap(),
|
|
|
|
vec![event]
|
|
|
|
);
|
|
|
|
|
2022-12-03 17:45:06 +05:30
|
|
|
// when no update event exist, None is returned
|
|
|
|
assert!(db
|
|
|
|
.get_latest_update_event(&site.hostname)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
// add multiple update events, see if latest is returned
|
|
|
|
db.log_event(&site.hostname, &EVENT_TYPE_UPDATE)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let latest_update_event_id = db
|
|
|
|
.log_event(&site.hostname, &EVENT_TYPE_UPDATE)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let latest_update_event_id_from_db = db
|
|
|
|
.get_latest_update_event(&site.hostname)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
latest_update_event_id_from_db.event_type,
|
|
|
|
*EVENT_TYPE_UPDATE
|
|
|
|
);
|
|
|
|
assert_eq!(latest_update_event_id_from_db.id, latest_update_event_id);
|
|
|
|
|
2022-12-20 07:11:21 +05:30
|
|
|
// add webhook
|
|
|
|
let gitea_url = Url::parse("https://example.org").unwrap();
|
|
|
|
let hook = db.new_webhook(gitea_url, NAME).await.unwrap();
|
|
|
|
assert_eq!(hook, db.get_wenhook(&hook.auth_token).await.unwrap());
|
|
|
|
assert_eq!(
|
|
|
|
db.get_wenhook(&hook.gitea_webhook_secret).await.err(),
|
|
|
|
Some(ServiceError::WebhookNotFound)
|
|
|
|
);
|
|
|
|
|
2022-12-27 20:15:15 +05:30
|
|
|
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-12-20 07:11:21 +05:30
|
|
|
|
2022-11-09 14:01:15 +05:30
|
|
|
// delete site
|
|
|
|
db.delete_site(p.username, &site.hostname).await.unwrap();
|
2022-11-10 16:19:28 +05:30
|
|
|
|
|
|
|
// test if hostname exists. Should be false
|
|
|
|
assert!(!db.hostname_exists(&site.hostname).await.unwrap());
|
2022-11-09 14:01:15 +05:30
|
|
|
}
|
2022-09-10 19:21:49 +05:30
|
|
|
}
|