Beispiel #1
0
def test_uploader_identity_ensure_member_role_can_be_changed_no_member(
        uploader_identity: UploaderIdentity, new_role: str) -> None:
    assert uploader_identity.can_member_role_be_changed(None,
                                                        new_role) is False
    with pytest.raises(ValidationError) as e:
        uploader_identity.ensure_member_role_can_be_changed(None, new_role)
    assert "Invalid member" in str(e.value)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
def test_uploader_identity_creation(user, author_name, should_fail):
    user.username = author_name
    if should_fail:
        with pytest.raises(ValidationError):
            UploaderIdentity.get_or_create_for_user(user)
    else:
        identity = UploaderIdentity.get_or_create_for_user(user)
        assert identity.name == author_name
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
def test_uploader_identity_add_member(uploader_identity: UploaderIdentity,
                                      role: str) -> None:
    assert uploader_identity.members.count() == 0
    membership = uploader_identity.add_member(UserFactory(), role)
    assert membership.role == role
    assert uploader_identity.members.count() == 1
    assert membership in uploader_identity.members.all()
Beispiel #11
0
def test_uploader_identity_get_default_for_user_conflict(user_type: str):
    user = TestUserTypes.get_user_by_type(user_type)
    if user and user.is_authenticated:
        UploaderIdentity.objects.create(
            name=strip_unsupported_characters(user.username))
    default_identity = UploaderIdentity.get_default_for_user(user)
    assert default_identity is None
Beispiel #12
0
 def get_form_kwargs(self, *args, **kwargs):
     kwargs = super(PackageCreateView,
                    self).get_form_kwargs(*args, **kwargs)
     kwargs["user"] = self.request.user
     kwargs["identity"] = UploaderIdentity.get_or_create_for_user(
         self.request.user)
     return kwargs
Beispiel #13
0
def test_package_upload(user, manifest_v1_data, community):

    icon_raw = io.BytesIO()
    icon = Image.new("RGB", (256, 256), "#FF0000")
    icon.save(icon_raw, format="PNG")

    readme = "# Test readme".encode("utf-8")
    manifest = json.dumps(manifest_v1_data).encode("utf-8")

    files = [
        ("README.md", readme),
        ("icon.png", icon_raw.getvalue()),
        ("manifest.json", manifest),
    ]

    zip_raw = io.BytesIO()
    with ZipFile(zip_raw, "a", ZIP_DEFLATED, False) as zip_file:
        for name, data in files:
            zip_file.writestr(name, data)

    file_data = {"file": SimpleUploadedFile("mod.zip", zip_raw.getvalue())}
    identity = UploaderIdentity.get_or_create_for_user(user)
    form = PackageUploadForm(
        user=user,
        identity=identity,
        files=file_data,
        community=community,
    )
    assert form.is_valid()
    version = form.save()
    assert version.name == manifest_v1_data["name"]
    assert version.package.owner == identity
Beispiel #14
0
 def get_form_kwargs(self, *args, **kwargs):
     kwargs = super().get_form_kwargs(*args, **kwargs)
     kwargs["user"] = self.request.user
     kwargs["identity"] = UploaderIdentity.get_or_create_for_user(
         self.request.user
     )
     kwargs["community"] = self.request.community
     return kwargs
Beispiel #15
0
 def get_form_kwargs(self, *args, **kwargs):
     kwargs = super().get_form_kwargs(*args, **kwargs)
     kwargs["user"] = self.request.user
     kwargs["community"] = self.request.community
     kwargs["initial"] = {
         "team": UploaderIdentity.get_default_for_user(self.request.user),
         "communities": [self.request.community],
     }
     return kwargs
Beispiel #16
0
def test_manifest_v1_null_fields(user, manifest_v1_data, field):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data[field] = None
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert "This field may not be null." in str(serializer.errors[field][0])
Beispiel #17
0
def test_manifest_v1_missing_fields(user, manifest_v1_data, field):
    identity = UploaderIdentity.get_or_create_for_user(user)
    del manifest_v1_data[field]
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert "This field is required." in str(serializer.errors[field][0])
Beispiel #18
0
def test_uploader_identity_create_for_user(
        user: UserType, username: str, expected_name: Optional[str]) -> None:
    user.username = username
    identity = UploaderIdentity.get_or_create_for_user(user)
    if expected_name:
        assert identity.name == expected_name
        assert identity.members.count() == 1
        assert identity.members.first().user == user
    else:
        assert identity is None
Beispiel #19
0
def test_manifest_v1_update(user, manifest_v1_data):
    identity = UploaderIdentity.get_or_create_for_user(user)
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    assert serializer.is_valid()
    with pytest.raises(NotImplementedError) as exc:
        serializer.update({}, serializer.validated_data)
    assert ".update() is not supported" in str(exc.value)
def test_manifest_v1_blank_fields(user, manifest_v1_data, field, empty_val, should_fail):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data[field] = empty_val
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    if should_fail:
        assert serializer.is_valid() is False
        assert "This field may not be blank." in str(serializer.errors[field][0])
    else:
        assert serializer.is_valid() is True
def test_manifest_v1_serializer_dependencies_invalid(user, manifest_v1_data, dependencies, error: str):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data["dependencies"] = dependencies
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    if error:
        assert serializer.is_valid() is False
        assert error in str(serializer.errors["dependencies"][0])
    else:
        assert serializer.is_valid() is True
def test_manifest_v1_serializer_website_url_validation(user, manifest_v1_data, url: str, error: str):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data["website_url"] = url
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    if error:
        assert serializer.is_valid() is False
        assert error in str(serializer.errors["website_url"][0])
    else:
        assert serializer.is_valid() is True
def test_manifest_v1_serializer_version_number_validation(user, manifest_v1_data, version: str, error: str):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data["version_number"] = version
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    if error:
        assert serializer.is_valid() is False
        assert error in str(serializer.errors["version_number"][0])
    else:
        assert serializer.is_valid() is True
Beispiel #24
0
def test_manifest_v1_invalid_key_formatting(user):
    data = {
        "name": "name",
        "versionNumber": "1.0.0",
        "websiteUrl": "",
        "description": "",
        "dependencies": [],
    }
    identity = UploaderIdentity.get_or_create_for_user(user)
    deserializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=data,
    )
    assert deserializer.is_valid() is False
Beispiel #25
0
def test_uploader_identity_create_for_user_name_taken(user: UserType,
                                                      role: str) -> None:
    would_be_name = strip_unsupported_characters(user.username)
    identity = UploaderIdentity.objects.create(name=would_be_name)
    if role:
        UploaderIdentityMember.objects.create(
            identity=identity,
            user=user,
            role=UploaderIdentityMemberRole.owner,
        )
    result = UploaderIdentity.get_or_create_for_user(user)
    if role:
        assert result == identity
    else:
        assert result is None
def test_manifest_v1_serializer_unresolved_dependency(user, manifest_v1_data, package_version):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data["dependencies"] = [
        "invalid-package-1.0.0",
        str(package_version.reference),
        "invalid-package-2.0.0",
    ]
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert len(serializer.errors["dependencies"]) == 2
    assert "No matching package found for reference" in str(serializer.errors["dependencies"][0])
    assert "No matching package found for reference" in str(serializer.errors["dependencies"][2])
Beispiel #27
0
def test_package_upload_with_extra_data(user, community, manifest_v1_data):

    icon_raw = io.BytesIO()
    icon = Image.new("RGB", (256, 256), "#FF0000")
    icon.save(icon_raw, format="PNG")

    readme = "# Test readme".encode("utf-8")
    manifest = json.dumps(manifest_v1_data).encode("utf-8")

    files = [
        ("README.md", readme),
        ("icon.png", icon_raw.getvalue()),
        ("manifest.json", manifest),
    ]

    zip_raw = io.BytesIO()
    with ZipFile(zip_raw, "a", ZIP_DEFLATED, False) as zip_file:
        for name, data in files:
            zip_file.writestr(name, data)

    category = PackageCategory.objects.create(
        name="Test Category",
        slug="test-category",
        community=community,
    )

    file_data = {"file": SimpleUploadedFile("mod.zip", zip_raw.getvalue())}
    identity = UploaderIdentity.get_or_create_for_user(user)
    form = PackageUploadForm(
        user=user,
        files=file_data,
        community=community,
        data={
            "categories": [category.pk],
            "has_nsfw_content": True,
            "team": identity.name,
            "communities": [community.identifier],
        },
    )
    assert form.is_valid()
    version = form.save()
    assert version.name == manifest_v1_data["name"]
    assert version.package.owner == identity
    listing = PackageListing.objects.filter(package=version.package).first()
    assert listing.categories.count() == 1
    assert listing.categories.first() == category
    assert listing.has_nsfw_content is True
Beispiel #28
0
def test_uploader_identity_get_default_for_user(
        user: UserType, existing_team_role: Optional[str]) -> None:
    existing_identity = None
    if existing_team_role:
        existing_identity = UploaderIdentity.objects.create(name="TestTeam")
        UploaderIdentityMember.objects.create(
            identity=existing_identity,
            user=user,
            role=existing_team_role,
        )

    default_identity = UploaderIdentity.get_default_for_user(user)

    if existing_team_role:
        assert default_identity == existing_identity
    else:
        assert bool(default_identity)
Beispiel #29
0
def test_manifest_v1_serializer_dependencies_valid(user, manifest_v1_data):
    reference = PackageReference.parse("actual_package-reference-1.0.0")
    PackageVersionFactory.create(
        package=PackageFactory.create(
            owner=UploaderIdentityFactory.create(name=reference.namespace, ),
            name=reference.name,
        ),
        name=reference.name,
        version_number=reference.version_str,
    )
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data["dependencies"] = [str(reference)]
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is True
def test_manifest_v1_serializer_too_many_dependencies(user, manifest_v1_data):
    identity = UploaderIdentity.get_or_create_for_user(user)
    reference_strings = [
        f"user-package-{i}.{i}.{i}" for i in range(101)
    ]
    manifest_v1_data["dependencies"] = reference_strings
    serializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    # Patch the validator because we don't want to generate 101 actual packages here
    serializer.fields["dependencies"].child.validators = [PackageReferenceValidator(
        require_version=True,
        resolve=False  # Otherwise the same, but don't try to resolve the references
    )]
    assert serializer.is_valid() is False
    assert len(serializer.errors["dependencies"]) == 1
    assert "Ensure this field has no more than 100 elements." in str(serializer.errors["dependencies"][0])