Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello committed Apr 17, 2024
1 parent 94b26ea commit e6ea3fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion capture/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub async fn event(
Ok(token) => token,
Err(err) => {
report_dropped_events("token_shape_invalid", request.events().len() as u64);
return Err(err)
return Err(err);
}
};
let is_historical = request.is_historical();
Expand Down
54 changes: 31 additions & 23 deletions capture/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl RawRequest {
pub fn is_historical(&self) -> bool {
match self {
RawRequest::Batch(req) => req.historical_migration.unwrap_or_default(),
_ => false
_ => false,
}
}
}
Expand All @@ -148,9 +148,7 @@ pub fn extract_token(events: &[RawEvent]) -> Result<String, CaptureError> {
return match distinct_tokens.len() {
0 => Err(CaptureError::NoTokenError),
1 => match distinct_tokens.iter().last() {
Some(Some(token)) => {
Ok(token.clone())
}
Some(Some(token)) => Ok(token.clone()),
_ => Err(CaptureError::NoTokenError),
},
_ => Err(CaptureError::MultipleTokensError),
Expand Down Expand Up @@ -223,12 +221,12 @@ impl ProcessedEvent {

#[cfg(test)]
mod tests {
use crate::token::InvalidTokenReason;
use base64::Engine as _;
use bytes::Bytes;
use rand::distributions::Alphanumeric;
use rand::Rng;
use serde_json::json;
use crate::token::InvalidTokenReason;

use super::CaptureError;
use super::RawRequest;
Expand All @@ -242,7 +240,9 @@ mod tests {
.expect("payload is not base64"),
);

let events = RawRequest::from_bytes(compressed_bytes).expect("failed to parse").events();
let events = RawRequest::from_bytes(compressed_bytes)
.expect("failed to parse")
.events();
assert_eq!(1, events.len());
assert_eq!(Some("my_token1".to_string()), events[0].extract_token());
assert_eq!("my_event1".to_string(), events[0].event);
Expand All @@ -262,7 +262,9 @@ mod tests {
.expect("payload is not base64"),
);

let events = RawRequest::from_bytes(compressed_bytes).expect("failed to parse").events();
let events = RawRequest::from_bytes(compressed_bytes)
.expect("failed to parse")
.events();
assert_eq!(1, events.len());
assert_eq!(Some("my_token2".to_string()), events[0].extract_token());
assert_eq!("my_event2".to_string(), events[0].event);
Expand All @@ -277,7 +279,9 @@ mod tests {
#[test]
fn extract_distinct_id() {
let parse_and_extract = |input: &'static str| -> Result<String, CaptureError> {
let parsed = RawRequest::from_bytes(input.into()).expect("failed to parse").events();
let parsed = RawRequest::from_bytes(input.into())
.expect("failed to parse")
.events();
parsed[0].extract_distinct_id()
};
// Return MissingDistinctId if not found
Expand Down Expand Up @@ -338,7 +342,9 @@ mod tests {
"distinct_id": distinct_id
}]);

let parsed = RawRequest::from_bytes(input.to_string().into()).expect("failed to parse").events();
let parsed = RawRequest::from_bytes(input.to_string().into())
.expect("failed to parse")
.events();
assert_eq!(
parsed[0].extract_distinct_id().expect("failed to extract"),
expected_distinct_id
Expand All @@ -348,7 +354,9 @@ mod tests {
#[test]
fn extract_and_verify_token() {
let parse_and_extract = |input: &'static str| -> Result<String, CaptureError> {
RawRequest::from_bytes(input.into()).expect("failed to parse").extract_and_verify_token()
RawRequest::from_bytes(input.into())
.expect("failed to parse")
.extract_and_verify_token()
};

let assert_extracted_token = |input: &'static str, expected: &str| {
Expand All @@ -365,41 +373,41 @@ mod tests {
// Return TokenValidationError if token empty
assert!(matches!(
parse_and_extract(r#"{"api_key": "", "batch":[{"event": "e"}]}"#),
Err(CaptureError::TokenValidationError(InvalidTokenReason::Empty))
Err(CaptureError::TokenValidationError(
InvalidTokenReason::Empty
))
));

// Return TokenValidationError if personal apikey
assert!(matches!(
parse_and_extract(r#"[{"event": "e", "token": "phx_hellothere"}]"#),
Err(CaptureError::TokenValidationError(InvalidTokenReason::PersonalApiKey))
Err(CaptureError::TokenValidationError(
InvalidTokenReason::PersonalApiKey
))
));

// Return MultipleTokensError if tokens don't match in array
assert!(matches!(
parse_and_extract(r#"[{"event": "e", "token": "token1"},{"event": "e", "token": "token2"}]"#),
parse_and_extract(
r#"[{"event": "e", "token": "token1"},{"event": "e", "token": "token2"}]"#
),
Err(CaptureError::MultipleTokensError)
));

// Return token from array if consistent
assert_extracted_token(
r#"[{"event":"e","token":"token1"},{"event":"e","token":"token1"}]"#,
"token1"
"token1",
);

// Return token from batch if present
assert_extracted_token(
r#"{"batch":[{"event":"e","token":"token1"}],"api_key":"batched"}"#,
"batched"
"batched",
);

// Return token from single event if present
assert_extracted_token(
r#"{"event":"e","$token":"single_token"}"#,
"single_token"
);
assert_extracted_token(
r#"{"event":"e","api_key":"single_token"}"#,
"single_token"
);
assert_extracted_token(r#"{"event":"e","$token":"single_token"}"#, "single_token");
assert_extracted_token(r#"{"event":"e","api_key":"single_token"}"#, "single_token");
}
}

0 comments on commit e6ea3fe

Please sign in to comment.