Skip to content

Commit

Permalink
feat: fcm verification (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryET committed Aug 1, 2023
1 parent 219b1ec commit d829e18
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
16 changes: 16 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let
nixpkgs = import <nixpkgs> {};
in
with nixpkgs;
stdenv.mkDerivation {
name = "echo-server";
buildInputs = [
cargo
rustc
pkgconfig
openssl.dev
nix
protobuf
];
OPENSSL_DEV=openssl.dev;
}
15 changes: 15 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ pub enum Error {

#[error("failed to load geoip database from s3")]
GeoIpS3Failed,

#[error("invalid fcm api key")]
BadFcmApiKey,
}

impl IntoResponse for Error {
Expand Down Expand Up @@ -198,6 +201,18 @@ impl IntoResponse for Error {
message: e.to_string(),
}
], vec![]),
Error::BadFcmApiKey => crate::handlers::Response::new_failure(StatusCode::BAD_REQUEST, vec![
ResponseError {
name: "bad_fcm_api_key".to_string(),
message: "The provided API Key was not valid".to_string(),
}
], vec![
ErrorField {
field: "api_key".to_string(),
description: "The provided API Key was not valid".to_string(),
location: ErrorLocation::Body,
}
]),
Error::Database(e) => crate::handlers::Response::new_failure(StatusCode::INTERNAL_SERVER_ERROR, vec![
ResponseError {
name: "sqlx".to_string(),
Expand Down
20 changes: 19 additions & 1 deletion src/handlers/update_fcm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use {
crate::{
error::{Error, Error::InvalidMultipartBody},
error::{
Error,
Error::{BadFcmApiKey, InvalidMultipartBody},
},
increment_counter,
state::AppState,
stores::tenant::TenantFcmUpdateParams,
Expand All @@ -9,6 +12,7 @@ use {
extract::{Multipart, Path, State},
Json,
},
fcm::FcmError,
serde::Serialize,
std::sync::Arc,
};
Expand Down Expand Up @@ -50,6 +54,20 @@ pub async fn handler(
return Err(InvalidMultipartBody);
}

// ---- checks
let fcm_api_key = body.api_key.clone();
let mut test_message_builder = fcm::MessageBuilder::new(&fcm_api_key, "wc-notification-test");
test_message_builder.dry_run(true);
let test_message = test_message_builder.finalize();
let test_notification = fcm::Client::new().send(test_message).await;
match test_notification {
Err(e) => match e {
FcmError::Unauthorized => Err(BadFcmApiKey),
_ => Ok(()),
},
Ok(_) => Ok(()),
}?;

// ---- handler
let update_body = TenantFcmUpdateParams {
fcm_api_key: body.api_key,
Expand Down
78 changes: 78 additions & 0 deletions tests/functional/multitenant/fcm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use {
crate::context::EchoServerContext,
echo_server::handlers::create_tenant::TenantRegisterBody,
random_string::generate,
test_context::test_context,
};

#[test_context(EchoServerContext)]
#[tokio::test]
async fn tenant_update_fcm(ctx: &mut EchoServerContext) {
let charset = "1234567890";
let random_tenant_id = generate(12, charset);
let payload = TenantRegisterBody {
id: random_tenant_id.clone(),
};

// Register tenant
let client = reqwest::Client::new();
let response = client
.post(format!("http://{}/tenants", ctx.server.public_addr))
.json(&payload)
.send()
.await
.expect("Call failed");

// Send valid API Key
let api_key = env!("ECHO_TEST_FCM_KEY");
let form = reqwest::multipart::Form::new().text("api_key", api_key);

let response = client
.post(format!(
"http://{}/tenants/{}/fcm",
ctx.server.public_addr, &random_tenant_id
))
.multipart(&form)
.send()
.await
.expect("Call failed");

assert!(
response.status().is_success(),
"Response was not successful"
);
}

#[test_context(EchoServerContext)]
#[tokio::test]
async fn tenant_update_fcm_bad(ctx: &mut EchoServerContext) {
let charset = "1234567890";
let random_tenant_id = generate(12, charset);
let payload = TenantRegisterBody {
id: random_tenant_id.clone(),
};

// Register tenant
let client = reqwest::Client::new();
let response = client
.post(format!("http://{}/tenants", ctx.server.public_addr))
.json(&payload)
.send()
.await
.expect("Call failed");

// Send invalid API Key
let form = reqwest::multipart::Form::new().text("api_key", "invalid-key");

let response = client
.post(format!(
"http://{}/tenants/{}/fcm",
ctx.server.public_addr, &random_tenant_id
))
.multipart(&form)
.send()
.await
.expect("Call failed");

assert!(!response.status().is_success(), "Response was successful");
}
1 change: 1 addition & 0 deletions tests/functional/multitenant/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Tests against the handlers
use {crate::context::EchoServerContext, test_context::test_context};

mod fcm;
mod tenancy;

#[test_context(EchoServerContext)]
Expand Down

0 comments on commit d829e18

Please sign in to comment.