Best Python code snippet using Kiwi_python
test_api_posts_view.py
Source:test_api_posts_view.py
1import json2def login(client_is_config, name, password):3 return client_is_config.post('/auth/login', data=dict(4 name=name,5 password=password6 ), follow_redirects=True)7def test_existing_post(client_is_config):8 response = client_is_config.get('/api-posts/2')9 data = response.json10 assert response.status_code == 20011 assert data["title"] == "Php"12 assert data["owner"] == "1"13 assert data["name"] == "tia"14 assert data["img_id"] == "id2"15def test_unexisting_post(client_is_config):16 response = client_is_config.get('/api-posts/289')17 data = response.json18 assert response.status_code == 40419 assert data["error"] == "post not found"20def test_login_return_token(client_is_config):21 response = client_is_config.post('/api-posts/login',22 data=json.dumps(dict(23 username='tia',24 password='123')),25 content_type='application/json')26 data = json.loads(response.data.decode())27 assert data['status'] == 'success'28 assert data['message'] == 'Successfully logged in.'29 assert data['auth_token']30 assert response.status_code == 20031def test_login_with_invalid_credentials_return_401(client_is_config):32 response = client_is_config.post(33 '/api-posts/login',34 data=json.dumps(dict(35 username='dummy',36 password='111'37 )),38 content_type='application/json')39 assert response.status_code == 40140 assert b'Credentials invalid!' in response.data41def test_delete_post_by_logged_user(client_is_config):42 #POstgres id 1243 response = client_is_config.post(44 '/api-posts/login',45 data=json.dumps(dict(46 username='ben',47 password='123'48 )),49 content_type='application/json')50 data = json.loads(response.data.decode())51 assert data['status'] == 'success'52 assert data['message'] == 'Successfully logged in.'53 assert data['auth_token']54 assert response.status_code == 20055 headers = {56 'Authorization' : 'Bearer ' + data['auth_token']57 }58 response_delete = client_is_config.delete('/api-posts/12',59 content_type='application/json',60 headers=headers,61 follow_redirects=True)62 delete_data = json.loads(response_delete.data.decode())63 assert response_delete.status_code == 20064 assert delete_data['message'] == 'Post deleted!'65def test_delete_post_by_logged_user_with_invalid_post_id(client_is_config):66 response = client_is_config.post('/api-posts/login',67 data=json.dumps(dict(68 username='ben',69 password='123')),70 content_type='application/json')71 data = json.loads(response.data.decode())72 assert data['message'] == 'Successfully logged in.'73 headers = {74 'Authorization' : 'Bearer ' + data['auth_token']75 }76 response_delete = client_is_config.delete('/api-posts/158',77 content_type='application/json',78 headers=headers,79 follow_redirects=True)80 delete_data = response_delete.json81 assert response_delete.status_code == 40482 assert delete_data['error'] == 'Post not found'83def test_delete_other_user_post_by_logged_user_403(client_is_config):84 response = client_is_config.post('/api-posts/login',85 data=json.dumps(dict(86 username='ben',87 password='123')),88 content_type='application/json')89 data = json.loads(response.data.decode())90 assert data['message'] == 'Successfully logged in.'91 headers = {92 'Authorization' : 'Bearer ' + data['auth_token']93 }94 response_delete = client_is_config.delete('/api-posts/5',95 content_type='application/json',96 headers=headers,97 follow_redirects=True)98 delete_data = response_delete.json99 assert response_delete.status_code == 403100 assert delete_data['error'] == 'Forbidden'101def test_delete_post_by_unlogged_user_401(client_is_config):102 headers = {103 'Authorization' : ""104 }105 response_delete = client_is_config.delete('/api-posts/5',106 content_type='application/json',107 headers=headers,108 follow_redirects=True)109 delete_data = response_delete.json110 assert response_delete.status_code == 401111 assert delete_data['message'] == 'Token is missing'112def test_logout(client_is_config):113 response = client_is_config.post('/api-posts/logout',114 content_type='application/json', follow_redirects=True)115 data = json.loads(response.data.decode())116 assert data['message'] == 'Successfully logged out.'117 assert response.status_code == 200118def test_set_credentials_should_set_pass(client_is_config):119 response = client_is_config.post('/api-posts/login',120 data=json.dumps(dict(121 username='oli',122 password='')),123 content_type='application/json',124 follow_redirects=True)125 data = json.loads(response.data.decode())126 assert data['message'] == 'Accepted'127 assert response.status_code == 202128 response_set_cred = client_is_config.post('/api-posts/9/set_credentials',129 data=json.dumps(dict(130 email='oli@jds.com',131 password='123',132 cf_password='123')),133 content_type='application/json',134 follow_redirects=True)135 data = json.loads(response_set_cred.data.decode())136 assert data['email'] == 'oli@jds.com'137 assert data['name'] == 'oli'138def test_set_credentials_user_with_pass_should_return_403(client_is_config):139 response_set_cred = client_is_config.post('/api-posts/8/set_credentials',140 data=json.dumps(dict(141 email='marc@gmail.com',142 password='123',143 cf_password='123')),144 content_type='application/json',145 follow_redirects=True)146 data = json.loads(response_set_cred.data.decode())147 assert data['error'] == 'Forbidden!'...
test_user_delete.py
Source:test_user_delete.py
1import allure2from lib.assertion import Assertions3from lib.base_case import BaseCase4from lib.my_requests import MyRequests5@allure.epic("Delete user cases")6@allure.testcase("https://software-testing.ru/lms/mod/assign/view.php?id=234827", "Negative and positive cases to check"7 "user deleting")8class TestUserDelete(BaseCase):9 @allure.description("Check if it's possible to delete reserved user")10 def test_delete_reserved_user(self):11 with allure.step("Login with the default user with ids '0-5'"):12 login_data = {13 "email": "vinkotov@example.com",14 "password": "1234"15 }16 response_login = MyRequests.post("/user/login", data=login_data)17 auth_sid = self.get_cookie(response_login, "auth_sid")18 token = self.get_header(response_login, "x-csrf-token")19 with allure.step("Delete user '2'"):20 response_delete = MyRequests.delete("/user/2", headers={"x-csrf-token": token},21 cookies={"auth_sid": auth_sid})22 Assertions.assert_code_status(response_delete, 400)23 assert response_delete.content.decode("utf-8") == \24 "Please, do not delete test users with ID 1, 2, 3, 4 or 5.", \25 f"Actual error '{response_delete.content.decode('utf-8')}' is not equal to the expected"26 @allure.description("Check user deleting after its creating")27 def test_delete_auth_user(self):28 with allure.step("New user creating"):29 register_data = self.prepare_registration_data()30 response = MyRequests.post("/user/", data=register_data)31 Assertions.assert_code_status(response, 200)32 Assertions.assert_json_has_key(response, "id")33 email = register_data["email"]34 password = register_data["password"]35 user_id = self.get_json_value(response, "id")36 with allure.step("Login with new user"):37 login_data = {38 "email": email,39 "password": password40 }41 response_login = MyRequests.post("/user/login", data=login_data)42 auth_sid = self.get_cookie(response_login, "auth_sid")43 token = self.get_header(response_login, "x-csrf-token")44 with allure.step("Delete new user using authorization of this user"):45 response_delete = MyRequests.delete(f"/user/{user_id}", headers={"x-csrf-token": token},46 cookies={"auth_sid": auth_sid})47 Assertions.assert_code_status(response_delete, 200)48 with allure.step("Get info about the delete user"):49 response_check = MyRequests.get(f"/user/{user_id}", headers={"x-csrf-token": token},50 cookies={"auth_sid": auth_sid})51 Assertions.assert_code_status(response_check, 404)52 assert response_check.content.decode("utf-8") == "User not found", \53 f"Actual error '{response_check.content.decode('utf-8')}' text is not equal to the expected"54 @allure.description("Check user deleting using another user")55 def test_delete_user_using_another_auth_user(self):56 # REGISTRATION57 register_data = self.prepare_registration_data()58 response = MyRequests.post("/user/", data=register_data)59 Assertions.assert_code_status(response, 200)60 Assertions.assert_json_has_key(response, "id")61 email = register_data["email"]62 password = register_data["password"]63 user_id_1 = self.get_json_value(response, "id")64 # LOGIN65 login_data = {66 "email": email,67 "password": password68 }69 response_login = MyRequests.post("/user/login", data=login_data)70 auth_sid = self.get_cookie(response_login, "auth_sid")71 token = self.get_header(response_login, "x-csrf-token")72 # REGISTRATION ANOTHER USER73 register_data = self.prepare_registration_data()74 response = MyRequests.post("/user/", data=register_data)75 Assertions.assert_code_status(response, 200)76 Assertions.assert_json_has_key(response, "id")77 user_id_2 = self.get_json_value(response, "id")78 # DELETE79 response_delete = MyRequests.delete(f"/user/{user_id_2}", headers={"x-csrf-token": token},80 cookies={"auth_sid": auth_sid})81 Assertions.assert_code_status(response_delete, 200)82 # CHECK DELETED USER83 response_check = MyRequests.get(f"/user/{user_id_2}", headers={"x-csrf-token": token},84 cookies={"auth_sid": auth_sid})85 Assertions.assert_code_status(response_check, 200)...
ex7_requests_and_method.py
Source:ex7_requests_and_method.py
1import requests2link = "https://playground.learnqa.ru/ajax/api/compare_query_type"3response_post = requests.post(link, data={"method": "POST"})4response_get = requests.get(link, params={"method": "GET"})5response_put = requests.put(link, data={"method": "PUT"})6response_delete = requests.delete(link, data={"method": ""})7response_head = requests.head(link, data={"method": "HEAD"})8print("1. ÐÐµÐ»Ð°ÐµÑ http-запÑÐ¾Ñ Ð»Ñбого Ñипа без паÑамеÑÑа method, опиÑаÑÑ ÑÑо бÑÐ´ÐµÑ Ð²ÑводиÑÑÑÑ Ð² ÑÑом ÑлÑÑае.")9print(response_post.text)10print(response_get.text)11print(response_put.text)12print(response_delete.text)13print("2. ÐÐµÐ»Ð°ÐµÑ http-запÑÐ¾Ñ Ð½Ðµ из ÑпиÑка. ÐапÑимеÑ, HEAD. ÐпиÑаÑÑ ÑÑо бÑÐ´ÐµÑ Ð²ÑводиÑÑÑÑ Ð² ÑÑом ÑлÑÑае.")14print(response_head.text)15print("3. ÐÐµÐ»Ð°ÐµÑ Ð·Ð°Ð¿ÑÐ¾Ñ Ñ Ð¿ÑавилÑнÑм знаÑением method. ÐпиÑаÑÑ ÑÑо бÑÐ´ÐµÑ Ð²ÑводиÑÑÑÑ Ð² ÑÑом ÑлÑÑае.")16print(response_post.text)17print(response_get.text)18print(response_put.text)19print(response_delete.text)20print("4. С помоÑÑÑ Ñикла пÑовеÑÑÐµÑ Ð²Ñе возможнÑе ÑоÑеÑÐ°Ð½Ð¸Ñ ÑеалÑнÑÑ
Ñипов запÑоÑа и знаÑений паÑамеÑÑа method.")21# ÐапÑÐ¸Ð¼ÐµÑ Ñ GET-запÑоÑом пеÑÐµÐ´Ð°ÐµÑ Ð·Ð½Ð°ÑÐµÐ½Ð¸Ñ Ð¿Ð°ÑамеÑÑа method Ñавное âGETâ, заÑем âPOSTâ, âPUTâ, âDELETEâ22# и Ñак далее. Ð Ñак Ð´Ð»Ñ Ð²ÑеÑ
Ñипов запÑоÑа.23# ÐайÑи Ñакое ÑоÑеÑание, когда ÑеалÑнÑй Ñип запÑоÑа не ÑÐ¾Ð²Ð¿Ð°Ð´Ð°ÐµÑ Ñо знаÑением паÑамеÑÑа, но ÑеÑÐ²ÐµÑ Ð¾ÑвеÑÐ°ÐµÑ Ñак, Ñловно вÑе ок.24# Ðли же наобоÑоÑ, когда ÑÐ¸Ð¿Ñ ÑовпадаÑÑ, но ÑеÑÐ²ÐµÑ ÑÑиÑаеÑ, ÑÑо ÑÑо не Ñак.25method = ['GET', 'POST', 'PUT', 'DELETE']26for x in method:27 response_get = requests.get(link, params={f"method": x})28 print("for GET request, method is", x, "status code is", response_get.status_code, response_get.text)29for x in method:30 response_post = requests.post(link, data={f"method": x})31 print("for POST request, method is", x, "status code is", response_post.status_code, response_post.text)32for x in method:33 response_put = requests.put(link, data={f"method": x})34 print("for PUT request, method is", x, "status code is", response_put.status_code, response_put.text)35for x in method:36 response_delete = requests.delete(link, data={f"method": x})...
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!!