How to use get_user_by_username method in tempest

Best Python code snippet using tempest_python

test_external_user_can_set_password.py

Source:test_external_user_can_set_password.py Github

copy

Full Screen

...41 def test_GIVEN_id_but_no_uuid_WHEN_password_THEN_page_with_error(self):42 user_service = UserService()43 username = "unique test name"44 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)45 user = user_service.get_user_by_username(username)46 response = self.app.get(47 url=url(controller='home', action='password', id=user.id)48 )49 assert_that(response.normal_body, contains_string("Invalid Password Request"), "Correct page")50 def test_GIVEN_id_but_no_uuid_WHEN_post_password_THEN_page_with_error(self):51 user_service = UserService()52 username = "unique test name"53 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)54 user = user_service.get_user_by_username(username)55 response = self.app.post(56 url=url(controller='home', action='password', id=user.id),57 params={}58 )59 assert_that(response.normal_body, contains_string("Invalid Password Request"), "Correct page")60 def test_GIVEN_valid_id_and_uuid_WHEN_password_THEN_page_with_no_error(self):61 user_service = UserService()62 username = "unique test name"63 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)64 user = user_service.get_user_by_username(username)65 user_service.set_forgot_password(user.id)66 user = user_service.get_user_by_username(username)67 response = self.app.get(68 url=url(controller='home', action='password', id=user.id, uuid=user.forgotten_password_uuid)69 )70 assert_that(response.normal_body, contains_string("Password Request"), "Correct page")71 assert_that(response.normal_body, is_not(contains_string("Your new password")), "tooltip is rewriten")72 assert_that(response.normal_body, contains_string('title="Username"'), "Username tooltip")73 assert_that(response.normal_body, contains_string('title="New password"'), "Username tooltip")74 assert_that(response.normal_body, contains_string('title="Retype your new password"'), "Username tooltip")75 def test_GIVEN_valid_id_and_invalid_uuid_WHEN_password_THEN_page_with_error(self):76 user_service = UserService()77 username = "unique test name"78 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)79 user = user_service.get_user_by_username(username)80 user_service.set_forgot_password(user.id)81 response = self.app.get(82 url=url(controller='home', action='password', id=user.id, uuid="not that uuid")83 )84 assert_that(response.normal_body, contains_string("Invalid Password Request"), "Invalid password page")85 def test_GIVEN_valid_id_and_invalid_uuid_WHEN_post_password_THEN_page_with_error(self):86 user_service = UserService()87 username = "unique test name"88 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)89 user = user_service.get_user_by_username(username)90 user_service.set_forgot_password(user.id)91 response = self.app.post(92 url=url(controller='home', action='password', id=user.id),93 params={'uuid':"not that uuid"}94 )95 assert_that(response.normal_body, contains_string("Invalid Password Request"), "Invalid password page")96 def test_GIVEN_invalid_id_and_valid_uuid_WHEN_password_THEN_page_with_error(self):97 user_service = UserService()98 username = "unique test name"99 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)100 user = user_service.get_user_by_username(username)101 user_service.set_forgot_password(user.id)102 response = self.app.get(103 url=url(controller='home', action='password', id=user.id + 1, uuid=user.forgotten_password_uuid)104 )105 assert_that(response.normal_body, contains_string("Invalid Password Request"), "Invalid password page")106 def test_GIVEN_invalid_id_and_valid_uuid_WHEN_post_password_THEN_page_with_error(self):107 user_service = UserService()108 username = "unique test name"109 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)110 user = user_service.get_user_by_username(username)111 user_service.set_forgot_password(user.id)112 response = self.app.post(113 url=url(controller='home', action='password', id=user.id + 1),114 params={'uuid': user.forgotten_password_uuid}115 )116 assert_that(response.normal_body, contains_string("Invalid Password Request"), "Invalid password page")117 def test_GIVEN_valid_id_and_valid_uuid_which_has_expired_WHEN_password_THEN_reset_forgotten_password(self):118 user_service = UserService()119 username = "unique test name"120 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)121 user = user_service.get_user_by_username(username)122 user_service.set_forgot_password(user.id)123 with session_scope() as session:124 user = user_service.get_user_by_id(user.id)125 user.forgotten_password_expiry_date = datetime.datetime.now() - datetime.timedelta(minutes=1)126 session.add(user)127 original_uuid = user.forgotten_password_uuid128 response = self.app.get(129 url=url(controller='home', action='password', id=user.id, uuid=original_uuid)130 )131 assert_that(response.normal_body, contains_string("Expired Password Request"), "Expired password page")132 with session_scope() as session:133 user = user_service.get_user_by_id(user.id)134 assert_that(user.forgotten_password_uuid, is_not(original_uuid), "uuid reset")135 def test_GIVEN_valid_id_and_valid_uuid_which_has_expired_WHEN_post_password_THEN_reset_forgotten_password(self):136 user_service = UserService()137 username = "unique test name"138 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)139 user = user_service.get_user_by_username(username)140 user_service.set_forgot_password(user.id)141 with session_scope() as session:142 user = user_service.get_user_by_id(user.id)143 user.forgotten_password_expiry_date = datetime.datetime.now() - datetime.timedelta(minutes=1)144 session.add(user)145 original_uuid = user.forgotten_password_uuid146 response = self.app.post(147 url=url(controller='home', action='password', id=user.id),148 params={'uuid':original_uuid}149 )150 assert_that(response.normal_body, contains_string("Expired Password Request"), "Expired password page")151 with session_scope() as session:152 user = user_service.get_user_by_id(user.id)153 assert_that(user.forgotten_password_uuid, is_not(original_uuid), "uuid reset")154 def test_GIVEN_valid_id_and_uuid_WHEN_post_new_password_THEN_login_page_with_message(self):155 user_service = UserService()156 username = "unique test name"157 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)158 user = user_service.get_user_by_username(username)159 user_service.set_forgot_password(user.id)160 user = user_service.get_user_by_username(username)161 new_password = 'long and hard to guess password'162 response = self.app.post(163 url=url(controller='home', action='password', id=user.id),164 params={165 'uuid': user.forgotten_password_uuid,166 'password_one': new_password,167 'password_two': new_password}168 )169 assert_that(response.status_code, is_(302), "Response is redirect")170 assert_that(urlparse(response.response.location).path, is_(url(controller='account', action='login')), "url")171 with session_scope() as session:172 user = user_service.get_user_by_id(user.id)173 assert_that(user.forgotten_password_uuid, is_(None), "uuid blanked")174 def test_GIVEN_valid_id_and_uuid_non_matching_password_WHEN_post_new_password_THEN_error_message(self):175 user_service = UserService()176 username = "unique test name"177 user_service.create(username, "test", "test", "email", constants.USER_ACCESS_LEVEL_EXTERNAL)178 user = user_service.get_user_by_username(username)179 user_service.set_forgot_password(user.id)180 user = user_service.get_user_by_username(username)181 new_password = 'long and hard to guess password'182 response = self.app.post(183 url=url(controller='home', action='password', id=user.id),184 params={185 'uuid': user.forgotten_password_uuid,186 'password_one': new_password,187 'password_two': "not new password"}188 )...

Full Screen

Full Screen

test_users.py

Source:test_users.py Github

copy

Full Screen

...8 update_user_token)9from app.users.models import User10def test_create_user():11 for db in get_test_db():12 assert (get_user_by_username(db, 'Alex') is None)13 test_user = User(username='Alex', password_hash='123')14 db.add(test_user)15 db.commit()16 assert (get_user_by_username(db, 'Alex') == test_user)17def test_unique_username():18 for db in get_test_db():19 test_user = User(username='Alex', password_hash='123')20 db.add(test_user)21 db.commit()22 test_user = User(username='Alex', password_hash='1234')23 db.add(test_user)24 with pytest.raises(Exception):25 db.commit()26def test_update_token():27 for db in get_test_db():28 test_user = User(username='Alex', password_hash='123')29 db.add(test_user)30 db.commit()31 access_token = str(uuid.uuid4())32 update_user_token(db, test_user, access_token)33 assert (get_user_by_username(db, 'Alex').token == access_token)34def test_get_user_by_token():35 for db in get_test_db():36 access_token = str(uuid.uuid4())37 expected_user = User(username='Alex', password_hash='123', token=access_token)38 db.add(expected_user)39 db.commit()40 expected_user = get_user_by_username(db, 'Alex')41 assert (get_user_by_token(db, expected_user.token) == expected_user)42def test_unique_token():43 for db in get_test_db():44 access_token = str(uuid.uuid4())45 test_user = User(username='Alex', password_hash='123', token=access_token)46 db.add(test_user)47 db.commit()48 test_user = User(username='Alexs', password_hash='1234', token=access_token)49 db.add(test_user)50 with pytest.raises(Exception):...

Full Screen

Full Screen

UserRepository.py

Source:UserRepository.py Github

copy

Full Screen

1from services.entities.Database import db2from services.entities.User import User3from services.data_access.CachedRepository import CachedRepository4class UserRepository:5 def get_user_by_username(self, username):6 return db.session.query(User)\7 .filter(User.username == username)\8 .first()9 10 def get_user_by_id(self, id):11 return db.session.query(User)\12 .filter(User.id == id)\13 .first()14 15 def add_user(self, user):16 db.session.add(user)17 db.session.commit()18 19 def update_user(self, user):20 db.session.merge(user)21 db.session.commit()22 23class CachedUserRepository(CachedRepository):24 def __init__(self, cache = None, repository = None):25 super(CachedUserRepository, self).__init__(cache, repository or UserRepository())26 27 def get_user_by_username(self, username):28 return self._get_or_add('user_name:{}'.format(username), lambda: self._repository.get_user_by_username(username))29 30 def get_user_by_id(self, id):31 return self._get_or_add('user_id:{}'.format(id), lambda: self._repository.get_user_by_id(id)) 32 33 def add_user(self, user):34 self._repository.add_user(user)35 36 def update_user(self, user):37 self._repository.update_user(user)38 39 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful