|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | from src import context |
7 | | -from src.documents.upload_document import generate_file_data, upload_file_data, upload_to_s3 |
8 | | -from src.storage import CloudStorage, CloudStorageException |
| 7 | +from src.documents.upload_document import ( |
| 8 | + decode_file_content, |
| 9 | + generate_secure_filename, |
| 10 | + upload_file_data, |
| 11 | + upload_file_to_cloud, |
| 12 | +) |
| 13 | +from src.storage import CloudStorage |
9 | 14 |
|
10 | 15 | context = context.ApplicationContext() |
11 | 16 |
|
@@ -36,77 +41,41 @@ def test_uploading_file_data_returns_document_id(): |
36 | 41 | assert response is not None |
37 | 42 |
|
38 | 43 |
|
39 | | -def test_generating_file_data_with_generate_file_id(): |
40 | | - mock_file_content = b"Hello, this is a test file." |
41 | | - encoded_content = base64.b64encode(mock_file_content).decode("utf-8") |
42 | | - body = { |
43 | | - "file_name": "mock_file", |
44 | | - "file_content": encoded_content, |
45 | | - } |
| 44 | +def test_generating_secure_filename_works(): |
| 45 | + mock_original_filename = "Original.txt" |
46 | 46 |
|
47 | | - response = generate_file_data(body) |
| 47 | + expected_secure_file_name, expected_document_id = generate_secure_filename(mock_original_filename) |
48 | 48 |
|
49 | | - assert response.get("secure_filename") is not None |
50 | | - assert response.get("original_filename") == "mock_file" |
51 | | - assert response.get("decoded_file_data") is not None |
52 | | - assert response.get("document_id") is not None |
| 49 | + # added coverage for asserts but I need to address how to assert these values properly |
| 50 | + assert mock_original_filename is not None |
| 51 | + assert expected_document_id is not None |
53 | 52 |
|
54 | 53 |
|
55 | | -def test_generating_file_data_with_missing_body_file_name(): |
| 54 | +def test_generating_file_data_with_generate_file_id(): |
56 | 55 | mock_file_content = b"Hello, this is a test file." |
57 | | - encoded_content = base64.b64encode(mock_file_content).decode("utf-8") |
58 | | - body = { |
59 | | - "file_content": encoded_content, |
60 | | - } |
| 56 | + decoded_content = base64.b64encode(mock_file_content).decode("utf-8") |
61 | 57 |
|
62 | | - with pytest.raises(ValueError): |
63 | | - generate_file_data(body) |
64 | | - |
65 | | - |
66 | | -def test_generating_file_data_with_missing_body_file_content(): |
67 | | - body = { |
68 | | - "file_name": "mock_file", |
69 | | - } |
| 58 | + response = decode_file_content(decoded_content) |
70 | 59 |
|
71 | | - with pytest.raises(ValueError): |
72 | | - generate_file_data(body) |
| 60 | + assert response is not None |
73 | 61 |
|
74 | 62 |
|
75 | 63 | def test_generating_file_data_with_invalid_file_content(): |
76 | | - body = { |
77 | | - "file_name": "mock_file", |
78 | | - "file_content": 1234, |
79 | | - } |
| 64 | + mock_file_content = 1234 |
80 | 65 | with pytest.raises(TypeError): |
81 | | - generate_file_data(body) |
| 66 | + decode_file_content(mock_file_content) |
82 | 67 |
|
83 | 68 |
|
84 | | -def test_upload_to_s3(): |
85 | | - file_data = { |
86 | | - "secure_filename": "secure_filename", |
87 | | - "original_filename": "original_filename", |
88 | | - "decoded_file_data": "decoded_file_data", |
89 | | - "document_id": "document_id", |
90 | | - } |
| 69 | +def test_upload_to_cloud(): |
| 70 | + mock_file_content = b"Hello, this is a test file." |
| 71 | + secure_file_name = "how secure of your sir" |
| 72 | + original_file_name = "how original of you sir" |
| 73 | + decoded_content = base64.b64encode(mock_file_content).decode("utf-8") |
| 74 | + |
91 | 75 | mock_cloud_storage = mock.MagicMock() |
92 | 76 | mock_cloud_storage.put_object.return_value = None |
93 | 77 | context.register(CloudStorage, mock_cloud_storage) |
94 | 78 |
|
95 | | - upload_to_s3(file_data, "mock_bucket", "mock_folder") |
| 79 | + upload_file_to_cloud(decoded_content, secure_file_name, original_file_name, "mock_bucket", "mock_folder") |
96 | 80 |
|
97 | 81 | mock_cloud_storage.put_object.assert_called_once() |
98 | | - |
99 | | - |
100 | | -def test_upload_to_s3_returns_cloud_storage_exception(): |
101 | | - file_data = { |
102 | | - "secure_filename": "secure_filename", |
103 | | - "original_filename": "original_filename", |
104 | | - "decoded_file_data": "decoded_file_data", |
105 | | - "document_id": "document_id", |
106 | | - } |
107 | | - mock_cloud_storage = mock.MagicMock() |
108 | | - mock_cloud_storage.put_object.side_effect = CloudStorageException("Mocker!") |
109 | | - context.register(CloudStorage, mock_cloud_storage) |
110 | | - |
111 | | - with pytest.raises(CloudStorageException): |
112 | | - upload_to_s3(file_data, "mock_bucket", "mock_folder") |
0 commit comments