How to use mock_pass method in toolium

Best Python code snippet using toolium_python

test_handlers.py

Source:test_handlers.py Github

copy

Full Screen

1import os2import unittest3import sqlite34import sms5from unittest.mock import patch, PropertyMock, Mock6class SimpleLoggingMixinTestCase(unittest.TestCase):7 def test_log_without_status_in_response(self):8 mixin = sms.SimpleLoggingMixin()9 response = {}10 with self.assertRaises(ValueError):11 mixin._log(response)12 def test_log_unxpected_value_of_status_in_response(self):13 mixin = sms.SimpleLoggingMixin()14 response = {'status': None}15 with self.assertRaises(ValueError):16 mixin._log(response)17 @patch('logging.getLogger')18 def test_log_ok_response(self, mock_logging):19 mixin = sms.SimpleLoggingMixin()20 response = {'status': 'ok', 'phone': '79149009900'}21 mixin._log(response)22 self.assertTrue(mock_logging.return_value.info.called)23 @patch('logging.getLogger')24 def test_log_error_response(self, mock_logging):25 mixin = sms.SimpleLoggingMixin()26 response = {'status': 'error', 'phone': '79149009900', 'error_code': 3500, 'error_msg': 'description'}27 mixin._log(response)28 self.assertTrue(mock_logging.return_value.info.called)29class SQLiteLoggingMixinTestCase(unittest.TestCase):30 def test_creation_of_db(self, uri='file::memory:'):31 mixin = sms.SQLiteLoggingMixin(uri)32 mixin.c.execute(mixin.DDL)33 mixin.c.connection.commit()34 mixin.c.execute('SELECT Count(*) AS c FROM Results;')35 self.assertEqual(mixin.c.fetchone(), (0,))36 return mixin37 def test_creation_of_file_db(self):38 db_path = '/tmp/testdb.sqlite'39 try:40 os.remove(db_path)41 except OSError:42 pass43 self.test_creation_of_db('file:{}?mode=rwc'.format(db_path))44 self.assertTrue(os.path.isfile(db_path))45 os.remove(db_path)46 def test_log_without_status_in_response(self):47 mixin = self.test_creation_of_db()48 response = {}49 with self.assertRaises(ValueError):50 mixin._log(response)51 def test_log_unxpected_value_of_status_in_response(self):52 mixin = self.test_creation_of_db()53 response = {'status': None}54 with self.assertRaises(ValueError):55 mixin._log(response)56 def test_log_ok_response(self):57 mixin = self.test_creation_of_db()58 response = {'status': 'ok', 'phone': '79149009900'}59 mixin._log(response)60 mixin.c.execute('SELECT success, phone AS c FROM Results;')61 self.assertEqual(mixin.c.fetchone(), (1.0, response['phone']))62 def test_log_error_response(self):63 mixin = self.test_creation_of_db()64 response = {'status': 'error', 'phone': '79149009900', 'error_code': 3500, 'error_msg': 'description'}65 mixin._log(response)66 mixin.c.execute('SELECT success, phone, error_code, error_msg AS c FROM Results;')67 self.assertEqual(mixin.c.fetchone(), (0.0, response['phone'], response['error_code'], response['error_msg']))68class SMSCRU_SMSHandlerMixinTestCase(unittest.TestCase):69 @patch('requests.get', return_value=Mock(status_code=0))70 @patch('sms.SMSCRU_SMSHandlerMixin._log', create=True)71 @patch('sms.SMSCRU_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')72 @patch('sms.SMSCRU_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')73 def test_send_invalid_HTTP_code_empty_user_data(self, mock_pass, mock_login, mock_log, mock_requests):74 mixin = sms.SMSCRU_SMSHandlerMixin()75 user_data = {}76 mixin.send(user_data)77 mock_log.assert_called_once_with({78 'status': 'error',79 'phone': '-',80 'error_code': None,81 'error_msg': 'data: {}'.format(user_data)82 })83 @patch('requests.get', return_value=Mock(status_code=0))84 @patch('sms.SMSCRU_SMSHandlerMixin._log', create=True)85 @patch('sms.SMSCRU_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')86 @patch('sms.SMSCRU_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')87 def test_send_invalid_HTTP_code(self, mock_pass, mock_login, mock_log, mock_requests):88 mixin = sms.SMSCRU_SMSHandlerMixin()89 user_data = {'status': 'ok', 'phone': '79149009900'}90 mixin.send(user_data)91 mock_log.assert_called_once_with({92 'status': 'error',93 'phone': user_data['phone'],94 'error_code': None,95 'error_msg': 'data: {}'.format(user_data)96 })97 @patch('requests.get', return_value=Mock(status_code=200,98 json=lambda x=None: {'status': 'ok', 'phone': '79149009900'}))99 @patch('sms.SMSCRU_SMSHandlerMixin._log', create=True)100 @patch('sms.SMSCRU_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')101 @patch('sms.SMSCRU_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')102 def test_send_valid_response(self, mock_pass, mock_login, mock_log, mock_requests):103 mixin = sms.SMSCRU_SMSHandlerMixin()104 user_data = {'status': 'ok', 'phone': '79149009900'}105 mixin.send(user_data)106 mock_log.assert_called_once_with(user_data)107class SMSTRAFFIC_SMSHandlerMixinTestCase(unittest.TestCase):108 @patch('requests.post', return_value=Mock(status_code=0, text='{"token": "a"}'))109 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')110 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')111 def test_init_with_invalid_HTTP_code(self, mock_pass, mock_login, mock_requests):112 with self.assertRaises(RuntimeError):113 mixin = sms.SMSTRAFFIC_SMSHandlerMixin()114 @patch('requests.post', return_value=Mock(status_code=200, text='{}'))115 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')116 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')117 def test_init_with_response_without_token(self, mock_pass, mock_login, mock_requests):118 with self.assertRaises(RuntimeError):119 mixin = sms.SMSTRAFFIC_SMSHandlerMixin()120 @patch('requests.post', return_value=Mock(status_code=200, text='{"token": "a"}'))121 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')122 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')123 def test_init_with_response_without_token(self, mock_pass, mock_login, mock_requests):124 mixin = sms.SMSTRAFFIC_SMSHandlerMixin()125 self.assertEqual(mixin.token, 'a')126 return mixin127 @patch('requests.post', return_value=Mock(status_code=0, text='{}'))128 @patch('sms.SMSTRAFFIC_SMSHandlerMixin._log', create=True)129 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')130 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')131 def test_send_invalid_HTTP_code_empty_user_data(self, mock_pass, mock_login, mock_log, mock_requests):132 mixin = self.test_init_with_response_without_token()133 user_data = {}134 mixin.send(user_data)135 mock_log.assert_called_once_with({136 'status': 'error',137 'phone': '-',138 'error_code': None,139 'error_msg': 'data: {}'.format(user_data)140 })141 @patch('requests.post', return_value=Mock(status_code=0, text='{"status": "ok", "phone": "79149009900"}'))142 @patch('sms.SMSTRAFFIC_SMSHandlerMixin._log', create=True)143 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')144 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')145 def test_send_invalid_HTTP_code(self, mock_pass, mock_login, mock_log, mock_requests):146 mixin = self.test_init_with_response_without_token()147 user_data = {'status': 'ok', 'phone': '79149009900'}148 mixin.send(user_data)149 mock_log.assert_called_once_with({150 'status': 'error',151 'phone': user_data['phone'],152 'error_code': None,153 'error_msg': 'data: {}'.format(user_data)154 })155 @patch('requests.post', return_value=Mock(status_code=200, text='{"status": "ok", "phone": "79149009900"}'))156 @patch('sms.SMSTRAFFIC_SMSHandlerMixin._log', create=True)157 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.login', create=True, new_callable=PropertyMock, return_value='login')158 @patch('sms.SMSTRAFFIC_SMSHandlerMixin.password', create=True, new_callable=PropertyMock, return_value='pass')159 def test_send_valid(self, mock_pass, mock_login, mock_log, mock_requests):160 mixin = self.test_init_with_response_without_token()161 user_data = {'status': 'ok', 'phone': '79149009900'}162 mixin.send(user_data)163 mock_log.assert_called_once_with(user_data)164class get_handlerMixinTestCase(unittest.TestCase):165 def test_invalid_handler_name(self):166 with self.assertRaises(ValueError):167 handler = sms.get_handler('a')168 def test_invalid_logger_name(self):169 with self.assertRaises(ValueError):170 handler = sms.get_handler('smsr.ru', 'a')171 def test_create_handler_with_simple_logger(self):172 handler = sms.get_handler('smsr.ru')173 self.assertIsInstance(handler, sms.SMSCRU_SMSHandlerMixin)174 self.assertIsInstance(handler, sms.BaseSMSHandler)175 self.assertIsInstance(handler, sms.SimpleLoggingMixin)176 def test_create_handler_with_db_logger(self):177 db_path = '/tmp/testdb.sqlite'178 try:179 os.remove(db_path)180 except OSError:181 pass182 conn = sqlite3.connect(db_path)183 conn.close()184 handler = sms.get_handler('smsr.ru', 'sqlite')185 self.assertIsInstance(handler, sms.SMSCRU_SMSHandlerMixin)186 self.assertIsInstance(handler, sms.BaseSMSHandler)187 self.assertIsInstance(handler, sms.SQLiteLoggingMixin)188 os.remove(db_path)189 def test_create_handler_with_db_logger_with_additional_params(self):190 db_path = '/tmp/testdb.sqlite'191 db_uri = 'file::memory:'192 try:193 os.remove(db_path)194 except OSError:195 pass196 handler = sms.get_handler('smsr.ru', 'sqlite', {'db_uri': db_uri})197 self.assertIsInstance(handler, sms.SMSCRU_SMSHandlerMixin)198 self.assertIsInstance(handler, sms.BaseSMSHandler)199 self.assertIsInstance(handler, sms.SQLiteLoggingMixin)...

Full Screen

Full Screen

test_login.py

Source:test_login.py Github

copy

Full Screen

1import unittest2import mock3from aims.login import main4class LoginTest(unittest.TestCase):5 @mock.patch('aims.login.Employee')6 @mock.patch('aims.login.decrypt_pass', return_value='pass')7 @mock.patch('aims.login.ConnectDb')8 @mock.patch('builtins.input', return_value='sample')9 @mock.patch('aims.login.getpass', return_value='pass')10 @mock.patch('aims.login.Menu')11 @mock.patch('aims.login.os')12 def test_main_employee(self, mock_os, mock_menu, mock_pass, mock_input, mock_db, mock_decrypt, mock_employee):13 mock_db().get_user_info.return_value = ['1', 'pass']14 mock_menu().draw_menu.return_value = 'EMPLOYEE'15 main()16 mock_decrypt.assert_called_once()17 mock_employee.assert_called_once()18 mock_db().close_conn.assert_called_once()19 mock_employee().employee_features.assert_called_once()20 mock_pass.assert_called_once()21 self.assertEqual(mock_os.system.call_count, 2)22 self.assertEqual(mock_input.call_count, 2)23 @mock.patch('aims.login.Supervisor')24 @mock.patch('aims.login.decrypt_pass', return_value='pass')25 @mock.patch('aims.login.ConnectDb')26 @mock.patch('aims.login.input', return_value='sample')27 @mock.patch('aims.login.getpass', return_value='pass')28 @mock.patch('aims.login.Menu')29 @mock.patch('aims.login.os')30 def test_main_supervisor(self, mock_os, mock_menu, mock_pass, mock_input, mock_db,31 mock_decrypt, mock_supervisor):32 mock_db().get_user_info.return_value = ['2', 'pass']33 mock_db().get_user_role.return_value = 'dummy'34 mock_menu().draw_menu.return_value = 'SUPERVISOR'35 main()36 mock_supervisor.assert_called_once()37 mock_decrypt.assert_called_once()38 mock_pass.assert_called_once()39 mock_db().close_conn.assert_called_once()40 mock_supervisor().supervisor_features.assert_called_once()41 self.assertEqual(mock_supervisor().supervisor_id, '2')42 self.assertEqual(mock_os.system.call_count, 2)43 self.assertEqual(mock_input.call_count, 2)44 @mock.patch('aims.login.Admin')45 @mock.patch('aims.login.decrypt_pass', return_value='pass')46 @mock.patch('aims.login.ConnectDb')47 @mock.patch('aims.login.input', return_value='sample')48 @mock.patch('aims.login.getpass', return_value='pass')49 @mock.patch('aims.login.Menu')50 @mock.patch('aims.login.os')51 def test_main_admin(self, mock_os, mock_menu, mock_pass, mock_input, mock_db, mock_decrypt, mock_admin):52 mock_db().get_user_info.return_value = ['3', 'pass']53 mock_db().get_user_role.return_value = 'dummy'54 mock_menu().draw_menu.return_value = 'ADMIN'55 main()56 mock_admin.assert_called_once()57 mock_db().close_conn.assert_called_once()58 mock_pass.assert_called_once()59 mock_decrypt.assert_called_once()60 mock_admin().admin_features.assert_called_once()61 self.assertEqual(mock_admin().admin_id, '3')62 self.assertEqual(mock_os.system.call_count, 2)63 self.assertEqual(mock_input.call_count, 2)64 @mock.patch('aims.login.decrypt_pass', return_value='pass2')65 @mock.patch('aims.login.ConnectDb')66 @mock.patch('builtins.input', return_value='sample')67 @mock.patch('aims.login.getpass', return_value='pass')68 @mock.patch('aims.login.Menu')69 @mock.patch('aims.login.os')70 @mock.patch('builtins.print')71 def test_main_invalid_credentials(self, mock_print, mock_os, mock_menu, mock_pass, mock_input, mock_db, mock_decrypt):72 mock_db().get_user_info.return_value = ['1', 'pass2']73 mock_menu().draw_menu.return_value = 'EMPLOYEE'74 mock_os.system.side_effect = ['', '', SystemExit]75 self.assertRaises(SystemExit, main)76 mock_decrypt.assert_called_once()77 mock_pass.assert_called_once()78 self.assertEqual(mock_os.system.call_count, 3)79 self.assertEqual(mock_input.call_count, 3)80 @mock.patch('aims.login.decrypt_pass', return_value='pass')81 @mock.patch('aims.login.ConnectDb')82 @mock.patch('builtins.input', return_value='sample')83 @mock.patch('aims.login.getpass', return_value='pass')84 @mock.patch('aims.login.Menu')85 @mock.patch('aims.login.os')86 @mock.patch('builtins.print')87 def test_main_not_authorized(self, mock_print, mock_os, mock_menu, mock_pass, mock_input, mock_db, mock_decrypt):88 mock_db().get_user_info.return_value = ['3', 'pass']89 mock_db().get_user_role.return_value = None90 mock_menu().draw_menu.return_value = 'ADMIN'91 mock_os.system.side_effect = ['', '', SystemExit]92 self.assertRaises(SystemExit, main)93 mock_decrypt.assert_called_once()94 mock_pass.assert_called_once()95 self.assertEqual(mock_os.system.call_count, 3)96 self.assertEqual(mock_input.call_count, 3)97 @mock.patch('builtins.print')98 @mock.patch('aims.login.sys')99 @mock.patch('aims.login.ConnectDb')100 @mock.patch('aims.login.os')101 def test_main_exception(self, mock_os, mock_db, mock_sys, mock_print):102 mock_os.system.side_effect = Exception103 main()104 mock_sys.exit.assert_called_once()105 mock_print.assert_called_once()106 mock_db().close_conn.assert_called_once()107if __name__ == '__main__':...

Full Screen

Full Screen

test_camera.py

Source:test_camera.py Github

copy

Full Screen

1# pylint: disable=protected-access,redefined-outer-name2"""Test to verify that Beward library works."""3from datetime import datetime4from unittest import TestCase5import requests_mock6from requests import ConnectTimeout7from beward import BewardCamera8from beward.const import ALARM_MOTION9from . import function_url, load_binary, load_fixture10from .const import MOCK_HOST, MOCK_PASS, MOCK_USER11class TestBewardCamera(TestCase):12 """Test case for BewardCamera class."""13 def test___init__(self):14 """Initialize test."""15 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)16 self.assertEqual(None, bwd.rtsp_port)17 self.assertEqual(0, bwd.stream)18 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS, rtsp_port=123, stream=2)19 self.assertEqual(123, bwd.rtsp_port)20 self.assertEqual(2, bwd.stream)21 @requests_mock.Mocker()22 def test_obtain_uris(self, mock):23 """Test that obtain urls from device."""24 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)25 self.assertIsNone(bwd._live_image_url)26 self.assertIsNone(bwd._rtsp_live_video_url)27 mock.register_uri("get", function_url("rtsp"), exc=ConnectTimeout)28 bwd.obtain_uris()29 expect = (30 "rtsp://" + MOCK_USER + ":" + MOCK_PASS + "@" + MOCK_HOST + ":554/av0_0"31 )32 self.assertEqual(expect, bwd._rtsp_live_video_url)33 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS, stream=1)34 mock.register_uri("get", function_url("rtsp"))35 bwd.obtain_uris()36 expect = (37 function_url("images", user=MOCK_USER, password=MOCK_PASS) + "?channel=0"38 )39 self.assertEqual(expect, bwd._live_image_url)40 expect = (41 "rtsp://" + MOCK_USER + ":" + MOCK_PASS + "@" + MOCK_HOST + ":554/av0_1"42 )43 self.assertEqual(expect, bwd._rtsp_live_video_url)44 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)45 mock.register_uri("get", function_url("rtsp"), text=load_fixture("rtsp.txt"))46 bwd.obtain_uris()47 expect = (48 "rtsp://" + MOCK_USER + ":" + MOCK_PASS + "@" + MOCK_HOST + ":47456/av0_0"49 )50 self.assertEqual(expect, bwd._rtsp_live_video_url)51 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS, rtsp_port=123, stream=2)52 mock.register_uri("get", function_url("rtsp"), text=load_fixture("rtsp.txt"))53 bwd.obtain_uris()54 expect = (55 "rtsp://" + MOCK_USER + ":" + MOCK_PASS + "@" + MOCK_HOST + ":123/av0_2"56 )57 self.assertEqual(expect, bwd._rtsp_live_video_url)58 @requests_mock.Mocker()59 def test_live_image_url(self, mock):60 """Test that obtain live image url from device."""61 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)62 mock.register_uri("get", function_url("rtsp"))63 self.assertIsNone(bwd._live_image_url)64 expect = (65 function_url("images", user=MOCK_USER, password=MOCK_PASS) + "?channel=0"66 )67 self.assertEqual(expect, bwd.live_image_url)68 @requests_mock.Mocker()69 def test_rtsp_live_video_url(self, mock):70 """Test that obtain RTSP live video url from device."""71 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)72 mock.register_uri("get", function_url("rtsp"), text=load_fixture("rtsp.txt"))73 self.assertIsNone(bwd._rtsp_live_video_url)74 expect = (75 "rtsp://" + MOCK_USER + ":" + MOCK_PASS + "@" + MOCK_HOST + ":47456/av0_0"76 )77 self.assertEqual(expect, bwd.rtsp_live_video_url)78 @requests_mock.Mocker()79 def test_live_image(self, mock):80 """Test that receive live image from device."""81 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)82 image = load_binary("image.jpg")83 mock.register_uri("get", function_url("images"), content=image)84 res = bwd.live_image85 self.assertIsNone(res)86 mock.register_uri(87 "get",88 function_url("images"),89 content=image,90 headers={"Content-Type": "image/jpeg"},91 )92 res = bwd.live_image93 self.assertEqual(image, res)94 @requests_mock.Mocker()95 def test__handle_alarm(self, mock):96 """Test that handle alarms."""97 bwd = BewardCamera(MOCK_HOST, MOCK_USER, MOCK_PASS)98 image = load_binary("image.jpg")99 # Check initial state100 self.assertIsNone(bwd.last_motion_timestamp)101 self.assertIsNone(bwd.last_motion_image)102 ts1 = datetime.now()103 mock.register_uri(104 "get",105 function_url("images"),106 content=image,107 headers={"Content-Type": "image/jpeg"},108 )109 bwd._handle_alarm(ts1, ALARM_MOTION, True)110 self.assertEqual(ts1, bwd.last_motion_timestamp)...

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