本文整理汇总了Python中app.userprofile.User类的典型用法代码示例。如果您正苦于以下问题:Python User类的具体用法?Python User怎么用?Python User使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_login_post_success
def test_login_post_success(self):
"""
Test logging in with valid credentials.
Expected result: The login succeeds, the user is redirected to the home page and greeted.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
response = self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
data = response.get_data(as_text=True)
self.assertNotIn('<h1>Log In</h1>', data)
self.assertIn('<h1>Dashboard</h1>', data)
self.assertIn(f'Welcome, {name}!', data)
示例2: test_logout_logged_in
def test_logout_logged_in(self):
"""
Test logging out with a logged in user.
Expected result: The user is logged out, redirected to the home page (and from there to the login page),
and shown a success message.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
response = self.client.get('/user/logout', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('You were successfully logged out.', data)
self.assertIn('<h1>Log In</h1>', data)
示例3: test_index_logged_in
def test_index_logged_in(self):
"""
Test accessing the dashboard with a logged in user.
Expected result: The user is shown the dashboard.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
response = self.client.get('/', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('<h1>Dashboard</h1>', data)
self.assertNotIn('<h1>Log In</h1>', data)
示例4: test_login_refresh_post_valid_password
def test_login_refresh_post_valid_password(self, mock_login_fresh: MagicMock):
"""
Test refreshing the login with a valid password.
Expected result: The refresh home page is shown, the login is refreshed.
"""
mock_login_fresh.return_value = False
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
response = self.client.post('/user/login/refresh', follow_redirects=True, data=dict(
password=password
))
data = response.get_data(as_text=True)
self.assertNotIn('<h1>Confirm Login</h1>', data)
self.assertNotIn('Invalid password', data)
self.assertIn(f'Welcome, {name}!', data)
self.assertIn('<h1>Dashboard</h1>', data)
示例5: test_reset_password_get_logged_in
def test_reset_password_get_logged_in(self):
"""
Test accessing the password reset form with a user who is logged in, and a valid token.
Expected result: The user is redirected to the home page.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
token_obj = ResetPasswordToken()
token_obj.user_id = user_id
token = token_obj.create()
response = self.client.get(f'/user/reset-password/{token}', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Dashboard', data)
self.assertNotIn('The token is invalid.', data)
self.assertNotIn('Reset Your Password', data)
self.assertNotIn('Your password has successfully been changed.', data)
示例6: test_delete_has_users_new_role
def test_delete_has_users_new_role(self):
"""
Test deleting a role if there are still users and a valid new role is given.
Expected result: The role is deleted. The role is assigned to all users who had the old role (but not to
others).
"""
# The role that will be deleted.
name = 'Administrator'
role = Role(name=name)
user = User('[email protected]', 'Jane Doe')
user.role = role
db.session.add(role)
db.session.add(user)
# The new role for the user.
new_role = Role(name='Guest')
db.session.add(new_role)
# Another role and user who will stay untouched.
other_role = Role(name='User')
other_user = User('[email protected]', 'John Doe')
other_user.role = other_role
db.session.add(other_role)
db.session.add(other_user)
db.session.commit()
role.delete(new_role)
loaded_role = Role.load_from_name(name)
self.assertIsNone(loaded_role)
self.assertEqual(new_role, user.role)
self.assertEqual(other_role, other_user.role)
示例7: test_delete_profile_request_failure
def test_delete_profile_request_failure(self):
"""
Test requesting the deletion of the user's account with an invalid form.
Expected result: No email is sent.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
with mail.record_messages() as outgoing:
response = self.client.post('/user/delete', follow_redirects=True, data=dict())
data = response.get_data(as_text=True)
self.assertEqual(0, len(outgoing))
self.assertNotIn('An email has been sent to your email address.', data)
self.assertNotIn('to delete your user profile.', data)
self.assertIn('<h1>User Profile</h1>', data)
示例8: test_delete_profile_failure
def test_delete_profile_failure(self):
"""
Test deleting the account with an invalid token.
Expected result: The account is not deleted.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
response = self.client.get('/user/delete/invalid-token', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertEqual(404, response.status_code)
self.assertIsNotNone(User.load_from_id(user_id))
self.assertNotIn('Your user profile and all data linked to it have been deleted.', data)
示例9: test_reset_password_request_logged_in
def test_reset_password_request_logged_in(self):
"""
Test accessing the password reset request form with a user who is logged in.
Expected result: The user is redirected to the home page.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
response = self.client.get('/user/reset-password', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Dashboard', data)
self.assertNotIn('Forgot Your Password?', data)
示例10: test_login_post_failure
def test_login_post_failure(self):
"""
Test logging in with invalid credentials.
Expected result: The login fails and the user stays on the login page.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
response = self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password='invalid' + password
))
data = response.get_data(as_text=True)
self.assertIn('<h1>Log In</h1>', data)
self.assertIn('Invalid email address or password.', data)
self.assertNotIn('<h1>Dashboard</h1>', data)
示例11: test_reset_password_post_failure_invalid_token
def test_reset_password_post_failure_invalid_token(self):
"""
Test posting to the password reset form with an anonymous user, an invalid token, and a valid form.
Expected result: The password is not updated and the user is shown a 404 error page.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
new_password = 'abcdef'
response = self.client.post('/user/reset-password/just-some-token', follow_redirects=True, data=dict(
password=new_password,
password_confirmation=new_password
))
data = response.get_data(as_text=True)
self.assertEqual(404, response.status_code)
self.assertNotIn('Your password has successfully been changed.', data)
self.assertFalse(user.check_password(new_password))
self.assertTrue(user.check_password(password))
示例12: test_user_profile_get
def test_user_profile_get(self):
"""
Test accessing the user profile page.
Expected result: The form is shown with prepopulated data.
"""
email = '[email protected]'
name = 'John Doe'
password = '123456'
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
with mail.record_messages() as outgoing:
response = self.client.get('/user/profile', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertEqual(0, len(outgoing))
self.assertIn('User Profile', data)
self.assertIn(f'value="{name}"', data)
self.assertIn(f'value="{email}"', data)
self.assertNotIn('Your changes have been saved.', data)
self.assertNotIn('An email has been sent to the new address', data)
示例13: test_login_refresh_get_fresh
def test_login_refresh_get_fresh(self):
"""
Test accessing the login refresh page with an freshly authorized user.
Expected result: The user is redirected to the home page.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
response = self.client.get('/user/login/refresh', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('<h1>Confirm Login</h1>', data)
self.assertIn('<h1>Dashboard</h1>', data)
示例14: test_reset_password_post_failure_invalid_input
def test_reset_password_post_failure_invalid_input(self):
"""
Test posting to the password reset form with an anonymous user, a valid token, and an invalid form.
Expected result: The password is not updated and the user is shown the reset password form.
"""
email = '[email protected]'
password = '123456'
name = 'John Doe'
user_id = 1
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual(user_id, user.id)
token_obj = ResetPasswordToken()
token_obj.user_id = user_id
token = token_obj.create()
new_password = 'abcdef'
response = self.client.post(f'/user/reset-password/{token}', follow_redirects=True, data=dict(
password=new_password,
password_confirmation=new_password + 'ghi'
))
data = response.get_data(as_text=True)
self.assertIn('Reset Your Password', data)
self.assertNotIn('The token is invalid.', data)
self.assertNotIn('Your password has successfully been changed.', data)
self.assertFalse(user.check_password(new_password))
self.assertTrue(user.check_password(password))
示例15: test_user_settings_post
def test_user_settings_post(self):
"""
Test posting to the user settings page.
Expected result: The form is shown with the new data, the language is updated.
"""
email = '[email protected]'
name = 'Jane Doe'
password = '123456'
user = User(email, name)
user.set_password(password)
db.session.add(user)
db.session.commit()
self.assertEqual('en', user.settings.language)
self.client.post('/user/login', follow_redirects=True, data=dict(
email=email,
password=password
))
new_language = 'de'
response = self.client.post('/user/settings', follow_redirects=True, data=dict(
language=new_language,
))
data = response.get_data(as_text=True)
self.assertNotIn('Settings', data)
self.assertNotIn('Your changes have been saved.', data)
self.assertIn('Einstellungen', data)
self.assertIn('Deine Änderungen wurden gespeichert.', data)
self.assertEqual(new_language, user.settings.language)