feat: save gitea instance app data
This commit is contained in:
parent
0f44135f73
commit
605fced22d
4 changed files with 112 additions and 565 deletions
6
migrations/20221215163426_librepages_gitea.sql
Normal file
6
migrations/20221215163426_librepages_gitea.sql
Normal file
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS librepages_gitea_instances (
|
||||
url VARCHAR(3000) NOT NULL UNIQUE,
|
||||
client_id TEXT NOT NULL,
|
||||
client_secret TEXT NOT NULL,
|
||||
ID SERIAL PRIMARY KEY NOT NULL
|
||||
);
|
566
sqlx-data.json
566
sqlx-data.json
|
@ -1,567 +1,3 @@
|
|||
{
|
||||
"db": "PostgreSQL",
|
||||
"10d30dade86d79210203bdbce4b6db5d2aa446b0f88ca834771ecbbe11be51fb": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "UPDATE librepages_sites SET deleted = true\n WHERE hostname = ($1)\n AND owned_by = ( SELECT ID FROM librepages_users WHERE name = $2);\n "
|
||||
},
|
||||
"12391b10cf16a807322c49c9cc7e0a015f26b445beacf4cdd4e7714f36b4adf6": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "site_secret",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "repo_url",
|
||||
"ordinal": 1,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "branch",
|
||||
"ordinal": 2,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "hostname",
|
||||
"ordinal": 3,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "pub_id",
|
||||
"ordinal": 4,
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT site_secret, repo_url, branch, hostname, pub_id\n FROM librepages_sites\n WHERE deleted = false\n AND owned_by = (SELECT ID FROM librepages_users WHERE name = $1 );\n "
|
||||
},
|
||||
"14cdc724af64942e93994f97e9eafc8272d15605eff7aab9e5177d01f2bf6118": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Timestamptz",
|
||||
"Text",
|
||||
"Uuid"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "INSERT INTO librepages_site_deploy_events\n (event_type, time, site, pub_id) VALUES (\n (SELECT iD from librepages_deploy_event_type WHERE name = $1),\n $2,\n (SELECT ID from librepages_sites WHERE hostname = $3),\n $4\n );\n "
|
||||
},
|
||||
"1be33ea4fe0e6079c88768ff912b824f4b0250193f2d086046c1fd0da125ae0c": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "password",
|
||||
"ordinal": 1,
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT name, password FROM librepages_users WHERE name = ($1)"
|
||||
},
|
||||
"279b5ae27935279b06d2799eef2da6a316324a05d23ba7a729c608c70168c496": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Varchar",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "UPDATE librepages_users set name = $1\n WHERE name = $2"
|
||||
},
|
||||
"39854fcbfb0247377c6c5ca70c2c0fa7804548848bf56f881eea2f8242e7a09d": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "time",
|
||||
"ordinal": 1,
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"name": "pub_id",
|
||||
"ordinal": 2,
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Uuid"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT\n librepages_deploy_event_type.name,\n librepages_site_deploy_events.time,\n librepages_site_deploy_events.pub_id\n FROM\n librepages_site_deploy_events\n INNER JOIN librepages_deploy_event_type ON\n librepages_deploy_event_type.ID = librepages_site_deploy_events.event_type\n WHERE\n librepages_site_deploy_events.site = (\n SELECT ID FROM librepages_sites WHERE hostname = $1\n )\n AND\n librepages_site_deploy_events.pub_id = $2\n "
|
||||
},
|
||||
"432fe829719ce8110f768b4a611724bb34191ac224d2143ff4c81548da75c103": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "repo_url",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "branch",
|
||||
"ordinal": 1,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "hostname",
|
||||
"ordinal": 2,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "owned_by",
|
||||
"ordinal": 3,
|
||||
"type_info": "Int4"
|
||||
},
|
||||
{
|
||||
"name": "site_secret",
|
||||
"ordinal": 4,
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT repo_url, branch, hostname, owned_by, site_secret\n FROM librepages_sites\n WHERE pub_id = $1\n AND\n owned_by = (SELECT ID from librepages_users WHERE name = $2)\n AND\n deleted = false;\n "
|
||||
},
|
||||
"53f3c21c06c8d1c218537dfa9183fd0604aaf28200d8aa12e97db4ac317df39e": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int4"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT name FROM librepages_users WHERE ID = $1"
|
||||
},
|
||||
"54f1ad328c83997d5e80686665d4cfef58d3529d24cb6caaa7ff301479e5d735": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "repo_url",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "branch",
|
||||
"ordinal": 1,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "hostname",
|
||||
"ordinal": 2,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "owned_by",
|
||||
"ordinal": 3,
|
||||
"type_info": "Int4"
|
||||
},
|
||||
{
|
||||
"name": "pub_id",
|
||||
"ordinal": 4,
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT repo_url, branch, hostname, owned_by, pub_id\n FROM librepages_sites\n WHERE site_secret = $1\n AND deleted = false;\n "
|
||||
},
|
||||
"5c5d774bde06c0ab83c3616a56a28f12dfd9c546cbaac9f246d3b350c587823e": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "DELETE FROM librepages_users WHERE name = ($1)"
|
||||
},
|
||||
"65f6181364cd8c6ed4eae3f62b5ae771a27e8da6e698c235ca77d4cec784d04b": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "site_secret",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "repo_url",
|
||||
"ordinal": 1,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "branch",
|
||||
"ordinal": 2,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "hostname",
|
||||
"ordinal": 3,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "pub_id",
|
||||
"ordinal": 4,
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT site_secret, repo_url, branch, hostname, pub_id\n FROM librepages_sites\n WHERE deleted = false\n AND owned_by = (SELECT ID FROM librepages_users WHERE name = $1 )\n AND hostname = $2;\n "
|
||||
},
|
||||
"6a557f851d4f47383b864085093beb0954e79779f21b655978f07e285281e0ac": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Varchar",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "UPDATE librepages_users set email = $1\n WHERE name = $2"
|
||||
},
|
||||
"6db98c6ae90b8eb98ace1a5acfa3c8af2b6ed7d51c6debda12637f5d7b076c15": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "exists",
|
||||
"ordinal": 0,
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
null
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT EXISTS (SELECT 1 from librepages_sites WHERE hostname = $1 AND deleted = false)"
|
||||
},
|
||||
"77612c85be99e6de2e4a6e3105ebaeb470d6cc57b2999b673a085da41c035f9e": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "time",
|
||||
"ordinal": 0,
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"name": "pub_id",
|
||||
"ordinal": 1,
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT\n time,\n pub_id\n FROM\n librepages_site_deploy_events\n WHERE\n site = (SELECT ID FROM librepages_sites WHERE hostname = $1)\n AND\n event_type = (SELECT ID FROM librepages_deploy_event_type WHERE name = $2)\n AND\n time = (\n SELECT MAX(time) \n FROM\n librepages_site_deploy_events\n WHERE\n site = (SELECT ID FROM librepages_sites WHERE hostname = $1)\n )\n "
|
||||
},
|
||||
"8735b654bc261571e6a5908d55a8217474c76bdff7f3cbcc71500a0fe13249db": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "exists",
|
||||
"ordinal": 0,
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
null
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT EXISTS (SELECT 1 from librepages_users WHERE email = $1)"
|
||||
},
|
||||
"924e756de5544cece865a10a7e136ecc6126e3a603947264cc7899387c18c819": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "UPDATE librepages_users set password = $1\n WHERE name = $2"
|
||||
},
|
||||
"b48c77db6e663d97df44bf9ec2ee92fd3e02f2dcbcdbd1d491e09fab2da68494": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "password",
|
||||
"ordinal": 1,
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT name, password FROM librepages_users WHERE email = ($1)"
|
||||
},
|
||||
"b8b1b3c5fa205b071f577b2ce9993ddfc7c99ada26aea48aa1c201c8c3c7fcf6": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Varchar",
|
||||
"Varchar",
|
||||
"Text",
|
||||
"Varchar",
|
||||
"Uuid",
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "\n INSERT INTO librepages_sites\n (site_secret, repo_url, branch, hostname, pub_id, owned_by)\n VALUES ($1, $2, $3, $4, $5, ( SELECT ID FROM librepages_users WHERE name = $6 ));\n "
|
||||
},
|
||||
"bdd4d2a1b0b97ebf8ed61cfd120b40146fbf3ea9afb5cd0e03c9d29860b6a26b": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "exists",
|
||||
"ordinal": 0,
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
null
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT EXISTS (SELECT 1 from librepages_users WHERE name = $1)"
|
||||
},
|
||||
"ced69a08729ffb906e8971dbdce6a8d4197bc9bb8ccd7c58b3a88eb7be73fc2e": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "email",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT email FROM librepages_users WHERE name = $1"
|
||||
},
|
||||
"d2327c1bcb40e18518c2112413a19a9b26eb0f54f83c53e968c9752d70c8dd4e": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 0,
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"name": "time",
|
||||
"ordinal": 1,
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"name": "pub_id",
|
||||
"ordinal": 2,
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT\n librepages_deploy_event_type.name,\n librepages_site_deploy_events.time,\n librepages_site_deploy_events.pub_id\n FROM\n librepages_site_deploy_events\n INNER JOIN librepages_deploy_event_type ON\n librepages_deploy_event_type.ID = librepages_site_deploy_events.event_type\n WHERE\n librepages_site_deploy_events.site = (\n SELECT ID FROM librepages_sites WHERE hostname = $1\n );\n "
|
||||
},
|
||||
"e4adf1bc9175eeb9d61b495653bb452039cc38818c8792acdc6a1c732b6f4554": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "exists",
|
||||
"ordinal": 0,
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
null
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "SELECT EXISTS (SELECT 1 from librepages_deploy_event_type WHERE name = $1)"
|
||||
},
|
||||
"f651da8f411b7977cb87dd8d4bd5d167661d7ef1d865747e76219453d386d593": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Varchar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "INSERT INTO librepages_deploy_event_type\n (name) VALUES ($1) ON CONFLICT (name) DO NOTHING;"
|
||||
},
|
||||
"faa4170a309f19a4abf1ca3f8dd3c0526945aa00f028ebf8bd7063825d448f5b": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Varchar",
|
||||
"Text",
|
||||
"Varchar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"query": "INSERT INTO librepages_users\n (name , password, email) VALUES ($1, $2, $3)"
|
||||
}
|
||||
"db": "PostgreSQL"
|
||||
}
|
94
src/db.rs
94
src/db.rs
|
@ -23,6 +23,7 @@ use sqlx::types::time::OffsetDateTime;
|
|||
use sqlx::ConnectOptions;
|
||||
use sqlx::PgPool;
|
||||
use tracing::error;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::errors::*;
|
||||
|
@ -621,6 +622,43 @@ impl Database {
|
|||
}
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
pub async fn new_gitea_instance(&self, payload: &AddGiteaInstance) -> ServiceResult<()> {
|
||||
sqlx::query!(
|
||||
"INSERT INTO librepages_gitea_instances
|
||||
(url , client_id, client_secret) VALUES ($1, $2, $3)",
|
||||
&payload.url.as_str(),
|
||||
payload.client_id,
|
||||
payload.client_secret,
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_register_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_gitea_instance(&self, url: &Url) -> ServiceResult<()> {
|
||||
sqlx::query!(
|
||||
"DELETE FROM librepages_gitea_instances WHERE url = ($1)",
|
||||
url.as_str()
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_register_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_gitea_password(&self, url: &Url) -> ServiceResult<GiteaInstance> {
|
||||
let res = sqlx::query_as!(
|
||||
GiteaInstance,
|
||||
"SELECT client_id, client_secret FROM librepages_gitea_instances WHERE url = ($1)",
|
||||
url.as_str()
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(|e| map_row_not_found_err(e, ServiceError::GiteaInstanceNotFound))?;
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
struct InnerSite {
|
||||
site_secret: String,
|
||||
|
@ -731,6 +769,19 @@ pub struct LibrePagesEvent {
|
|||
pub id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct GiteaInstance {
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct AddGiteaInstance {
|
||||
pub url: Url,
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
}
|
||||
|
||||
fn now_unix_time_stamp() -> OffsetDateTime {
|
||||
OffsetDateTime::now_utc()
|
||||
}
|
||||
|
@ -769,6 +820,8 @@ fn map_register_err(e: sqlx::Error) -> ServiceError {
|
|||
ServiceError::UsernameTaken
|
||||
} else if msg.contains("librepages_users_email_key") {
|
||||
ServiceError::EmailTaken
|
||||
} else if msg.contains("librepages_gitea_instances_url_key") {
|
||||
ServiceError::GiteaInstanceRegistered
|
||||
} else {
|
||||
error!("{}", msg);
|
||||
ServiceError::InternalServerError
|
||||
|
@ -1028,4 +1081,45 @@ mod tests {
|
|||
// test if hostname exists. Should be false
|
||||
assert!(!db.hostname_exists(&site.hostname).await.unwrap());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
pub async fn test_gitea_instance_methods() {
|
||||
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);
|
||||
|
||||
let url = Url::parse("https://test_gitea_instance_methods.example.org").unwrap();
|
||||
let client_id = "longid";
|
||||
let client_secret = "longsecret";
|
||||
|
||||
let _ = db.delete_gitea_instance(&url).await;
|
||||
|
||||
let payload = AddGiteaInstance {
|
||||
client_secret: client_secret.into(),
|
||||
client_id: client_id.into(),
|
||||
url: url.clone(),
|
||||
};
|
||||
db.new_gitea_instance(&payload).await.unwrap();
|
||||
assert_eq!(
|
||||
db.new_gitea_instance(&payload).await.err(),
|
||||
Some(ServiceError::GiteaInstanceRegistered)
|
||||
);
|
||||
|
||||
let res = db.get_gitea_password(&url).await.unwrap();
|
||||
assert_eq!(res.client_id, client_id);
|
||||
assert_eq!(res.client_secret, client_secret);
|
||||
db.delete_gitea_instance(&url).await.unwrap();
|
||||
assert_eq!(
|
||||
db.get_gitea_password(&url).await.err(),
|
||||
Some(ServiceError::GiteaInstanceNotFound)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -178,6 +178,14 @@ pub enum ServiceError {
|
|||
#[display(fmt = "Passwords don't match")]
|
||||
/// passwords don't match
|
||||
PasswordsDontMatch,
|
||||
|
||||
/// Gitea instance is registered
|
||||
#[display(fmt = "Gitea instance is registered")]
|
||||
GiteaInstanceRegistered,
|
||||
|
||||
/// Gitea instance not found
|
||||
#[display(fmt = "Gitea instance not found")]
|
||||
GiteaInstanceNotFound,
|
||||
}
|
||||
|
||||
impl From<ParseError> for ServiceError {
|
||||
|
@ -252,6 +260,9 @@ impl ResponseError for ServiceError {
|
|||
ServiceError::ClosedForRegistration => StatusCode::FORBIDDEN, //FORBIDDEN,
|
||||
ServiceError::NotAnEmail => StatusCode::BAD_REQUEST, //BADREQUEST,
|
||||
ServiceError::WrongPassword => StatusCode::UNAUTHORIZED, //UNAUTHORIZED,
|
||||
//
|
||||
ServiceError::GiteaInstanceRegistered => StatusCode::BAD_REQUEST,
|
||||
ServiceError::GiteaInstanceNotFound => StatusCode::NOT_FOUND,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue