def test_get_when_not_logged_in_404s_if_user_not_found( self, pyramid_request, user_model): pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} user_model.get_by_activation.return_value = None with pytest.raises(httpexceptions.HTTPNotFound): views.ActivateController(pyramid_request).get_when_not_logged_in()
def test_get_when_not_logged_in_redirects_if_activation_not_found( self, activation_model, pyramid_request): """ If the activation code doesn't match any activation then we redirect to the front page and flash a message suggesting that they may already be activated and can log in. This happens if a user clicks on an activation link from an email after they've already been activated, for example. (This also happens if users visit a bogus activation URL, but we're happy to do this same redirect in that edge case.) """ pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} activation_model.get_by_code.return_value = None result = views.ActivateController( pyramid_request).get_when_not_logged_in() error_flash = pyramid_request.session.peek_flash('error') assert isinstance(result, httpexceptions.HTTPFound) assert error_flash assert error_flash[0].startswith( "We didn't recognize that activation link.")
def test_get_when_not_logged_in_404s_if_id_not_int(self): request = DummyRequest(matchdict={ 'id': 'abc', # Not an int. 'code': 'abc456'}) with pytest.raises(httpexceptions.HTTPNotFound): views.ActivateController(request).get_when_not_logged_in()
def test_get_when_not_logged_in_successful_notifies( self, notify, pyramid_request, user_model, ActivationEvent): pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} user_model.get_by_activation.return_value.id = 123 views.ActivateController(pyramid_request).get_when_not_logged_in() notify.assert_called_once_with(ActivationEvent.return_value)
def test_get_when_logged_in_already_logged_in_when_id_not_an_int(self): request = DummyRequest(matchdict={ 'id': 'abc', # Not an int. 'code': 'abc456'}, authenticated_user=mock.Mock(id=123, spec=['id'])) with pytest.raises(httpexceptions.HTTPNotFound): views.ActivateController(request).get_when_logged_in()
def test_get_when_not_logged_in_looks_up_activation_by_code( self, activation_model, user_model): request = DummyRequest(matchdict={'id': '123', 'code': 'abc456'}) user_model.get_by_activation.return_value.id = 123 views.ActivateController(request).get_when_not_logged_in() activation_model.get_by_code.assert_called_with('abc456')
def test_get_when_not_logged_in_looks_up_user_by_activation( self, activation_model, pyramid_request, user_model): pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} user_model.get_by_activation.return_value.id = 123 views.ActivateController(pyramid_request).get_when_not_logged_in() user_model.get_by_activation.assert_called_once_with( pyramid_request.db, activation_model.get_by_code.return_value)
def test_get_when_not_logged_in_successful_creates_ActivationEvent( self, user_model, ActivationEvent): request = DummyRequest(matchdict={'id': '123', 'code': 'abc456'}) user_model.get_by_activation.return_value.id = 123 views.ActivateController(request).get_when_not_logged_in() ActivationEvent.assert_called_once_with( request, user_model.get_by_activation.return_value)
def test_get_when_not_logged_in_successful_flashes_message( self, pyramid_request, user_model): pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} user_model.get_by_activation.return_value.id = 123 views.ActivateController(pyramid_request).get_when_not_logged_in() success_flash = pyramid_request.session.peek_flash('success') assert success_flash assert success_flash[0].startswith("Your account has been activated")
def test_get_when_not_logged_in_successful_activates_user( self, pyramid_request, user_model): pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} pyramid_request.db.delete = mock.create_autospec( pyramid_request.db.delete, return_value=None) user_model.get_by_activation.return_value.id = 123 views.ActivateController(pyramid_request).get_when_not_logged_in() user_model.get_by_activation.return_value.activate.assert_called_once_with( )
def test_get_when_not_logged_in_successful_flashes_message(self, user_model): request = DummyRequest(matchdict={'id': '123', 'code': 'abc456'}) request.session.flash = mock_flash_function() user_model.get_by_activation.return_value.id = 123 views.ActivateController(request).get_when_not_logged_in() assert request.session.flash.call_count == 1 assert request.session.flash.call_args[0][0].startswith( "Your account has been activated")
def test_get_when_logged_in_already_logged_in_to_different_account( self, pyramid_request): pyramid_request.authenticated_user = mock.Mock(id=124, spec=['id']) pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} result = views.ActivateController(pyramid_request).get_when_logged_in() error_flash = pyramid_request.session.peek_flash('error') assert isinstance(result, httpexceptions.HTTPFound) assert error_flash assert error_flash[0].startswith( "You're already logged in to a different account")
def test_get_when_logged_in_already_logged_in_to_same_account( self, pyramid_request): pyramid_request.authenticated_user = mock.Mock(id=123, spec=['id']) pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} result = views.ActivateController(pyramid_request).get_when_logged_in() success_flash = pyramid_request.session.peek_flash('success') assert isinstance(result, httpexceptions.HTTPFound) assert success_flash assert success_flash[0].startswith( "Your account has been activated and you're logged in")
def test_get_when_logged_in_already_logged_in_to_same_account(self): request = DummyRequest(matchdict={'id': '123', 'code': 'abc456'}, authenticated_user=mock.Mock(id=123, spec=['id'])) request.session.flash = mock_flash_function() result = views.ActivateController(request).get_when_logged_in() assert isinstance(result, httpexceptions.HTTPFound) assert request.session.flash.call_count == 1 assert request.session.flash.call_args[0][0].startswith( "Your account has been activated and you're now signed in")
def test_get_when_not_logged_in_404s_if_user_id_does_not_match_hash( self, pyramid_request, user_model): """ We don't want to let a user with a valid hash activate a different user's account! """ pyramid_request.matchdict = {'id': '123', 'code': 'abc456'} user_model.get_by_activation.return_value.id = 2 # Not the same id. with pytest.raises(httpexceptions.HTTPNotFound): views.ActivateController(pyramid_request).get_when_not_logged_in()
def test_get_when_not_logged_in_looks_up_user_by_activation( self, activation_model, user_model): request = DummyRequest(db=mock.sentinel.db_session, matchdict={'id': '123', 'code': 'abc456'}) user_model.get_by_activation.return_value.id = 123 views.ActivateController(request).get_when_not_logged_in() user_model.get_by_activation.assert_called_once_with( mock.sentinel.db_session, activation_model.get_by_code.return_value)