feat: test utils init separate tmp dirs for deployed pages and assert status codes
This commit is contained in:
parent
68d63bba07
commit
58e928eeda
3 changed files with 48 additions and 11 deletions
|
@ -76,6 +76,7 @@ mod tests {
|
|||
#[actix_rt::test]
|
||||
async fn deploy_update_works() {
|
||||
let ctx = tests::get_data().await;
|
||||
println!("[log] test configuration {:#?}", ctx.settings);
|
||||
let app = get_app!(ctx).await;
|
||||
let page = ctx.settings.pages.get(0);
|
||||
let page = page.unwrap();
|
||||
|
@ -90,7 +91,7 @@ mod tests {
|
|||
post_request!(&payload, V1_API_ROUTES.deploy.update).to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert!(tests::check_status(resp, StatusCode::OK).await);
|
||||
|
||||
payload.secret = page.branch.clone();
|
||||
|
||||
|
@ -99,6 +100,6 @@ mod tests {
|
|||
post_request!(&payload, V1_API_ROUTES.deploy.update).to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
assert!(tests::check_status(resp, StatusCode::NOT_FOUND).await);
|
||||
}
|
||||
}
|
||||
|
|
11
src/meta.rs
11
src/meta.rs
|
@ -66,15 +66,10 @@ mod tests {
|
|||
#[actix_rt::test]
|
||||
async fn build_details_works() {
|
||||
let ctx = tests::get_data().await;
|
||||
println!("[log] test configuration {:#?}", ctx.settings);
|
||||
let app = get_app!(ctx).await;
|
||||
|
||||
let resp = test::call_service(
|
||||
&app,
|
||||
test::TestRequest::get()
|
||||
.uri(V1_API_ROUTES.meta.build_details)
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let resp = get_request!(app, V1_API_ROUTES.meta.build_details);
|
||||
assert!(tests::check_status(resp, StatusCode::OK).await);
|
||||
}
|
||||
}
|
||||
|
|
43
src/tests.rs
43
src/tests.rs
|
@ -14,13 +14,41 @@
|
|||
* 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::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use mktemp::Temp;
|
||||
|
||||
use crate::ctx::Ctx;
|
||||
use crate::page::Page;
|
||||
use crate::settings::Settings;
|
||||
|
||||
pub async fn get_data() -> Arc<Ctx> {
|
||||
let settings = Settings::new().unwrap();
|
||||
let mut settings = Settings::new().unwrap();
|
||||
|
||||
let tmp_dir = Temp::new_dir().unwrap();
|
||||
println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap());
|
||||
let tmp_dir = tmp_dir.as_path();
|
||||
let mut pages = Vec::with_capacity(settings.pages.len());
|
||||
for page in settings.pages.iter() {
|
||||
let name = Path::new(&page.path).file_name().unwrap().to_str().unwrap();
|
||||
let path = tmp_dir.join(name);
|
||||
let page = Page {
|
||||
path: path.to_str().unwrap().to_string(),
|
||||
secret: page.secret.clone(),
|
||||
branch: page.branch.clone(),
|
||||
repo: page.repo.clone(),
|
||||
};
|
||||
|
||||
pages.push(Arc::new(page));
|
||||
}
|
||||
|
||||
settings.pages = pages;
|
||||
println!("[log] Initialzing settings again with test config");
|
||||
settings.init();
|
||||
|
||||
Ctx::new(settings)
|
||||
}
|
||||
|
||||
|
@ -99,3 +127,16 @@ macro_rules! get_app {
|
|||
test::init_service(get_app!("APP").app_data(crate::WebData::new($ctx.clone())))
|
||||
};
|
||||
}
|
||||
|
||||
/// Utility function to check for status of a test response, attempt response payload serialization
|
||||
/// and print payload if response status doesn't match expected status
|
||||
pub async fn check_status(resp: ServiceResponse, expected: StatusCode) -> bool {
|
||||
let status = resp.status();
|
||||
if status != expected {
|
||||
eprintln!("[error] Expected status code: {expected} received: {status}");
|
||||
let response: serde_json::Value = actix_web::test::read_body_json(resp).await;
|
||||
eprintln!("[error] Body:\n{:#?}", response);
|
||||
}
|
||||
|
||||
status == expected
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue