Skip to content
Open
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
57 changes: 30 additions & 27 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -359,33 +359,36 @@ components:
minLength: 1
UploadFilePartInstruction:
description: 'This model describes how a single part of a multipart file upload should be processed by the client. The upload links may only be valid for a specific time period, after which the links expire. Server vendors should make sure to have a reasonably long expiration date for clients to upload large files'
allOf:
- $ref: '#/components/schemas/LinkData'
- type: object
required:
- http_method
- content_range_start
- content_range_end
properties:
http_method:
type: string
enum: [POST, PUT]
additional_headers:
$ref: '#/components/schemas/Headers'
include_authorization:
description: 'Whether or not to include the authorization request header in the file upload request. Including the authorization header with some cloud storage providers might fail the request'
type: boolean
default: false
multipart_form_data:
$ref: '#/components/schemas/MultipartFormData'
content_range_start:
description: 'The inclusive, zero index based start for this part'
type: integer
format: int64
content_range_end:
description: 'The inclusive, zero index based end for this part'
type: integer
format: int64
type: object
additionalProperties: false
required:
- url
- http_method
- content_range_start
- content_range_end
properties:
url:
type: string
minLength: 1
http_method:
type: string
enum: [POST, PUT]
additional_headers:
$ref: '#/components/schemas/Headers'
include_authorization:
description: 'Whether or not to include the authorization request header in the file upload request. Including the authorization header with some cloud storage providers might fail the request'
type: boolean
default: false
multipart_form_data:
$ref: '#/components/schemas/MultipartFormData'
content_range_start:
description: 'The inclusive, zero index based start for this part'
type: integer
format: int64
content_range_end:
description: 'The inclusive, zero index based end for this part'
type: integer
format: int64
MultipartFormData:
type: object
additionalProperties: false
Expand Down
103 changes: 103 additions & 0 deletions tests/test_upload_file_part_instruction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""Regression checks for the JSON Schema subset used by the upload models.

Requires PyYAML and jsonschema. Run: python -m unittest discover -s tests -v
No server, credentials, network requests, or model files are required.
"""

from copy import deepcopy
from pathlib import Path
import unittest

from jsonschema import Draft4Validator
import yaml


SPECIFICATION = yaml.safe_load(
(Path(__file__).resolve().parents[1] / "swagger.yaml").read_text()
)
PART = {
"url": "https://cde.example.test/upload/part/1",
"http_method": "PUT",
"content_range_start": 0,
"content_range_end": 1023,
}


def validator(model):
# These models use the Draft 4-compatible subset of OpenAPI 3.0 Schema.
# Keep references rooted in the complete, unmodified specification.
return Draft4Validator({
"$ref": "#/components/schemas/" + model,
"components": SPECIFICATION["components"],
})


class UploadFilePartInstructionTests(unittest.TestCase):
def test_minimal_instruction(self):
for method in ("POST", "PUT"):
with self.subTest(method=method):
validator("UploadFilePartInstruction").validate({
**PART, "http_method": method,
})

def test_all_optional_properties(self):
validator("UploadFilePartInstruction").validate({
**PART,
"additional_headers": {
"values": [{"name": "Content-Type", "value": "application/octet-stream"}],
},
"include_authorization": True,
"multipart_form_data": {"prefix": "cHJlZml4", "suffix": "c3VmZml4"},
})

def test_each_required_property(self):
for name in PART:
with self.subTest(property=name):
incomplete = deepcopy(PART)
del incomplete[name]
self.assertFalse(validator("UploadFilePartInstruction").is_valid(incomplete))

def test_empty_url(self):
self.assertFalse(validator("UploadFilePartInstruction").is_valid({**PART, "url": ""}))

def test_unsupported_http_method(self):
self.assertFalse(validator("UploadFilePartInstruction").is_valid({
**PART, "http_method": "GET",
}))

def test_property_types(self):
for name, value in (
("url", 42),
("content_range_start", "0"),
("content_range_end", 1.5),
("include_authorization", "false"),
):
with self.subTest(property=name):
self.assertFalse(validator("UploadFilePartInstruction").is_valid({
**PART, name: value,
}))

def test_unknown_property(self):
self.assertFalse(validator("UploadFilePartInstruction").is_valid({
**PART, "unexpected": True,
}))

def test_link_data_remains_closed(self):
validator("LinkData").validate({"url": PART["url"]})
for invalid in ({}, {"url": ""}, PART):
with self.subTest(link=invalid):
self.assertFalse(validator("LinkData").is_valid(invalid))

def test_instruction_in_documents_to_upload_response(self):
validator("DocumentsToUpload").validate({
"documents_to_upload": [{
"session_file_id": "file-1",
"upload_file_parts": [PART],
"upload_completion": {"url": "https://cde.example.test/upload/complete"},
"upload_cancellation": {"url": "https://cde.example.test/upload/cancel"},
}],
})


if __name__ == "__main__":
unittest.main()