def test_get_oauth_access_token_response_missing_fields(self): with raises(requests.RequestException): responses.add(responses.POST, self.url_base + self.oauth_api_path, json={"issuedFor": "learning_public_api"}) SAPSuccessFactorsAPIClient.get_oauth_access_token( self.url_base, self.client_id, self.client_secret, self.company_id, self.user_id, self.user_type)
def test_get_oauth_access_token_response_non_json(self): """ Test get_oauth_access_token with non json type response""" with raises(requests.RequestException): responses.add( responses.POST, urljoin(self.url_base, self.oauth_api_path), ) SAPSuccessFactorsAPIClient.get_oauth_access_token( self.url_base, self.client_id, self.client_secret, self.company_id, self.user_id, self.user_type)
def test_get_oauth_access_token(): oauth_api_path = "learning/oauth-api/rest/v1/token" url_base = "http://test.successfactors.com/" client_id = "client_id" client_secret = "client_secret" company_id = "company_id" user_id = "user_id" expires_in = 1485383526 access_token = "access_token" SAPSuccessFactorsGlobalConfiguration.objects.create( completion_status_api_path="", course_api_path="", oauth_api_path=oauth_api_path) expected_response_body = { "expiresIn": expires_in, "access_token": access_token } expected_response = (access_token, datetime.datetime.utcfromtimestamp(expires_in)) responses.add( # pylint: disable=no-member responses.POST, # pylint: disable=no-member url_base + oauth_api_path, json=expected_response_body) actual_response = SAPSuccessFactorsAPIClient.get_oauth_access_token( url_base, client_id, client_secret, company_id, user_id) assert actual_response == expected_response assert len(responses.calls) == 1 # pylint: disable=no-member assert responses.calls[0].request.url == url_base + oauth_api_path # pylint: disable=no-member
def test_get_oauth_access_token_response_missing_fields(): with raises(requests.RequestException): oauth_api_path = "learning/oauth-api/rest/v1/token" url_base = "http://test.successfactors.com/" client_id = "client_id" client_secret = "client_secret" company_id = "company_id" user_id = "user_id" SAPSuccessFactorsGlobalConfiguration.objects.create( completion_status_api_path="", course_api_path="", oauth_api_path=oauth_api_path) responses.add( # pylint: disable=no-member responses.POST, # pylint: disable=no-member url_base + oauth_api_path, json={"issuedFor": "learning_public_api"}) SAPSuccessFactorsAPIClient.get_oauth_access_token( url_base, client_id, client_secret, company_id, user_id)
def test_get_oauth_access_token(self): expected_response = (self.access_token, NOW + datetime.timedelta(seconds=self.expires_in)) responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) actual_response = SAPSuccessFactorsAPIClient.get_oauth_access_token( self.url_base, self.client_id, self.client_secret, self.company_id, self.user_id, self.user_type) assert actual_response == expected_response assert len(responses.calls) == 1 assert responses.calls[ 0].request.url == self.url_base + self.oauth_api_path
def has_access_token(self, obj): """ Confirms the presence and validity of the access token for the SAP SuccessFactors client instance Returns: a bool value indicating the presence of the access token Args: obj: The instance of SAPSuccessFactorsEnterpriseCustomerConfiguration being rendered with this admin form. """ try: access_token, expires_at = SAPSuccessFactorsAPIClient.get_oauth_access_token( obj.sapsf_base_url, obj.key, obj.secret, obj.sapsf_company_id, obj.sapsf_user_id, obj.user_type) except (RequestException, ValueError): return False return bool(access_token and expires_at)
def test_get_oauth_access_token(self): expected_response = ( self.access_token, datetime.datetime.utcfromtimestamp(self.expires_in + int(time.time()))) responses.add( # pylint: disable=no-member responses.POST, # pylint: disable=no-member self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) actual_response = SAPSuccessFactorsAPIClient.get_oauth_access_token( self.url_base, self.client_id, self.client_secret, self.company_id, self.user_id, self.user_type) assert actual_response == expected_response assert len(responses.calls) == 1 # pylint: disable=no-member assert responses.calls[ 0].request.url == self.url_base + self.oauth_api_path # pylint: disable=no-member