def test_process_origin_return(self): cors = CORS(allow_origins_list=['test.com']) cors._process_origin = mock.Mock(return_value=False) cors._process_credentials = mock.Mock() headers = {'origin': 'rackspace.com'} self.simulate_cors_request(cors, headers=headers, preflight=False) self.assertEqual(cors._process_origin.call_count, 1) self.assertEqual(cors._process_credentials.call_count, 0)
def test_process_credentials_regex(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() cors = CORS(allow_credentials_origins_regex='.*\.rackspace\..*') cors._set_allow_credentials = mock.Mock() self.assertEqual( cors._process_credentials(fake_req, fake_resp, 'www.rackspace.com'), True) cors._set_allow_credentials.assert_called_once_with(fake_resp)
def test_process_credentials_disallow(self): fake_req = mock.MagicMock() fake_resp = mock.MagicMock() cors = CORS(allow_credentials_origins_list=['not_rackspace'], allow_credentials_origins_regex='.*\.rackspace\..*') cors._set_allow_credentials = mock.Mock() self.assertEqual( cors._process_credentials(fake_req, fake_resp, 'some_other_domain.lan'), False) self.assertEqual(cors._set_allow_credentials.call_count, 0)
def test_process_credentials_called(self): cors = CORS(allow_all_origins=True, allow_all_methods=True, allow_all_headers=True) cors._process_methods = mock.Mock(return_value=True) cors._process_credentials = mock.Mock() headers = {'origin': 'rackspace.com'} self.simulate_cors_request(cors, headers=headers, preflight=True) cors._process_credentials.assert_called_once_with( self.resource.captured_req, self.resource.captured_resp, 'rackspace.com')
def test_header_not_allowed(self): cors = CORS(allow_all_origins=True, allow_all_methods=True, allow_headers_list=['test_header']) cors._process_methods = mock.Mock(return_value=True) cors._process_credentials = mock.Mock() headers = { 'origin': 'rackspace.com', 'access-control-request-headers': 'not_allowed_header' } self.simulate_cors_request(cors, headers=headers, preflight=True) self.assertEqual(cors._process_methods.call_count, 1) self.assertEqual(cors._process_credentials.call_count, 0)