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