test: add serde serialization roundtrip tests#140
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (3)
WalkthroughThree test modules are added across related JWK crates to validate serde serialization and deserialization roundtrips. Tests comprehensively cover JSON structure integrity, deterministic ordering, field naming consistency, and type preservation for multiple JWK variants and the JwksBuilder. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/uselesskey-core-jwk-shape/tests/serde_roundtrip.rs`:
- Around line 348-361: Replace the single loop-based test function
public_jwk_all_variants_roundtrip_via_value with parameterized rstest cases so
each PublicJwk variant is its own test case: create separate rstest parameters
that supply PublicJwk::Rsa(rsa_public("rsa")), PublicJwk::Ec(ec_public("ec")),
and PublicJwk::Okp(okp_public("okp")) to a test function that performs the same
serde_json to_string/from_str and to_value assertions and uses jwk.kid() in the
assert message; ensure you add the rstest attribute and import so failures are
reported per-variant instead of as one loop.
- Around line 519-556: The test
all_concrete_jwk_types_deterministic_serialization manually builds a Vec of
serialized pairs for each JWK constructor (rsa_public, rsa_private, ec_public,
ec_private, okp_public, okp_private, oct_jwk); refactor it to an
rstest-parameterized test that takes each constructor name/value as a case,
import and use #[rstest] (and the rstest::rstest macro) to supply the 7 inputs,
then inside the body call serde_json::to_string twice for the given constructor
and assert_eq! the results. Keep references to the original helper functions
(rsa_public, rsa_private, ec_public, ec_private, okp_public, okp_private,
oct_jwk) as the sources for test values and remove the manual Vec-based loop.
ℹ️ Review info
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (3)
crates/uselesskey-core-jwk-builder/tests/serde_roundtrip.rscrates/uselesskey-core-jwk-shape/tests/serde_roundtrip.rscrates/uselesskey-core-jwk/tests/serde_roundtrip.rs
| fn public_jwk_all_variants_roundtrip_via_value() { | ||
| let variants: Vec<PublicJwk> = vec![ | ||
| PublicJwk::Rsa(rsa_public("rsa")), | ||
| PublicJwk::Ec(ec_public("ec")), | ||
| PublicJwk::Okp(okp_public("okp")), | ||
| ]; | ||
|
|
||
| for jwk in &variants { | ||
| let json_str = serde_json::to_string(jwk).unwrap(); | ||
| let parsed: Value = serde_json::from_str(&json_str).unwrap(); | ||
| let direct = serde_json::to_value(jwk).unwrap(); | ||
| assert_eq!(parsed, direct, "roundtrip failed for kid={}", jwk.kid()); | ||
| } | ||
| } |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
Loop-based variant testing works but could use rstest for test isolation.
The loop approach produces a single test that iterates over variants. With rstest, each variant would be a separate test case, providing better failure isolation and clearer CI output. This is optional since the current implementation works correctly and includes meaningful error messages.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@crates/uselesskey-core-jwk-shape/tests/serde_roundtrip.rs` around lines 348 -
361, Replace the single loop-based test function
public_jwk_all_variants_roundtrip_via_value with parameterized rstest cases so
each PublicJwk variant is its own test case: create separate rstest parameters
that supply PublicJwk::Rsa(rsa_public("rsa")), PublicJwk::Ec(ec_public("ec")),
and PublicJwk::Okp(okp_public("okp")) to a test function that performs the same
serde_json to_string/from_str and to_value assertions and uses jwk.kid() in the
assert message; ensure you add the rstest attribute and import so failures are
reported per-variant instead of as one loop.
| #[test] | ||
| fn all_concrete_jwk_types_deterministic_serialization() { | ||
| // Test each concrete type produces identical output on repeated serialization | ||
| let json_pairs: Vec<(String, String)> = vec![ | ||
| ( | ||
| serde_json::to_string(&rsa_public("det")).unwrap(), | ||
| serde_json::to_string(&rsa_public("det")).unwrap(), | ||
| ), | ||
| ( | ||
| serde_json::to_string(&rsa_private("det")).unwrap(), | ||
| serde_json::to_string(&rsa_private("det")).unwrap(), | ||
| ), | ||
| ( | ||
| serde_json::to_string(&ec_public("det")).unwrap(), | ||
| serde_json::to_string(&ec_public("det")).unwrap(), | ||
| ), | ||
| ( | ||
| serde_json::to_string(&ec_private("det")).unwrap(), | ||
| serde_json::to_string(&ec_private("det")).unwrap(), | ||
| ), | ||
| ( | ||
| serde_json::to_string(&okp_public("det")).unwrap(), | ||
| serde_json::to_string(&okp_public("det")).unwrap(), | ||
| ), | ||
| ( | ||
| serde_json::to_string(&okp_private("det")).unwrap(), | ||
| serde_json::to_string(&okp_private("det")).unwrap(), | ||
| ), | ||
| ( | ||
| serde_json::to_string(&oct_jwk("det")).unwrap(), | ||
| serde_json::to_string(&oct_jwk("det")).unwrap(), | ||
| ), | ||
| ]; | ||
|
|
||
| for (first, second) in &json_pairs { | ||
| assert_eq!(first, second, "deterministic serialization failed"); | ||
| } | ||
| } |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
Consider using rstest for parameterized deterministic serialization tests.
This test creates pairs manually for each JWK type. Per coding guidelines, rstest is recommended for parameterized tests, which would make this more maintainable:
♻️ Suggested refactor using rstest
+use rstest::rstest;
+
+#[rstest]
+#[case::rsa_public(|| serde_json::to_string(&rsa_public("det")).unwrap())]
+#[case::rsa_private(|| serde_json::to_string(&rsa_private("det")).unwrap())]
+#[case::ec_public(|| serde_json::to_string(&ec_public("det")).unwrap())]
+#[case::ec_private(|| serde_json::to_string(&ec_private("det")).unwrap())]
+#[case::okp_public(|| serde_json::to_string(&okp_public("det")).unwrap())]
+#[case::okp_private(|| serde_json::to_string(&okp_private("det")).unwrap())]
+#[case::oct(|| serde_json::to_string(&oct_jwk("det")).unwrap())]
+fn jwk_type_deterministic_serialization<F: Fn() -> String>(#[case] serializer: F) {
+ let first = serializer();
+ let second = serializer();
+ assert_eq!(first, second, "deterministic serialization failed");
+}As per coding guidelines: "Use rstest for parameterized tests".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@crates/uselesskey-core-jwk-shape/tests/serde_roundtrip.rs` around lines 519 -
556, The test all_concrete_jwk_types_deterministic_serialization manually builds
a Vec of serialized pairs for each JWK constructor (rsa_public, rsa_private,
ec_public, ec_private, okp_public, okp_private, oct_jwk); refactor it to an
rstest-parameterized test that takes each constructor name/value as a case,
import and use #[rstest] (and the rstest::rstest macro) to supply the 7 inputs,
then inside the body call serde_json::to_string twice for the given constructor
and assert_eq! the results. Keep references to the original helper functions
(rsa_public, rsa_private, ec_public, ec_private, okp_public, okp_private,
oct_jwk) as the sources for test values and remove the manual Vec-based loop.
c313a10 to
41a1a50
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
41a1a50 to
05ed6a9
Compare
Adds 51 serde serialization roundtrip tests for JWK shape types across three crates: uselesskey-core-jwk-shape (39 tests), uselesskey-core-jwk-builder (8 tests), and uselesskey-core-jwk (4 tests). Tests cover JSON structure, deterministic serialization, roundtrip via Value, untagged enum serialization, serde rename attributes, malformed input handling, and builder ordering.