Skip to content

Commit

Permalink
return error for invalid data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cojenco committed Feb 26, 2024
1 parent def33e4 commit cd5d13b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
7 changes: 3 additions & 4 deletions gcs/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ def init_write_object_grpc(cls, db, request_iterator, context):
upload.complete = True
continue
else:
print("WARNING unexpected data field %s\n" % data)
continue
testbench.error.invalid("Invalid data field in upload", context)
return None, False

content = checksummed_data.content
crc32c_hash = (
Expand Down Expand Up @@ -332,8 +332,7 @@ def process_bidi_write_object_grpc(cls, db, request_iterator, context):
upload.complete = True
continue
else:
print("WARNING unexpected data field %s\n" % data)
continue
return testbench.error.invalid("Invalid data field in upload", context)

content = checksummed_data.content
crc32c_hash = (
Expand Down
20 changes: 9 additions & 11 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def test_init_object_write_grpc_final_message_empty_data(self):
self.assertEqual(upload.metadata.name, "object")
self.assertEqual(upload.metadata.bucket, "projects/_/buckets/bucket-name")

def test_init_object_write_grpc_message_empty_data(self):
def test_init_object_write_grpc_message_invalid_data(self):
request = testbench.common.FakeRequest(
args={}, data=json.dumps({"name": "bucket-name"})
)
Expand All @@ -719,14 +719,11 @@ def test_init_object_write_grpc_message_empty_data(self):
db = unittest.mock.Mock()
db.get_bucket = unittest.mock.MagicMock(return_value=bucket)
db.get_upload = unittest.mock.MagicMock(return_value=upload)
upload, is_resumable = gcs.upload.Upload.init_write_object_grpc(
db, [r1], context
upload, _ = gcs.upload.Upload.init_write_object_grpc(db, [r1], context)
self.assertIsNone(upload)
context.abort.assert_called_once_with(
grpc.StatusCode.INVALID_ARGUMENT, unittest.mock.ANY
)
self.assertIsNotNone(upload)
self.assertFalse(upload.complete)
self.assertTrue(is_resumable)
self.assertEqual(upload.metadata.name, "object")
self.assertEqual(upload.metadata.bucket, "projects/_/buckets/bucket-name")

def test_process_bidi_write_grpc_resumable(self):
request = testbench.common.FakeRequest(
Expand Down Expand Up @@ -1025,7 +1022,7 @@ def test_process_bidi_write_grpc_final_message_empty_data(self):
self.assertEqual(blob.bucket, "projects/_/buckets/bucket-name")
self.assertTrue(upload.complete)

def test_process_bidi_write_grpc_message_empty_data(self):
def test_process_bidi_write_grpc_message_invalid_data(self):
request = testbench.common.FakeRequest(
args={}, data=json.dumps({"name": "bucket-name"})
)
Expand All @@ -1049,8 +1046,9 @@ def test_process_bidi_write_grpc_message_empty_data(self):
db = unittest.mock.Mock()
db.get_bucket = unittest.mock.MagicMock(return_value=bucket)
db.get_upload = unittest.mock.MagicMock(return_value=upload)
responses = list(
gcs.upload.Upload.process_bidi_write_object_grpc(db, [r1], context)
list(gcs.upload.Upload.process_bidi_write_object_grpc(db, [r1], context))
context.abort.assert_called_once_with(
grpc.StatusCode.INVALID_ARGUMENT, unittest.mock.ANY
)
self.assertFalse(upload.complete)

Expand Down

0 comments on commit cd5d13b

Please sign in to comment.