Best Python code snippet using autotest_python
test_utils.py
Source:test_utils.py
1# -*- coding: utf-8 -*-2from __future__ import absolute_import, unicode_literals3from oauthlib.oauth1.rfc5849.utils import *4from oauthlib.common import unicode_type5from ...unittest import TestCase6class UtilsTests(TestCase):7 sample_params_list = [8 ("notoauth", "shouldnotbehere"),9 ("oauth_consumer_key", "9djdj82h48djs9d2"),10 ("oauth_token", "kkk9d7dh3k39sjv7"),11 ("notoautheither", "shouldnotbehere")12 ]13 sample_params_dict = {14 "notoauth": "shouldnotbehere",15 "oauth_consumer_key": "9djdj82h48djs9d2",16 "oauth_token": "kkk9d7dh3k39sjv7",17 "notoautheither": "shouldnotbehere"18 }19 sample_params_unicode_list = [20 ("notoauth", "shouldnotbehere"),21 ("oauth_consumer_key", "9djdj82h48djs9d2"),22 ("oauth_token", "kkk9d7dh3k39sjv7"),23 ("notoautheither", "shouldnotbehere")24 ]25 sample_params_unicode_dict = {26 "notoauth": "shouldnotbehere",27 "oauth_consumer_key": "9djdj82h48djs9d2",28 "oauth_token": "kkk9d7dh3k39sjv7",29 "notoautheither": "shouldnotbehere"30 }31 authorization_header = """OAuth realm="Example",32 oauth_consumer_key="9djdj82h48djs9d2",33 oauth_token="kkk9d7dh3k39sjv7",34 oauth_signature_method="HMAC-SHA1",35 oauth_timestamp="137131201",36 oauth_nonce="7d8f3e4a",37 oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D" """.strip()38 bad_authorization_headers = (39 "OAuth",40 "OAuth oauth_nonce=",41 "Negotiate b2F1dGhsaWI=",42 "OA",43 )44 def test_filter_params(self):45 # The following is an isolated test function used to test the filter_params decorator.46 @filter_params47 def special_test_function(params, realm=None):48 """ I am a special test function """49 return 'OAuth ' + ','.join(['='.join([k, v]) for k, v in params])50 # check that the docstring got through51 self.assertEqual(special_test_function.__doc__, " I am a special test function ")52 # Check that the decorator filtering works as per design.53 # Any param that does not start with 'oauth'54 # should not be present in the filtered params55 filtered_params = special_test_function(self.sample_params_list)56 self.assertNotIn("notoauth", filtered_params)57 self.assertIn("oauth_consumer_key", filtered_params)58 self.assertIn("oauth_token", filtered_params)59 self.assertNotIn("notoautheither", filtered_params)60 def test_filter_oauth_params(self):61 # try with list62 # try with list63 # try with list64 self.assertEqual(len(self.sample_params_list), 4)65 # Any param that does not start with 'oauth'66 # should not be present in the filtered params67 filtered_params = filter_oauth_params(self.sample_params_list)68 self.assertEqual(len(filtered_params), 2)69 self.assertTrue(filtered_params[0][0].startswith('oauth'))70 self.assertTrue(filtered_params[1][0].startswith('oauth'))71 # try with dict72 # try with dict73 # try with dict74 self.assertEqual(len(self.sample_params_dict), 4)75 # Any param that does not start with 'oauth'76 # should not be present in the filtered params77 filtered_params = filter_oauth_params(self.sample_params_dict)78 self.assertEqual(len(filtered_params), 2)79 self.assertTrue(filtered_params[0][0].startswith('oauth'))80 self.assertTrue(filtered_params[1][0].startswith('oauth'))81 def test_escape(self):82 self.assertRaises(ValueError, escape, b"I am a string type. Not a unicode type.")83 self.assertEqual(escape("I am a unicode type."), "I%20am%20a%20unicode%20type.")84 self.assertIsInstance(escape("I am a unicode type."), unicode_type)85 def test_unescape(self):86 self.assertRaises(ValueError, unescape, b"I am a string type. Not a unicode type.")87 self.assertEqual(unescape("I%20am%20a%20unicode%20type."), 'I am a unicode type.')88 self.assertIsInstance(unescape("I%20am%20a%20unicode%20type."), unicode_type)89 def test_parse_authorization_header(self):90 # make us some headers91 authorization_headers = parse_authorization_header(self.authorization_header)92 # is it a list?93 self.assertIsInstance(authorization_headers, list)94 # are the internal items tuples?95 for header in authorization_headers:96 self.assertIsInstance(header, tuple)97 # are the internal components of each tuple unicode?98 for k, v in authorization_headers:99 self.assertIsInstance(k, unicode_type)100 self.assertIsInstance(v, unicode_type)101 # let's check the parsed headers created102 correct_headers = [103 ("oauth_nonce", "7d8f3e4a"),104 ("oauth_timestamp", "137131201"),105 ("oauth_consumer_key", "9djdj82h48djs9d2"),106 ('oauth_signature', 'djosJKDKJSD8743243%2Fjdk33klY%3D'),107 ('oauth_signature_method', 'HMAC-SHA1'),108 ('oauth_token', 'kkk9d7dh3k39sjv7'),109 ('realm', 'Example')]110 self.assertEqual(sorted(authorization_headers), sorted(correct_headers))111 # Check against malformed headers.112 for header in self.bad_authorization_headers:...
tests_messages.py
Source:tests_messages.py
1import json2from http import HTTPStatus3import pytest4import requests5@pytest.fixture(scope='session')6def message_create_url(base_url, chat_default_id_messages):7 return f'{base_url}/v1/chats/{chat_default_id_messages}/messages'8@pytest.fixture(scope='session')9def message_get_url(base_url, chat_default_id_messages):10 return f'{base_url}/v1/chats/{chat_default_id_messages}/messages'11@pytest.fixture(scope='session')12def chat_default_id_messages(authorization_headers, base_url):13 chat_create_url = f'{base_url}/v1/chats'14 data = {15 "chat_name": "MSG_CHAT"16 }17 resp = requests.post(chat_create_url, json=data, headers=authorization_headers)18 json_content = json.loads(resp.content)19 chat_id = json_content['chat_id']20 return chat_id21@pytest.fixture(scope='session')22def user_default_id(authorization_headers, base_url, chat_default_id_messages):23 user_create_url = f'{base_url}/v1/chats/{chat_default_id_messages}/users'24 data = {25 "user_name": "dark_killer111"26 }27 resp = requests.post(user_create_url, json=data, headers=authorization_headers)28 json_content = json.loads(resp.content)29 user_id = json_content['user_id']30 return user_id31def test_send_message_unauthorized(message_create_url, user_default_id):32 data = {33 "message": "HELLO WORLD!"34 }35 params = {36 "user_id": str(user_default_id)37 }38 resp = requests.post(message_create_url, json=data, params=params)39 assert resp.status_code == HTTPStatus.UNAUTHORIZED40def test_send_message_invalid_user_id(message_create_url, user_default_id, authorization_headers):41 data = {42 "message": "HELLO WORLD!"43 }44 params = {45 "user_id": str(10101011)46 }47 resp = requests.post(message_create_url, headers=authorization_headers, json=data, params=params)48 assert resp.status_code == HTTPStatus.NOT_FOUND49def test_send_message_invalid_params(message_create_url, user_default_id, authorization_headers):50 data = {51 "wrong_field": "HELLO WORLD!"52 }53 params = {54 "user_id": str(10101011)55 }56 resp = requests.post(message_create_url, headers=authorization_headers, json=data, params=params)57 assert resp.status_code == HTTPStatus.BAD_REQUEST58def test_send_message_created(message_create_url, user_default_id, authorization_headers):59 params = {60 "user_id": str(user_default_id)61 }62 for i in range(10):63 elem = {64 "message": f'hello - {i}'65 }66 resp = requests.post(message_create_url,67 headers=authorization_headers,68 json=elem,69 params=params)70 assert resp.status_code == HTTPStatus.CREATED71@pytest.mark.parametrize(72 "limit",73 [74 None,75 101029939376 ],77)78def test_get_messages_with_invalid_limit(limit, message_get_url, authorization_headers):79 params = {80 "limit": limit81 }82 resp = requests.get(message_get_url, headers=authorization_headers, params=params)83 assert resp.status_code == HTTPStatus.BAD_REQUEST84@pytest.mark.parametrize(85 "limit",86 [87 1, 5, 1088 ],89)90def test_get_messages_with_valid_limit(limit, message_create_url, authorization_headers):91 limit_params = {"limit": limit}92 resp = requests.get(message_create_url, headers=authorization_headers, params=limit_params)93 assert resp.status_code == HTTPStatus.OK94 messages = json.loads(resp.content)95 assert len(messages['messages']) == limit96@pytest.mark.parametrize(97 "limit, from_",98 [99 (5, 2),100 (10, 2),101 (10, 9)102 ],103)104def test_get_messages_with_limit_offset(limit, from_, message_create_url, authorization_headers):105 params = {"limit": limit, "from": from_}106 resp = requests.get(message_create_url, headers=authorization_headers, params=params)...
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!!