Skip to content

Commit

Permalink
Add tuf tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Puehringer <[email protected]>
  • Loading branch information
lukpueh committed Jun 20, 2024
1 parent fbc76f0 commit 34a749f
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/unit/tuf/test_tuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ def test_post_bootstrap(self, monkeypatch):
with pytest.raises(tuf.RSTUFError):
tuf.post_bootstrap(self.server, payload)

def test_post_artifacts(self, monkeypatch):
payload = {
"targets": [
{
"path": "foo",
"info": {
"length": 42,
"hashes": {"blake2b-256": "deadbeef"},
},
}
]
}

resp_json = {"data": {"task_id": self.task_id}}
resp = stub(
raise_for_status=(lambda *a: None), json=(lambda *a, **kw: resp_json)
)
post = call_recorder(lambda *a, **kw: resp)
monkeypatch.setattr(tuf.requests, "post", post)

# Test success
result = tuf.post_artifacts(self.server, payload)

assert result == self.task_id
assert post.calls == [call(f"{self.server}/api/v1/artifacts", json=payload)]

# Test fail with incomplete response json (i.e. no bootstrap error)
del resp_json["data"]
with pytest.raises(tuf.RSTUFNoBootstrapError):
tuf.post_artifacts(self.server, payload)

def test_wait_for_success(self, monkeypatch):
get_task_state = call_recorder(lambda *a: "SUCCESS")
monkeypatch.setattr(tuf, "get_task_state", get_task_state)
Expand Down Expand Up @@ -88,3 +119,55 @@ def test_wait_for_success_error(self, state, iterations, monkeypatch):
tuf.wait_for_success(self.server, self.task_id)

assert get_task_state.calls == [call(self.server, self.task_id)] * iterations

def test_update_metadata(self, db_request, monkeypatch):
project_id = "id"
project_name = "name"

project = stub(normalized_name=project_name)

one = call_recorder(lambda: project)
db_request.db.query = lambda a: stub(filter=lambda a: stub(one=one))

# Test early return, if no RSTUF API URL configured
db_request.registry.settings = {"tuf.rstuf_api_url": None}
tuf.update_metadata(db_request, project_id)

assert not one.calls

# Test regular run
rstuf_url = "url"
index_digest = "digest"
index_size = 42

db_request.registry.settings = {"tuf.rstuf_api_url": rstuf_url}

render = call_recorder(lambda *a, **kw: (index_digest, None, index_size))
monkeypatch.setattr(tuf, "render_simple_detail", render)

post = call_recorder(lambda *a: self.task_id)
monkeypatch.setattr(tuf, "post_artifacts", post)

wait = call_recorder(lambda *a: None)
monkeypatch.setattr(tuf, "wait_for_success", wait)

tuf.update_metadata(db_request, project_id)
assert one.calls == [call()]
assert render.calls == [call(project, db_request, store=True)]
assert post.calls == [
call(
rstuf_url,
{
"targets": [
{
"path": project_name,
"info": {
"length": index_size,
"hashes": {"blake2b-256": index_digest},
},
}
]
},
)
]
assert wait.calls == [call(rstuf_url, self.task_id)]

0 comments on commit 34a749f

Please sign in to comment.