How to use get_identity_provider method in tempest

Best Python code snippet using tempest_python

oauth_utility.py

Source:oauth_utility.py Github

copy

Full Screen

...26REFRESH_TOKEN = "refresh_token"27PASSWORD = "password"28OAUTH2_CONFIG_TYPE = "oauth2"29OIDC_CONFIG_TYPE = "oidc"30def get_identity_provider(server, session):31 '''32 Get the identity provider for the given vc/server33 Sample can be found at34 https://github.com/vmware/vsphere-automation-sdk-python/blob/master/samples/vsphere/oauth/list_external_identity_providers.py35 '''36 stub_config = StubConfigurationFactory.new_std_configuration(37 get_requests_connector(38 session=session,39 url=HTTP_ENDPOINT.format(40 server)))41 id_client = Providers(stub_config)42 providers = id_client.list()43 identity_provider = ""44 for provider in providers:45 if provider.is_default:46 identity_provider = provider47 break48 return identity_provider49def get_saml_assertion(server, session, access_token, id_token=None):50 """51 Exchange access token to saml token to connect to VC52 Sample can be found at53 https://github.com/vmware/vsphere-automation-sdk-python/blob/master/samples/vsphere/oauth/exchange_access_id_token_for_saml.py54 """55 stub_config = StubConfigurationFactory.new_std_configuration(56 get_requests_connector(57 session=session,58 url=HTTP_ENDPOINT.format(server)59 )60 )61 oauth_security_context = create_oauth_security_context(access_token)62 stub_config.connector.set_security_context(oauth_security_context)63 token_exchange = TokenExchange(stub_config)64 exchange_spec = token_exchange.ExchangeSpec(65 grant_type=token_exchange.TOKEN_EXCHANGE_GRANT,66 subject_token_type=token_exchange.ACCESS_TOKEN_TYPE,67 actor_token_type=token_exchange.ID_TOKEN_TYPE,68 requested_token_type=token_exchange.SAML2_TOKEN_TYPE,69 actor_token=id_token, subject_token=access_token)70 response = token_exchange.exchange(exchange_spec)71 saml_token = response.access_token72 # convert saml token to saml assertion73 samlAssertion = etree.tostring(74 etree.XML(base64.decodebytes(75 bytes(saml_token, 'utf-8')76 ))77 ).decode('utf-8')78 return samlAssertion79def get_endpoints(identity_provider):80 """81 Extract different ednpoints from the identity provider object82 Note that the endpoint naming convention might vary for different providers83 Currently, support is provided for84 oauth2 -> Cloud Service Provider (CSP)85 oidc -> Microssoft ADFS86 """87 if identity_provider.auth_query_params is not None:88 auth_query_params = identity_provider.auth_query_params89 else:90 auth_query_params = {}91 if identity_provider.config_tag.lower() == OAUTH2_CONFIG_TYPE:92 auth_endpoint = identity_provider.oauth2.auth_endpoint93 token_endpoint = identity_provider.oauth2.token_endpoint94 auth_query_params.update(identity_provider.oauth2.auth_query_params)95 if identity_provider.config_tag.lower() == OIDC_CONFIG_TYPE:96 auth_endpoint = identity_provider.oidc.discovery_endpoint97 token_endpoint = identity_provider.oidc.auth_endpoint98 auth_query_params.update(identity_provider.oidc.auth_query_params)99 return [auth_endpoint, token_endpoint, auth_query_params]100def get_basic_auth_string(id, secret):101 """102 Return authorization string103 """104 auth_string = id + ":" + secret105 auth_string = "Basic " + base64.b64encode(auth_string.encode()).decode()106 return auth_string107def login_using_client_credentials(server, session, client_id, client_secret):108 """109 Get access token when grant_type is client_credentials110 """111 identity_provider = get_identity_provider(server, session)112 [discovery_endpoint, token_endpoint, auth_query_params] = \113 get_endpoints(identity_provider)114 headers = {115 'Content-Type': 'application/x-www-form-urlencoded',116 'Authorization': get_basic_auth_string(client_id, client_secret),117 'Accept': 'application/json'118 }119 data = {120 'grant_type': CLIENT_CREDENTIALS121 }122 response = session.post(token_endpoint, headers=headers, data=data).json()123 access_token = response['access_token']124 return get_saml_assertion(server, session, access_token)125def login_using_authorization_code(126 server,127 session,128 client_id,129 client_secret,130 redirect_uri,131 callback):132 """133 Get access token when grant_type is authorization_code134 """135 identity_provider = get_identity_provider(server, session)136 [auth_endpoint, token_endpoint, auth_query_params] = \137 get_endpoints(identity_provider)138 state = uuid.uuid1()139 auth_endpoint += "?client_id=" + client_id + "&redirect_uri=" + \140 redirect_uri + "&state=" + str(state)141 for key, value in auth_query_params.items():142 auth_endpoint += "&" + key + "="143 if isinstance(value, list):144 auth_endpoint += value[0]145 [code, state] = callback(auth_endpoint)146 headers = {147 "Content-Type": "application/x-www-form-urlencoded",148 "Authorization": get_basic_auth_string(client_id, client_secret),149 "Accept": "application/json"150 }151 data = {152 "grant_type": AUTHORIZATION_CODE,153 "client_id": client_id,154 "client_secret": client_secret,155 "redirect_uri": redirect_uri,156 "code": code,157 "state": state158 }159 response = session.post(token_endpoint, headers=headers, data=data).json()160 access_token = response['access_token']161 return get_saml_assertion(server, session, access_token)162def login_using_refresh_token(163 server,164 session,165 client_id,166 client_secret,167 refresh_token):168 """169 Get access token when grant_type is refresh_token170 """171 identity_provider = get_identity_provider(server, session)172 [auth_endpoint, token_endpoint, auth_query_params] = \173 get_endpoints(identity_provider)174 headers = {175 "Content-Type": "application/x-www-form-urlencoded",176 "Authorization": get_basic_auth_string(client_id, client_secret),177 "Accept": "application/json"178 }179 data = {180 "grant_type": REFRESH_TOKEN,181 "refresh_token": refresh_token182 }183 response = session.post(token_endpoint, headers=headers, data=data).json()184 access_token = response['access_token']185 return get_saml_assertion(server, session, access_token)186def login_using_password(server, session, username, password):187 """188 Get access token when grant_type is password189 """190 identity_provider = get_identity_provider(server, session)191 [auth_endpoint, token_endpoint, auth_query_params] = \192 get_endpoints(identity_provider)193 headers = {194 "Content-Type": "application/x-www-form-urlencoded",195 "Authorization": get_basic_auth_string(username, password),196 "Accept": "application/json"197 }198 data = {199 "grant_type": PASSWORD,200 "username": username,201 "password": password202 }203 response = session.post(token_endpoint, headers=headers, data=data).json()204 print(response)...

Full Screen

Full Screen

test_oci_identity_provider_facts.py

Source:test_oci_identity_provider_facts.py Github

copy

Full Screen

...34 return mocker.patch.object(oci_utils, "list_all_resources")35@pytest.fixture()36def call_with_backoff_patch(mocker):37 return mocker.patch.object(oci_utils, "call_with_backoff")38def get_identity_provider(**kwargs):39 identity_provider = Saml2IdentityProvider(40 id="ocid1.saml2idp.oc1..xxxxxEXAMPLExxxxx"41 )42 for attr, val in six.iteritems(kwargs):43 setattr(identity_provider, attr, val)44 return identity_provider45def get_identity_providers():46 return [47 get_identity_provider(48 id="ocid1.saml2idp.oc1..xxxxxEXAMPLExxxxx1", protocol="SAML2"49 ),50 get_identity_provider(51 id="ocid1.saml2idp.oc1..xxxxxEXAMPLExxxxx2", protocol="SAML2"52 ),53 ]54def get_module(**kwargs):55 params = {}56 params.update(kwargs)57 module = FakeModule(**params)58 return module59def get_response(status=200, headers=None, data=None, request=None):60 if not headers:61 headers = dict()62 return oci.Response(status, headers, data, request)63def test_get_identity_provider_raises_service_error(64 identity_client, call_with_backoff_patch65):66 call_with_backoff_patch.side_effect = ServiceError(67 500, "InternalServerError", dict(), "Internal Server Error"68 )69 with pytest.raises(ServiceError) as exc_info:70 oci_identity_provider_facts.get_identity_provider(identity_client, get_module())71 se = exc_info.value72 assert se.status == 50073 assert se.code == "InternalServerError"74 assert se.message == "Internal Server Error"75def test_get_identity_provider(identity_client, call_with_backoff_patch):76 identity_provider = get_identity_provider()77 call_with_backoff_patch.return_value = get_response(data=identity_provider)78 result = oci_identity_provider_facts.get_identity_provider(79 identity_client, get_module()80 )81 assert len(result) == 182 call_with_backoff_patch.assert_called_once()83 assert result[0]["id"] == identity_provider.id84def test_list_identity_providers_raises_service_error(85 identity_client, list_all_resources_patch86):87 list_all_resources_patch.side_effect = ServiceError(88 500, "InternalServerError", dict(), "Internal Server Error"89 )90 with pytest.raises(ServiceError) as exc_info:91 oci_identity_provider_facts.list_identity_providers(92 identity_client, get_module()93 )94 se = exc_info.value95 assert se.status == 50096 assert se.code == "InternalServerError"97 assert se.message == "Internal Server Error"98def test_list_identity_providers_when_no_identity_providers_exist(99 identity_client, list_all_resources_patch100):101 list_all_resources_patch.return_value = []102 result = oci_identity_provider_facts.list_identity_providers(103 identity_client, get_module()104 )105 assert len(result) == 0106def test_list_identity_providers_when_identity_providers_exist(107 identity_client, list_all_resources_patch108):109 identity_providers = get_identity_providers()110 list_all_resources_patch.return_value = identity_providers111 result = oci_identity_provider_facts.list_identity_providers(112 identity_client, get_module()113 )114 assert len(result) == 2115 assert list_all_resources_patch.call_count == 1116def test_list_identity_providers_filter_by_name(117 identity_client, list_all_resources_patch118):119 module = get_module(120 name="testidentityprovider",121 protocol="SAML2",122 compartment_id="ocid1.tenancy.oc1..xxxxxEXAMPLExxxxx",123 )124 list_all_resources_patch.return_value = [125 get_identity_provider(name="testidentityprovider")126 ]127 result = oci_identity_provider_facts.list_identity_providers(128 identity_client, module129 )130 assert len(result) == 1131 list_all_resources_patch.assert_called_with(132 identity_client.list_identity_providers,133 protocol="SAML2",134 compartment_id="ocid1.tenancy.oc1..xxxxxEXAMPLExxxxx",135 name="testidentityprovider",...

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