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

start adding some tests for API auth #28394

Merged
merged 11 commits into from
Aug 24, 2020
Merged

start adding some tests for API auth #28394

merged 11 commits into from
Aug 24, 2020

Conversation

czue
Copy link
Member

@czue czue commented Aug 19, 2020

Related to #28388 I thought it'd be good to have a test suite we can use to ensure we don't mess anything up.

We do have some tests around this functionality spread around, but they're all integration tests at the API-level and I thought it'd be better to be able to test the primitives.

This is just a start - planning on expanding it out tomorrow (but fine to review/merge as is)

@czue czue added the product/invisible Change has no end-user visible impact label Aug 19, 2020
class LoginAndDomainAuthenticationTest(AuthenticationTestBase):

def test_login_with_domain(self):
self.assertAuthenticationSuccess(LoginAndDomainAuthentication(), self._get_request(domain=self.domain))
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this fail since it has no auth?

Copy link
Member Author

@czue czue Aug 20, 2020

Choose a reason for hiding this comment

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

eagle eyes. Thanks, it was returning a 401 HttpResponse which was not failing on assertTrue. This whole "sometimes it's true or false and sometimes it's an HttpResponse" thing is really bugging me. I'm pretty sure it's entirely unneccessary, but just haven't gone through enough code to change it yet. Hopefully soon. 🙏

(fixed in the latest 3 commits, which revealed some other issues with the domain checks...)

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if the reason we do this is to expose things like this to the API callers, and whether it's worth keeping it that way because of that: https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/domain/decorators.py#L433-L437

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this is by design in Tastypie. Here's part of the docstring from is_authenticated:

Should return either ``True`` if allowed, ``False`` if not or an
        ``HttpResponse`` if you need something custom.

I guess the question is whether we should try and be more consistent and always return an HTTP response. I do think the custom messages are useful at times, particularly the one you linked.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah agreed, now that I get the use case I think the tradeoff between improvement in API docs is worth the additional code complexity

Copy link
Contributor

@millerdev millerdev left a comment

Choose a reason for hiding this comment

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

Approved, pending @snopoke's comment.

corehq/apps/api/tests/test_auth.py Outdated Show resolved Hide resolved
@czue
Copy link
Member Author

czue commented Aug 21, 2020

added some more checks. think this is ready for re-review

@snopoke snopoke merged commit 7c1a306 into master Aug 24, 2020
@snopoke snopoke deleted the cz/api-auth-tests branch August 24, 2020 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product/invisible Change has no end-user visible impact
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants