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

Add QueryableFake gateway #293

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

christopherdcunha
Copy link

Description

This change adds a fake gateway that can be queried programatically.

Motivation and Context

This allows my functional tests to receive tokens and complete the 2FA flow.

How Has This Been Tested?

This has been tested via the unittests in this PR and a functional test similar to the one in the docstring of the QueryableFake class.

Screenshots (if appropriate):

Documentation configuration.html#general-settings

image

Documentation configuration.html#queryablefake-gateway

image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@christopherdcunha christopherdcunha force-pushed the feature/add-queryable-fake-gateway-for-unit-tests branch from ebfc9ed to 26c56f5 Compare March 14, 2019 12:38
@codecov
Copy link

codecov bot commented Mar 14, 2019

Codecov Report

Merging #293 into master will increase coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #293      +/-   ##
=========================================
+ Coverage   96.54%   96.6%   +0.06%     
=========================================
  Files          39      39              
  Lines        1648    1681      +33     
  Branches      116     118       +2     
=========================================
+ Hits         1591    1624      +33     
  Misses         35      35              
  Partials       22      22
Impacted Files Coverage Δ
two_factor/gateways/fake.py 100% <100%> (ø) ⬆️
tests/test_gateways.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 268c0d6...cfc695d. Read the comment docs.

Copy link
Collaborator

@moggers87 moggers87 left a comment

Choose a reason for hiding this comment

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

I've got a few comments and questions, especially around how the new gateway would work with multiple tests.

Thanks for adding to the documentation (not enough people do that!)

@@ -19,6 +19,8 @@ General Settings
Twilio_.
* ``'two_factor.gateways.fake.Fake'`` for development, recording tokens to the
default logger.
* ``'two_factor.gateways.fake.QueryableFake'`` for testing, recording tokens
to the ``QueryableFake`` singleton.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The new gateway is not a singleton. I know what you're trying to say here (sms_tokens and call_tokens are initialised when the class is created not when an object is instantiated) but "singleton" would mean that things like QueryableFake().sms_tokens = {} would affect other instances of QueryableFake because there would only be one instance of the class.

@@ -117,3 +117,15 @@ def test_gateway(self, logger):
fake.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
logger.info.assert_called_with(
'Fake SMS to %s: "Your token is: %s"', '+123', code)


class QueryableFakeGatewayTest(TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

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

More tests please. E.g. do call_tokens and sms_tokens get reset properly after each testcase?

- Add the `QueryableFake.reset` method
- Add unit test explaining how to reset the QueryableFake
- Remove the mention of QueryableFake being a singleton
@christopherdcunha
Copy link
Author

Thanks for the review @moggers87 - I've added commit cfc695d which addresses the issues you mentioned (or tries to at least). If you are happy with the changes, then I'll squash the commit and push.

@@ -28,6 +30,8 @@ General Settings
Twilio_.
* ``'two_factor.gateways.fake.Fake'`` for development, recording tokens to the
default logger.
* ``'two_factor.gateways.fake.QueryableFake'`` for testing, recording tokens
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this is merged, the text "Currently two gateways are bundled:" no longer holds.

@Bouke
Copy link
Collaborator

Bouke commented May 12, 2019

An alternative would be to just use fixed tokens instead. This would simplify the tests, while allowing the same use case. For example, it could work e.g. like this:

  • 000000 -> valid token
  • 000001 -> invalid token

@christopherdcunha
Copy link
Author

Thanks for the review @Bouke

RE: The fixed tokens approach.

I think that you're suggesting an approach where PhoneDevice.verify_token(self, token) will simply return token == '000000'. And a test case would simply check if gateways.fake.Fake.send_sms(device, token) was called.

Is my understanding correct?

@dopry
Copy link
Contributor

dopry commented May 5, 2022

@Bouke @christopherdcunha, is this still something worth pursuing? It sounds like there may need to be some consensus about whether this is the best approach to testing.

@christopherdcunha
Copy link
Author

@Bouke @christopherdcunha, is this still something worth pursuing? It sounds like there may need to be some consensus about whether this is the best approach to testing.

Agreed. I personally prefer/use the approach I've suggested here, but I'm totally happy to close this PR if it doesn't make sense for the broader "library user base".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants