async def _do_bootstrap_organization( config_dir, password: str, password_check: str, human_handle: HumanHandle, device_label: str, bootstrap_addr: BackendOrganizationBootstrapAddr, ): if password != password_check: raise JobResultError("password-mismatch") if len(password) < 8: raise JobResultError("password-size") try: async with apiv1_backend_anonymous_cmds_factory( addr=bootstrap_addr) as cmds: new_device = await bootstrap_organization( cmds=cmds, human_handle=human_handle, device_label=device_label) save_device_with_password(config_dir, new_device, password) return new_device, password except InviteNotFoundError as exc: raise JobResultError("not-found", info=str(exc)) from exc except InviteAlreadyUsedError as exc: raise JobResultError("already-bootstrapped", info=str(exc)) from exc except BackendConnectionRefused as exc: raise JobResultError("invalid-url", info=str(exc)) from exc except BackendNotAvailable as exc: raise JobResultError("backend-offline", info=str(exc)) from exc except BackendConnectionError as exc: raise JobResultError("refused-by-backend", info=str(exc)) from exc
async def test_handshake_unknown_organization(running_backend, coolorg): unknown_org_addr = BackendOrganizationAddr.build( backend_addr=coolorg.addr, organization_id="dummy", root_verify_key=coolorg.root_verify_key) with pytest.raises(BackendConnectionRefused) as exc: async with apiv1_backend_anonymous_cmds_factory( unknown_org_addr) as cmds: await cmds.ping() assert str(exc.value) == "Invalid handshake information"
async def test_good(running_backend, backend, alice, bob, alice_backend_cmds, user_fs_factory, with_labels): org_id = OrganizationID("NewOrg") org_token = "123456" await backend.organization.create(org_id, org_token) organization_addr = BackendOrganizationBootstrapAddr.build( running_backend.addr, org_id, org_token) if with_labels: human_handle = HumanHandle(email="*****@*****.**", label="Zack") device_label = "PC1" else: human_handle = None device_label = None async with apiv1_backend_anonymous_cmds_factory( addr=organization_addr) as cmds: new_device = await bootstrap_organization(cmds, human_handle=human_handle, device_label=device_label) assert new_device is not None assert new_device.organization_id == org_id assert new_device.device_label == device_label assert new_device.human_handle == human_handle assert new_device.profile == UserProfile.ADMIN # Test the behavior of this new device async with user_fs_factory(new_device, initialize_in_v0=True) as newfs: await newfs.workspace_create("wa") await newfs.sync() # Test the device in correct in the backend backend_user, backend_device = await backend.user.get_user_with_device( org_id, new_device.device_id) assert backend_user.user_id == new_device.user_id assert backend_user.human_handle == new_device.human_handle assert backend_user.profile == new_device.profile assert backend_user.user_certifier is None if with_labels: assert backend_user.user_certificate != backend_user.redacted_user_certificate else: assert backend_user.user_certificate == backend_user.redacted_user_certificate assert backend_device.device_id == new_device.device_id assert backend_device.device_label == new_device.device_label assert backend_device.device_certifier is None if with_labels: assert backend_device.device_certificate != backend_device.redacted_device_certificate else: assert backend_device.device_certificate == backend_device.redacted_device_certificate
async def test_handshake_rvk_mismatch(running_backend, coolorg, otherorg): bad_rvk_org_addr = BackendOrganizationAddr.build( backend_addr=coolorg.addr, organization_id=coolorg.organization_id, root_verify_key=otherorg.root_verify_key, ) with pytest.raises(BackendConnectionRefused) as exc: async with apiv1_backend_anonymous_cmds_factory( bad_rvk_org_addr) as cmds: await cmds.ping() assert str( exc.value ) == "Root verify key for organization differs between client and server"
async def test_invalid_token(running_backend, backend): org_id = OrganizationID("NewOrg") old_token = "123456" new_token = "abcdef" await backend.organization.create(org_id, old_token) await backend.organization.create(org_id, new_token) organization_addr = BackendOrganizationBootstrapAddr.build( running_backend.addr, org_id, old_token) async with apiv1_backend_anonymous_cmds_factory( addr=organization_addr) as cmds: with pytest.raises(InviteNotFoundError): await bootstrap_organization(cmds, human_handle=None, device_label=None)
async def test_already_bootstrapped(running_backend, backend, alice, bob, alice_backend_cmds, user_fs_factory): org_id = OrganizationID("NewOrg") org_token = "123456" await backend.organization.create(org_id, org_token) organization_addr = BackendOrganizationBootstrapAddr.build( running_backend.addr, org_id, org_token) async with apiv1_backend_anonymous_cmds_factory( addr=organization_addr) as cmds: await bootstrap_organization(cmds, human_handle=None, device_label=None) with pytest.raises(InviteAlreadyUsedError): await bootstrap_organization(cmds, human_handle=None, device_label=None)
async def initialize_test_organization( config_dir: Path, backend_address: BackendAddr, password: str, administration_token: str, force: bool, additional_users_number: int, additional_devices_number: int, ) -> Tuple[LocalDevice, LocalDevice, LocalDevice]: configure_logging("WARNING") organization_id = OrganizationID("Org") # Create organization async with apiv1_backend_administration_cmds_factory( backend_address, administration_token) as administration_cmds: rep = await administration_cmds.organization_create(organization_id) assert rep["status"] == "ok" bootstrap_token = rep["bootstrap_token"] organization_bootstrap_addr = BackendOrganizationBootstrapAddr.build( backend_address, organization_id, bootstrap_token) # Bootstrap organization and Alice user and create device "laptop" for Alice async with apiv1_backend_anonymous_cmds_factory( organization_bootstrap_addr) as anonymous_cmds: alice_device = await bootstrap_organization( cmds=anonymous_cmds, human_handle=HumanHandle(label="Alice", email="*****@*****.**"), device_label="laptop", ) save_device_with_password(config_dir, alice_device, password, force=force) config = load_config(config_dir, debug="DEBUG" in os.environ) # Create context manager, alice_client will be needed for the rest of the script async with logged_client_factory(config, alice_device) as alice_client: async with backend_authenticated_cmds_factory( addr=alice_device.organization_addr, device_id=alice_device.device_id, signing_key=alice_device.signing_key, ) as alice_cmds: # Create new device "pc" for Alice other_alice_device = await _register_new_device( cmds=alice_cmds, author=alice_device, device_label="pc") save_device_with_password(config_dir, other_alice_device, password, force=force) # Add additional random device for alice if additional_devices_number > 0: print( f"Adding {additional_devices_number} devices in the test group" ) print(" ... please wait ...") await _add_random_device( cmds=alice_cmds, config_dir=config_dir, force=force, password=password, device=alice_device, additional_devices_number=additional_devices_number, ) print(" Done ") # Invite Bob in organization bob_device = await _register_new_user( cmds=alice_cmds, author=alice_device, device_label="laptop", human_handle=HumanHandle(email="*****@*****.**", label="Bob"), profile=UserProfile.STANDARD, ) save_device_with_password(config_dir, bob_device, password, force=force) # Create Alice workspace alice_ws_id = await alice_client.user_fs.workspace_create( "alice_workspace") # Create context manager async with logged_client_factory(config, bob_device) as bob_client: # Create Bob workspace bob_ws_id = await bob_client.user_fs.workspace_create( "bob_workspace") # Bob share workspace with Alice await bob_client.user_fs.workspace_share( bob_ws_id, alice_device.user_id, WorkspaceRole.MANAGER) # Alice share workspace with Bob await alice_client.user_fs.workspace_share( alice_ws_id, bob_device.user_id, WorkspaceRole.MANAGER) # Add additional random users if additional_users_number > 0: print( f"Adding {additional_users_number} users in the test group" ) print(" ... please wait ...") await _add_random_users( cmds=alice_cmds, author=alice_device, alice_client=alice_client, bob_client=bob_client, alice_ws_id=alice_ws_id, bob_ws_id=bob_ws_id, additional_users_number=additional_users_number, ) print(" Done ") # Synchronize every device for device in (alice_device, other_alice_device, bob_device): async with logged_client_factory(config, device) as client: await client.user_fs.process_last_messages() await client.user_fs.sync() return (alice_device, other_alice_device, bob_device)
async def test_anonymous_cmds_has_right_methods(running_backend, coolorg): async with apiv1_backend_anonymous_cmds_factory(coolorg.addr) as cmds: for method_name in APIV1_ANONYMOUS_CMDS: assert hasattr(cmds, method_name) for method_name in ALL_CMDS - APIV1_ANONYMOUS_CMDS: assert not hasattr(cmds, method_name)
async def test_handshake_organization_expired(running_backend, expiredorg): with pytest.raises(BackendConnectionRefused) as exc: async with apiv1_backend_anonymous_cmds_factory( expiredorg.addr) as cmds: await cmds.ping() assert str(exc.value) == "Trial organization has expired"
async def test_ping(running_backend, coolorg): async with apiv1_backend_anonymous_cmds_factory(coolorg.addr) as cmds: rep = await cmds.ping("Hello World !") assert rep == {"status": "ok", "pong": "Hello World !"}
async def test_backend_closed_cmds(running_backend, coolorg): async with apiv1_backend_anonymous_cmds_factory(coolorg.addr) as cmds: pass with pytest.raises(trio.ClosedResourceError): await cmds.ping()
async def test_backend_switch_offline(running_backend, coolorg): async with apiv1_backend_anonymous_cmds_factory(coolorg.addr) as cmds: await cmds.ping() with running_backend.offline(): with pytest.raises(BackendNotAvailable): await cmds.ping()
def _cmds_factory(keepalive): return apiv1_backend_anonymous_cmds_factory(coolorg.addr, keepalive=keepalive)
async def apiv1_anonymous_backend_cmds(running_backend, coolorg): async with apiv1_backend_anonymous_cmds_factory(coolorg.addr) as cmds: yield cmds