Skip to main content

bitwarden_encrypted_migrator/
encrypted_migrator_client.rs

1use bitwarden_core::{Client, FromClient};
2
3use crate::migration::{Migration, run_migrations};
4
5/// Runs the encrypted migrations of the account.
6#[bitwarden_ffi::wasm_object]
7pub struct EncryptedMigratorClient {
8    migrations: Vec<Migration>,
9}
10
11impl FromClient for EncryptedMigratorClient {
12    fn from_client(client: &Client) -> Self {
13        Self {
14            migrations: Migration::all(client),
15        }
16    }
17}
18
19#[bitwarden_ffi::wasm_export]
20impl EncryptedMigratorClient {
21    /// Runs each migration once, in registration order. A failed migration does not stop the
22    /// others.
23    ///
24    /// Two runs at the same time can send the same changes to the server, so the caller must not
25    /// start a run before the previous run ends.
26    pub async fn run_migrations(&self) {
27        run_migrations(&self.migrations).await;
28    }
29}
30
31/// Extension trait that exposes [`EncryptedMigratorClient`] on [`Client`].
32pub trait EncryptedMigratorClientExt {
33    /// Returns an [`EncryptedMigratorClient`].
34    fn encrypted_migrator(&self) -> EncryptedMigratorClient;
35}
36
37impl EncryptedMigratorClientExt for Client {
38    fn encrypted_migrator(&self) -> EncryptedMigratorClient {
39        EncryptedMigratorClient::from_client(self)
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use bitwarden_organizations::OrganizationUserType;
46
47    use super::*;
48    use crate::migrations::account_recovery_v2_upgrade::tests::{
49        client, expect_no_pending, organization,
50    };
51
52    /// The mock checks that the migration sent its request once.
53    #[tokio::test]
54    async fn test_run_migrations_runs_each_migration() {
55        let owner = organization(OrganizationUserType::Owner);
56        let owner_id = owner.details.id;
57        let client = client(&[owner], &[owner_id], |mock| {
58            expect_no_pending(mock, owner_id);
59        })
60        .await;
61
62        client.encrypted_migrator().run_migrations().await;
63    }
64}