Example #1
0
    def test_reset_password_invalid_action(self, pyramid_request):
        data = {
            'action': 'invalid-action',
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data),
        )
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: pretend.stub(),
            ITokenService: token_service,
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token: not a password reset token", queue='error'
            ),
        ]
Example #2
0
    def test_reset_password_invalid_user(self, pyramid_request):
        data = {
            'action': 'password-reset',
            'user.id': '8ad1a4ac-e016-11e6-bf01-fe55135034f3',
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data),
        )
        user_service = pretend.stub(
            get_user=pretend.call_recorder(lambda userid: None),
        )
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: user_service,
            ITokenService: token_service,
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token - User not found", queue='error'
            ),
        ]
        assert user_service.get_user.calls == [
            pretend.call(uuid.UUID(data['user.id'])),
        ]
Example #3
0
    def test_reset_password_invalid_user(self, pyramid_request):
        data = {
            'action': 'password-reset',
            'user.id': '8ad1a4ac-e016-11e6-bf01-fe55135034f3',
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data),
        )
        user_service = pretend.stub(
            get_user=pretend.call_recorder(lambda userid: None),
        )
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: user_service,
            ITokenService: token_service,
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token: user not found", queue='error'
            ),
        ]
        assert user_service.get_user.calls == [
            pretend.call(uuid.UUID(data['user.id'])),
        ]
Example #4
0
    def test_reset_password_invalid_action(self, pyramid_request):
        data = {
            'action': 'invalid-action',
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data),
        )
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: pretend.stub(),
            ITokenService: token_service,
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token - Not a password reset token", queue='error'
            ),
        ]
Example #5
0
    def test_reset_password_invalid_user(self, pyramid_request):
        data = {
            "action": "password-reset",
            "user.id": "8ad1a4ac-e016-11e6-bf01-fe55135034f3",
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data))
        user_service = pretend.stub(
            get_user=pretend.call_recorder(lambda userid: None))
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: user_service,
            ITokenService: token_service,
            IPasswordBreachedService: pretend.stub(),
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None)

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call("accounts.request-password-reset")
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call("Invalid token: user not found", queue="error")
        ]
        assert user_service.get_user.calls == [
            pretend.call(uuid.UUID(data["user.id"]))
        ]
Example #6
0
    def test_reset_password_last_login_changed(self, pyramid_request):
        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(hours=1)
        data = {
            "action": "password-reset",
            "user.id": "8ad1a4ac-e016-11e6-bf01-fe55135034f3",
            "user.last_login": str(now),
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data))
        user = pretend.stub(last_login=later)
        user_service = pretend.stub(
            get_user=pretend.call_recorder(lambda userid: user))
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: user_service,
            ITokenService: token_service,
            IPasswordBreachedService: pretend.stub(),
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None)

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call("accounts.request-password-reset")
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token: user has logged in since this token was "
                "requested",
                queue="error",
            )
        ]
Example #7
0
    def test_invalid_token(self, pyramid_request, user_service, token_service):
        form_inst = pretend.stub()
        form_class = pretend.call_recorder(lambda *args, **kwargs: form_inst)

        def get_user_by_token(token):
            raise InvalidPasswordResetToken('message')

        pyramid_request.GET.update({"token": "RANDOM_KEY"})
        token_service.get_user_by_token = pretend.call_recorder(
            get_user_by_token)
        pyramid_request.find_service = pretend.call_recorder(
            lambda interface, **kwargs: {
                IUserService: user_service,
                IUserTokenService: token_service,
            }[interface])
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None)

        views.reset_password(pyramid_request, _form_class=form_class)

        assert form_class.calls == []
        assert pyramid_request.find_service.calls == [
            pretend.call(IUserService, context=None),
            pretend.call(IUserTokenService, context=None),
        ]
        assert token_service.get_user_by_token.calls == [
            pretend.call("RANDOM_KEY"),
        ]
        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call('message', queue='error'),
        ]
Example #8
0
    def test_reset_password_password_date_changed(self, pyramid_request):
        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(hours=1)
        data = {
            'action': 'password-reset',
            'user.id': '8ad1a4ac-e016-11e6-bf01-fe55135034f3',
            'user.last_login': str(now),
            'user.password_date': str(now),
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data), )
        user = pretend.stub(last_login=now, password_date=later)
        user_service = pretend.stub(
            get_user=pretend.call_recorder(lambda userid: user), )
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: user_service,
            ITokenService: token_service,
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None)

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token - Password has already been changed since this "
                "token was requested",
                queue='error',
            ),
        ]
Example #9
0
 def test_redirect_authenticated_user(self):
     pyramid_request = pretend.stub(authenticated_userid=1)
     pyramid_request.route_path = pretend.call_recorder(
         lambda a: "/the-redirect")
     result = views.reset_password(pyramid_request)
     assert isinstance(result, HTTPSeeOther)
     assert result.headers["Location"] == "/the-redirect"
Example #10
0
    def test_get(self, db_request, user_service, token_service):
        user = UserFactory.create()
        form_inst = pretend.stub()
        form_class = pretend.call_recorder(lambda *args, **kwargs: form_inst)

        db_request.GET.update({"token": "RANDOM_KEY"})
        token_service.get_user_by_token = pretend.call_recorder(
            lambda token: user)
        db_request.find_service = pretend.call_recorder(
            lambda interface, **kwargs: {
                IUserService: user_service,
                IUserTokenService: token_service,
            }[interface])

        result = views.reset_password(db_request, _form_class=form_class)

        assert result["form"] is form_inst
        assert form_class.calls == [
            pretend.call(
                db_request.GET,
                username=user.username,
                full_name=user.name,
                email=user.email,
                user_service=user_service,
            )
        ]
        assert token_service.get_user_by_token.calls == [
            pretend.call("RANDOM_KEY"),
        ]
        assert db_request.find_service.calls == [
            pretend.call(IUserService, context=None),
            pretend.call(IUserTokenService, context=None),
        ]
Example #11
0
 def test_redirect_authenticated_user(self):
     pyramid_request = pretend.stub(authenticated_userid=1)
     pyramid_request.route_path = pretend.call_recorder(
         lambda a: '/the-redirect'
     )
     result = views.reset_password(pyramid_request)
     assert isinstance(result, HTTPSeeOther)
     assert result.headers["Location"] == "/the-redirect"
Example #12
0
    def test_reset_password(self, db_request, user_service, token_service):
        user = UserFactory.create()
        db_request.method = "POST"
        db_request.POST.update({"token": "RANDOM_KEY"})
        form_obj = pretend.stub(
            new_password=pretend.stub(data="password_value"),
            validate=pretend.call_recorder(lambda *args: True))

        form_class = pretend.call_recorder(lambda *args, **kwargs: form_obj)

        db_request.route_path = pretend.call_recorder(lambda name: "/")
        token_service.loads = pretend.call_recorder(
            lambda token: {
                'action': 'password-reset',
                'user.id': str(user.id),
                'user.last_login': str(user.last_login),
                'user.password_date': str(user.password_date),
            })
        user_service.update_user = pretend.call_recorder(lambda *a, **kw: None)
        db_request.find_service = pretend.call_recorder(
            lambda interface, **kwargs: {
                IUserService: user_service,
                ITokenService: token_service,
            }[interface])
        db_request.session.flash = pretend.call_recorder(lambda *a, **kw: None)

        now = datetime.datetime.utcnow()

        with freezegun.freeze_time(now):
            result = views.reset_password(db_request, _form_class=form_class)

        assert isinstance(result, HTTPSeeOther)
        assert result.headers["Location"] == "/"
        assert form_obj.validate.calls == [pretend.call()]
        assert form_class.calls == [
            pretend.call(token=db_request.params['token'],
                         username=user.username,
                         full_name=user.name,
                         email=user.email,
                         user_service=user_service),
        ]
        assert db_request.route_path.calls == [pretend.call('index')]
        assert token_service.loads.calls == [
            pretend.call('RANDOM_KEY'),
        ]
        assert user_service.update_user.calls == [
            pretend.call(user.id, password=form_obj.new_password.data),
            pretend.call(user.id, last_login=now),
        ]
        assert db_request.session.flash.calls == [
            pretend.call("You have successfully reset your password",
                         queue="success"),
        ]
        assert db_request.find_service.calls == [
            pretend.call(IUserService, context=None),
            pretend.call(ITokenService, name="password"),
            pretend.call(IUserService, context=None),
        ]
Example #13
0
    def test_reset_password_loads_failure(self, pyramid_request, exception,
                                          message):
        def loads(token):
            raise exception

        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: pretend.stub(),
            ITokenService: pretend.stub(loads=loads),
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None)

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(message, queue='error'),
        ]
Example #14
0
    def test_reset_password_password_date_changed(self, pyramid_request):
        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(hours=1)
        data = {
            'action': 'password-reset',
            'user.id': '8ad1a4ac-e016-11e6-bf01-fe55135034f3',
            'user.last_login': str(now),
            'user.password_date': str(now),
        }
        token_service = pretend.stub(
            loads=pretend.call_recorder(lambda token: data),
        )
        user = pretend.stub(last_login=now, password_date=later)
        user_service = pretend.stub(
            get_user=pretend.call_recorder(lambda userid: user),
        )
        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: user_service,
            ITokenService: token_service,
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(
                "Invalid token - Password has already been changed since this "
                "token was requested",
                queue='error',
            ),
        ]
Example #15
0
    def test_reset_password_loads_failure(
            self, pyramid_request, exception, message):

        def loads(token):
            raise exception

        pyramid_request.find_service = lambda interface, **kwargs: {
            IUserService: pretend.stub(),
            ITokenService: pretend.stub(loads=loads),
        }[interface]
        pyramid_request.params = {"token": "RANDOM_KEY"}
        pyramid_request.route_path = pretend.call_recorder(lambda name: "/")
        pyramid_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        views.reset_password(pyramid_request)

        assert pyramid_request.route_path.calls == [
            pretend.call('accounts.request-password-reset'),
        ]
        assert pyramid_request.session.flash.calls == [
            pretend.call(message, queue='error'),
        ]
Example #16
0
    def test_get(self, db_request, user_service, token_service):
        user = UserFactory.create()
        form_inst = pretend.stub()
        form_class = pretend.call_recorder(lambda *args, **kwargs: form_inst)

        breach_service = pretend.stub(check_password=lambda pw: False)

        db_request.GET.update({"token": "RANDOM_KEY"})
        token_service.loads = pretend.call_recorder(
            lambda token: {
                "action": "password-reset",
                "user.id": str(user.id),
                "user.last_login": str(user.last_login),
                "user.password_date": str(user.password_date),
            })
        db_request.find_service = pretend.call_recorder(
            lambda interface, **kwargs: {
                IUserService: user_service,
                ITokenService: token_service,
                IPasswordBreachedService: breach_service,
            }[interface])

        result = views.reset_password(db_request, _form_class=form_class)

        assert result["form"] is form_inst
        assert form_class.calls == [
            pretend.call(
                token=db_request.params["token"],
                username=user.username,
                full_name=user.name,
                email=user.email,
                user_service=user_service,
                breach_service=breach_service,
            )
        ]
        assert token_service.loads.calls == [pretend.call("RANDOM_KEY")]
        assert db_request.find_service.calls == [
            pretend.call(IUserService, context=None),
            pretend.call(IPasswordBreachedService, context=None),
            pretend.call(ITokenService, name="password"),
        ]
Example #17
0
    def test_get(self, db_request, user_service, token_service):
        user = UserFactory.create()
        form_inst = pretend.stub()
        form_class = pretend.call_recorder(lambda *args, **kwargs: form_inst)

        db_request.GET.update({"token": "RANDOM_KEY"})
        token_service.loads = pretend.call_recorder(
            lambda token: {
                'action': 'password-reset',
                'user.id': str(user.id),
                'user.last_login': str(user.last_login),
                'user.password_date': str(user.password_date),
            }
        )
        db_request.find_service = pretend.call_recorder(
            lambda interface, **kwargs: {
                IUserService: user_service,
                ITokenService: token_service,
            }[interface]
        )

        result = views.reset_password(db_request, _form_class=form_class)

        assert result["form"] is form_inst
        assert form_class.calls == [
            pretend.call(
                db_request.GET,
                username=user.username,
                full_name=user.name,
                email=user.email,
                user_service=user_service,
            )
        ]
        assert token_service.loads.calls == [
            pretend.call("RANDOM_KEY"),
        ]
        assert db_request.find_service.calls == [
            pretend.call(IUserService, context=None),
            pretend.call(ITokenService, name="password"),
        ]
Example #18
0
    def test_reset_password(self, db_request, user_service, token_service):
        user = UserFactory.create()
        db_request.method = "POST"
        db_request.POST.update({"token": "RANDOM_KEY"})
        form_obj = pretend.stub(
            password=pretend.stub(data="password_value"),
            validate=pretend.call_recorder(lambda *args: True)
        )

        form_class = pretend.call_recorder(lambda *args, **kwargs: form_obj)

        db_request.route_path = pretend.call_recorder(lambda name: "/")
        token_service.loads = pretend.call_recorder(
            lambda token: {
                'action': 'password-reset',
                'user.id': str(user.id),
                'user.last_login': str(user.last_login),
                'user.password_date': str(user.password_date),
            }
        )
        user_service.update_user = pretend.call_recorder(lambda *a, **kw: None)
        db_request.find_service = pretend.call_recorder(
            lambda interface, **kwargs: {
                IUserService: user_service,
                ITokenService: token_service,
            }[interface]
        )
        db_request.session.flash = pretend.call_recorder(
            lambda *a, **kw: None
        )

        now = datetime.datetime.utcnow()

        with freezegun.freeze_time(now):
            result = views.reset_password(db_request, _form_class=form_class)

        assert isinstance(result, HTTPSeeOther)
        assert result.headers["Location"] == "/"
        assert form_obj.validate.calls == [pretend.call()]
        assert form_class.calls == [
            pretend.call(
                db_request.POST,
                username=user.username,
                full_name=user.name,
                email=user.email,
                user_service=user_service
            ),
        ]
        assert db_request.route_path.calls == [pretend.call('index')]
        assert token_service.loads.calls == [
            pretend.call('RANDOM_KEY'),
        ]
        assert user_service.update_user.calls == [
            pretend.call(user.id, password=form_obj.password.data),
            pretend.call(user.id, last_login=now),
        ]
        assert db_request.session.flash.calls == [
            pretend.call(
                "You have successfully reset your password", queue="success"
            ),
        ]
        assert db_request.find_service.calls == [
            pretend.call(IUserService, context=None),
            pretend.call(ITokenService, name="password"),
            pretend.call(IUserService, context=None),
        ]