Show More
@@ -1,111 +1,126 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2019 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 | import os |
|
20 | import os | |
21 | import pytest |
|
21 | import pytest | |
22 |
|
22 | |||
23 | from rhodecode.lib.ext_json import json |
|
23 | from rhodecode.lib.ext_json import json | |
|
24 | from rhodecode.model.db import Session, FileStore | |||
24 | from rhodecode.tests import TestController |
|
25 | from rhodecode.tests import TestController | |
25 | from rhodecode.apps.file_store import utils, config_keys |
|
26 | from rhodecode.apps.file_store import utils, config_keys | |
26 |
|
27 | |||
27 |
|
28 | |||
28 | def route_path(name, params=None, **kwargs): |
|
29 | def route_path(name, params=None, **kwargs): | |
29 | import urllib |
|
30 | import urllib | |
30 |
|
31 | |||
31 | base_url = { |
|
32 | base_url = { | |
32 | 'upload_file': '/_file_store/upload', |
|
33 | 'upload_file': '/_file_store/upload', | |
33 | 'download_file': '/_file_store/download/{fid}', |
|
34 | 'download_file': '/_file_store/download/{fid}', | |
34 |
|
35 | |||
35 | }[name].format(**kwargs) |
|
36 | }[name].format(**kwargs) | |
36 |
|
37 | |||
37 | if params: |
|
38 | if params: | |
38 | base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) |
|
39 | base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) | |
39 | return base_url |
|
40 | return base_url | |
40 |
|
41 | |||
41 |
|
42 | |||
42 | class TestFileStoreViews(TestController): |
|
43 | class TestFileStoreViews(TestController): | |
43 |
|
44 | |||
44 | @pytest.mark.parametrize("fid, content, exists", [ |
|
45 | @pytest.mark.parametrize("fid, content, exists", [ | |
45 | ('abcde-0.jpg', "xxxxx", True), |
|
46 | ('abcde-0.jpg', "xxxxx", True), | |
46 | ('abcde-0.exe', "1234567", True), |
|
47 | ('abcde-0.exe', "1234567", True), | |
47 | ('abcde-0.jpg', "xxxxx", False), |
|
48 | ('abcde-0.jpg', "xxxxx", False), | |
48 | ]) |
|
49 | ]) | |
49 | def test_get_files_from_store(self, fid, content, exists, tmpdir): |
|
50 | def test_get_files_from_store(self, fid, content, exists, tmpdir, user_util): | |
50 | self.log_user() |
|
51 | user = self.log_user() | |
|
52 | user_id = user['user_id'] | |||
|
53 | repo_id = user_util.create_repo().repo_id | |||
51 | store_path = self.app._pyramid_settings[config_keys.store_path] |
|
54 | store_path = self.app._pyramid_settings[config_keys.store_path] | |
|
55 | store_uid = fid | |||
52 |
|
56 | |||
53 | if exists: |
|
57 | if exists: | |
54 | status = 200 |
|
58 | status = 200 | |
55 | store = utils.get_file_storage({config_keys.store_path: store_path}) |
|
59 | store = utils.get_file_storage({config_keys.store_path: store_path}) | |
56 | filesystem_file = os.path.join(str(tmpdir), fid) |
|
60 | filesystem_file = os.path.join(str(tmpdir), fid) | |
57 | with open(filesystem_file, 'wb') as f: |
|
61 | with open(filesystem_file, 'wb') as f: | |
58 | f.write(content) |
|
62 | f.write(content) | |
59 |
|
63 | |||
60 | with open(filesystem_file, 'rb') as f: |
|
64 | with open(filesystem_file, 'rb') as f: | |
61 |
|
|
65 | store_uid, metadata = store.save_file(f, fid, extra_metadata={'filename': fid}) | |
|
66 | ||||
|
67 | entry = FileStore.create( | |||
|
68 | file_uid=store_uid, filename=metadata["filename"], | |||
|
69 | file_hash=metadata["sha256"], file_size=metadata["size"], | |||
|
70 | file_display_name='file_display_name', | |||
|
71 | file_description='repo artifact `{}`'.format(metadata["filename"]), | |||
|
72 | check_acl=True, user_id=user_id, | |||
|
73 | scope_repo_id=repo_id | |||
|
74 | ) | |||
|
75 | Session().add(entry) | |||
|
76 | Session().commit() | |||
62 |
|
77 | |||
63 | else: |
|
78 | else: | |
64 | status = 404 |
|
79 | status = 404 | |
65 |
|
80 | |||
66 |
response = self.app.get(route_path('download_file', fid= |
|
81 | response = self.app.get(route_path('download_file', fid=store_uid), status=status) | |
67 |
|
82 | |||
68 | if exists: |
|
83 | if exists: | |
69 | assert response.text == content |
|
84 | assert response.text == content | |
70 |
file_store_path = os.path.dirname(store.resolve_name( |
|
85 | file_store_path = os.path.dirname(store.resolve_name(store_uid, store_path)[1]) | |
71 |
metadata_file = os.path.join(file_store_path, |
|
86 | metadata_file = os.path.join(file_store_path, store_uid + '.meta') | |
72 | assert os.path.exists(metadata_file) |
|
87 | assert os.path.exists(metadata_file) | |
73 | with open(metadata_file, 'rb') as f: |
|
88 | with open(metadata_file, 'rb') as f: | |
74 | json_data = json.loads(f.read()) |
|
89 | json_data = json.loads(f.read()) | |
75 |
|
90 | |||
76 | assert json_data |
|
91 | assert json_data | |
77 | assert 'size' in json_data |
|
92 | assert 'size' in json_data | |
78 |
|
93 | |||
79 | def test_upload_files_without_content_to_store(self): |
|
94 | def test_upload_files_without_content_to_store(self): | |
80 | self.log_user() |
|
95 | self.log_user() | |
81 | response = self.app.post( |
|
96 | response = self.app.post( | |
82 | route_path('upload_file'), |
|
97 | route_path('upload_file'), | |
83 | params={'csrf_token': self.csrf_token}, |
|
98 | params={'csrf_token': self.csrf_token}, | |
84 | status=200) |
|
99 | status=200) | |
85 |
|
100 | |||
86 | assert response.json == { |
|
101 | assert response.json == { | |
87 | u'error': u'store_file data field is missing', |
|
102 | u'error': u'store_file data field is missing', | |
88 | u'access_path': None, |
|
103 | u'access_path': None, | |
89 | u'store_fid': None} |
|
104 | u'store_fid': None} | |
90 |
|
105 | |||
91 | def test_upload_files_bogus_content_to_store(self): |
|
106 | def test_upload_files_bogus_content_to_store(self): | |
92 | self.log_user() |
|
107 | self.log_user() | |
93 | response = self.app.post( |
|
108 | response = self.app.post( | |
94 | route_path('upload_file'), |
|
109 | route_path('upload_file'), | |
95 | params={'csrf_token': self.csrf_token, 'store_file': 'bogus'}, |
|
110 | params={'csrf_token': self.csrf_token, 'store_file': 'bogus'}, | |
96 | status=200) |
|
111 | status=200) | |
97 |
|
112 | |||
98 | assert response.json == { |
|
113 | assert response.json == { | |
99 | u'error': u'filename cannot be read from the data field', |
|
114 | u'error': u'filename cannot be read from the data field', | |
100 | u'access_path': None, |
|
115 | u'access_path': None, | |
101 | u'store_fid': None} |
|
116 | u'store_fid': None} | |
102 |
|
117 | |||
103 | def test_upload_content_to_store(self): |
|
118 | def test_upload_content_to_store(self): | |
104 | self.log_user() |
|
119 | self.log_user() | |
105 | response = self.app.post( |
|
120 | response = self.app.post( | |
106 | route_path('upload_file'), |
|
121 | route_path('upload_file'), | |
107 | upload_files=[('store_file', 'myfile.txt', 'SOME CONTENT')], |
|
122 | upload_files=[('store_file', 'myfile.txt', 'SOME CONTENT')], | |
108 | params={'csrf_token': self.csrf_token}, |
|
123 | params={'csrf_token': self.csrf_token}, | |
109 | status=200) |
|
124 | status=200) | |
110 |
|
125 | |||
111 | assert response.json['store_fid'] |
|
126 | assert response.json['store_fid'] |
General Comments 0
You need to be logged in to leave comments.
Login now