From b00491cd9e4bf2669b1afdb0b94869869ca409af Mon Sep 17 00:00:00 2001 From: Max Kalashnikoff Date: Tue, 14 Nov 2023 18:43:56 +0100 Subject: [PATCH] feat(decrypted_notify): adding always_raw for the client registration --- .../1699982551_add_always_raw_to_clients.sql | 2 ++ src/handlers/register_client.rs | 2 ++ src/stores/client.rs | 18 +++++++++++++----- tests/functional/singletenant/push.rs | 1 + tests/functional/singletenant/registration.rs | 3 +++ tests/functional/stores/client.rs | 12 ++++++++++++ tests/functional/stores/notification.rs | 1 + 7 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 migrations/1699982551_add_always_raw_to_clients.sql diff --git a/migrations/1699982551_add_always_raw_to_clients.sql b/migrations/1699982551_add_always_raw_to_clients.sql new file mode 100644 index 00000000..fecb4475 --- /dev/null +++ b/migrations/1699982551_add_always_raw_to_clients.sql @@ -0,0 +1,2 @@ +ALTER TABLE public.clients + ADD COLUMN always_raw BOOLEAN DEFAULT FALSE; diff --git a/src/handlers/register_client.rs b/src/handlers/register_client.rs index dc605548..5ea6a1c9 100644 --- a/src/handlers/register_client.rs +++ b/src/handlers/register_client.rs @@ -28,6 +28,7 @@ pub struct RegisterBody { #[serde(rename = "type")] pub push_type: String, pub token: String, + pub always_raw: Option, } pub async fn handler( @@ -93,6 +94,7 @@ pub async fn handler( tenant_id: tenant_id.clone(), push_type, token: body.token, + always_raw: body.always_raw.unwrap_or(false), }) .await?; diff --git a/src/stores/client.rs b/src/stores/client.rs index 3fa5e64f..36251eb8 100644 --- a/src/stores/client.rs +++ b/src/stores/client.rs @@ -14,6 +14,7 @@ pub struct Client { pub push_type: ProviderKind, #[sqlx(rename = "device_token")] pub token: String, + pub always_raw: bool, } #[async_trait] @@ -46,15 +47,22 @@ impl ClientStore for sqlx::PgPool { .await?; let mut insert_query = sqlx::QueryBuilder::new( - "INSERT INTO public.clients (id, tenant_id, push_type, device_token)", + "INSERT INTO public.clients (id, tenant_id, push_type, device_token, always_raw)", ); insert_query.push_values( - vec![(id, tenant_id, client.push_type, client.token)], + vec![( + id, + tenant_id, + client.push_type, + client.token, + client.always_raw, + )], |mut b, client| { b.push_bind(client.0) .push_bind(client.1) .push_bind(client.2) - .push_bind(client.3); + .push_bind(client.3) + .push_bind(client.4); }, ); insert_query.build().execute(&mut transaction).await?; @@ -65,8 +73,8 @@ impl ClientStore for sqlx::PgPool { async fn get_client(&self, tenant_id: &str, id: &str) -> stores::Result { let res = sqlx::query_as::( - "SELECT tenant_id, push_type, device_token FROM public.clients WHERE id = $1 and \ - tenant_id = $2", + "SELECT tenant_id, push_type, device_token, always_raw FROM public.clients WHERE id = \ + $1 and tenant_id = $2", ) .bind(id) .bind(tenant_id) diff --git a/tests/functional/singletenant/push.rs b/tests/functional/singletenant/push.rs index fc2714b5..ce41a65e 100644 --- a/tests/functional/singletenant/push.rs +++ b/tests/functional/singletenant/push.rs @@ -48,6 +48,7 @@ async fn create_client(ctx: &mut EchoServerContext) -> (ClientId, MockServer) { client_id: client_id.clone(), push_type: "noop".to_string(), token: token.clone(), + always_raw: Some(true), }; // Register client diff --git a/tests/functional/singletenant/registration.rs b/tests/functional/singletenant/registration.rs index 20bb8e26..a5251a6f 100644 --- a/tests/functional/singletenant/registration.rs +++ b/tests/functional/singletenant/registration.rs @@ -23,6 +23,7 @@ async fn test_registration(ctx: &mut EchoServerContext) { client_id: client_id.clone(), push_type: "noop".to_string(), token: "test".to_string(), + always_raw: Some(true), }; let jwt = relay_rpc::auth::AuthToken::new(client_id.value().clone()) @@ -54,6 +55,7 @@ async fn test_registration(ctx: &mut EchoServerContext) { client_id, push_type: "noop".to_string(), token: "new_token".to_string(), + always_raw: Some(true), }; let response = client .post(format!("http://{}/clients", ctx.server.public_addr)) @@ -91,6 +93,7 @@ async fn test_deregistration(ctx: &mut EchoServerContext) { client_id: client_id.clone(), push_type: "noop".to_string(), token: "test".to_string(), + always_raw: Some(true), }; let client = reqwest::Client::new(); diff --git a/tests/functional/stores/client.rs b/tests/functional/stores/client.rs index a420916a..e146e180 100644 --- a/tests/functional/stores/client.rs +++ b/tests/functional/stores/client.rs @@ -17,6 +17,7 @@ async fn client_creation(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Noop, token, + always_raw: true, }) .await .unwrap(); @@ -34,6 +35,7 @@ async fn client_creation_fcm(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Fcm, token, + always_raw: true, }) .await .unwrap(); @@ -51,6 +53,7 @@ async fn client_creation_apns(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Apns, token, + always_raw: true, }) .await .unwrap(); @@ -70,6 +73,7 @@ async fn client_upsert_token(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Fcm, token: token.clone(), + always_raw: false, }) .await .unwrap(); @@ -108,11 +112,13 @@ async fn client_upsert_token(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Apns, token: updated_token.clone(), + always_raw: true, }) .await .unwrap(); let updated_token_result = ctx.clients.get_client(TENANT_ID, &client_id).await.unwrap(); assert_eq!(updated_token_result.token, updated_token); + assert!(updated_token_result.always_raw); // Cleaning up records ctx.clients @@ -133,6 +139,7 @@ async fn client_upsert_id(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Fcm, token: token.clone(), + always_raw: true, }) .await .unwrap(); @@ -171,6 +178,7 @@ async fn client_upsert_id(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Fcm, token: token.clone(), + always_raw: true, }) .await .unwrap(); @@ -200,6 +208,7 @@ async fn client_create_same_id_and_token(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Fcm, token: token.clone(), + always_raw: true, }) .await .unwrap(); @@ -238,6 +247,7 @@ async fn client_create_same_id_and_token(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Noop, token: token.clone(), + always_raw: true, }) .await .unwrap(); @@ -263,6 +273,7 @@ async fn client_deletion(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Noop, token, + always_raw: true, }) .await .unwrap(); @@ -280,6 +291,7 @@ async fn client_fetch(ctx: &mut StoreContext) { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Noop, token: token.clone(), + always_raw: true, }) .await .unwrap(); diff --git a/tests/functional/stores/notification.rs b/tests/functional/stores/notification.rs index 4892c0b9..0e47151b 100644 --- a/tests/functional/stores/notification.rs +++ b/tests/functional/stores/notification.rs @@ -21,6 +21,7 @@ pub async fn create_client(client_store: &ClientStoreArc) -> String { tenant_id: TENANT_ID.to_string(), push_type: ProviderKind::Noop, token, + always_raw: true, }) .await .expect("failed to create client for notification test");