Пример #1
0
    async def create(
        self,
        id: OrganizationID,
        bootstrap_token: str,
        active_users_limit: Union[UnsetType, Optional[int]] = Unset,
        user_profile_outsider_allowed: Union[UnsetType, bool] = Unset,
    ) -> None:
        org = self._organizations.get(id)
        # Allow overwritting of not-yet-bootstrapped organization
        if org and org.root_verify_key:
            raise OrganizationAlreadyExistsError()
        if active_users_limit is Unset:
            active_users_limit = self._config.organization_initial_active_users_limit
        if user_profile_outsider_allowed is Unset:
            user_profile_outsider_allowed = (
                self._config.organization_initial_user_profile_outsider_allowed
            )

        self._organizations[id] = Organization(
            organization_id=id,
            bootstrap_token=bootstrap_token,
            is_expired=False,
            root_verify_key=None,
            active_users_limit=active_users_limit,
            user_profile_outsider_allowed=user_profile_outsider_allowed,
        )
Пример #2
0
async def test_organization_create_already_exists_not_bootstrapped(
        backend, backend_rest_send, expired):
    organization_id = OrganizationID("NewOrg")
    original_bootstrap_token = "123"
    await backend.organization.create(id=organization_id,
                                      bootstrap_token=original_bootstrap_token)
    if expired:
        await backend.organization.update(id=organization_id, is_expired=True)

    status, _, body = await backend_rest_send(
        f"/administration/organizations",
        method="POST",
        body={"organization_id": str(organization_id)},
    )
    assert (status, body) == ((200, "OK"), {"bootstrap_token": ANY})

    # Token should be regenerated each time, and the configuration should be overwritten
    assert body["bootstrap_token"] != original_bootstrap_token

    org = await backend.organization.get(id=organization_id)
    assert org == Organization(
        organization_id=organization_id,
        bootstrap_token=body["bootstrap_token"],
        is_expired=False,
        root_verify_key=None,
        user_profile_outsider_allowed=True,
        active_users_limit=None,
    )
Пример #3
0
    async def create(
        self, id: OrganizationID, bootstrap_token: str, expiration_date: Optional[DateTime] = None
    ) -> None:
        org = self._organizations.get(id)

        # Allow overwritting of not-yet-bootstrapped organization
        if org and org.root_verify_key:
            raise OrganizationAlreadyExistsError()

        self._organizations[id] = Organization(
            organization_id=id, bootstrap_token=bootstrap_token, expiration_date=expiration_date
        )
Пример #4
0
    async def _get(conn, id: OrganizationID) -> Organization:
        data = await conn.fetchrow(*_q_get_organization(organization_id=id))
        if not data:
            raise OrganizationNotFoundError()

        rvk = VerifyKey(data[1]) if data[1] else None
        return Organization(
            organization_id=id,
            bootstrap_token=data[0],
            root_verify_key=rvk,
            expiration_date=data[2],
        )
Пример #5
0
    async def _get(conn, id: OrganizationID) -> Organization:
        data = await conn.fetchrow(
            """
                SELECT bootstrap_token, root_verify_key
                FROM organizations WHERE organization_id = $1
                """,
            id,
        )
        if not data:
            raise OrganizationNotFoundError()

        rvk = VerifyKey(data[1]) if data[1] else None
        return Organization(organization_id=id, bootstrap_token=data[0], root_verify_key=rvk)
Пример #6
0
async def test_organization_create(backend, backend_rest_send):
    organization_id = OrganizationID("NewOrg")
    status, _, body = await backend_rest_send(
        f"/administration/organizations",
        method="POST",
        body={"organization_id": str(organization_id)},
    )
    assert (status, body) == ((200, "OK"), {"bootstrap_token": ANY})

    org = await backend.organization.get(organization_id)
    assert org == Organization(
        organization_id=organization_id,
        bootstrap_token=body["bootstrap_token"],
        is_expired=False,
        root_verify_key=None,
        user_profile_outsider_allowed=True,
        active_users_limit=None,
    )
Пример #7
0
    async def _get(conn,
                   id: OrganizationID,
                   for_update: bool = False) -> Organization:
        if for_update:
            data = await conn.fetchrow(*_q_get_organization_for_update(
                organization_id=id.str))
        else:
            data = await conn.fetchrow(*_q_get_organization(
                organization_id=id.str))
        if not data:
            raise OrganizationNotFoundError()

        rvk = VerifyKey(data[1]) if data[1] else None
        return Organization(
            organization_id=id,
            bootstrap_token=data[0],
            root_verify_key=rvk,
            is_expired=data[2],
            active_users_limit=data[3],
            user_profile_outsider_allowed=data[4],
        )
Пример #8
0
    async def create(self, id: OrganizationID, bootstrap_token: str) -> None:
        if id in self._organizations:
            raise OrganizationAlreadyExistsError()

        self._organizations[id] = Organization(organization_id=id,
                                               bootstrap_token=bootstrap_token)
Пример #9
0
async def test_organization_create_with_custom_initial_config(
        backend, backend_rest_send):
    organization_id = OrganizationID("NewOrg")
    original_bootstrap_token = "123"
    await backend.organization.create(id=organization_id,
                                      bootstrap_token=original_bootstrap_token)

    status, _, body = await backend_rest_send(
        f"/administration/organizations",
        method="POST",
        body={
            "organization_id": str(organization_id),
            "user_profile_outsider_allowed": False,
            "active_users_limit": None,
        },
    )
    assert (status, body) == ((200, "OK"), {"bootstrap_token": ANY})

    org = await backend.organization.get(organization_id)
    assert org == Organization(
        organization_id=organization_id,
        bootstrap_token=body["bootstrap_token"],
        is_expired=False,
        root_verify_key=None,
        user_profile_outsider_allowed=False,
        active_users_limit=None,
    )

    # New custom initial config should be taken into account each time the org is recreated
    status, _, body = await backend_rest_send(
        f"/administration/organizations",
        method="POST",
        body={
            "organization_id": str(organization_id),
            "user_profile_outsider_allowed": True,
            "active_users_limit": 10,
        },
    )
    assert (status, body) == ((200, "OK"), {"bootstrap_token": ANY})

    org = await backend.organization.get(organization_id)
    assert org == Organization(
        organization_id=organization_id,
        bootstrap_token=body["bootstrap_token"],
        is_expired=False,
        root_verify_key=None,
        user_profile_outsider_allowed=True,
        active_users_limit=10,
    )

    # Default initial config should also be used if org is recreated without custom config
    status, _, body = await backend_rest_send(
        f"/administration/organizations",
        method="POST",
        body={"organization_id": str(organization_id)},
    )
    assert (status, body) == ((200, "OK"), {"bootstrap_token": ANY})

    org = await backend.organization.get(organization_id)
    assert org == Organization(
        organization_id=organization_id,
        bootstrap_token=body["bootstrap_token"],
        is_expired=False,
        root_verify_key=None,
        user_profile_outsider_allowed=True,
        active_users_limit=None,
    )