Skip to content

Commit

Permalink
add tests samples
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Oct 5, 2024
1 parent 10ae438 commit 4dbab80
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion inngest/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<T, E> Handler<T, E> {
};

let signature = Signature::new(&sig, &key, &body);
if let Err(err) = signature.verify() {
if let Err(err) = signature.verify(false) {
return Err(err);
}
}
Expand Down
88 changes: 87 additions & 1 deletion inngest/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,94 @@ impl Signature {
}
}

fn hash(key: &str) -> String {
"".to_string()
}

// TODO: implement signature validation
pub fn verify(&self) -> Result<(), Error> {
pub fn verify(&self, ignore_ts: bool) -> Result<(), Error> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

const SIGNING_KEY: &str =
"signkey-test-8ee2262a15e8d3c42d6a840db7af3de2aab08ef632b32a37a687f24b34dba3ff";
const HASHED_SIGNING_KEY: &str =
"signkey-test-e4bf4a2e7f55c7eb954b6e72f8f69628fbc409fe7da6d0f6958770987dcf0e02";
const SIGNATURE: &str =
"t=1689920619&s=31df77f5b1b029de4bfce3a77e0517aa4ce0f5e2195a6467fc126a489ca2330b";

#[test]
fn test_hashed_signing_key() {
let hashed = Signature::hash(SIGNING_KEY);
assert_eq!(hashed, HASHED_SIGNING_KEY);
}

fn event() -> Value {
json!({
"id": "",
"name": "inngest/scheduled.timer",
"data": {},
"user": {},
"ts": 1_674_082_830_001 as i64,
"v": "1"
})
}

fn body() -> Value {
let evt = event();

json!({
"ctx": {
"fn_id": "local-testing-local-cron",
"run_id": "01GQ3HTEZ01M7R8Z9PR1DMHDN1",
"step_id": "step"
},
"event": &evt,
"events": [&evt],
"steps": {},
"use_api": false
})
}

#[test]
fn test_verify_if_signature_is_valid() {
let body = body();
let sig = Signature::new(SIGNATURE, SIGNING_KEY, &body);
let res = sig.verify(true);
assert!(res.is_ok());
}

#[ignore]
#[test]
fn test_verify_if_signature_is_expired() {
let body = body();
let sig = Signature::new(SIGNATURE, SIGNING_KEY, &body);
let res = sig.verify(false);
assert!(res.is_err());
}

#[ignore]
#[test]
fn test_verify_if_signature_is_invalid() {
let body = body();
let invalid_sig = format!("{}hello", SIGNATURE);
let sig = Signature::new(&invalid_sig, SIGNING_KEY, &body);
let res = sig.verify(true);
assert!(res.is_err());
}

#[ignore]
#[test]
fn test_verify_for_random_input() {
let body = body();
let sig = Signature::new("10", SIGNING_KEY, &body);
let res = sig.verify(true);
assert!(res.is_err());
}
}

0 comments on commit 4dbab80

Please sign in to comment.