use crate::builders::{CrateBuilder, VersionBuilder};
use crate::util::{RequestHelper, TestApp};
use http::StatusCode;

#[tokio::test(flavor = "multi_thread")]
async fn test_non_blocked_download_route() {
    let (app, anon, user) = TestApp::init()
        .with_config(|config| {
            config.block.routes.clear();
        })
        .with_user()
        .await;

    let mut conn = app.db_conn().await;

    CrateBuilder::new("foo", user.as_model().id)
        .version(VersionBuilder::new("1.0.0"))
        .expect_build(&mut conn)
        .await;

    let status = anon
        .get::<()>("/api/v1/crates/foo/1.0.0/download")
        .await
        .status();
    assert_eq!(status, StatusCode::FOUND);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocked_download_route() {
    let (app, anon, user) = TestApp::init()
        .with_config(|config| {
            config.block.routes.clear();
            config
                .block
                .routes
                .insert("/api/v1/crates/{name}/{version}/download".into());
        })
        .with_user()
        .await;

    let mut conn = app.db_conn().await;

    CrateBuilder::new("foo", user.as_model().id)
        .version(VersionBuilder::new("1.0.0"))
        .expect_build(&mut conn)
        .await;

    let status = anon
        .get::<()>("/api/v1/crates/foo/1.0.0/download")
        .await
        .status();
    assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
}
