Ejemplo n.º 1
0
class ManifestV1Serializer(serializers.Serializer):
    def __init__(self, *args, **kwargs):
        if "user" not in kwargs:
            raise AttributeError("Missing required key word parameter: user")
        if "uploader" not in kwargs:
            raise AttributeError(
                "Missing required key word parameter: uploader")
        self.user = kwargs.pop("user")
        self.uploader = kwargs.pop("uploader")
        super().__init__(*args, **kwargs)

    name = PackageNameField()
    version_number = PackageVersionField()
    website_url = serializers.CharField(
        max_length=PackageVersion._meta.get_field("website_url").max_length,
        allow_blank=True,
    )
    description = serializers.CharField(
        max_length=PackageVersion._meta.get_field("description").max_length,
        allow_blank=True,
    )
    dependencies = serializers.ListField(
        child=DependencyField(),
        max_length=100,
        allow_empty=True,
    )

    def validate(self, data):
        result = super().validate(data)
        if not self.uploader.can_user_upload(self.user):
            raise ValidationError(
                f"Missing privileges to upload under author {self.uploader.name}"
            )
        reference = PackageReference(self.uploader.name, result["name"],
                                     result["version_number"])
        if reference.exists:
            raise ValidationError(
                "Package of the same name and version already exists")
        if has_duplicate_packages(result["dependencies"]):
            raise ValidationError(
                "Cannot depend on multiple versions of the same package")
        if does_contain_package(result["dependencies"], reference):
            raise ValidationError("Package depending on itself is not allowed")
        return result

    def update(self, instance, validated_data):
        raise NotImplementedError(".update() is not supported")

    def create(self, validated_data):
        raise NotImplementedError(".create() is not supported")
Ejemplo n.º 2
0
def test_fields_list_dependency_field():
    field = serializers.ListField(
        child=DependencyField(),
        max_length=100,
        allow_empty=True,
    )
    identity = UploaderIdentityFactory.create(name="tester")
    versions = [
        PackageVersionFactory.create(
            package=PackageFactory.create(owner=identity, name=f"package_{i}"),
            name=f"package_{i}",
        ) for i in range(10)
    ]
    references = [x.reference for x in versions]
    reference_strings = [str(x) for x in references]
    result = field.run_validation(reference_strings)
    assert len(result) == 10
    assert isinstance(result[0], PackageReference)
    assert result == references
Ejemplo n.º 3
0
def test_fields_dependency_valid(package_version):
    field = DependencyField()
    result = field.run_validation(package_version.reference)
    assert isinstance(result, PackageReference)
    assert result == package_version.reference
    assert field.to_representation(result) == str(package_version.reference)
Ejemplo n.º 4
0
def test_fields_dependency_nonexisting_reference():
    field = DependencyField()
    with pytest.raises(ValidationError) as exception:
        field.run_validation("someUser-somePackage-1.0.0")
    assert "No matching package found for reference" in str(exception.value)
Ejemplo n.º 5
0
def test_fields_dependency_missing_version():
    field = DependencyField()
    with pytest.raises(ValidationError) as exception:
        field.run_validation("someUser-somePackage")
    assert "Package reference is missing version" in str(exception.value)
Ejemplo n.º 6
0
def test_fields_dependency_invalid_reference():
    field = DependencyField()
    with pytest.raises(ValidationError) as exception:
        field.run_validation("not a reference")
    assert "Invalid package reference string" in str(exception.value)