From 143eabdc0b949bcb0a39f2a53cfb34d251a13b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Garbaci=C5=84ski?= <57113816+kgarbacinski@users.noreply.github.com> Date: Mon, 2 Sep 2024 14:36:10 +0200 Subject: [PATCH] OCT-1861: Cover /recalculate with API Tests (#394) ## Description ## Definition of Done 1. [ ] Acceptance criteria are met. 2. [ ] PR is manually tested before the merge by developer(s). - [ ] Happy path is manually checked. 3. [ ] PR is manually tested by QA when their assistance is required (1). - [ ] Octant Areas & Test Cases are checked for impact and updated if required (2). 4. [ ] Unit tests are added unless there is a reason to omit them. 5. [ ] Automated tests are added when required. 6. [ ] The code is merged. 7. [ ] Tech documentation is added / updated, reviewed and approved (including mandatory approval by a code owner, should such exist for changed files). - [ ] BE: Swagger documentation is updated. 8. [ ] When required by QA: - [ ] Deployed to the relevant environment. - [ ] Passed system tests. --- (1) Developer(s) in coordination with QA decide whether it's required. For small tickets introducing small changes QA assistance is most probably not required. (2) [Octant Areas & Test Cases](https://docs.google.com/spreadsheets/d/1cRe6dxuKJV3a4ZskAwWEPvrFkQm6rEfyUCYwLTYw_Cc). --- backend/tests/api-e2e/test_api_delegation.py | 60 ++++++++++++++++++++ backend/tests/conftest.py | 18 ++++++ 2 files changed, 78 insertions(+) diff --git a/backend/tests/api-e2e/test_api_delegation.py b/backend/tests/api-e2e/test_api_delegation.py index 8722656b86..cf6bfdcc04 100644 --- a/backend/tests/api-e2e/test_api_delegation.py +++ b/backend/tests/api-e2e/test_api_delegation.py @@ -57,6 +57,66 @@ def test_delegation(client: Client, payload: ScoreDelegationPayload): assert delegatee_score["status"] == "Known" assert float(delegatee_score["score"]) == float(delegator_score["score"]) + # check if the secondary address is actually used off + resp, code = client.delegate( + primary_address=payload.primary_addr, + secondary_address=payload.secondary_addr, + primary_address_signature=payload.primary_addr_signature, + secondary_address_signature=payload.secondary_addr_signature, + ) + + assert code == 400 + assert resp["message"] == "Delegation already exists" + + +@pytest.mark.api +def test_recalculate_in_delegation(client: Client, payload: ScoreDelegationPayload): + """ + Recalculation can actually return two different results: + - if the delegation does not exist, it will return 400 + - if the delegation exists, i.e. secondary address exists in the database, it will return 400 + it's due to the fact that the recalculation is already stoned for a secondary address in our implementation + """ + client.move_to_next_epoch(STARTING_EPOCH + 1) + client.move_to_next_epoch(STARTING_EPOCH + 2) + client.move_to_next_epoch(STARTING_EPOCH + 3) + + epoch_no = client.wait_for_sync(STARTING_EPOCH + 3) + app.logger.debug(f"indexed epoch: {epoch_no}") + + database.user.add_user(USER1_ADDRESS) + database.user.add_user(USER2_ADDRESS) + + # try to recalculate before delegation + data, status = client.delegation_recalculate( + primary_address=payload.primary_addr, + secondary_address=payload.secondary_addr, + primary_address_signature=payload.primary_addr_signature, + secondary_address_signature=payload.secondary_addr_signature, + ) + assert data["message"] == "Delegation does not exists" + assert status == 400 + + # make a delegation + _, status = client.delegate( + primary_address=payload.primary_addr, + secondary_address=payload.secondary_addr, + primary_address_signature=payload.primary_addr_signature, + secondary_address_signature=payload.secondary_addr_signature, + ) + assert status == 201 + + # recalculate after delegation + data, status = client.delegation_recalculate( + primary_address=payload.primary_addr, + secondary_address=payload.secondary_addr, + primary_address_signature=payload.primary_addr_signature, + secondary_address_signature=payload.secondary_addr_signature, + ) + + assert data["message"] == "Invalid recalculation request" + assert status == 400 + @pytest.mark.api def test_check_delegation(client: Client, payload: ScoreDelegationPayload): diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index d1a9bcc027..a6533926ce 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -957,6 +957,24 @@ def delegate( ) return json.loads(rv.text), rv.status_code + def delegation_recalculate( + self, + primary_address: str, + secondary_address: str, + primary_address_signature: str, + secondary_address_signature: str, + ) -> tuple[dict, int]: + rv = self._flask_client.put( + "/delegation/recalculate", + json={ + "primaryAddr": primary_address, + "secondaryAddr": secondary_address, + "primaryAddrSignature": primary_address_signature, + "secondaryAddrSignature": secondary_address_signature, + }, + ) + return json.loads(rv.text), rv.status_code + @property def config(self): return self._flask_client.application.config