How to use mock_up method in autotest

Best Python code snippet using autotest_python

test_ssh_auth.py

Source:test_ssh_auth.py Github

copy

Full Screen

1"""2 :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>3"""4import pytest5import salt.states.ssh_auth as ssh_auth6from tests.support.mock import MagicMock, patch7@pytest.fixture8def configure_loader_modules():9 return {ssh_auth: {}}10def test_present():11 """12 Test to verifies that the specified SSH key13 is present for the specified user.14 """15 name = "sshkeys"16 user = "root"17 source = "salt://ssh_keys/id_rsa.pub"18 ret = {"name": name, "changes": {}, "result": True, "comment": ""}19 mock = MagicMock(return_value="exists")20 mock_data = MagicMock(side_effect=["replace", "new"])21 with patch.dict(22 ssh_auth.__salt__, {"ssh.check_key": mock, "ssh.set_auth_key": mock_data}23 ):24 with patch.dict(ssh_auth.__opts__, {"test": True}):25 comt = "The authorized host key sshkeys is already present for user root"26 ret.update({"comment": comt})27 assert ssh_auth.present(name, user, source) == ret28 with patch.dict(ssh_auth.__opts__, {"test": False}):29 comt = "The authorized host key sshkeys for user root was updated"30 ret.update({"comment": comt, "changes": {name: "Updated"}})31 assert ssh_auth.present(name, user, source) == ret32 comt = "The authorized host key sshkeys for user root was added"33 ret.update({"comment": comt, "changes": {name: "New"}})34 assert ssh_auth.present(name, user, source) == ret35def test_absent():36 """37 Test to verifies that the specified SSH key is absent.38 """39 name = "sshkeys"40 user = "root"41 source = "salt://ssh_keys/id_rsa.pub"42 ret = {"name": name, "changes": {}, "result": None, "comment": ""}43 mock = MagicMock(44 side_effect=["User authorized keys file not present", "Key removed"]45 )46 mock_up = MagicMock(side_effect=["update", "updated"])47 with patch.dict(48 ssh_auth.__salt__, {"ssh.rm_auth_key": mock, "ssh.check_key": mock_up}49 ):50 with patch.dict(ssh_auth.__opts__, {"test": True}):51 comt = "Key sshkeys for user root is set for removal"52 ret.update({"comment": comt})53 assert ssh_auth.absent(name, user, source) == ret54 comt = "Key is already absent"55 ret.update({"comment": comt, "result": True})56 assert ssh_auth.absent(name, user, source) == ret57 with patch.dict(ssh_auth.__opts__, {"test": False}):58 comt = "User authorized keys file not present"59 ret.update({"comment": comt, "result": False})60 assert ssh_auth.absent(name, user, source) == ret61 comt = "Key removed"62 ret.update({"comment": comt, "result": True, "changes": {name: "Removed"}})63 assert ssh_auth.absent(name, user, source) == ret64def test_manage():65 """66 Test to verifies that the specified SSH key is absent.67 """68 user = "root"69 ret = {"name": "", "changes": {}, "result": None, "comment": ""}70 mock_rm = MagicMock(71 side_effect=["User authorized keys file not present", "Key removed"]72 )73 mock_up = MagicMock(side_effect=["update", "updated"])74 mock_set = MagicMock(side_effect=["replace", "new"])75 mock_keys = MagicMock(76 return_value={77 "somekey": {78 "enc": "ssh-rsa",79 "comment": "user@host",80 "options": [],81 "fingerprint": "b7",82 }83 }84 )85 with patch.dict(86 ssh_auth.__salt__,87 {88 "ssh.rm_auth_key": mock_rm,89 "ssh.set_auth_key": mock_set,90 "ssh.check_key": mock_up,91 "ssh.auth_keys": mock_keys,92 },93 ):94 with patch("salt.states.ssh_auth.present") as call_mocked_present:95 mock_present = {"comment": "", "changes": {}, "result": None}96 call_mocked_present.return_value = mock_present97 with patch.dict(ssh_auth.__opts__, {"test": True}):98 assert ssh_auth.manage("sshid", ["somekey"], user) == ret99 comt = "somekey Key set for removal"100 ret.update({"comment": comt})101 assert ssh_auth.manage("sshid", [], user) == ret102 with patch("salt.states.ssh_auth.present") as call_mocked_present:103 mock_present = {"comment": "", "changes": {}, "result": True}104 call_mocked_present.return_value = mock_present105 with patch.dict(ssh_auth.__opts__, {"test": False}):106 ret = {"name": "", "changes": {}, "result": True, "comment": ""}107 assert ssh_auth.manage("sshid", ["somekey"], user) == ret108 with patch("salt.states.ssh_auth.absent") as call_mocked_absent:109 mock_absent = {"comment": "Key removed"}110 call_mocked_absent.return_value = mock_absent111 ret.update(112 {113 "comment": "",114 "result": True,115 "changes": {"somekey": "Key removed"},116 }117 )118 assert ssh_auth.manage("sshid", ["addkey"], user) == ret119 # add a key120 with patch("salt.states.ssh_auth.present") as call_mocked_present:121 mock_present = {122 "comment": "The authorized host key newkey for user {} was added".format(123 user124 ),125 "changes": {"newkey": "New"},126 "result": True,127 }128 call_mocked_present.return_value = mock_present129 with patch.dict(ssh_auth.__opts__, {"test": False}):130 ret = {131 "name": "",132 "changes": {"newkey": "New"},133 "result": True,134 "comment": "",135 }...

Full Screen

Full Screen

test_upapi.py

Source:test_upapi.py Github

copy

Full Screen

1"""2Unit tests for upapi.__init__3"""4import mock5import tests.unit6import upapi7import upapi.endpoints8import upapi.exceptions9import upapi.scopes10class TestGetAccessToken(tests.unit.TestResource):11 """12 Tests upapi.get_access_token13 """14 def test_get_access_token(self):15 """16 Verify access token getter.17 """18 #19 # No creds, so getter should raise.20 #21 self.assertRaises(upapi.exceptions.MissingCredentials, upapi.get_access_token)22 #23 # Set creds and get the right token24 #25 upapi.credentials = self.credentials26 self.assertEqual(upapi.get_access_token(), self.token)27class TestSetAccessToken(tests.unit.TestSDK):28 """29 Tests upapi.set_access_token30 """31 def test_set_access_token(self):32 """33 Verify the token is set correctly by trying to get it back.34 """35 upapi.set_access_token(self.token)36 self.assertEqual(upapi.get_access_token(), self.token)37class TestUp(tests.unit.TestSDK):38 """39 Tests upapi.up40 """41 @mock.patch('upapi.base.UpApi', autospec=True)42 def test_up(self, mock_upapi):43 """44 Verify UpApi object creation.45 :param mock_upapi: mocked UpApi object46 """47 upapi.up()48 mock_upapi.assert_called_with(49 upapi.client_id,50 upapi.client_secret,51 upapi.redirect_uri,52 app_scope=upapi.scope,53 credentials_storage=upapi.credentials_storage,54 user_credentials=upapi.credentials)55class TestGetRedirectUrl(tests.unit.TestSDK):56 """57 Tests upapi.get_redirect_url58 """59 @mock.patch('upapi.base.UpApi.get_redirect_url', autospec=True)60 def test_get_redirect_url(self, mock_redirect):61 """62 Verify redirect URL gets generated correctly.63 :param mock_redirect: mocked UpApi method64 """65 upapi.get_redirect_url()66 self.assertTrue(mock_redirect.called)67class TestGetToken(tests.unit.TestSDK):68 """69 Tests upapi.get_token70 """71 @mock.patch('upapi.up', autospec=True)72 def test_get_token(self, mock_up):73 """74 Verify that global credentials get set.75 :param mock_up: mock UpApi getter76 """77 #78 # Credentials not set yet.79 #80 self.assertIsNone(upapi.credentials)81 #82 # Set up the mock to return the proper token and credential values83 #84 mock_up.return_value = mock.Mock('upapi.base.UpApi', autospec=True)85 mock_up.return_value.get_up_token = mock.Mock('upapi.base.UpApi.get_up_token', autospec=True)86 mock_up.return_value.get_up_token.return_value = self.token87 mock_up.return_value.credentials = mock.Mock('upapi.base.UpApi.credentials', autospec=True)88 #89 # Set the token and credentials. Then verify.90 #91 callback_url = 'https://callback.url'92 token = upapi.get_token(callback_url)93 mock_up.assert_called_with()94 mock_up.return_value.get_up_token.assert_called_with(callback_url)95 self.assertEqual(token, self.token)96 self.assertEqual(upapi.credentials, mock_up.return_value.credentials)97class TestRefreshToken(tests.unit.TestSDK):98 """99 Tests upapi.refresh_token100 """101 @mock.patch('upapi.up', autospec=True)102 def test_refresh_token(self, mock_up):103 """104 Verify that global credentials get set correctly when we refresh.105 :param mock_up: mock UpApi getter106 """107 upapi.token = self.token108 #109 # Set up the mocks to return the token and credentials110 #111 mock_up.return_value = mock.Mock('upapi.base.UpApi', autospec=True)112 mock_up.return_value.refresh_token = mock.Mock('upapi.base.UpApi.refresh_token', autospec=True)113 refreshed_token = {'access_token': 'refreshed_token'}114 mock_up.return_value.refresh_token.return_value = refreshed_token115 mock_up.return_value.credentials = mock.Mock('upapi.base.UpApi.credentials', autospec=True)116 #117 # Refresh and test118 #119 token = upapi.refresh_token()120 mock_up.assert_called_with()121 mock_up.return_value.refresh_token.assert_called_with()122 self.assertEqual(token, refreshed_token)123 self.assertEqual(upapi.credentials, mock_up.return_value.credentials)124class TestDisconnect(tests.unit.TestSDK):125 """126 Tests upapi.disconnect127 """128 @mock.patch('upapi.up', autospec=True)129 def test_disconnect(self, mock_up):130 """131 Verify that a disconnect makes the global credentials None.132 :param mock_up: mock UpApi getter133 """134 upapi.credentials = self.credentials135 mock_up.return_value = mock.Mock('upapi.base.UpApi', autospec=True)136 mock_up.return_value.disconnect = mock.Mock('upapi.base.UpApi.disconnect', autospec=True)137 upapi.disconnect()138 mock_up.assert_called_with()139 mock_up.return_value.disconnect.assert_called_with()140 self.assertIsNone(upapi.credentials)141class TestGetUser(tests.unit.TestSDK):142 """143 Tests upapi.get_user144 """145 @mock.patch('upapi.user.User', autospec=True)146 def test_get_user(self, mock_user):147 """148 Verify User object gets greated with global values.149 :param mock_user: mocked User object150 """151 upapi.get_user()152 mock_user.assert_called_with(153 upapi.client_id,154 upapi.client_secret,155 upapi.redirect_uri,156 app_scope=upapi.scope,157 credentials_storage=upapi.credentials_storage,...

Full Screen

Full Screen

MatrixTesting.py

Source:MatrixTesting.py Github

copy

Full Screen

1import sys2sys.path.append('../ProyectoPython_IE0117_2021-II')3from mine_sweeper_backend.GeneralMatrix import GeneralMatrix as GeneralMatrix4# BoxClasses import BoxClass as BoxClass5def print_matrix_testing(matrix):6 for row in matrix:7 print("")8 for col in row:9 print(col, end=" ")10# Testing general matrix for int:11print("3x4 matrix of int objects")12matrix_test = GeneralMatrix(3,4,int).matrix13print_matrix_testing(matrix_test)14# Testing general matrix for object:15print("2x2 matrix of objects")16matrix_test = GeneralMatrix(2,2,object).matrix17print_matrix_testing(matrix_test)18# Testing general matrix for object:19print("9x7 matrix of 0")20general_matrix_object = GeneralMatrix(9,7,0)21matrix_test = general_matrix_object.matrix22print_matrix_testing(matrix_test)23# Testing object substitution matrix for object:24print("\n\nSubstituting 0 in (0,4) for \'x\''")25general_matrix_object.set_element_value(0,4,'x')26print_matrix_testing(matrix_test)27# Testing object substitution matrix for object:28print("\n\nTesting get_element(), expected: \'x\''")29get_elem_test = general_matrix_object.get_element(0,4)30print_matrix_testing(get_elem_test)31# Testing str():32print("\n\nTesting \'str()\' method:\n")33print(str(general_matrix_object))34# Testing get_matrix_dimensions:35print("\n\nTesting \'get_matrix_dimensions()\' method: expexted (9,7)\n")36size_of_general_matrix_object = general_matrix_object.get_matrix_dimensions()37print(size_of_general_matrix_object)38# Making a mock up matrix39def mock_up_matrix():40 mock_up_matrix_elems = [41 # [0 , 1 , 2 , 3, 4 , 5, 6 cols/rows42 [0 , 'a', 1 , 2, '*', 1, 0 ], # 043 ['c', 'b', '*', 3, '*', 1, 0 ], # 1 44 [0 , 2 , '*', 2, 1 , 1, 0 ], # 245 [0 , 1 , 0 , 1, 1 , 1, 1 ], # 346 [0 , 0 , 0 , 1, '*', 2, '*'], # 447 [0 , 0 , 0 , 0, 0 , 0, 0 ] # 548 ]49 50 mock_up = GeneralMatrix(6,7)51 for row_ind, row in enumerate(mock_up_matrix_elems):52 for col_ind, elem in enumerate(row):53 mock_up.set_element_value(row_ind,col_ind, elem)54 return mock_up55print("\nPrinting a mock up matrix:\n")56mock_up = mock_up_matrix()57print(str(mock_up))58# Testing get_adjacent_elements:59print("\n\nTesting \'get_adjacent_elements(0,0)\' method: expexted ['a', 'c', 'b']\n")60mock_up_adjacent = mock_up.get_adjacent_elements(0,0)61print(mock_up_adjacent)62print("\n\nTesting \'get_adjacent_coordinates(0,0)\' method: expexted [(0,1), (1,0), (1,1)]\n")63mock_up_adjacent = mock_up.get_adjacent_coordinates(0,0)64print(mock_up_adjacent)65print("\n\nTesting \'get_adjacent_coordinates_and_elements(0,0)\' method: expexted [(0,1,'a'), (1,0, 'c'), (1,1, 'b')]\n")66mock_up_adjacent = mock_up.get_adjacent_coordinates_and_elements(0,0)67print(mock_up_adjacent)68print("\n\nTesting \'get_adjacent_elements(4,4)\' method: expexted [1, 1, 1, 1, 2, 0, 0, 0]\n")69mock_up_adjacent = mock_up.get_adjacent_elements(4,4)70print(mock_up_adjacent)71# Testing Matrix iterator:72print("\n\nTesting \'__iter__()\' method: expexted the rows of the mock up matrix\n")73for row in mock_up:74 print(row)75print("\n\nTesting \'__iter__()\' with enumerator method: expexted the rows of the mock up matrix\n")76for ind_rw, row in enumerate(mock_up):77 print("Index: " + str(ind_rw))...

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