Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
articles.rs 19.6 KiB
Newer Older
Pierre Jarriges's avatar
Pierre Jarriges committed
use crate::{middleware::AuthenticatedAdminMiddleware, model::Article, AppState};
use actix_web::{
    delete, get, post, put,
    web::{Data, Form, Json, Path},
    HttpRequest, HttpResponse, Responder,
};
use chrono::Utc;
use futures::stream::StreamExt;
use serde::{Deserialize, Serialize};
use wither::{
    bson::{doc, oid::ObjectId, DateTime},
    mongodb::Collection,
    prelude::Model,
};

#[derive(Deserialize, Serialize)]
pub struct ArticleTitleFormData {
    pub title: String,
}

fn get_collection(app_state: &AppState) -> Collection<Article> {
    app_state.db.collection_with_type::<Article>("articles")
}

#[post("/post-article")]
pub async fn post_article(
    app_state: Data<AppState>,
    article_data: Json<Article>,
    middleware: Data<AuthenticatedAdminMiddleware<'_>>,
    req: HttpRequest,
) -> impl Responder {
    if middleware.exec(&app_state, &req, None).await.is_err() {
        return HttpResponse::Unauthorized().finish();
    }

    let mut article_data = article_data.into_inner();
    article_data.date = Some(DateTime(Utc::now()));

    match get_collection(&app_state)
        .insert_one(article_data, None)
        .await
    {
        Ok(res) => HttpResponse::Created().json(res),
Pierre Jarriges's avatar
Pierre Jarriges committed
        Err(e) => {
            HttpResponse::InternalServerError().body(format!("Error inserting new article {:?}", e))
        }
    }
}

#[put("/update-article/{article_id}")]
pub async fn update_article(
    app_state: Data<AppState>,
    article_data: Json<Article>,
    middleware: Data<AuthenticatedAdminMiddleware<'_>>,
    article_id: Path<String>,
    req: HttpRequest,
) -> impl Responder {
    if middleware.exec(&app_state, &req, None).await.is_err() {
        return HttpResponse::Unauthorized().finish();
    }

    let article_id = match ObjectId::with_string(&article_id.into_inner()) {
        Ok(id) => id,
        Err(_) => {
            return HttpResponse::BadRequest()
                .body("Failed to convert article_id to ObjectId. String may be malformed")
        }
    };

    let mut article_data = article_data.into_inner();
    article_data.date = Some(DateTime(Utc::now()));

    match get_collection(&app_state)
        .find_one_and_replace(doc! {"_id": &article_id}, article_data, None)
        .await
    {
        Ok(res) => HttpResponse::Ok().json(res.unwrap()),
Pierre Jarriges's avatar
Pierre Jarriges committed
        Err(_) => HttpResponse::InternalServerError().finish(),
    }
}

#[delete("/delete-article/{article_id}")]
pub async fn delete_article(
    app_state: Data<AppState>,
    middleware: Data<AuthenticatedAdminMiddleware<'_>>,
    article_id: Path<String>,
    req: HttpRequest,
) -> impl Responder {
    if middleware.exec(&app_state, &req, None).await.is_err() {
        return HttpResponse::Unauthorized().finish();
    }

    let article_id = match ObjectId::with_string(&article_id.into_inner()) {
        Ok(id) => id,
        Err(_) => {
            return HttpResponse::BadRequest()
                .body("Failed to convert article_id to ObjectId. String may be malformed")
        }
    };

    match get_collection(&app_state)
        .find_one_and_delete(doc! {"_id": &article_id}, None)
        .await
    {
        Ok(_) => HttpResponse::Accepted().body("Article was deleted"),
        Err(e) => HttpResponse::InternalServerError().body(&format!("{:?}", e)),
Pierre Jarriges's avatar
Pierre Jarriges committed
#[get("/articles/{category}/{locale}")]
Pierre Jarriges's avatar
Pierre Jarriges committed
pub async fn get_articles_by_category(
    app_state: Data<AppState>,
Pierre Jarriges's avatar
Pierre Jarriges committed
    path: Path<(String, String)>,
Pierre Jarriges's avatar
Pierre Jarriges committed
) -> impl Responder {
Pierre Jarriges's avatar
Pierre Jarriges committed
    let (category, locale) = path.into_inner();

Pierre Jarriges's avatar
Pierre Jarriges committed
    match get_collection(&app_state)
Pierre Jarriges's avatar
Pierre Jarriges committed
        .find(doc! {"category": category, "locale": locale}, None)
Pierre Jarriges's avatar
Pierre Jarriges committed
        .await
    {
        Ok(mut cursor) => {
            let mut results: Vec<Article> = Vec::new();

            while let Some(result) = cursor.next().await {
                match result {
                    Ok(article) => {
                        results.push(article);
                    }
                    Err(_) => {
                        return HttpResponse::InternalServerError().finish();
                    }
                }
            }
            HttpResponse::Ok().json(results)
        }
        Err(_) => HttpResponse::InternalServerError().finish(),
    }
}

#[get("/article/{article_id}")]
pub async fn get_article(app_state: Data<AppState>, article_id: Path<String>) -> impl Responder {
    let article_id = match ObjectId::with_string(&article_id.into_inner()) {
        Ok(id) => id,
        Err(_) => {
            return HttpResponse::BadRequest()
                .body("Failed to convert article_id to ObjectId. String may be malformed")
        }
    };

    match Article::find_one(&app_state.db, doc! {"_id":&article_id}, None).await {
        Ok(art) => {
            if art.is_none() {
                return HttpResponse::NotFound().body("Article was not found");
            }

            HttpResponse::Ok().json(art)
        }
        Err(e) => HttpResponse::InternalServerError().body(format!("Database error: {:#?}", e)),
    }
}

#[post("/article-by-title")]
Pierre Jarriges's avatar
Pierre Jarriges committed
pub async fn get_article_by_title(
    app_state: Data<AppState>,
    form_data: Form<ArticleTitleFormData>,
) -> impl Responder {
    let title = form_data.into_inner().title;
    match Article::find_one(&app_state.db, doc! {"title":title}, None).await {
        Ok(art) => {
            if art.is_none() {
                return HttpResponse::NotFound().body("Article was not found");
            }

            HttpResponse::Ok().json(art)
        }
        Err(e) => HttpResponse::InternalServerError().body(format!("Database error: {:#?}", e)),
    }
}

#[get("/articles")]
pub async fn get_all_articles(app_state: Data<AppState>) -> impl Responder {
    match get_collection(&app_state).find(None, None).await {
        Ok(mut cursor) => {
            let mut results: Vec<Article> = Vec::new();
            while let Some(result) = cursor.next().await {
                match result {
                    Ok(article) => {
                        results.push(article);
                    }
                    Err(_) => {
                        return HttpResponse::InternalServerError().finish();
                    }
                }
            }
            HttpResponse::Ok().json(results)
        }
        Err(_) => HttpResponse::InternalServerError().finish(),
    }
}

Pierre Jarriges's avatar
Pierre Jarriges committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@@
 *@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@@
 *  _______   ______    ______   _______   *@@
 * |__   __@ |  ____@  /  ____@ |__   __@  *@@
 *    |  @   |  @__    \_ @_       |  @    *@@
 *    |  @   |   __@     \  @_     |  @    *@@
 *    |  @   |  @___   ____\  @    |  @    *@@
 *    |__@   |______@  \______@    |__@    *@@
 *                                         *@@
 *@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@@
 *@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@*/

#[cfg(test)]
mod test_articles {
    use super::*;
    use crate::middleware::get_auth_cookie;
    use crate::model::{AdminAuthCredentials, Administrator};
    use actix_web::{
        http::{Method, StatusCode},
        test,
        web::Bytes,
        App,
    };
    use wither::bson::Bson;

    async fn insert_test_article(
        app_state: &AppState,
        test_article: Article,
    ) -> Result<(ObjectId, String), String> {
        let title = test_article.title.to_owned();
        match get_collection(&app_state)
            .insert_one(test_article, None)
            .await
        {
            Ok(inserted) => match inserted.inserted_id {
                Bson::ObjectId(id) => Ok((id, title)),
                _ => Err(String::from("Failed to parse inserted_id")),
            },
            Err(e) => Err(format!("{:?}", e)),
        }
    }

    async fn delete_test_article(
        app_state: &AppState,
        article_id: &ObjectId,
    ) -> Result<i64, String> {
        match get_collection(&app_state)
            .delete_one(doc! {"_id": article_id}, None)
            .await
        {
            Ok(delete_result) => Ok(delete_result.deleted_count),
            Err(e) => Err(format!("{:?}", e)),
        }
    }

    async fn get_authenticated_admin(app_state: &AppState) -> Administrator {
        Administrator::authenticated(
            app_state,
            AdminAuthCredentials {
                username: app_state.env.default_admin_username.to_owned(),
                password: app_state.env.default_admin_password.to_owned(),
            },
        )
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn test_post_article() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .app_data(Data::new(AuthenticatedAdminMiddleware::new(
                    "kuadrado-admin-auth",
                )))
                .service(post_article),
        )
        .await;

        let article = Article::test_article();

        let admin_user = get_authenticated_admin(&app_state).await;

        let req = test::TestRequest::with_uri("/post-article")
            .method(Method::POST)
            .header("Content-Type", "application/json")
            .header("Accept", "text/html")
            .cookie(get_auth_cookie(
                "kuadrado-admin-auth",
                app_state
                    .encryption
                    .decrypt(&admin_user.auth_token.unwrap())
                    .to_owned(),
            ))
            .set_payload(Bytes::from(serde_json::to_string(&article).unwrap()))
            .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::CREATED);

        let find_inserted = Article::find_one(&app_state.db, doc! {"title": &article.title}, None)
            .await
            .unwrap();

        assert!(find_inserted.is_some());
        assert_eq!(find_inserted.unwrap().title, article.title);

        get_collection(&app_state)
            .delete_one(doc! {"title": article.title}, None)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_post_article_unauthorized() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .app_data(Data::new(AuthenticatedAdminMiddleware::new(
                    "kuadrado-admin-auth",
                )))
                .service(post_article),
        )
        .await;

        let article = Article::test_article();

        let req = test::TestRequest::with_uri("/post-article")
            .method(Method::POST)
            .header("Content-Type", "application/json")
            .header("Accept", "text/html")
            .cookie(get_auth_cookie(
                "wrong-cookie",
                app_state.encryption.random_ascii_lc_string(32),
            ))
            .set_payload(Bytes::from(serde_json::to_string(&article).unwrap()))
            .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_update_article() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .app_data(Data::new(AuthenticatedAdminMiddleware::new(
                    "kuadrado-admin-auth",
                )))
                .service(update_article),
        )
        .await;

        let mut article = Article::test_article();

        let (article_id, _) = insert_test_article(&app_state, article.clone())
            .await
            .unwrap();

        article.title = "changed title".to_string();

        let admin_user = get_authenticated_admin(&app_state).await;

        let req = test::TestRequest::with_uri(
            format!("/update-article/{}", article_id.to_hex()).as_str(),
        )
        .method(Method::PUT)
        .header("Content-Type", "application/json")
        .header("Accept", "text/html")
        .cookie(get_auth_cookie(
            "kuadrado-admin-auth",
            app_state
                .encryption
                .decrypt(&admin_user.auth_token.unwrap())
                .to_owned(),
        ))
        .set_payload(Bytes::from(serde_json::to_string(&article).unwrap()))
        .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::OK);

        let find_inserted = Article::find_one(&app_state.db, doc! {"_id": &article_id}, None)
            .await
            .unwrap();

        assert!(find_inserted.is_some());
        assert_eq!(find_inserted.unwrap().title, "changed title");

        let del_count = delete_test_article(&app_state, &article_id).await.unwrap();
        assert_eq!(del_count, 1);
    }

    #[tokio::test]
    async fn test_update_article_unauthorized() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .app_data(Data::new(AuthenticatedAdminMiddleware::new(
                    "kuadrado-admin-auth",
                )))
                .service(update_article),
        )
        .await;

        let article = Article::test_article();

        let req = test::TestRequest::with_uri(
            format!("/update-article/{}", ObjectId::new().to_hex()).as_str(),
        )
        .method(Method::PUT)
        .header("Content-Type", "application/json")
        .header("Accept", "text/html")
        .cookie(get_auth_cookie(
            "wrong-cookie",
            app_state.encryption.random_ascii_lc_string(32),
        ))
        .set_payload(Bytes::from(serde_json::to_string(&article).unwrap()))
        .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_delete_article() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .app_data(Data::new(AuthenticatedAdminMiddleware::new(
                    "kuadrado-admin-auth",
                )))
                .service(delete_article),
        )
        .await;

        let article = Article::test_article();
        let (article_id, _) = insert_test_article(&app_state, article.clone())
            .await
            .unwrap();

        let admin_user = get_authenticated_admin(&app_state).await;

        let req = test::TestRequest::with_uri(
            format!("/delete-article/{}", article_id.to_hex()).as_str(),
        )
        .method(Method::DELETE)
        .cookie(get_auth_cookie(
            "kuadrado-admin-auth",
            app_state
                .encryption
                .decrypt(&admin_user.auth_token.unwrap())
                .to_owned(),
        ))
        .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::ACCEPTED);

        let find_inserted = Article::find_one(&app_state.db, doc! {"_id": &article_id}, None)
            .await
            .unwrap();

        assert!(find_inserted.is_none());
    }

    #[tokio::test]
    async fn test_delete_article_unauthorized() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .app_data(Data::new(AuthenticatedAdminMiddleware::new(
                    "kuadrado-admin-auth",
                )))
                .service(delete_article),
        )
        .await;

        let article = Article::test_article();

        let req = test::TestRequest::with_uri(
            format!("/delete-article/{}", ObjectId::new().to_hex()).as_str(),
        )
        .method(Method::DELETE)
        .cookie(get_auth_cookie(
            "wrong-cookie",
            app_state.encryption.random_ascii_lc_string(32),
        ))
        .set_payload(Bytes::from(serde_json::to_string(&article).unwrap()))
        .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_get_article() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .service(get_article),
        )
        .await;

        let article = Article::test_article();
        let (article_id, article_title) = insert_test_article(&app_state, article.clone())
            .await
            .unwrap();

        let req = test::TestRequest::with_uri(format!("/article/{}", article_id.to_hex()).as_str())
            .header("Accept", "application/json")
            .method(Method::GET)
            .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::OK);

        let result: Article = test::read_body_json(resp).await;
        assert_eq!(result.title, article_title);

        let del_count = delete_test_article(&app_state, &article_id).await.unwrap();
        assert_eq!(del_count, 1);
    }

    #[tokio::test]
    async fn test_get_article_by_title() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .service(get_article_by_title),
        )
        .await;

        let article = Article::test_article();
        let (article_id, article_title) = insert_test_article(&app_state, article.clone())
            .await
            .unwrap();

        let req = test::TestRequest::with_uri("/article-by-title")
            .header("Accept", "application/json")
            .method(Method::POST)
Pierre Jarriges's avatar
Pierre Jarriges committed
            .set_form(&ArticleTitleFormData {
                title: article_title.to_owned(),
            })
            .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::OK);
        let result: Article = test::read_body_json(resp).await;
        assert_eq!(result.title, article_title);

        let del_count = delete_test_article(&app_state, &article_id).await.unwrap();
        assert_eq!(del_count, 1);
    }

    #[tokio::test]
    async fn test_get_articles_by_category() {
        dotenv::dotenv().ok();

        let app_state = AppState::for_test().await;

        let mut app = test::init_service(
            App::new()
                .app_data(Data::new(app_state.clone()))
                .service(get_articles_by_category),
        )
        .await;

        let article = Article::test_article();
        let (article_id, article_title) = insert_test_article(&app_state, article.clone())
            .await
            .unwrap();

Pierre Jarriges's avatar
Pierre Jarriges committed
        let req = test::TestRequest::with_uri("/articles/testing/fr")
Pierre Jarriges's avatar
Pierre Jarriges committed
            .header("Accept", "application/json")
            .method(Method::GET)
            .to_request();

        let resp = test::call_service(&mut app, req).await;

        assert_eq!(resp.status(), StatusCode::OK);

        let results: Vec<Article> = test::read_body_json(resp).await;
        let find_inserted = results.iter().find(|&art| art.title == article_title);
        assert!(find_inserted.is_some());

        let del_count = delete_test_article(&app_state, &article_id).await.unwrap();
        assert_eq!(del_count, 1);
    }
}