def test_permission_required_one_of_has_permission(self): """ Test the `permission_required` decorator if the user has one of the requested permission, but not all. Expected result: The decorated view function is returned. """ email = '*****@*****.**' name = 'Jane Doe' password = '******' user = User(email, name) user.set_password(password) user.role = Role('Administrator') user.role.permissions = Permission.EditRole db.session.add(user) db.session.commit() user.login(email, password) self.assertTrue(user.role.has_permission(Permission.EditRole)) self.assertFalse(user.role.has_permission(Permission.EditUser)) view_function = permission_required_one_of( Permission.EditRole, Permission.EditUser)(self.view_function) response = view_function() self.assertEqual(self.view_function(), response)
def test_permission_required_all_not_all_permissions(self): """ Test the `permission_required_all` decorator if the user does not have all the requested permissions. Expected result: The request is aborted with an error 403. """ email = '*****@*****.**' name = 'Jane Doe' password = '******' user = User(email, name) user.set_password(password) user.role = Role('Administrator') user.role.permissions = Permission.EditRole db.session.add(user) db.session.commit() user.login(email, password) self.assertTrue(user.role.has_permission(Permission.EditRole)) self.assertFalse(user.role.has_permission(Permission.EditUser)) with self.assertRaises(Forbidden): permission_required_all(Permission.EditRole, Permission.EditUser)(self.view_function)()
def test_permission_required_one_of_has_permission(self): """ Test the `permission_required` decorator if the user has one of the requested permission, but not all. Expected result: The decorated view function is returned. """ email = '*****@*****.**' name = 'Jane Doe' password = '******' user = User(email, name) user.set_password(password) user.role = Role('Administrator') user.role.permissions = Permission.EditRole db.session.add(user) db.session.commit() user.login(email, password) self.assertTrue(user.role.has_permission(Permission.EditRole)) self.assertFalse(user.role.has_permission(Permission.EditUser)) view_function = permission_required_one_of(Permission.EditRole, Permission.EditUser)(self.view_function) response = view_function() self.assertEqual(self.view_function(), response)
def test_get_locale_from_user(self, mock_request: MagicMock): """ Test getting the locale from a user who is logged in. Expected result: The user's preferred language is returned. """ # Mock the best_match() function to ensure it is not called. mock_request.accept_languages = MagicMock() mock_request.accept_languages.best_match = MagicMock(return_value='de') email = '*****@*****.**' name = 'Jane Doe' password = '******' user = User(email, name) user.set_password(password) db.session.add(user) db.session.commit() user.login(email, password) user.settings._language = 'fr' language = get_locale() self.assertEqual(user.settings._language, language) mock_request.accept_languages.best_match.assert_not_called()
def login() -> ResponseType: """ Show a login form to the user. If they submitted the login form, try to log them in and redirect them to the homepage. :return: The response for this view. """ form = LoginForm() if form.validate_on_submit(): # Try to login the user. user = User.login(email=form.email.data, password=form.password.data, remember_me=form.remember_me.data) if user: # Login succeeded. flash(_('Welcome, %(name)s!', name=user.name)) next_page = get_next_page() return redirect(next_page) # Login failed. Just show the login form again. flash(_('Invalid email address or password.'), 'error') return render_template('userprofile/login.html', title=_('Log In'), form=form)
def login() -> str: """ Show a login form to the user. If they submitted the login form, try to log them in and redirect them to the homepage. :return: The HTML response. """ form = LoginForm() if form.validate_on_submit(): # Try to login the user. user = User.login(email=form.email.data, password=form.password.data, remember_me=form.remember_me.data) if user: # Login succeeded. flash(_('Welcome, %(name)s!', name=user.name)) next_page = get_next_page() return redirect(next_page) # Login failed. Just show the login form again. flash(_('Invalid email address or password.'), 'error') return render_template('userprofile/login.html', title=_('Log In'), form=form)