feat: test utils to add site

This commit is contained in:
Aravinth Manivannan 2022-11-10 16:18:52 +05:30
parent 7fb29e0d7a
commit 74a33cf044
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 24 additions and 21 deletions

View File

@ -14,7 +14,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use actix_web::{ use actix_web::{
@ -27,12 +26,16 @@ use mktemp::Temp;
use serde::Serialize; use serde::Serialize;
use crate::ctx::api::v1::auth::{Login, Register}; use crate::ctx::api::v1::auth::{Login, Register};
use crate::ctx::api::v1::pages::AddSite;
use crate::ctx::Ctx; use crate::ctx::Ctx;
use crate::errors::*; use crate::errors::*;
use crate::page::Page;
use crate::settings::Settings; use crate::settings::Settings;
use crate::*; use crate::*;
const HOSTNAME: &str = "example.org";
pub const REPO_URL: &str = "https://github.com/mCaptcha/website/";
pub const BRANCH: &str = "gh-pages";
pub async fn get_ctx() -> (Temp, Arc<Ctx>) { pub async fn get_ctx() -> (Temp, Arc<Ctx>) {
// mktemp::Temp is returned because the temp directory created // mktemp::Temp is returned because the temp directory created
// is removed once the variable goes out of scope // is removed once the variable goes out of scope
@ -40,23 +43,8 @@ pub async fn get_ctx() -> (Temp, Arc<Ctx>) {
let tmp_dir = Temp::new_dir().unwrap(); let tmp_dir = Temp::new_dir().unwrap();
println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap()); println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap());
let mut pages = Vec::with_capacity(settings.pages.len());
let page_base_path = tmp_dir.as_path().join("base_path"); let page_base_path = tmp_dir.as_path().join("base_path");
for page in settings.pages.iter() { settings.page.base_path = page_base_path.to_str().unwrap().into();
let name = Path::new(&page.path).file_name().unwrap().to_str().unwrap();
let path = tmp_dir.as_path().join(name);
let page = Page {
path: path.to_str().unwrap().to_string(),
secret: page.secret.clone(),
branch: page.branch.clone(),
repo: page.repo.clone(),
domain: "mcaptcha.org".into(),
};
pages.push(Arc::new(page));
}
settings.pages = pages;
settings.init(); settings.init();
println!("[log] Initialzing settings again with test config"); println!("[log] Initialzing settings again with test config");
settings.init(); settings.init();
@ -201,9 +189,10 @@ impl Ctx {
post_request!(&msg, crate::V1_API_ROUTES.auth.register).to_request(), post_request!(&msg, crate::V1_API_ROUTES.auth.register).to_request(),
) )
.await; .await;
// let resp_err: ErrorToResponse = actix_web::test::read_body_json(resp).await; if resp.status() != StatusCode::OK {
// panic!("{}", resp_err.error); let resp_err: ErrorToResponse = actix_web::test::read_body_json(resp).await;
assert_eq!(resp.status(), StatusCode::OK); panic!("{}", resp_err.error);
}
} }
/// signin util /// signin util
@ -278,4 +267,18 @@ impl Ctx {
//println!("{}", resp_err.error); //println!("{}", resp_err.error);
assert_eq!(resp_err.error, format!("{}", err)); assert_eq!(resp_err.error, format!("{}", err));
} }
pub async fn add_test_site(&self, owner: String, hostname: String) {
let msg = AddSite {
repo_url: REPO_URL.into(),
branch: BRANCH.into(),
hostname,
owner,
};
self.add_site(msg).await.unwrap();
}
pub fn get_test_hostname(&self, unique: &str) -> String {
format!("{unique}.{HOSTNAME}")
}
} }