|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +from pretend import call, call_recorder, stub |
| 13 | + |
| 14 | +from warehouse import tuf |
| 15 | + |
| 16 | + |
| 17 | +class TestTUFTasks: |
| 18 | + task_id = "123456" |
| 19 | + |
| 20 | + def test_update_metadata(self, db_request, monkeypatch): |
| 21 | + project_id = "id" |
| 22 | + project_name = "name" |
| 23 | + |
| 24 | + project = stub(normalized_name=project_name) |
| 25 | + |
| 26 | + one = call_recorder(lambda: project) |
| 27 | + db_request.db.query = lambda a: stub(filter=lambda a: stub(one=one)) |
| 28 | + |
| 29 | + rstuf_url = "url" |
| 30 | + index_digest = "digest" |
| 31 | + index_size = 42 |
| 32 | + |
| 33 | + db_request.registry.settings = {"rstuf.api_url": rstuf_url} |
| 34 | + |
| 35 | + render = call_recorder(lambda *a, **kw: (index_digest, None, index_size)) |
| 36 | + tuf.tasks.render_simple_detail = render |
| 37 | + |
| 38 | + post = call_recorder(lambda *a: self.task_id) |
| 39 | + monkeypatch.setattr(tuf.tasks, "post_artifacts", post) |
| 40 | + |
| 41 | + wait = call_recorder(lambda *a: None) |
| 42 | + monkeypatch.setattr(tuf.tasks, "wait_for_success", wait) |
| 43 | + |
| 44 | + tuf.tasks.update_metadata(db_request, project_id) |
| 45 | + assert one.calls == [call()] |
| 46 | + assert render.calls == [call(project, db_request, store=True)] |
| 47 | + assert post.calls == [ |
| 48 | + call( |
| 49 | + rstuf_url, |
| 50 | + { |
| 51 | + "targets": [ |
| 52 | + { |
| 53 | + "path": project_name, |
| 54 | + "info": { |
| 55 | + "length": index_size, |
| 56 | + "hashes": {"blake2b-256": index_digest}, |
| 57 | + }, |
| 58 | + } |
| 59 | + ] |
| 60 | + }, |
| 61 | + ) |
| 62 | + ] |
| 63 | + assert wait.calls == [call(rstuf_url, self.task_id)] |
| 64 | + |
| 65 | + def test_update_metadata_no_rstuf_api_url(self, db_request): |
| 66 | + project_id = "id" |
| 67 | + project_name = "name" |
| 68 | + |
| 69 | + project = stub(normalized_name=project_name) |
| 70 | + |
| 71 | + one = call_recorder(lambda: project) |
| 72 | + db_request.db.query = lambda a: stub(filter=lambda a: stub(one=one)) |
| 73 | + |
| 74 | + # Test early return, if no RSTUF API URL configured |
| 75 | + db_request.registry.settings = {"rstuf.api_url": None} |
| 76 | + tuf.tasks.update_metadata(db_request, project_id) |
| 77 | + |
| 78 | + assert not one.calls |
0 commit comments