def test_creation(self): user_id = pretend.stub() user_service = pretend.stub() form = forms.RecoveryCodeAuthenticationForm(user_id=user_id, user_service=user_service) assert form.user_id is user_id assert form.user_service is user_service
def test_valid_recovery_code(self): form = forms.RecoveryCodeAuthenticationForm( data={"recovery_code_value": "valid"}, user_id=pretend.stub(), user_service=pretend.stub(check_recovery_code=pretend. call_recorder(lambda *a, **kw: True)), ) assert form.validate()
def test_missing_value(self): form = forms.RecoveryCodeAuthenticationForm( data={"recovery_code_value": ""}, user_id=pretend.stub(), user_service=pretend.stub(), ) assert not form.validate() assert form.recovery_code_value.errors.pop( ) == "This field is required."
def test_invalid_recovery_code(self, pyramid_config): form = forms.RecoveryCodeAuthenticationForm( data={"recovery_code_value": "invalid"}, user_id=pretend.stub(), user_service=pretend.stub(check_recovery_code=pretend. call_recorder(lambda *a, **kw: False)), ) assert not form.validate() assert str( form.recovery_code_value.errors.pop()) == "Invalid Recovery Code."
def test_valid_recovery_code(self, monkeypatch): request = pretend.stub(remote_addr="1.2.3.4") user = pretend.stub(id=pretend.stub(), username="******") form = forms.RecoveryCodeAuthenticationForm( request=request, data={"recovery_code_value": "valid"}, user_id=pretend.stub(), user_service=pretend.stub( check_recovery_code=pretend.call_recorder( lambda *a, **kw: True), get_user=lambda _: user, ), ) send_recovery_code_used_email = pretend.call_recorder( lambda request, user: None) monkeypatch.setattr(forms, "send_recovery_code_used_email", send_recovery_code_used_email) assert form.validate() assert send_recovery_code_used_email.calls == [ pretend.call(request, user) ]
def test_invalid_recovery_code(self, pyramid_config, exception, expected_reason, expected_error): request = pretend.stub(remote_addr="127.0.0.1") user_service = pretend.stub( check_recovery_code=pretend.raiser(exception), record_event=pretend.call_recorder(lambda *a, **kw: None), ) form = forms.RecoveryCodeAuthenticationForm( request=request, data={"recovery_code_value": "invalid"}, user_id=1, user_service=user_service, ) assert not form.validate() assert str(form.recovery_code_value.errors.pop()) == expected_error assert user_service.record_event.calls == [ pretend.call( 1, tag="account:login:failure", additional={"reason": expected_reason}, ) ]