-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
New test is failing in CI because it fails to connect to an eligibility server. I think we'll either need to
|
I think mocking is the way to go, even if we end up moving/copying it to the other repo soon. |
506d75b
to
5f77f27
Compare
@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.
fdbf62e
to
9e4e0cf
Compare
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. |
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.
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.
👏
For sure! |
Ok, thanks for the feedback @afeld! I'll put this PR back into draft and see what additional coverage I can get in. |
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.
9e4e0cf
to
b5fd5c6
Compare
There was a problem hiding this 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!
I'm going to keep this PR open at least through Monday in case @thekaveman wanted to look through it too. |
There was a problem hiding this 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.
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! 🙏 |
Closes #550
Summary
This PR adds new unit tests for the
eligibility
app'sconfirm
view function. The Eligibility API is consumed as a part of the logic forconfirm
- namely, the part that callsclient.verify(sub, name)
.Tests added
test_confirm_success
benefits
fixture data for creation of mock response. I copied these from eligibility-servertest_confirm_failure_error_on_request
ApiError
when an exception prevents us from successfully sending a request to the APItest_confirm_failure_unexpected_status_code
ApiError
test_confirm_failure_error_tokenizing_request
TokenError
test_confirm_failure_error_tokenizing_response
TokenError
when an exception prevents us from tokenizing the responseNotes
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.