mcaptcha/api/v1/notifications/
add.rs1use actix_identity::Identity;
7use actix_web::{web, HttpResponse, Responder};
8use serde::{Deserialize, Serialize};
9
10use crate::errors::*;
11use crate::AppData;
12
13use db_core::AddNotification;
14
15#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
16pub struct AddNotificationRequest {
17 pub to: String,
18 pub heading: String,
19 pub message: String,
20}
21
22#[my_codegen::post(
24 path = "crate::V1_API_ROUTES.notifications.add",
25 wrap = "crate::api::v1::get_middleware()"
26)]
27pub async fn add_notification(
28 payload: web::Json<AddNotificationRequest>,
29 data: AppData,
30 id: Identity,
31) -> ServiceResult<impl Responder> {
32 let sender = id.identity().unwrap();
33 let p = AddNotification {
36 from: &sender,
37 to: &payload.to,
38 message: &payload.message,
39 heading: &payload.heading,
40 };
41
42 data.db.create_notification(&p).await?;
43
44 Ok(HttpResponse::Ok())
45}
46
47#[cfg(test)]
48pub mod tests {
49 use actix_web::http::StatusCode;
50 use actix_web::test;
51
52 use super::*;
53 use crate::tests::*;
54 use crate::*;
55
56 #[actix_rt::test]
57 async fn notification_works_pg() {
58 let data = pg::get_data().await;
59 notification_works(data).await;
60 }
61
62 #[actix_rt::test]
63 async fn notification_works_maria() {
64 let data = maria::get_data().await;
65 notification_works(data).await;
66 }
67
68 pub async fn notification_works(data: ArcData) {
69 const NAME1: &str = "notifuser1";
70 const NAME2: &str = "notiuser2";
71 const PASSWORD: &str = "longpassworddomain";
72 const EMAIL1: &str = "testnotification1@a.com";
73 const EMAIL2: &str = "testnotification2@a.com";
74
75 let data = &data;
76
77 delete_user(data, NAME1).await;
78 delete_user(data, NAME2).await;
79
80 register_and_signin(data, NAME1, EMAIL1, PASSWORD).await;
81 register_and_signin(data, NAME2, EMAIL2, PASSWORD).await;
82 let (_creds, signin_resp) = signin(data, NAME1, PASSWORD).await;
83 let cookies = get_cookie!(signin_resp);
84 let app = get_app!(data).await;
85
86 let msg = AddNotificationRequest {
87 to: NAME2.into(),
88 heading: "Test notification".into(),
89 message: "Testing notifications with a dummy message".into(),
90 };
91
92 let send_notification_resp = test::call_service(
93 &app,
94 post_request!(&msg, V1_API_ROUTES.notifications.add)
95 .cookie(cookies.clone())
96 .to_request(),
97 )
98 .await;
99 assert_eq!(send_notification_resp.status(), StatusCode::OK);
100 }
101}