Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved repository code quality #320

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion repository/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
from django.contrib import admin
from .models import NestedProgram

admin.site.register(NestedProgram)

@admin.register(NestedProgram)
class NestedProgramAdmin(admin.ModelAdmin):
"""NestedProgramAdmin."""
8 changes: 7 additions & 1 deletion repository/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import uuid
from django.core.validators import FileExtensionValidator
from django.db import models


Expand Down Expand Up @@ -42,4 +43,9 @@ class NestedProgram(models.Model):
arguments = models.JSONField(null=True, default=empty_dict)
tags = models.JSONField(null=True, default=empty_list)
public = models.BooleanField(default=True)
artifact = models.FileField(upload_to="artifacts_%Y_%m_%d", null=False, blank=False)
artifact = models.FileField(
upload_to="artifacts_%Y_%m_%d",
null=False,
blank=False,
validators=[FileExtensionValidator(allowed_extensions=["tar"])],
)
55 changes: 49 additions & 6 deletions repository/tests/core/test_v1_nested_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ def test_create_nested_program_returns_201(self):
os.path.dirname(os.path.realpath(__file__)),
"..",
"fixtures",
"initial_data.json",
)
"artifact.tar",
),
"rb",
) as file:
nested_program_input["artifact"] = File(file)
nested_program_input["artifact"] = file
response = self.client.post(
url, data=nested_program_input, format="multipart"
)
Expand Down Expand Up @@ -195,10 +196,11 @@ def test_nested_program_list_validation_returns_400(self):
os.path.dirname(os.path.realpath(__file__)),
"..",
"fixtures",
"initial_data.json",
)
"artifact.tar",
),
"rb",
) as file:
nested_program_input["artifact"] = File(file)
nested_program_input["artifact"] = file
response = self.client.post(
url, data=nested_program_input, format="multipart"
)
Expand Down Expand Up @@ -229,6 +231,47 @@ def test_nested_program_dict_validation_returns_400(self):
self.client.force_login(test_user)

url = reverse("v1:nested-programs-list")
with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"fixtures",
"artifact.tar",
),
"rb",
) as file:
nested_program_input["artifact"] = file
response = self.client.post(
url, data=nested_program_input, format="multipart"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

failed_validation_fields_list = list(response.json().keys())
self.assertListEqual(failed_validation_fields_list, fields_to_check)

def test_nested_program_artifact_validation_returns_400(self):
"""
The value for artifact is not a tar file, a non-allowed value for this field and returns a 400
"""
fields_to_check = ["artifact"]
nested_program_input = {
"title": "Awesome nested program",
"description": "Awesome nested program description",
"entrypoint": "nested_program.py",
"working_dir": "./",
"version": "0.0.1",
"env_vars": json.dumps({"DEBUG": True}),
"arguments": json.dumps({}),
"tags": json.dumps(["dev"]),
"dependencies": json.dumps([]),
"public": True,
}
test_user = User.objects.get(username="test_user")

self.client.force_login(test_user)

url = reverse("v1:nested-programs-list")

with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down
Binary file added repository/tests/fixtures/artifact.tar
Binary file not shown.