Пример #1
0
def test_uploader_identity_is_last_owner(uploader_identity) -> None:
    member1 = UploaderIdentityMemberFactory(
        identity=uploader_identity,
        role=UploaderIdentityMemberRole.owner,
    )
    member2 = UploaderIdentityMemberFactory(
        identity=uploader_identity,
        role=UploaderIdentityMemberRole.member,
    )
    assert uploader_identity.members.count() == 2
    assert uploader_identity.members.owners().count() == 1
    assert uploader_identity.is_last_owner(member1) is True
    assert uploader_identity.is_last_owner(member2) is False
    assert uploader_identity.is_last_owner(None) is False

    member2.role = UploaderIdentityMemberRole.owner
    member2.save()

    assert uploader_identity.members.owners().count() == 2
    assert uploader_identity.is_last_owner(member1) is False
    assert uploader_identity.is_last_owner(member2) is False

    member1.role = UploaderIdentityMemberRole.member
    member1.save()

    assert uploader_identity.members.owners().count() == 1
    assert uploader_identity.is_last_owner(member1) is False
    assert uploader_identity.is_last_owner(member2) is True
Пример #2
0
def test_uploader_identity_can_user_upload(user, role, expected) -> None:
    identity = UploaderIdentityFactory.create()
    if role:
        UploaderIdentityMemberFactory.create(
            user=user,
            identity=identity,
            role=role,
        )
    assert identity.can_user_upload(user) == expected
Пример #3
0
def test_uploader_identity_ensure_member_role_can_be_changed(
        uploader_identity: UploaderIdentity, old_role: str,
        new_role: str) -> None:
    member = UploaderIdentityMemberFactory(identity=uploader_identity,
                                           role=old_role)
    is_last_owner = (old_role == UploaderIdentityMemberRole.owner
                     and new_role == UploaderIdentityMemberRole.member)
    if is_last_owner:
        UploaderIdentityMemberFactory(identity=uploader_identity,
                                      role=UploaderIdentityMemberRole.owner)
    assert uploader_identity.can_member_role_be_changed(member,
                                                        new_role) is True
    uploader_identity.ensure_member_role_can_be_changed(member, new_role)
Пример #4
0
def test_uploader_identity_ensure_member_can_be_removed(
        uploader_identity: UploaderIdentity, role: str) -> None:
    member = UploaderIdentityMemberFactory(
        role=role,
        identity=uploader_identity,
    )
    if role == UploaderIdentityMemberRole.owner:
        UploaderIdentityMemberFactory(
            identity=uploader_identity,
            role=UploaderIdentityMemberRole.owner,
        )
    assert uploader_identity.can_member_be_removed(member) is True
    uploader_identity.ensure_member_can_be_removed(member)
Пример #5
0
def test_uploader_identity_member_can_be_promoted(role) -> None:
    membership = UploaderIdentityMemberFactory(role=role)
    result_map = {
        UploaderIdentityMemberRole.owner: False,
        UploaderIdentityMemberRole.member: True,
    }
    assert membership.can_be_promoted == result_map[role]
Пример #6
0
def test_uploader_identity_ensure_user_can_disband_has_packages(
        uploader_identity: UploaderIdentity, package: Package) -> None:
    member = UploaderIdentityMemberFactory(
        identity=uploader_identity, role=UploaderIdentityMemberRole.owner)
    assert uploader_identity.can_user_disband(member.user) is False
    with pytest.raises(ValidationError) as e:
        uploader_identity.ensure_user_can_disband(member.user)
    assert "Unable to disband teams with packages" in str(e.value)
Пример #7
0
def test_uploader_identity_ensure_member_role_can_be_changed_invalid_role(
        uploader_identity: UploaderIdentity, role: Optional[str]) -> None:
    member = UploaderIdentityMemberFactory(
        identity=uploader_identity, role=UploaderIdentityMemberRole.member)
    assert uploader_identity.can_member_role_be_changed(member, role) is False
    with pytest.raises(ValidationError) as e:
        uploader_identity.ensure_member_role_can_be_changed(member, role)
    assert "New role is invalid" in str(e.value)
Пример #8
0
def test_uploader_identity_ensure_member_can_be_removed_wrong_identity(
    uploader_identity: UploaderIdentity, ) -> None:
    member = UploaderIdentityMemberFactory(
        role=UploaderIdentityMemberRole.member)
    assert uploader_identity.can_member_be_removed(member) is False
    with pytest.raises(ValidationError) as e:
        uploader_identity.ensure_member_can_be_removed(member)
    assert "Member is not a part of this uploader identity" in str(e.value)
Пример #9
0
def test_uploader_identity_ensure_member_role_can_be_changed_last_owner(
    uploader_identity: UploaderIdentity, ) -> None:
    new_role = UploaderIdentityMemberRole.member
    member = UploaderIdentityMemberFactory(
        identity=uploader_identity, role=UploaderIdentityMemberRole.owner)
    assert uploader_identity.can_member_role_be_changed(member,
                                                        new_role) is False
    with pytest.raises(ValidationError) as e:
        uploader_identity.ensure_member_role_can_be_changed(member, new_role)
    assert "Cannot remove last owner from team" in str(e.value)
Пример #10
0
def test_uploader_identity_ensure_member_can_be_removed_last_owner(
    uploader_identity: UploaderIdentity, ) -> None:
    owner = UploaderIdentityMemberFactory(
        identity=uploader_identity,
        role=UploaderIdentityMemberRole.owner,
    )
    assert uploader_identity.members.count() == 1
    assert uploader_identity.can_member_be_removed(owner) is False
    with pytest.raises(ValidationError) as e:
        uploader_identity.ensure_member_can_be_removed(owner)
    assert "Cannot remove last owner from team" in str(e.value)
Пример #11
0
def create_test_service_account_user():
    identity_owner = ThunderstoreUserFactory()
    identity = UploaderIdentityFactory()
    UploaderIdentityMemberFactory(user=identity_owner,
                                  identity=identity,
                                  role="owner")
    form = CreateServiceAccountForm(
        user=identity_owner,
        data={
            "identity": identity,
            "nickname": "Nickname"
        },
    )
    assert form.is_valid()
    return form.save().user