Skip to content

make session backwards compatible #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 2 additions & 105 deletions pgml-dashboard/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

204 changes: 196 additions & 8 deletions pgml-dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ pub fn replace_banner_product(
context: &Cluster,
) -> Result<Response, Error> {
let mut all_notification_cookies = Notifications::get_viewed(cookies);

let current_notification_cookie = all_notification_cookies.iter().position(|x| x.id == id);

match current_notification_cookie {
Expand All @@ -408,8 +407,8 @@ pub fn replace_banner_product(

Notifications::update_viewed(&all_notification_cookies, cookies);

// Get the notification that triggered this call.
// Guaranteed to exist since it built the component that called this, so this is safe to unwrap.
// Get the notification that triggered this call..
// unwrap notifications if fine since we should panic if this is missing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be panicking if anything optional like this is missing. We should default to vec![].

let last_notification = context
.notifications
.as_ref()
Expand All @@ -424,11 +423,15 @@ pub fn replace_banner_product(
.into_iter()
.filter(|n: &Notification| -> bool {
let n = n.clone().set_viewed(n.id == id);
Notification::product_filter(
&n,
last_notification.clone().unwrap().level.clone(),
deployment_id.clone(),
)
if last_notification.clone().is_none() {
return false;
} else {
Notification::product_filter(
&n,
last_notification.clone().unwrap().level.clone(),
deployment_id.clone(),
)
}
})
.next(),
_ => None,
Expand Down Expand Up @@ -519,3 +522,188 @@ pub fn routes() -> Vec<Route> {
pub async fn migrate(pool: &PgPool) -> anyhow::Result<()> {
Ok(sqlx::migrate!("./migrations").run(pool).await?)
}

#[cfg(test)]
mod test {
use super::*;
use crate::components::sections::footers::MarketingFooter;
use crate::guards::Cluster;
use rocket::fairing::AdHoc;
use rocket::http::Cookie;
use rocket::local::asynchronous::Client;

#[sqlx::test]
async fn test_remove_modal() {
let rocket = rocket::build().mount("/", routes());
let client = Client::untracked(rocket).await.unwrap();

let cookie = vec![
NotificationCookie {
id: "1".to_string(),
time_viewed: Some(chrono::Utc::now() - chrono::Duration::days(1)),
time_modal_viewed: Some(chrono::Utc::now() - chrono::Duration::days(1)),
},
NotificationCookie {
id: "2".to_string(),
time_viewed: None,
time_modal_viewed: None,
},
];

let response = client
.get("/notifications/product/modal/remove_modal?id=1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like the wrong place to implement this. This should be in our web app, and we should pass it through the context into the dashboard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean here. The test, or the endpoint should be in web app? I felt like both are needed here since notifications include marketing notifications.

.private_cookie(Cookie::new("session", Notifications::safe_serialize_session(&cookie)))
.dispatch()
.await;

let time_modal_viewed = Notifications::get_viewed(response.cookies())
.get(0)
.unwrap()
.time_modal_viewed;

// Update modal view time for existing notification cookie
assert_eq!(time_modal_viewed.is_some(), true);

let response = client
.get("/notifications/product/modal/remove_modal?id=3")
.private_cookie(Cookie::new("session", Notifications::safe_serialize_session(&cookie)))
.dispatch()
.await;

let time_modal_viewed = Notifications::get_viewed(response.cookies())
.get(0)
.unwrap()
.time_modal_viewed;

// Update modal view time for new notification cookie
assert_eq!(time_modal_viewed.is_some(), true);
}

#[sqlx::test]
async fn test_remove_banner_product() {
let rocket = rocket::build().mount("/", routes());
let client = Client::untracked(rocket).await.unwrap();

let cookie = vec![
NotificationCookie {
id: "1".to_string(),
time_viewed: Some(chrono::Utc::now() - chrono::Duration::days(1)),
time_modal_viewed: Some(chrono::Utc::now() - chrono::Duration::days(1)),
},
NotificationCookie {
id: "2".to_string(),
time_viewed: None,
time_modal_viewed: Some(chrono::Utc::now() - chrono::Duration::days(1)),
},
];

let response = client
.get("/notifications/product/remove_banner?id=1&target=ajskghjfbs")
.private_cookie(Cookie::new("session", Notifications::safe_serialize_session(&cookie)))
.dispatch()
.await;

let time_viewed = Notifications::get_viewed(response.cookies())
.get(0)
.unwrap()
.time_viewed;

// Update view time for existing notification cookie
assert_eq!(time_viewed.is_some(), true);

let response = client
.get("/notifications/product/remove_banner?id=3&target=ajfadghs")
.private_cookie(Cookie::new("session", Notifications::safe_serialize_session(&cookie)))
.dispatch()
.await;

let time_viewed = Notifications::get_viewed(response.cookies())
.get(0)
.unwrap()
.time_viewed;

// Update view time for new notification cookie
assert_eq!(time_viewed.is_some(), true);
}

#[sqlx::test]
async fn test_replace_banner_product() {
let notification1 = Notification::new("Test notification 1")
.set_level(&NotificationLevel::ProductMedium)
.set_deployment("1");
let notification2 = Notification::new("Test notification 2")
.set_level(&NotificationLevel::ProductMedium)
.set_deployment("1");
let _notification3 = Notification::new("Test notification 3")
.set_level(&NotificationLevel::ProductMedium)
.set_deployment("2");
let _notification4 = Notification::new("Test notification 4").set_level(&NotificationLevel::ProductMedium);
let _notification5 = Notification::new("Test notification 5").set_level(&NotificationLevel::ProductMarketing);

let rocket = rocket::build()
.attach(AdHoc::on_request("request", |req, _| {
Box::pin(async {
req.local_cache(|| Cluster {
pool: None,
context: Context {
user: models::User::default(),
cluster: models::Cluster::default(),
dropdown_nav: StaticNav { links: vec![] },
product_left_nav: StaticNav { links: vec![] },
marketing_footer: MarketingFooter::new().render_once().unwrap(),
head_items: None,
},
notifications: Some(vec![
Notification::new("Test notification 1")
.set_level(&NotificationLevel::ProductMedium)
.set_deployment("1"),
Notification::new("Test notification 2")
.set_level(&NotificationLevel::ProductMedium)
.set_deployment("1"),
Notification::new("Test notification 3")
.set_level(&NotificationLevel::ProductMedium)
.set_deployment("2"),
Notification::new("Test notification 4").set_level(&NotificationLevel::ProductMedium),
Notification::new("Test notification 5").set_level(&NotificationLevel::ProductMarketing),
]),
});
})
}))
.mount("/", routes());

let client = Client::tracked(rocket).await.unwrap();

let response = client
.get(format!(
"/notifications/product/replace_banner?id={}&deployment_id=1",
notification1.id
))
.dispatch()
.await;

let body = response.into_string().await.unwrap();
let rsp_contains_next_notification = body.contains("Test notification 2");

// Ensure the banner is replaced with next notification of same type
assert_eq!(rsp_contains_next_notification, true);

let response = client
.get(format!(
"/notifications/product/replace_banner?id={}&deployment_id=1",
notification2.id
))
.dispatch()
.await;

let body = response.into_string().await.unwrap();
let rsp_contains_next_notification_3 = body.contains("Test notification 3");
let rsp_contains_next_notification_4 = body.contains("Test notification 4");
let rsp_contains_next_notification_5 = body.contains("Test notification 5");

// Ensure the next notification is not found since none match deployment id or level
assert_eq!(
rsp_contains_next_notification_3 && rsp_contains_next_notification_4 && rsp_contains_next_notification_5,
false
);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy