How to use test_user_agent method in SeleniumBase

Best Python code snippet using SeleniumBase

test_error_handling.py

Source:test_error_handling.py Github

copy

Full Screen

1from unittest import mock2import tap_sailthru.client as client3import unittest4import requests5# mocked response class6class Mockresponse:7 def __init__(self, status_code, json, raise_error, headers=None):8 self.status_code = status_code9 self.raise_error = raise_error10 self.text = json11 self.headers = headers12 def raise_for_status(self):13 if not self.raise_error:14 return self.status_code15 raise requests.HTTPError("Sample message")16 def json(self):17 return self.text18# function to get mocked response19def get_response(status_code, json={}, raise_error=False, headers=None):20 return Mockresponse(status_code, json, raise_error, headers)21@mock.patch("requests.Session.request")22@mock.patch("time.sleep")23class TestExceptionHandling(unittest.TestCase):24 """25 Test cases to verify error is raised with proper message26 """27 def test_400_error_response_message(self, mocked_sleep, mocked_request):28 """29 Test case to verify 400 error message from response30 """31 # mock json error response32 response_json = {"error": 9, "errormsg": "Bad request for the URL."}33 mocked_request.return_value = get_response(400, response_json, True)34 # create sailthru client35 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")36 with self.assertRaises(client.SailthruBadRequestError) as e:37 # function call38 sailthru_client._build_request("test_endpoint", {}, "GET")39 # verify the error is raised as expected with message40 self.assertEqual(str(e.exception), "HTTP-error-code: 400, Error: 9, Message: Bad request for the URL.")41 def test_401_error_response_message(self, mocked_sleep, mocked_request):42 """43 Test case to verify 401 error message from response44 """45 # mock json error response46 response_json = {"error": 9, "errormsg": "Unauthorized for the URL."}47 mocked_request.return_value = get_response(401, response_json, True)48 49 # create sailthru client50 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")51 with self.assertRaises(client.SailthruUnauthorizedError) as e:52 # function call53 sailthru_client._build_request("test_endpoint", {}, "GET")54 # verify the error is raised as expected with message55 self.assertEqual(str(e.exception), "HTTP-error-code: 401, Error: 9, Message: Unauthorized for the URL.")56 def test_403_error_response_message(self, mocked_sleep, mocked_request):57 """58 Test case to verify 403 error message from response59 """60 # mock json error response61 response_json = {"error": 9, "errormsg": "Forbidden for the URL."}62 mocked_request.return_value = get_response(403, response_json, True)63 64 # create sailthru client65 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")66 with self.assertRaises(client.SailthruForbiddenError) as e:67 # function call68 sailthru_client._build_request("test_endpoint", {}, "GET")69 # verify the error is raised as expected with message70 self.assertEqual(str(e.exception), "HTTP-error-code: 403, Error: 9, Message: Forbidden for the URL.")71 def test_404_error_response_message(self, mocked_sleep, mocked_request):72 """73 Test case to verify 404 error message from response74 """75 # mock json error response76 response_json = {"error": 9, "errormsg": "Not Found."}77 mocked_request.return_value = get_response(404, response_json, True)78 79 # create sailthru client80 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")81 with self.assertRaises(client.SailthruNotFoundError) as e:82 # function call83 sailthru_client._build_request("test_endpoint", {}, "GET")84 # verify the error is raised as expected with message85 self.assertEqual(str(e.exception), "HTTP-error-code: 404, Error: 9, Message: Not Found.")86 def test_405_error_response_message(self, mocked_sleep, mocked_request):87 """88 Test case to verify 405 error message from response89 """90 # mock json error response91 response_json = {"error": 9, "errormsg": "Method not found for the URL."}92 mocked_request.return_value = get_response(405, response_json, True)93 94 # create sailthru client95 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")96 with self.assertRaises(client.SailthruMethodNotFoundError) as e:97 # function call98 sailthru_client._build_request("test_endpoint", {}, "GET")99 # verify the error is raised as expected with message100 self.assertEqual(str(e.exception), "HTTP-error-code: 405, Error: 9, Message: Method not found for the URL.")101 def test_409_error_response_message(self, mocked_sleep, mocked_request):102 """103 Test case to verify 409 error message from response104 """105 # mock json error response106 response_json = {"error": 9, "errormsg": "Conflict occurred for the URL."}107 mocked_request.return_value = get_response(409, response_json, True)108 109 # create sailthru client110 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")111 with self.assertRaises(client.SailthruConflictError) as e:112 # function call113 sailthru_client._build_request("test_endpoint", {}, "GET")114 # verify the error is raised as expected with message115 self.assertEqual(str(e.exception), "HTTP-error-code: 409, Error: 9, Message: Conflict occurred for the URL.")116 def test_429_error_response_message(self, mocked_sleep, mocked_request):117 """118 Test case to verify 429 error message from response119 """120 # mock json error response121 response_json = {"error": 9, "errormsg": "Rate limit exceeded for the URL."}122 mocked_request.return_value = get_response(429, response_json, True, {"X-Rate-Limit-Remaining": 1})123 124 # create sailthru client125 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")126 with self.assertRaises(client.SailthruClient429Error) as e:127 # function call128 sailthru_client._build_request("test_endpoint", {}, "GET")129 # verify the error is raised as expected with message130 self.assertEqual(str(e.exception), "HTTP-error-code: 429, Error: 9, Message: Rate limit exceeded for the URL.")131 def test_500_error_response_message(self, mocked_sleep, mocked_request):132 """133 Test case to verify 500 error message from response134 """135 # mock json error response136 response_json = {"error": 9, "errormsg": "Internal server error occurred."}137 mocked_request.return_value = get_response(500, response_json, True)138 139 # create sailthru client140 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")141 with self.assertRaises(client.SailthruInternalServerError) as e:142 # function call143 sailthru_client._build_request("test_endpoint", {}, "GET")144 # verify the error is raised as expected with message145 self.assertEqual(str(e.exception), "HTTP-error-code: 500, Error: 9, Message: Internal server error occurred.")146 def test_400_error_custom_message(self, mocked_sleep, mocked_request):147 """148 Test case to verify 400 error custom message149 """150 # mock json error response151 response_json = {"error": 9, "message": "Bad request for the URL."}152 mocked_request.return_value = get_response(400, response_json, True)153 154 # create sailthru client155 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")156 with self.assertRaises(client.SailthruBadRequestError) as e:157 # function call158 sailthru_client._build_request("test_endpoint", {}, "GET")159 # verify the error is raised as expected with message160 self.assertEqual(str(e.exception), "HTTP-error-code: 400, Error: 9, Message: The request is missing or has a bad parameter.")161 def test_401_error_custom_message(self, mocked_sleep, mocked_request):162 """163 Test case to verify 401 error custom message164 """165 # mock json error response166 response_json = {"error": 9, "message": "Unauthorized for the URL."}167 mocked_request.return_value = get_response(401, response_json, True)168 169 # create sailthru client170 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")171 with self.assertRaises(client.SailthruUnauthorizedError) as e:172 # function call173 sailthru_client._build_request("test_endpoint", {}, "GET")174 # verify the error is raised as expected with message175 self.assertEqual(str(e.exception), "HTTP-error-code: 401, Error: 9, Message: Invalid authorization credentials.")176 def test_403_error_custom_message(self, mocked_sleep, mocked_request):177 """178 Test case to verify 403 error custom message179 """180 # mock json error response181 response_json = {"error": 9, "message": "Forbidden for the URL."}182 mocked_request.return_value = get_response(403, response_json, True)183 184 # create sailthru client185 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")186 with self.assertRaises(client.SailthruForbiddenError) as e:187 # function call188 sailthru_client._build_request("test_endpoint", {}, "GET")189 # verify the error is raised as expected with message190 self.assertEqual(str(e.exception), "HTTP-error-code: 403, Error: 9, Message: User does not have permission to access the resource.")191 def test_404_error_custom_message(self, mocked_sleep, mocked_request):192 """193 Test case to verify 404 error custom message194 """195 # mock json error response196 response_json = {"error": 9, "message": "Not Found."}197 mocked_request.return_value = get_response(404, response_json, True)198 199 # create sailthru client200 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")201 with self.assertRaises(client.SailthruNotFoundError) as e:202 # function call203 sailthru_client._build_request("test_endpoint", {}, "GET")204 # verify the error is raised as expected with message205 self.assertEqual(str(e.exception), "HTTP-error-code: 404, Error: 9, Message: The resource you have specified cannot be found.")206 def test_405_error_custom_message(self, mocked_sleep, mocked_request):207 """208 Test case to verify 405 error custom message209 """210 # mock json error response211 response_json = {"error": 9, "message": "Method not found for the URL."}212 mocked_request.return_value = get_response(405, response_json, True)213 214 # create sailthru client215 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")216 with self.assertRaises(client.SailthruMethodNotFoundError) as e:217 # function call218 sailthru_client._build_request("test_endpoint", {}, "GET")219 # verify the error is raised as expected with message220 self.assertEqual(str(e.exception), "HTTP-error-code: 405, Error: 9, Message: The provided HTTP method is not supported by the URL.")221 def test_409_error_custom_message(self, mocked_sleep, mocked_request):222 """223 Test case to verify 409 error custom message224 """225 # mock json error response226 response_json = {"error": 9, "message": "Conflict occurred for the URL."}227 mocked_request.return_value = get_response(409, response_json, True)228 229 # create sailthru client230 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")231 with self.assertRaises(client.SailthruConflictError) as e:232 # function call233 sailthru_client._build_request("test_endpoint", {}, "GET")234 # verify the error is raised as expected with message235 self.assertEqual(str(e.exception), "HTTP-error-code: 409, Error: 9, Message: The request could not be completed due to a conflict with the current state of the server.")236 def test_429_error_custom_message(self, mocked_sleep, mocked_request):237 """238 Test case to verify 429 error custom message239 """240 # mock json error response241 response_json = {"error": 9, "message": "Rate limit exceeded for the URL."}242 mocked_request.return_value = get_response(429, response_json, True, {"X-Rate-Limit-Remaining": 1})243 244 # create sailthru client245 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")246 with self.assertRaises(client.SailthruClient429Error) as e:247 # function call248 sailthru_client._build_request("test_endpoint", {}, "GET")249 # verify the error is raised as expected with message250 self.assertEqual(str(e.exception), "HTTP-error-code: 429, Error: 9, Message: API rate limit exceeded, please retry after some time.")251 def test_500_error_custom_message(self, mocked_sleep, mocked_request):252 """253 Test case to verify 500 error custom message254 """255 # mock json error response256 response_json = {"error": 9, "message": "Internal server error occurred."}257 mocked_request.return_value = get_response(500, response_json, True)258 259 # create sailthru client260 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")261 with self.assertRaises(client.SailthruInternalServerError) as e:262 # function call263 sailthru_client._build_request("test_endpoint", {}, "GET")264 # verify the error is raised as expected with message265 self.assertEqual(str(e.exception), "HTTP-error-code: 500, Error: 9, Message: An error has occurred at Sailthru's end.")266 @mock.patch("tap_sailthru.client.LOGGER.warning")267 def test_403_and_99_error_custom_message(self, mocked_logger_warning, mocked_sleep, mocked_request):268 """269 Test case to verify for 403 error ane 99 sailthru error code270 we do not raise error and log that error with warning271 """272 # mock json error response273 response_json = {"error": 99, "message": "You may not export a blast that has not been sent"}274 mocked_request.return_value = get_response(403, response_json, True)275 # create sailthru client276 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")277 # function call278 actual_resp = sailthru_client._build_request("test_endpoint", {}, "GET")279 # verify the logger.warning is called with expected message280 mocked_logger_warning.assert_called_with("{}".format(response_json))281 # verify the response we got is same as the mocked response282 self.assertEqual(actual_resp, response_json)283 def test_200_response(self, mocked_sleep, mocked_request):284 """285 Test case to verify error is not raise for 200 status code286 """287 # mock json error response288 response_json = {"key1": "value1", "key2": "value2"}289 mocked_request.return_value = get_response(200, response_json)290 291 # create sailthru client292 sailthru_client = client.SailthruClient("test_api_key", "test_api_secret", "test_user_agent")293 # function call294 response = sailthru_client._build_request("test_endpoint", {}, "GET")295 # verify the mocked data is coming as expected...

Full Screen

Full Screen

test_base_url.py

Source:test_base_url.py Github

copy

Full Screen

1import unittest2from tap_tiktok_ads.client import TikTokClient3from unittest import mock4class Mockresponse:5 def __init__(self, status_code, json, headers=None):6 self.status_code = status_code7 self.text = json8 self.headers = headers9 def json(self):10 return self.text11# function to get mocked response12def get_response(status_code, json={}, headers=None):13 return Mockresponse(status_code, json, headers)14class TestSandboxUrl(unittest.TestCase):15 """16 Tets the sandbox url is called when passed in the config17 """18 @mock.patch("tap_tiktok_ads.client.requests.Session.request")19 @mock.patch("tap_tiktok_ads.client.TikTokClient.check_access_token")20 def test_sandbox_url_in_request(self, mock_check_access_token, mock_request):21 '''Verify request() is called with sandbox url when sandbox config parameter is True'''22 # set config23 config = {24 "access_token": "test_access_token",25 "user_agent": "test_user_agent",26 "sandbox": True27 }28 mock_request.return_value = get_response(200, {"code": 0, "message": "True"})29 client = TikTokClient(config.get("access_token"), [], config.get('sandbox'), config.get("user_agent"))30 client.request("GET", path = 'test')31 mock_request.assert_called_with('GET', 'https://sandbox-ads.tiktok.com/open_api/v1.2/test', timeout=300.0, headers={'Access-Token': 'test_access_token', 'Accept': 'application/json', 'User-Agent': 'test_user_agent'})32 @mock.patch("tap_tiktok_ads.client.requests.Session.request")33 @mock.patch("tap_tiktok_ads.client.TikTokClient.check_access_token")34 def test_business_url_in_request(self, mock_check_access_token, mock_request):35 '''Verify request() is called with business url when sandbox config parameter is False'''36 # set config37 config = {38 "access_token": "test_access_token",39 "user_agent": "test_user_agent",40 "sandbox": False41 }42 mock_request.return_value = get_response(200, {"code": 0, "message": "True"})43 client = TikTokClient(config.get("access_token"), [], config.get('sandbox'), config.get("user_agent"))44 client.request("GET", path = 'test')45 mock_request.assert_called_with('GET', 'https://business-api.tiktok.com/open_api/v1.2/test', timeout=300.0, headers={'Access-Token': 'test_access_token', 'Accept': 'application/json', 'User-Agent': 'test_user_agent'})46 @mock.patch("tap_tiktok_ads.client.requests.Session.request")47 @mock.patch("tap_tiktok_ads.client.TikTokClient.check_access_token")48 def test_business_url_in_request_when_not_passed_in_config(self, mock_check_access_token, mock_request):49 '''Verify request() is called with business url when sandbox config parameter not passed'''50 # set config51 config = {52 "access_token": "test_access_token",53 "user_agent": "test_user_agent"54 }55 mock_request.return_value = get_response(200, {"code": 0, "message": "True"})56 client = TikTokClient(config.get("access_token"), [], user_agent= config.get("user_agent"))57 client.request("GET", path = 'test')58 mock_request.assert_called_with('GET', 'https://business-api.tiktok.com/open_api/v1.2/test', timeout=300.0, headers={'Access-Token': 'test_access_token', 'Accept': 'application/json', 'User-Agent': 'test_user_agent'})59 @mock.patch("tap_tiktok_ads.client.requests.Session.request")60 @mock.patch("tap_tiktok_ads.client.TikTokClient.check_access_token")61 def test_business_url_in_request_when_string_passed_in_config(self, mock_check_access_token, mock_request):62 '''Verify request() is called with business url when sandbox config parameter not passed'''63 # set config64 config = {65 "access_token": "test_access_token",66 "user_agent": "test_user_agent",67 "sandbox": "False"68 }69 mock_request.return_value = get_response(200, {"code": 0, "message": "True"})70 client = TikTokClient(config.get("access_token"), [], user_agent= config.get("user_agent"))71 client.request("GET", path = 'test')72 mock_request.assert_called_with('GET', 'https://business-api.tiktok.com/open_api/v1.2/test', timeout=300.0, headers={'Access-Token': 'test_access_token', 'Accept': 'application/json', 'User-Agent': 'test_user_agent'})73 @mock.patch("tap_tiktok_ads.client.requests.Session.get")74 def test_sandbox_url_in_check_access_token(self, mock_get):75 '''Verify get() is called with sandbox url when sandbox config parameter is True'''76 # set config77 config = {78 "access_token": "test_access_token",79 "user_agent": "test_user_agent",80 "sandbox": True81 }82 mock_get.return_value = get_response(200, {"code": 0, "message": "True"})83 client = TikTokClient(config.get("access_token"), [], config.get('sandbox'), config.get("user_agent"))84 client.check_access_token()85 mock_get.assert_called_with(url='https://sandbox-ads.tiktok.com/open_api/v1.2/user/info', timeout=300.0, headers={'User-Agent': 'test_user_agent', 'Access-Token': 'test_access_token', 'Accept': 'application/json'})86 @mock.patch("tap_tiktok_ads.client.requests.Session.get")87 def test_business_url_in_check_access_token(self, mock_get):88 '''Verify get() is called with sandbox url when sandbox config parameter is False'''89 # set config90 config = {91 "access_token": "test_access_token",92 "user_agent": "test_user_agent",93 "sandbox": False94 }95 mock_get.return_value = get_response(200, {"code": 0, "message": "True"})96 client = TikTokClient(config.get("access_token"), [], config.get('sandbox'), config.get("user_agent"))97 client.check_access_token()98 mock_get.assert_called_with(url='https://business-api.tiktok.com/open_api/v1.2/user/info', timeout=300.0, headers={'User-Agent': 'test_user_agent', 'Access-Token': 'test_access_token', 'Accept': 'application/json'})99 @mock.patch("tap_tiktok_ads.client.requests.Session.get")100 def test_business_url_in_check_access_token_when_not_passed_in_config(self, mock_get):101 '''Verify get() is called with sandbox url when sandbox config parameter is not passed'''102 # set config103 config = {104 "access_token": "test_access_token",105 "user_agent": "test_user_agent"106 }107 mock_get.return_value = get_response(200, {"code": 0, "message": "True"})108 client = TikTokClient(config.get("access_token"), [], user_agent=config.get("user_agent"))109 client.check_access_token()110 mock_get.assert_called_with(url='https://business-api.tiktok.com/open_api/v1.2/user/info', timeout=300.0, headers={'User-Agent': 'test_user_agent', 'Access-Token': 'test_access_token', 'Accept': 'application/json'})111 @mock.patch("tap_tiktok_ads.client.requests.Session.get")112 def test_business_url_in_check_access_token_when_string_passed_in_config(self, mock_get):113 '''Verify get() is called with sandbox url when sandbox config parameter is not passed'''114 # set config115 config = {116 "access_token": "test_access_token",117 "user_agent": "test_user_agent",118 "sandbox": "False"119 }120 mock_get.return_value = get_response(200, {"code": 0, "message": "True"})121 client = TikTokClient(config.get("access_token"), [], config.get('sandbox'), config.get("user_agent"))122 client.check_access_token()...

Full Screen

Full Screen

test_cert.py

Source:test_cert.py Github

copy

Full Screen

...9 pass10class TestCert(unittest.TestCase):11 def setUp(self):12 self.logger = Logger()13 def test_test_user_agent(self):14 self.assertFalse(test_user_agent("Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0", self.logger), "android")15 self.assertTrue(test_user_agent("Mozilla/5.0 (Mobile; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "mobile")16 self.assertTrue(test_user_agent("Mozilla/5.0 (Tablet; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "tablet")17 self.assertTrue(test_user_agent("Mozilla/5.0 (Mobile; nnnn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "example device")18 self.assertFalse(test_user_agent("Mozilla/5.0 (Mobile; nn nn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "invalid device")19 self.assertFalse(test_user_agent("Mozilla/5.0 (Mobile; nn;nn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "invalid device")20 self.assertFalse(test_user_agent("Mozilla/5.0 (Mobile; nn/nn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "invalid device")21 self.assertFalse(test_user_agent("Mozilla/5.0 (Mobile; nn(nn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "invalid device")22 self.assertFalse(test_user_agent("Mozilla/5.0 (Mobile; nn)nn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "invalid device")23 self.assertTrue(test_user_agent("Mozilla/5.0 (Mobile; nnnn ; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "extra whitespace in device")24 self.assertTrue(test_user_agent("Mozilla/5.0 (Mobile;nnnn; rv:26.0) Gecko/26.0 Firefox/26.0", self.logger), "no whitespace in device")25if __name__ == '__main__':...

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 SeleniumBase 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