Exemple #1
0
def test_manifest_v1_requires_uploader(user, manifest_v1_data):
    with pytest.raises(AttributeError) as exc:
        _ = ManifestV1Serializer(
            data=manifest_v1_data,
            user=user,
        )
    assert "Missing required key word parameter: uploader" in str(exc.value)
Exemple #2
0
def test_manifest_v1_serializer_duplicate_dependency(user, manifest_v1_data,
                                                     package_version):
    UploaderIdentityMember.objects.create(
        user=user,
        identity=package_version.owner,
        role=UploaderIdentityMemberRole.owner,
    )
    pkg = PackageFactory.create(
        owner=package_version.owner,
        name="somepackage",
    )
    version1 = PackageVersionFactory.create(
        package=pkg,
        name="somepackage",
        version_number="1.0.0",
    )
    version2 = PackageVersionFactory.create(
        package=pkg,
        name="somepackage",
        version_number="2.0.0",
    )
    manifest_v1_data["dependencies"] = [
        str(version1.reference),
        str(version2.reference),
    ]
    serializer = ManifestV1Serializer(
        user=user,
        uploader=package_version.owner,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert len(serializer.errors["non_field_errors"]) == 1
    assert "Cannot depend on multiple versions of the same package" in str(
        serializer.errors["non_field_errors"][0])
def test_manifest_v1_serializer_missing_privileges(user, uploader_identity, manifest_v1_data):
    serializer = ManifestV1Serializer(
        user=user,
        uploader=uploader_identity,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert len(serializer.errors["non_field_errors"]) == 1
    assert "Missing privileges to upload under author" in str(serializer.errors["non_field_errors"][0])
Exemple #4
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])
Exemple #5
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])
def test_manifest_v1_deserialize_serialize(user, manifest_v1_data, package_version):
    identity = UploaderIdentity.get_or_create_for_user(user)
    manifest_v1_data["dependencies"] = [str(package_version.reference)]
    deserializer = ManifestV1Serializer(
        user=user,
        uploader=identity,
        data=manifest_v1_data,
    )
    assert deserializer.is_valid()
    validated_data = deserializer.validated_data
    assert validated_data
    assert isinstance(validated_data["dependencies"][0], PackageReference)
    assert validated_data["dependencies"][0] == package_version.reference
    serializer = ManifestV1Serializer(
        instance=validated_data,
        user=user,
        uploader=identity,
    )
    serialized_data = serializer.data
    assert serialized_data == manifest_v1_data
Exemple #7
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_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
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
Exemple #12
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
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])
def test_manifest_v1_serializer_version_already_exists(user, manifest_v1_data, package_version):
    UploaderIdentityMember.objects.create(
        user=user,
        identity=package_version.owner,
        role=UploaderIdentityMemberRole.owner,
    )
    manifest_v1_data["name"] = package_version.name
    manifest_v1_data["version_number"] = package_version.version_number
    serializer = ManifestV1Serializer(
        user=user,
        uploader=package_version.owner,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert len(serializer.errors["non_field_errors"]) == 1
    assert "Package of the same name and version already exists" in str(serializer.errors["non_field_errors"][0])
Exemple #15
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
Exemple #16
0
    def validate_manifest(self, manifest_str):
        try:
            manifest_data = json.loads(manifest_str)
        except json.decoder.JSONDecodeError as exc:
            raise ValidationError(f"Unable to parse manifest.json: {exc}")

        serializer = ManifestV1Serializer(
            user=self.user,
            uploader=self.identity,
            data=manifest_data,
        )
        if serializer.is_valid():
            self.manifest = serializer.validated_data
        else:
            errors = unpack_serializer_errors("manifest.json",
                                              serializer.errors)
            errors = ValidationError(
                [f"{key}: {value}" for key, value in errors.items()])
            self.add_error(None, errors)
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])
def test_manifest_v1_serializer_self_dependency(user, manifest_v1_data, package_version):
    UploaderIdentityMember.objects.create(
        user=user,
        identity=package_version.owner,
        role=UploaderIdentityMemberRole.owner,
    )
    manifest_v1_data["name"] = package_version.name
    manifest_v1_data["version_number"] = "1" + package_version.version_number
    manifest_v1_data["dependencies"] = [
        str(package_version.reference),
    ]
    serializer = ManifestV1Serializer(
        user=user,
        uploader=package_version.owner,
        data=manifest_v1_data,
    )
    assert serializer.is_valid() is False
    assert len(serializer.errors["non_field_errors"]) == 1
    assert "Package depending on itself is not allowed" in str(serializer.errors["non_field_errors"][0])
Exemple #19
0
    def validate_manifest(self, manifest_str):
        try:
            manifest_data = json.loads(manifest_str)
        except UnicodeDecodeError as exc:
            raise ValidationError([
                f"Unable to parse manifest.json: {exc}\n",
                "Make sure the manifest.json is UTF-8 compatible",
            ])
        except json.decoder.JSONDecodeError as exc:
            raise ValidationError(f"Unable to parse manifest.json: {exc}")

        serializer = ManifestV1Serializer(
            user=self.user,
            uploader=self.cleaned_data.get("team"),
            data=manifest_data,
        )
        if serializer.is_valid():
            self.manifest = serializer.validated_data
        else:
            errors = unpack_serializer_errors("manifest.json",
                                              serializer.errors)
            errors = ValidationError(
                [f"{key}: {value}" for key, value in errors.items()])
            self.add_error(None, errors)