Skip to content

Commit

Permalink
feat(decrypted_notify): adding always_raw for the client registrati…
Browse files Browse the repository at this point in the history
…on (#279)
  • Loading branch information
geekbrother committed Nov 21, 2023
1 parent c442c03 commit 16a377a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions migrations/1699982551_add_always_raw_to_clients.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE public.clients
ADD COLUMN always_raw BOOLEAN DEFAULT FALSE;
2 changes: 2 additions & 0 deletions src/handlers/register_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct RegisterBody {
#[serde(rename = "type")]
pub push_type: String,
pub token: String,
pub always_raw: Option<bool>,
}

pub async fn handler(
Expand Down Expand Up @@ -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?;

Expand Down
18 changes: 13 additions & 5 deletions src/stores/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Client {
pub push_type: ProviderKind,
#[sqlx(rename = "device_token")]
pub token: String,
pub always_raw: bool,
}

#[async_trait]
Expand Down Expand Up @@ -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?;
Expand All @@ -65,8 +73,8 @@ impl ClientStore for sqlx::PgPool {

async fn get_client(&self, tenant_id: &str, id: &str) -> stores::Result<Client> {
let res = sqlx::query_as::<sqlx::postgres::Postgres, Client>(
"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)
Expand Down
1 change: 1 addition & 0 deletions tests/functional/singletenant/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(false),
};

// Register client
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/singletenant/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(false),
};

let jwt = relay_rpc::auth::AuthToken::new(client_id.value().clone())
Expand Down Expand Up @@ -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(false),
};
let response = client
.post(format!("http://{}/clients", ctx.server.public_addr))
Expand Down Expand Up @@ -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(false),
};

let client = reqwest::Client::new();
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/stores/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn client_creation(ctx: &mut StoreContext) {
tenant_id: TENANT_ID.to_string(),
push_type: ProviderKind::Noop,
token,
always_raw: false,
})
.await
.unwrap();
Expand All @@ -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: false,
})
.await
.unwrap();
Expand All @@ -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: false,
})
.await
.unwrap();
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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: false,
})
.await
.unwrap();
Expand Down Expand Up @@ -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: false,
})
.await
.unwrap();
Expand Down Expand Up @@ -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: false,
})
.await
.unwrap();
Expand Down Expand Up @@ -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: false,
})
.await
.unwrap();
Expand All @@ -263,6 +273,7 @@ async fn client_deletion(ctx: &mut StoreContext) {
tenant_id: TENANT_ID.to_string(),
push_type: ProviderKind::Noop,
token,
always_raw: false,
})
.await
.unwrap();
Expand All @@ -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: false,
})
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions tests/functional/stores/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: false,
})
.await
.expect("failed to create client for notification test");
Expand Down

0 comments on commit 16a377a

Please sign in to comment.