async def alice_invite(running_backend, backend, alice): device_id = DeviceID("Zack@pc1") # Modify address subdomain to be able to switch it offline whithout # disconnecting the inviter organization_addr = addr_with_device_subdomain(alice.organization_addr, device_id) invitation = { "addr": BackendOrganizationClaimUserAddr.build(organization_addr, "Zack", "123456"), "token": "123456", "user_id": device_id.user_id, "device_name": device_id.device_name, "password": "******", } async def _invite(): await invite_and_create_user(alice, invitation["user_id"], invitation["token"], True) async with trio.open_service_nursery() as nursery: with backend.event_bus.listen() as spy: nursery.start_soon(_invite) await spy.wait_with_timeout("event.connected", {"event_name": "user.claimed"}) yield invitation nursery.cancel_scope.cancel()
def _local_device_factory( base_device_id: Optional[str] = None, org: OrganizationFullData = coolorg, is_admin: bool = None, ): nonlocal count if not base_device_id: count += 1 base_device_id = f"user{count}@dev0" org_devices = devices[org.organization_id] device_id = DeviceID(base_device_id) assert not any(d for d in org_devices if d.device_id == device_id) parent_device = None try: # If the user already exists, we must retreive it data parent_device = next(d for d in org_devices if d.user_id == device_id.user_id) if is_admin is not None and is_admin is not parent_device.is_admin: raise ValueError( "is_admin is set but user already exists, with a different is_admin value." ) is_admin = parent_device.is_admin except StopIteration: is_admin = bool(is_admin) # Force each device to access the backend trough a different hostname so # tcp stream spy can switch offline certains while keeping the others online org_addr = addr_with_device_subdomain(org.addr, device_id) device = generate_new_device(device_id, org_addr, is_admin=is_admin) if parent_device is not None: device = device.evolve( private_key=parent_device.private_key, user_manifest_id=parent_device.user_manifest_id, user_manifest_key=parent_device.user_manifest_key, ) org_devices.append(device) return device
def _offline_for(device_id): return offline(addr_with_device_subdomain(server.addr, device_id))
def _local_device_factory( base_device_id: Optional[str] = None, org: OrganizationFullData = coolorg, profile: Optional[UserProfile] = None, has_human_handle: bool = True, base_human_handle: Optional[str] = None, has_device_label: bool = True, base_device_label: Optional[str] = None, ): nonlocal count if not base_device_id: count += 1 base_device_id = f"user{count}@dev0" org_devices = devices[org.organization_id] device_id = DeviceID(base_device_id) assert not any(d for d in org_devices if d.device_id == device_id) if not has_device_label: assert base_device_label is None device_label = None elif not base_device_label: device_label = f"My {device_id.device_name} machine" else: device_label = base_device_label if not has_human_handle: assert base_human_handle is None human_handle = None elif base_human_handle: if isinstance(base_human_handle, HumanHandle): human_handle = base_human_handle else: match = re.match(r"(.*) <(.*)>", base_human_handle) if match: label, email = match.groups() else: label = base_human_handle email = f"{device_id.user_id}@example.com" human_handle = HumanHandle(email=email, label=label) else: name = device_id.user_id.capitalize() human_handle = HumanHandle( email=f"{device_id.user_id}@example.com", label=f"{name}y Mc{name}Face" ) parent_device = None try: # If the user already exists, we must retrieve it data parent_device = next(d for d in org_devices if d.user_id == device_id.user_id) if profile is not None and profile != parent_device.profile: raise ValueError( "profile is set but user already exists, with a different profile value." ) profile = parent_device.profile except StopIteration: profile = profile or UserProfile.STANDARD # Force each device to access the backend trough a different hostname so # tcp stream spy can switch offline certains while keeping the others online org_addr = addr_with_device_subdomain(org.addr, device_id) device = generate_new_device( organization_addr=org_addr, device_id=device_id, profile=profile, human_handle=human_handle, device_label=device_label, ) if parent_device is not None: device = device.evolve( private_key=parent_device.private_key, user_manifest_id=parent_device.user_manifest_id, user_manifest_key=parent_device.user_manifest_key, ) org_devices.append(device) return device