Changeset 76ebb1b in OpenWorkouts-current for ow/tests/views/test_user.py


Ignore:
Timestamp:
Feb 18, 2019, 12:54:45 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
4af38e8
Parents:
d6da99e
Message:

(#29) Add user verification by email on signup.

From now on, when a new user signs up, we set the account into an "unverified"
state. In order to complete the signup procedure, the user has to click on a
link we send by email to the email address provided on signup.

IMPORTANT: A new dependency has been added, pyramid_mailer, so remember to
install it in any existing openworkouts environment (this is done automatically
if you use the ./bin/start script):

pip install pyramid_mailer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/views/test_user.py

    rd6da99e r76ebb1b  
    66from unittest.mock import Mock, patch
    77from io import BytesIO
     8from uuid import UUID
    89
    910import pytest
     
    112113            })
    113114        return request
     115
     116    @patch('ow.views.user.remember')
     117    def test_verify_already_verified(self, remember, dummy_request, john):
     118        john.verified = True
     119        response = user_views.verify(john, dummy_request)
     120        assert isinstance(response, HTTPFound)
     121        assert response.location == dummy_request.resource_url(john)
     122        # user was not authenticated
     123        assert not remember.called
     124        # verified status did not change
     125        assert john.verified
     126
     127    @patch('ow.views.user.remember')
     128    def test_verify_no_subpath(self, remember, dummy_request, john):
     129        response = user_views.verify(john, dummy_request)
     130        # the verify info page is rendered, we don't pass anything to the
     131        # rendering context
     132        assert response == {}
     133        # user was not authenticated
     134        assert not remember.called
     135        # verified status did not change
     136        assert not john.verified
     137
     138    def test_verify_subpath_not_verified(self, dummy_request, john):
     139        dummy_request.subpath = ['not_the_token']
     140        response = user_views.verify(john, dummy_request)
     141        # the verify info page is rendered, we don't pass anything to the
     142        # rendering context
     143        assert response == {}
     144
     145    @patch('ow.views.user.remember')
     146    def test_verify_wrong_token(self, remember, dummy_request, john):
     147        token = 'some-uuid4'
     148        john.verification_token = 'some-other-uuid4'
     149        dummy_request.subpath = [token]
     150        response = user_views.verify(john, dummy_request)
     151        # the verify info page is rendered, we don't pass anything to the
     152        # rendering context
     153        assert response == {}
     154        # user was not authenticated
     155        assert not remember.called
     156        # verified status did not change, neither did the token
     157        assert not john.verified
     158        assert john.verification_token == 'some-other-uuid4'
     159
     160    @patch('ow.views.user.remember')
     161    def test_verify_verifying(self, remember, dummy_request, john):
     162        token = 'some-uuid4'
     163        john.verification_token = token
     164        dummy_request.subpath = [token]
     165        response = user_views.verify(john, dummy_request)
     166        # redirect to user page
     167        assert isinstance(response, HTTPFound)
     168        assert response.location == dummy_request.resource_url(john)
     169        # user was authenticated after verified
     170        remember.assert_called_with(dummy_request, str(john.uid))
     171        # user has been verified
     172        assert john.verified
    114173
    115174    def test_dashboard_redirect_unauthenticated(self, root):
     
    369428        request.POST['email'] = 'john.doe@example.net'
    370429        request.POST['password'] = 'badpassword'
     430        # verify the user first
     431        request.root.users[0].verified = True
    371432        response = user_views.login(request.root, request)
    372433        assert response['message'] == u'Wrong password'
    373434
    374435    @patch('ow.views.user.remember')
    375     def test_login_post_ok(self, rem, dummy_request, john):
     436    def test_login_post_unverified(self, rem, dummy_request, john):
    376437        request = dummy_request
    377438        request.method = 'POST'
     
    379440        request.POST['email'] = 'john.doe@example.net'
    380441        request.POST['password'] = 's3cr3t'
     442        response = user_views.login(request.root, request)
     443        assert response['message'] == u'You have to verify your account first'
     444
     445    @patch('ow.views.user.remember')
     446    def test_login_post_ok(self, rem, dummy_request, john):
     447        request = dummy_request
     448        request.method = 'POST'
     449        request.POST['submit'] = True
     450        request.POST['email'] = 'john.doe@example.net'
     451        request.POST['password'] = 's3cr3t'
     452        # verify the user first
     453        john.verified = True
    381454        response = user_views.login(request.root, request)
    382455        assert isinstance(response, HTTPFound)
     
    620693        assert response['form'].errorlist() == ''
    621694
    622     def test_signup_post_ok(self, signup_post_request):
     695    @patch('ow.views.user.send_verification_email')
     696    def test_signup_post_ok(self, sve, signup_post_request):
    623697        request = signup_post_request
    624698        assert 'jack.black@example.net' not in request.root.emails
     
    629703        assert 'jack.black@example.net' in request.root.emails
    630704        assert 'JackBlack' in request.root.all_nicknames
     705        # user is in "unverified" state
     706        user = request.root.get_user_by_email('jack.black@example.net')
     707        assert not user.verified
     708        assert isinstance(user.verification_token, UUID)
     709        # also, we sent an email to that user
     710        sve.assert_called_once_with(request, user)
    631711
    632712    def test_signup_missing_required(self, signup_post_request):
Note: See TracChangeset for help on using the changeset viewer.