Skip to content

refactor(test): Extract common crypto mock server helper #5005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2025

Conversation

BillCarsonFr
Copy link
Member

@BillCarsonFr BillCarsonFr commented May 6, 2025

Draft because it is a follow up of #4998
Extracted a helper to create a wiremock server that allows to perform full end to end encryption tests.

Note: In the existing verification test there was a known_devices list, but this was not used (the test work fine if you remove it), so I removed it when extracting the mock helpers

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@BillCarsonFr BillCarsonFr changed the base branch from main to valere/crypto/to_device_encrypt_helper May 6, 2025 08:55
Comment on lines 92 to 101
pub struct MatrixKeysMockServer {
/// The underlying [`wiremock`] [`MockServer`]
pub server: MockServer,
keys: Arc<Mutex<Keys>>,
token_to_user_id_map: Arc<Mutex<BTreeMap<String, OwnedUserId>>>,
token_counter: AtomicU32,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really incorporate the Keys struct into MatrixMockServer instead it creates another mock server concept with much less functionality.

I don't think we'd want to have two of those.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I did refactor it to extend the existing MockServer 9766ca1
I kept the crypto API mocking in the new file for code organisation, Is it ok?

I did a second commit to take benefit of the refactoring and re-use a functionnality already in the base mock server instead of using another custom method fcb9070

Base automatically changed from valere/crypto/to_device_encrypt_helper to main May 19, 2025 11:20
@BillCarsonFr BillCarsonFr force-pushed the valere/crypto/extract_crypto_mockserver_helper branch from 50829d3 to cf13d30 Compare May 19, 2025 12:45
Copy link

codecov bot commented May 19, 2025

Codecov Report

Attention: Patch coverage is 76.35468% with 48 lines in your changes missing coverage. Please review.

Project coverage is 85.76%. Comparing base (21de891) to head (fcb9070).
Report is 253 commits behind head on main.

Files with missing lines Patch % Lines
...ates/matrix-sdk/src/test_utils/mocks/encryption.rs 73.18% 48 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5005      +/-   ##
==========================================
- Coverage   85.82%   85.76%   -0.06%     
==========================================
  Files         325      326       +1     
  Lines       35984    36183     +199     
==========================================
+ Hits        30883    31034     +151     
- Misses       5101     5149      +48     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BillCarsonFr BillCarsonFr marked this pull request as ready for review June 4, 2025 07:12
@BillCarsonFr BillCarsonFr requested a review from a team as a code owner June 4, 2025 07:12
@BillCarsonFr BillCarsonFr requested review from andybalaam and poljar and removed request for a team June 4, 2025 07:12
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have preferred some doc examples on the individual methods instead on the impl block which won't be shown anywhere besides in the source as far as I know.

Some renaming wouldn't have hurt either. I think we can address that in separate PRs so we don't block your stack of PRs on this for too long.

request.event_type.clone(),
request.txn_id.clone(),
request.messages.clone(),
);

let send_result = self
.client
.send_inner(request, Some(RequestConfig::short_retry()), Default::default())
.send_inner(ruma_request, Some(RequestConfig::short_retry()), Default::default())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this added by accident? It's not really topical to the PR.

Comment on lines +45 to +65
/// Extends the `MatrixMockServer` with useful methods to help mocking
/// matrix crypto API and perform integration test with encryption.
///
/// It implements mock endpoints for the `keys/upload`, will store the uploaded
/// devices and serves them back for incoming `keys/query`. It is also storing
/// and claiming one-time-keys, allowing to set up working olm sessions.
///
/// Adds some helpers like `exhaust_one_time_keys` that allows to simulate a
/// client running out of otks. More can be added if needed later.
///
/// It works like this:
/// * Start by creating the mock server like this [`MatrixMockServer::new`].
/// * Then mock the crypto API endpoints
/// [`MatrixMockServer::mock_crypto_endpoints_preset`].
/// * Create your test client using
/// [`MatrixMockServer::client_builder_for_crypto_end_to_end`], this is
/// important as it will set up an access token that will allow to know what
/// client is doing what request.
///
/// The [`MatrixMockServer::set_up_alice_and_bob_for_encryption`] will set up
/// two olm machines aware of each other and ready to communicate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't get rendered anywhere, right?

@poljar poljar merged commit f7265c3 into main Jun 5, 2025
48 of 49 checks passed
@poljar poljar deleted the valere/crypto/extract_crypto_mockserver_helper branch June 5, 2025 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants