Best Python code snippet using tempest_python
test_user.py
Source:test_user.py
1from .conftest import get_last_user, get_token2from .test_data import (login_creds_200, login_creds_403, login_creds_404,3 login_creds_alt_200, post_user_data_200,4 post_user_data_400, update_user_data_200)5class TestUser:6 class TestUserLogin:7 def test_login_200(self, client, create_user, delete_user):8 response = client.post(9 "/login", headers={"Authorization": "Basic " + login_creds_200}10 )11 assert response.status_code == 20012 def test_login_401(self, client, create_user, delete_user):13 response = client.post("/login")14 assert response.status_code == 40115 def test_login_403(self, client, create_user, delete_user):16 response = client.post(17 "/login", headers={"Authorization": "Basic " + login_creds_403}18 )19 assert response.status_code == 40320 def test_login_404(self, client, create_user, delete_user):21 response = client.post(22 "/login", headers={"Authorization": "Basic " + login_creds_404}23 )24 assert response.status_code == 40425 class TestUserLogout:26 def test_logout_200(self, client, create_user, delete_user):27 response = client.delete(28 "/logout", headers={"Authorization": "Bearer " + get_token(client)}29 )30 assert response.status_code == 20031 def test_logout_401(self, client, create_user, delete_user):32 response = client.delete("/logout")33 assert response.status_code == 40134 def test_logout_422(self, client, create_user, delete_user):35 response = client.delete(36 "/logout", headers={"Authorization": "Bearer " + "invalid_token"}37 )38 assert response.status_code == 42239 class TestUserGetUsers:40 def test_get_users_200(self, client, create_admin, delete_user):41 response = client.get(42 "/user", headers={"Authorization": "Bearer " + get_token(client)}43 )44 assert response.status_code == 20045 def test_get_users_403(46 self, client, create_admin, delete_user_alt, delete_user47 ):48 response = client.get(49 "/user",50 headers={51 "Authorization": "Bearer " + get_token(client, login_creds_alt_200)52 },53 )54 assert response.status_code == 40355 class TestUserPost:56 def test_post_user_200(self, client, delete_user):57 response = client.post("/user", json=post_user_data_200)58 assert response.status_code == 20059 def test_post_user_400(self, client):60 response = client.post("/user", json=post_user_data_400)61 assert response.status_code == 40062 class TestUserDeleteById:63 def test_delete_user_200(self, client, create_user):64 user_id = get_last_user().user_id65 url = "/user/" + str(user_id)66 response = client.delete(67 url, headers={"Authorization": "Bearer " + get_token(client)}68 )69 assert response.status_code == 20070 def test_delete_user_401(self, client, create_user):71 user_id = get_last_user().user_id72 url = "/user/" + str(user_id)73 response = client.delete(url)74 assert response.status_code == 40175 def test_delete_user_403(76 self, client, create_user, create_user_alt, delete_user_alt, delete_user77 ):78 user_id = get_last_user().user_id79 url = "/user/" + str(user_id)80 response = client.delete(81 url,82 headers={83 "Authorization": "Bearer " + get_token(client, login_creds_200)84 },85 )86 assert response.status_code == 40387 def test_delete_user_404(self, client, create_user, delete_user):88 user_id = get_last_user().user_id89 url = "/user/" + str(user_id + 1)90 response = client.delete(91 url, headers={"Authorization": "Bearer " + get_token(client)}92 )93 assert response.status_code == 40494 def test_delete_user_422(self, client, create_user, delete_user):95 user_id = get_last_user().user_id96 url = "/user/" + str(user_id)97 response = client.delete(98 url, headers={"Authorization": "Bearer " + "invalid_token"}99 )100 assert response.status_code == 422101 class TestUserUpdateById:102 def test_update_user_200(self, client, create_user, delete_user):103 user_id = get_last_user().user_id104 url = "/user/" + str(user_id)105 response = client.put(106 url,107 json=update_user_data_200,108 headers={"Authorization": "Bearer " + get_token(client)},109 )110 assert response.status_code == 200111 def test_update_user_401(self, client, create_user, delete_user):112 user_id = get_last_user().user_id113 url = "/user/" + str(user_id)114 response = client.put(url, json=update_user_data_200)115 assert response.status_code == 401116 def test_update_user_403(117 self, client, create_user, create_user_alt, delete_user_alt, delete_user118 ):119 user_id = get_last_user().user_id120 url = "/user/" + str(user_id)121 response = client.put(122 url,123 json=update_user_data_200,124 headers={125 "Authorization": "Bearer " + get_token(client, login_creds_200)126 },127 )128 assert response.status_code == 403129 def test_update_user_404(self, client, create_user, delete_user):130 user_id = get_last_user().user_id131 url = "/user/" + str(user_id + 1)132 response = client.put(133 url,134 json=update_user_data_200,135 headers={"Authorization": "Bearer " + get_token(client)},136 )137 assert response.status_code == 404138 def test_update_user_422(self, client, create_user, delete_user):139 user_id = get_last_user().user_id140 url = "/user/" + str(user_id)141 response = client.put(142 url,143 json=update_user_data_200,144 headers={"Authorization": "Bearer " + "invalid_token"},145 )146 assert response.status_code == 422147 class TestUserGetById:148 def test_get_user_200(self, client, create_user, delete_user):149 user_id = get_last_user().user_id150 url = "/user/" + str(user_id)151 response = client.get(152 url, headers={"Authorization": "Bearer " + get_token(client)}153 )154 assert response.status_code == 200155 def test_get_user_401(self, client, create_user, delete_user):156 user_id = get_last_user().user_id157 url = "/user/" + str(user_id)158 response = client.get(url)159 assert response.status_code == 401160 def test_get_user_403(161 self, client, create_user, create_user_alt, delete_user_alt, delete_user162 ):163 user_id = get_last_user().user_id164 url = "/user/" + str(user_id)165 response = client.get(166 url,167 headers={168 "Authorization": "Bearer " + get_token(client, login_creds_200)169 },170 )171 assert response.status_code == 403172 def test_get_user_404(self, client, create_user, delete_user):173 user_id = get_last_user().user_id174 url = "/user/" + str(user_id + 1)175 response = client.get(176 url, headers={"Authorization": "Bearer " + get_token(client)}177 )178 assert response.status_code == 404179 def test_get_user_422(self, client, create_user, delete_user):180 user_id = get_last_user().user_id181 url = "/user/" + str(user_id)182 response = client.get(183 url, headers={"Authorization": "Bearer " + "invalid_token"}184 )185 assert response.status_code == 422186 class TestUserGetByUsername:187 def test_get_user_200(self, client, create_user, delete_user):188 username = get_last_user().username189 url = "/user/" + str(username)190 response = client.get(191 url, headers={"Authorization": "Bearer " + get_token(client)}192 )193 assert response.status_code == 200194 def test_get_user_401(self, client, create_user, delete_user):195 username = get_last_user().username196 url = "/user/" + str(username)197 response = client.get(url)198 assert response.status_code == 401199 def test_get_user_403(200 self, client, create_user, create_user_alt, delete_user_alt, delete_user201 ):202 username = get_last_user().username203 url = "/user/" + str(username)204 response = client.get(205 url,206 headers={207 "Authorization": "Bearer " + get_token(client, login_creds_200)208 },209 )210 assert response.status_code == 403211 def test_get_user_404(self, client, create_user, delete_user):212 username = get_last_user().username213 url = "/user/" + str(username + "aaaa")214 response = client.get(215 url, headers={"Authorization": "Bearer " + get_token(client)}216 )217 assert response.status_code == 404218 def test_get_user_422(self, client, create_user, delete_user):219 username = get_last_user().username220 url = "/user/" + str(username)221 response = client.get(222 url, headers={"Authorization": "Bearer " + "invalid_token"}223 )224 assert response.status_code == 422225 class TestUserDeleteByUsername:226 def test_delete_user_200(self, client, create_user):227 user_id = get_last_user().username228 url = "/user/" + str(user_id)229 response = client.delete(230 url, headers={"Authorization": "Bearer " + get_token(client)}231 )232 assert response.status_code == 200233 def test_delete_user_401(self, client, create_user):234 user_id = get_last_user().username235 url = "/user/" + str(user_id)236 response = client.delete(url)237 assert response.status_code == 401238 def test_delete_user_403(239 self, client, create_user, create_user_alt, delete_user_alt, delete_user240 ):241 username = get_last_user().username242 url = "/user/" + str(username)243 response = client.delete(244 url,245 headers={246 "Authorization": "Bearer " + get_token(client, login_creds_200)247 },248 )249 assert response.status_code == 403250 def test_delete_user_404(self, client, create_user, delete_user):251 username = get_last_user().username252 url = "/user/" + str(username + "aaa")253 response = client.delete(254 url, headers={"Authorization": "Bearer " + get_token(client)}255 )256 assert response.status_code == 404257 def test_delete_user_422(self, client, create_user, delete_user):258 username = get_last_user().username259 url = "/user/" + str(username)260 response = client.delete(261 url, headers={"Authorization": "Bearer " + "invalid_token"}262 )263 assert response.status_code == 422264 # TODO: fix PUT user by username265 # class TestUserUpdateByUsername:266 # def test_update_user_200(self, client, create_user, delete_user):267 # username = get_last_user().username268 # url = "/user/" + str(username)269 # response = client.put(url,270 # json=update_user_data_200,271 # headers={'Authorization': 'Bearer ' +272 # get_token(client)}273 # )274 # assert response.status_code == 200275 # def test_update_user_401(self, client, create_user, delete_user):276 # username = get_last_user().username277 # url = "/user/" + str(username)278 # response = client.put(url, json=update_user_data_200)279 # assert response.status_code == 401280 # def test_update_user_403(self, client, create_user, create_user_alt,281 # delete_user_alt, delete_user):282 # username = get_last_user().username283 # url = "/user/" + str(username)284 # response = client.put(url,285 # json=update_user_data_200,286 # headers={'Authorization': 'Bearer ' +287 # get_token(client, login_creds_200)}288 # )289 # assert response.status_code == 403290 # def test_update_user_404(self, client, create_user, delete_user):291 # username = get_last_user().username292 # url = "/user/" + str(username + "aaa")293 # response = client.put(url,294 # json=update_user_data_200,295 # headers={'Authorization': 'Bearer ' +296 # get_token(client)}297 # )298 # assert response.status_code == 404299 # def test_update_user_422(self, client, create_user, delete_user):300 # username = get_last_user().username301 # url = "/user/" + str(username)302 # response = client.put(url,303 # json=update_user_data_200,304 # headers={'Authorization': 'Bearer ' +305 # 'invalid_token'}306 # )...
test_delete_user.py
Source:test_delete_user.py
...20class DeleteUserTestCase(WorkoutManagerTestCase):21 '''22 Tests deleting the user account and all his data23 '''24 def delete_user(self, fail=False):25 '''26 Helper function27 '''28 response = self.client.get(reverse('core:user:delete'))29 self.assertEqual(30 User.objects.filter(31 username='test').count(), 1)32 if fail:33 self.assertEqual(response.status_code, 302)34 else:35 self.assertEqual(response.status_code, 200)36 # Wrong user password37 if not fail:38 response = self.client.post(39 reverse('core:user:delete'), {40 'password': 'not the user password'})41 self.assertEqual(response.status_code, 200)42 self.assertEqual(43 User.objects.filter(44 username='test').count(), 1)45 # Correct user password46 response = self.client.post(47 reverse('core:user:delete'), {48 'password': 'testtest'})49 self.assertEqual(response.status_code, 302)50 if fail:51 self.assertEqual(52 User.objects.filter(53 username='test').count(), 1)54 else:55 self.assertEqual(56 User.objects.filter(57 username='test').count(), 0)58 def test_delete_user_logged_in(self):59 '''60 Tests deleting the own account as a logged in user61 '''62 self.user_login('test')63 self.delete_user(fail=False)64 def test_delete_user_anonymous(self):65 '''66 Tests deleting the own account as an anonymous user67 '''68 self.delete_user(fail=True)69class DeleteUserByAdminTestCase(WorkoutManagerTestCase):70 '''71 Tests deleting a user account by a gym administrator72 '''73 def delete_user(self, fail=False):74 '''75 Helper function76 '''77 response = self.client.get(78 reverse(79 'core:user:delete',80 kwargs={81 'user_pk': 2}))82 self.assertEqual(83 User.objects.filter(84 username='test').count(), 1)85 if fail:86 self.assertIn(87 response.status_code,88 (302,89 403),90 'Unexpected status code for user {0}'.format(91 self.current_user))92 else:93 self.assertEqual(94 response.status_code,95 200,96 'Unexpected status code for user {0}'.format(97 self.current_user))98 # Wrong admin password99 if not fail:100 response = self.client.post(101 reverse(102 'core:user:delete', kwargs={103 'user_pk': 2}), {104 'password': 'blargh'})105 self.assertEqual(response.status_code, 200)106 self.assertEqual(107 User.objects.filter(108 username='test').count(), 1)109 # Correct user password110 response = self.client.post(111 reverse(112 'core:user:delete', kwargs={113 'user_pk': 2}), {114 'password': self.current_password})115 if fail:116 self.assertIn(response.status_code, (302, 403))117 self.assertEqual(118 User.objects.filter(119 username='test').count(), 1)120 else:121 self.assertEqual(response.status_code, 302)122 self.assertEqual(123 User.objects.filter(124 username='test').count(), 0)125 def test_delete_user_manager(self):126 '''127 Tests deleting the user account as a gym manager128 '''129 self.user_login('manager1')130 self.delete_user(fail=False)131 def test_delete_user_manager2(self):132 '''133 Tests deleting the user account as a gym manager134 '''135 self.user_login('manager2')136 self.delete_user(fail=False)137 def test_delete_user_general_manager(self):138 '''139 Tests deleting the user account as a general manager140 '''141 self.user_login('general_manager1')142 self.delete_user(fail=False)143 def test_delete_user_general_manager2(self):144 '''145 Tests deleting the user account as a general manager146 '''147 self.user_login('general_manager2')148 self.delete_user(fail=False)149 def test_delete_user(self):150 '''151 Tests deleting the user account as a regular user152 '''153 self.user_login('test')154 self.delete_user(fail=True)155 def test_delete_user_trainer(self):156 '''157 Tests deleting the user account as a gym trainer158 '''159 self.user_login('trainer1')160 self.delete_user(fail=True)161 def test_delete_user_trainer2(self):162 '''163 Tests deleting the user account as a gym trainer164 '''165 self.user_login('trainer4')166 self.delete_user(fail=True)167 def test_delete_user_trainer_other(self):168 '''169 Tests deleting the user account as a gym trainer of another gym170 '''171 self.user_login('trainer4')172 self.delete_user(fail=True)173 def test_delete_user_manager_other(self):174 '''175 Tests deleting the user account as a gym manager of another gym176 '''177 self.user_login('manager3')178 self.delete_user(fail=True)179 def test_delete_user_member(self):180 '''181 Tests deleting the user account as a gym member182 '''183 self.user_login('member1')184 self.delete_user(fail=True)185 def test_delete_other_user_member(self):186 '''187 Tests deleting the user account as a gym member188 '''189 self.user_login('member4')190 self.delete_user(fail=True)191 def test_delete_user_anonymous(self):192 '''193 Tests deleting the user account as an anonymous user194 '''...
ProfileManageUsersHandler.py
Source:ProfileManageUsersHandler.py
...29 if self.content.is_administrator(delete_user):30 if len(self.content.get_users_from_role(0, "administrator")) > 1:31 if delete_user == self.get_user_id():32 #Figure out what to do when admins remove themselves33 self.content.delete_user(delete_user)34 else:35 result = f"{delete_user} is an administrator and can only be deleted by that user."36 else:37 result = f"Error: At least one administrator must remain in the system."38 elif self.content.user_has_role(delete_user, course_id, "instructor"):39 if len(self.content.get_users_from_role(course_id, "instructor")) > 1:40 if self.content.is_administrator(self.get_user_id()):41 self.content.delete_user(delete_user)42 result = f"Success: The user '{delete_user}' has been deleted."43 else:44 result = "Instructors can only be removed by administrators."45 else:46 result = f"Error: The user '{delete_user}' is the only instructor for their course. They cannot be deleted until another instructor is assigned to the course."47 else:48 self.content.delete_user(delete_user)49 result = f"Success: The user '{delete_user}' has been deleted."50 else:51 result = f"Error: The user '{delete_user}' does not exist."52 else:53 if not remove_user:54 result = f"Error: Please enter a user."55 self.render("profile_manage_users.html", page="manage_users", result=result, user_info=self.get_user_info(), is_administrator=self.is_administrator(), is_instructor=self.is_instructor(), is_assistant=self.is_assistant())56 except Exception as inst:...
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!!