Best Python code snippet using locust
test_api_users.py
Source:test_api_users.py
1import pytest2from typing import Dict3# from time import time4from flask import url_for5from application.users.models.users import UserModel6from application.users.models.confirmations import ConfirmationModel7@pytest.fixture8def saved_user_instance(client, user_instance) -> UserModel:9 def _method(values: Dict = {}) -> UserModel:10 _saved_user = user_instance(values=values)11 _saved_user.save_to_db()12 yield _saved_user13 if _saved_user.is_exist:14 _saved_user.delete_fm_db()15 yield16 return _method17# @pytest.mark.active18def test_users_post(client, random_email, user_get_schema):19 # create user without email20 _user_create_json = {21 'password': 'qwerty'22 }23 resp = client.post(url_for('users_bp.user'), json=_user_create_json)24 assert resp.status_code == 40025 assert isinstance(resp.json, str)26 assert resp.json.find('email') != -127 # create user without password28 _user_create_json = {29 'email': random_email()30 }31 resp = client.post(url_for('users_bp.user'), json=_user_create_json)32 assert resp.status_code == 40033 assert isinstance(resp.json, str)34 assert resp.json == 'Password must be non-empty.'35 # Do create user36 _user_create_json = {37 'email': random_email(),38 'password': 'qwerty'39 }40 resp = client.post(url_for('users_bp.user'), json=_user_create_json)41 _email = resp.json.get('payload').get('email')42 assert _email == _user_create_json.get('email')43 assert resp.status_code == 20144 assert isinstance(resp.json.get('message'), str)45 assert resp.json.get('message').find(_email) != -146 assert isinstance(resp.json.get('payload'), Dict)47 _user = UserModel.find_by_email(_email)48 # _created_user_json = user_schema.dump(_user)49 _created_user_json = user_get_schema.dump(_user)50 assert _created_user_json.get('locale_id', 'nope') == 'en'51 assert _created_user_json.get('created') is not None52 assert _created_user_json.get('email') == _email53 assert _created_user_json.get('time_zone') == 354 assert _created_user_json.get('role_id', 'nope') is None55 _confirmation = ConfirmationModel.find_by_user_id(_created_user_json.get('id'))56 assert _confirmation is not None57 # print('users, test_api_users, _confirmation ->', _confirmation)58# @pytest.mark.active59@pytest.mark.parametrize(60 'lng, message_word',61 [62 ('en', 'payload'),63 ('ru', 'ÑообÑениÑ')64 ]65)66def test_users_get_success(client, saved_user_instance, user_get_schema,67 access_token, lng, message_word):68 _user_gen = saved_user_instance({'role_id': 'user'})69 _admin_gen = saved_user_instance({'role_id': 'admin'})70 _user = next(_user_gen)71 _admin = next(_admin_gen)72 # find user by own email, not admin user73 params = {'email': _user.email}74 _access_token = access_token(_user)75 _headers = {76 'Authorization': f'Bearer {_access_token}',77 'Accept-Language': lng78 }79 resp = client.get(url_for('users_bp.user', **params), headers=_headers)80 assert resp.status_code == 20081 assert isinstance(resp.json.get('message'), str)82 assert resp.json.get('message').find(message_word) != -183 assert isinstance(resp.json.get('payload').get('role'), Dict)84 assert isinstance(resp.json.get('payload').get('locale'), Dict)85 # getting info by admin about other user86 params = {'email': _user.email}87 _access_token = access_token(_admin)88 _headers = {89 'Authorization': f'Bearer {_access_token}',90 'Accept-Language': lng91 }92 resp = client.get(url_for('users_bp.user', **params), headers=_headers)93 assert resp.status_code == 20094 assert isinstance(resp.json.get('message'), str)95 assert resp.json.get('message').find(message_word) != -196 assert isinstance(resp.json.get('payload').get('role'), Dict)97 assert isinstance(resp.json.get('payload').get('locale'), Dict)98 # print('\ncode ->', resp.status_code)99 # print('json ->', resp.json)100 next(_user_gen)101 next(_admin_gen)102# @pytest.mark.active103@pytest.mark.parametrize(104 'lng, message_word',105 [106 ('en', 'email'),107 ('ru', 'поÑÑ')108 ]109)110def test_users_get_fail(client, saved_user_instance, user_get_schema,111 access_token, lng, message_word):112 _user_gen = saved_user_instance({'role_id': 'user'})113 _admin_gen = saved_user_instance({'role_id': 'admin'})114 _user = next(_user_gen)115 _admin = next(_admin_gen)116 # try to access query without header117 params = {'email': _user.email}118 _headers = {119 'Accept-Language': lng120 }121 resp = client.get(url_for('users_bp.user', **params), headers=_headers)122 assert resp.status_code == 401123 assert resp.json.get('error') == 'authorization_required'124 # try to find other user info not admin user125 params = {'email': _admin.email}126 _access_token = access_token(_user)127 _headers = {128 'Authorization': f'Bearer {_access_token}',129 'Accept-Language': lng130 }131 resp = client.get(url_for('users_bp.user', **params), headers=_headers)132 assert resp.status_code == 401133 assert isinstance(resp.json.get('message'), str)134 assert 'payload' not in resp.json.keys()135 assert resp.json.get('message').find(message_word) != -1136 # Try to find account with wrong email137 params = {'email': 'wrong@r.mail'}138 _access_token = access_token(_admin)139 _headers = {140 'Authorization': f'Bearer {_access_token}',141 'Accept-Language': lng142 }143 resp = client.get(url_for('users_bp.user', **params), headers=_headers)144 assert resp.status_code == 404145 assert isinstance(resp.json.get('message'), str)146 assert 'payload' not in resp.json.keys()147 assert resp.json.get('message').find(message_word) != -1148 # print('\ncode ->', resp.status_code)149 # print('json ->', resp.json)150 next(_user_gen)...
test_model_users.py
Source:test_model_users.py
1# import pytest2# from typing import Dict3# from typing import Dict, List4# from datetime import datetime5# from random import randint6from application.users.models.users import UserModel7from application.users.models.confirmations import ConfirmationModel8from application.users.modules.dbs_init_users import create_default_admin9# from application.global_init_data import global_constants10# from application.users.local_init_data_users import users_constants11# @pytest.mark.active12def test_user_find_all(saved_user_instance, user_get_schema):13 '''14 Get list of user without searching criterian or emply criteraon.15 '''16 # Clean up user table17 ConfirmationModel.query.delete()18 UserModel.query.delete()19 create_default_admin()20 # create couple of users for searching21 _user_keys = []22 _user_gens = []23 _users = []24 _user_keys.append({'user_name': 'Sergey'}) # [0]25 _user_keys.append({**_user_keys[0], 'role_id': 'admin'}) # [1]26 for index, _user_key in enumerate(_user_keys):27 _user_gens.append(saved_user_instance(_user_key))28 # print('\nunit, users, test_user_find_all, _user_gen ->', _user_gens[index])29 _users.append(next(_user_gens[index]))30 print('\nunit, users, test_user_find_all, _user ->', _users[index])31 # find by user name32 _result = UserModel.find(_user_keys[0])33 assert len(_result) == 234 assert user_get_schema.dump(_result[0]).get(35 'user_name') == _user_keys[0].get('user_name')36 assert user_get_schema.dump(_result[1]).get(37 'role_id') == _user_keys[1].get('role_id')38 print('\nunit, users, test_user_find_all, role_id ->',39 user_get_schema.dump(_result[1]).get('role_id'))40 # user_schema.dump(_result[1]).get('role_id'))41 # Clean up db42 for _user_gen in _user_gens:43 next(_user_gen)44# @pytest.mark.active45def test_set_access(saved_user_instance, user_get_schema):46 _user_gen = saved_user_instance()47 _user = next(_user_gen)48 assert user_get_schema.dump(_user).get('accessed') is None49 _user.set_accessed()50 assert isinstance(user_get_schema.dump(_user).get('accessed'), str)51 # print('\nunit, users, test_set_access, accessed ->',52 # type(user_get_schema.dump(_user).get('accessed')))53 next(_user_gen)54# @pytest.mark.active55def test_update_iss(saved_user_instance, user_get_schema):56 _email = 'test@email.mail'57 _user_gen = saved_user_instance()58 _user = next(_user_gen)59 # is_own_id60 _id = user_get_schema.dump(_user).get('id')61 assert _user.is_own_id(_id)62 # is_own_email63 assert not _user.is_own_email(_email)64 _user.update({'email': _email})65 assert _user.is_own_email(_email)66 # is_valid67 assert _user.is_valid68 # is_power69 assert not _user.is_power70 _user.update({'role_id': 'power_user'})71 assert _user.is_power72 # is_power73 assert not _user.is_admin74 _user.update({'role_id': 'admin'})75 assert _user.is_admin76 # print('\nunit, users, test_update_iss, testing ->', _user.is_own_email(_email))77 next(_user_gen)78# @pytest.mark.active79def test_get_tokens(saved_user_instance):80 _user_gen = saved_user_instance()81 _user = next(_user_gen)82 # access tokens83 _result = _user.get_tokens()84 assert 'access_token' in _result.keys()85 assert 'refresh_token' in _result.keys()86 # fresh token87 _result = _user.get_fresh_token()88 assert isinstance(_result, str)89 next(_user_gen)90# @pytest.mark.active91def test_find_by_id_email(saved_user_instance, user_get_schema):92 _email = 'test@email.mail'93 _user_gen = saved_user_instance({'email': _email})94 _user = next(_user_gen)95 # find_by_id96 _id = user_get_schema.dump(_user).get('id')97 _user_id = UserModel.find_by_id(_id)98 assert user_get_schema.dump(_user_id).get('email') == _email99 # find_by_email100 _user_email = UserModel.find_by_email(_email)101 assert user_get_schema.dump(_user_email).get('id') == _id102 # print('\nuser model testing ->', user_get_schema.dump(_user_email).get('id'))103 next(_user_gen)104# @pytest.mark.active105def test_update_password(saved_user_instance):106 _password = 'ojwoi888*()'107 _user_gen = saved_user_instance()108 _user = next(_user_gen)109 # update_password, check_password110 _user.update_password(_password)111 assert _user.check_password(_password)112 # print('\nuser model testing ->', user_get_schema.dump(_user_email).get('id'))113 next(_user_gen)114# @pytest.mark.active115def test_delete_fm_db(saved_user_instance, user_get_schema):116 _user_gen = saved_user_instance()117 _user = next(_user_gen)118 _id = user_get_schema.dump(_user).get('id')119 # just delete created user120 _user_id = UserModel.find_by_id(_id)121 assert isinstance(_user_id, UserModel)122 _user_id.delete_fm_db()123 assert UserModel.find_by_id(_id) is None124 # try to kill admin No1 without and with flag kill_first125 _admin = UserModel.find_by_id(1)126 assert isinstance(_admin, UserModel)127 _admin.delete_fm_db()128 assert isinstance(_admin, UserModel)129 _admin.delete_fm_db(kill_first=True)130 assert UserModel.find_by_id(1) is None131 # Create admin No1 again132 create_default_admin()133 # Clean up db...
test_model_users_confirmation.py
Source:test_model_users_confirmation.py
1import pytest2# from random import randint3# from time import time4from application.users.models.confirmations import ConfirmationModel5# @pytest.fixture6# def confirmaion_instance():7# _confirmaion = ConfirmationModel(user_id=randint(2, 10))8# # print(_confirmaion.id)9# # _confirmaion.save_to_db()10# return _confirmaion11# # confirmation_left = ConfirmationModel.find_first()12# # while confirmation_left:13# # # print(confirmation_left.user_id)14# # confirmation_left.delete_fm_db()15# # confirmation_left = ConfirmationModel.find_first()16# # # print('Nothing left')17@pytest.fixture18def saved_confirmaion_instance(client, saved_user_instance, user_get_schema):19 def _method():20 _user_gen = saved_user_instance()21 _user = next(_user_gen)22 _confimation = ConfirmationModel(user_id=user_get_schema.dump(_user).get('id'))23 _confimation.save_to_db()24 yield _confimation25 if _confimation.is_exist:26 _confimation.delete_fm_db()27 if _user.is_exist:28 _user.delete_fm_db()29 yield30 return _method31 # yield _confimation32# @pytest.mark.active33def test_confirmaion_finds_all(saved_confirmaion_instance):34 _confirmaiton_gen = saved_confirmaion_instance()35 _confirmaiton = next(_confirmaiton_gen)36 _id = _confirmaiton.id37 _user_id = _confirmaiton.user_id38 # find_by_id39 _user_by_id = ConfirmationModel.find_by_id('0')40 assert _user_by_id is None41 _user_by_id = ConfirmationModel.find_by_id(_id)42 assert _user_by_id.id == _id43 # find_by_user_id44 _user_by_user_id = ConfirmationModel.find_by_user_id('_user_id')45 assert _user_by_user_id is None46 _user_by_user_id = ConfirmationModel.find_by_user_id(_user_id)47 assert _user_by_user_id.id == _id48 # find_first49 _user_find_first = ConfirmationModel.find_first()50 assert _user_find_first.id == _id51 next(_confirmaiton_gen)52 # _user_find_first = ConfirmationModel.find_first()53 # assert _user_find_first is None54# @pytest.mark.active55def test_confirmaion_iss_force_to_expier(saved_confirmaion_instance):56 _confirmaiton_gen = saved_confirmaion_instance()57 _confirmaiton = next(_confirmaiton_gen)58 # is_exist59 assert _confirmaiton.is_exist60 # is_confirmed61 assert not _confirmaiton.is_confirmed62 _confirmaiton.confirmed = True63 assert _confirmaiton.is_confirmed64 # is_expired65 assert not _confirmaiton.is_expired66 _confirmaiton.force_to_expire()67 assert _confirmaiton.is_expired...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!