Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Eligibility API #575

Merged
merged 8 commits into from
May 18, 2022
Merged

Tests: Eligibility API #575

merged 8 commits into from
May 18, 2022

Conversation

angela-tran
Copy link
Member

@angela-tran angela-tran commented May 11, 2022

Closes #550

Summary

This PR adds new unit tests for the eligibility app's confirm view function. The Eligibility API is consumed as a part of the logic for confirm - namely, the part that calls client.verify(sub, name).

Tests added

  • test_confirm_success
    • Tests the happy path
    • Requires use of working server private key and client public key that correspond with benefits fixture data for creation of mock response. I copied these from eligibility-server
  • test_confirm_failure_error_on_request
    • Parameterized test that checks we throw an ApiError when an exception prevents us from successfully sending a request to the API
  • test_confirm_failure_unexpected_status_code
    • Tests that unexpected response codes throw an ApiError
  • test_confirm_failure_error_tokenizing_request
    • Tests that exception thrown during request tokenization results in a TokenError
  • test_confirm_failure_error_tokenizing_response
    • Parameterized test that checks we throw a TokenError when an exception prevents us from tokenizing the response

Notes

These tests try to cover the various success/error paths as much as possible. We can also add tests with more nuanced inputs in the eligibility-api repo when we take on #141.

@angela-tran angela-tran added this to the Eligibility API refactor milestone May 11, 2022
@angela-tran angela-tran self-assigned this May 11, 2022
@github-actions github-actions bot added the tests Related to automated testing (unit, UI, integration, etc.) label May 11, 2022
@angela-tran
Copy link
Member Author

angela-tran commented May 11, 2022

New test is failing in CI because it fails to connect to an eligibility server.

I think we'll either need to

@angela-tran angela-tran changed the title Eligibility API tests Tests: Eligibility API May 11, 2022
@thekaveman
Copy link
Member

We definitely don't want pytest tests hitting anything real - e.g. #553 just disabled pytest ability to make external network requests.

Don't worry about refactoring things later for #141 - the task here is to get tests running for the current Eligibility API code.

@afeld
Copy link
Contributor

afeld commented May 11, 2022

I think mocking is the way to go, even if we end up moving/copying it to the other repo soon.

@angela-tran
Copy link
Member Author

angela-tran commented May 11, 2022

@thekaveman @afeld How much coverage do you think we should strive for here? There are several error paths with the Eligibility API code - for example, when making the request, when getting back the response, and various points when parsing the response (reading response text , decrypting the signed token, verifying the signature). Just want to make sure we actually want all that coverage before I start writing tests for all those different errors.

Also, I was able to mock the eligibility-server response. Does the approach of using HTTPretty look ok to you?

Also, remove comments about POST body needing to match server
implementation.

Our tests should not require a real instance to be up and running, and
in particular, our CI configuration is such that we will not have one.
@angela-tran
Copy link
Member Author

@thekaveman @afeld How much coverage do you think we should strive for here? There are several error paths with the Eligibility API code - for example, when making the request, when getting back the response, and various points when parsing the response (reading response text , decrypting the signed token, verifying the signature). Just want to make sure we actually want all that coverage before I start writing tests for all those different errors.

Also, I was able to mock the eligibility-server response. Does the approach of using HTTPretty look ok to you?

Re: coverage, I decided that the cases related to decrypting/verifying signatures don't need to be done here and would be better/easier to write tests for in the eligibility-api repo.

@angela-tran angela-tran marked this pull request as ready for review May 11, 2022 23:57
@angela-tran angela-tran requested a review from a team as a code owner May 11, 2022 23:57
@afeld
Copy link
Contributor

afeld commented May 12, 2022

How much coverage do you think we should strive for here?

I say "write tests until you're sick of it" 😉 Just to throw a number out there, I'm happy around 90% overall and per file (with some lower, as some code isn't easily testable). Arguably should be higher given while we are without error monitoring (#448). That said, some tests are better than no tests (for this part of the codebase), so I would merge this sooner than later.

want to make sure we actually want all that coverage before I start writing tests for all those different errors … cases related to decrypting/verifying signatures don't need to be done here and would be better/easier to write tests for in the eligibility-api repo

You may find the tests easier to write if you refactor smaller functions out of the Eligibility views and write unit tests against those… that refactored code would then more easily get turned into a standalone package. In other words, it may be easiest to build out the eligibility-api package in this repository as just standalone Python files, then move it to the other repository once it's stabilized a bit.

I was able to mock the eligibility-server response.

👏

Does the approach of using HTTPretty look ok to you?

For sure!

@angela-tran
Copy link
Member Author

Ok, thanks for the feedback @afeld! I'll put this PR back into draft and see what additional coverage I can get in.

@angela-tran angela-tran marked this pull request as draft May 12, 2022 21:11
The approach of throwing an exception in the body of the HTTPretty
response was causing a PytestUnhandledThreadExceptionWarning to be
logged. Switching that out in favor of mocker.patch is sufficient for
our needs.
There were two cases where I could not figure out the needed
input-munging to cause the desired exceptions. Left these cases as
comments.
@angela-tran
Copy link
Member Author

We went from 27% coverage to 95% coverage for eligibility/api.py!

Before After
image image

@angela-tran angela-tran marked this pull request as ready for review May 13, 2022 01:45
Copy link
Contributor

@afeld afeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the one comment. Otherwise, good to go!

tests/pytest/eligibility/test_views.py Outdated Show resolved Hide resolved
@angela-tran
Copy link
Member Author

I'm going to keep this PR open at least through Monday in case @thekaveman wanted to look through it too.

Copy link
Member

@thekaveman thekaveman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to block since this is definitely better than the low coverage in this area that we had before!

This feels like combining a few different concerns into the same tests though, namely how the form/view work, and how the API client works. I am also not a fan of what is essentially a reimplementation of the token encryption and signing logic as a helper for test setup. That feels... bad.

I would think the view tests should use a fully mocked client, not even getting to the point of having to mock HTTP requests, encryption, etc. These tests would ensure the view logic is correct when various inputs and outputs of the API are processed.

Separate tests for the Eligibility API / client / etc. could get in to the mechanics of working with the request and response tokens, encryption keys, servers, and the like.

@angela-tran
Copy link
Member Author

This feels like combining a few different concerns into the same tests though, namely how the form/view work, and how the API client works.

This is a really good point, and I'm glad you pointed this out. I've created #595 to cover this, which should be done as a part of the Eligibility API refactor (should not be left behind as technical debt).

Thank you @afeld and @thekaveman for your thorough reviews and feedback! 🙏

@angela-tran angela-tran merged commit f8b5e52 into dev May 18, 2022
@angela-tran angela-tran deleted the tests/eligibility-api branch May 18, 2022 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Related to automated testing (unit, UI, integration, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tests for eligibility API
3 participants