Skip to content

Commit

Permalink
OCT-1861: Cover /recalculate with API Tests (#394)
Browse files Browse the repository at this point in the history
## 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).
  • Loading branch information
kgarbacinski authored Sep 2, 2024
1 parent 44a889f commit 143eabd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
60 changes: 60 additions & 0 deletions backend/tests/api-e2e/test_api_delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 143eabd

Please sign in to comment.