################################################################################ Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) ## and others ## ## All rights reserved. This program and the accompanying materials ## are made available under the terms of the Apache License, Version 2.0 ## which accompanies this distribution, and is available at ## http://www.apache.org/licenses/LICENSE-2.0 ################################################################################importcopyfromapi.appimportAppfromapi.middleware.authenticationimportAuthenticationMiddlewarefromapi.responders.responder_baseimportResponderBasefromapi.backends.ldap_accessimportLDAPAccessfromfalcon.testingimportTestCasefromtest.api.responders_test.test_dataimportbasefromunittest.mockimportMagicMockfromutils.mongo_accessimportMongoAccessdefmock_auth_method(*args):returnNoneclassTestBase(TestCase):defsetUp(self,authenticate=False):super().setUp()# mockself.authenticate=authenticateifnotauthenticate:self.original_auth_method=AuthenticationMiddleware.process_requestAuthenticationMiddleware.process_request=mock_auth_methodResponderBase.get_constants_by_name=MagicMock(side_effect=lambdaname:base.CONSTANTS_BY_NAMES[name])# mock mongo accessMongoAccess.mongo_connect=MagicMock()MongoAccess.db=MagicMock()MongoAccess.client=MagicMock()# mock ldap accessLDAPAccess.get_ldap_params=MagicMock()LDAPAccess.connect_ldap_server=MagicMock()log_level='debug'self.app=App(log_level=log_level).get_app()defvalidate_get_request(self,url,params={},headers=None,mocks={},side_effects={},expected_code=base.SUCCESSFUL_CODE,expected_response=None):self.validate_request("GET",url,params,headers,"",mocks,side_effects,expected_code,expected_response)defvalidate_request(self,action,url,params,headers,body,mocks,side_effects,expected_code,expected_response):formock_method,mock_datainmocks.items():mock_method.return_value=mock_dataformock_method,side_effectinside_effects.items():mock_method.side_effect=side_effectresult=self.simulate_request(action,url,params=params,headers=headers,body=body)self.assertEqual(result.status,expected_code)ifexpected_response:self.assertEqual(result.json,expected_response)defvalidate_post_request(self,url,headers={},body="",mocks={},side_effects={},expected_code=base.CREATED_CODE,expected_response=None):self.validate_request("POST",url,{},headers,body,mocks,side_effects,expected_code,expected_response)defvalidate_delete_request(self,url,params={},headers={},mocks={},side_effects={},expected_code=base.SUCCESSFUL_CODE,expected_response=None):self.validate_request("DELETE",url,params,headers,"",mocks,side_effects,expected_code,expected_response)defget_updated_data(self,original_data,deleted_keys=[],updates={}):copy_data=copy.deepcopy(original_data)forkeyindeleted_keys:delcopy_data[key]forkey,valueinupdates.items():copy_data[key]=valuereturncopy_datadeftearDown(self):# if the authentication method has been mocked, it needs to be reset after testifnotself.authenticate:AuthenticationMiddleware.process_request=self.original_auth_method