def test_expired_access_token(self): """ If our token expires after some call, make sure to get it again. Make a call, have the token expire after waiting some time (technically no time since time is frozen), and make a call again and notice 2 OAuth calls in total are required. """ expired_token_response_body = {"expires_in": 0, "access_token": self.access_token} responses.add( responses.POST, self.url_base + self.oauth_api_path, json=expired_token_response_body, status=200 ) expected_course_response_body = self.content_payload expected_course_response_body["@odata.context"] = "$metadata#OcnCourses/$entity" responses.add( responses.POST, self.url_base + self.course_api_path, json=expected_course_response_body, status=200 ) sap_client = SAPSuccessFactorsAPIClient(self.enterprise_config) sap_client.create_content_metadata(self.content_payload) sap_client.create_content_metadata(self.content_payload) assert len(responses.calls) == 4 assert responses.calls[0].request.url == self.url_base + self.oauth_api_path assert responses.calls[1].request.url == self.url_base + self.course_api_path assert responses.calls[2].request.url == self.url_base + self.oauth_api_path assert responses.calls[3].request.url == self.url_base + self.course_api_path
def test_expired_access_token(self): expired_token_response_body = { "expires_in": 0, "access_token": self.access_token } responses.add(responses.POST, self.url_base + self.oauth_api_path, json=expired_token_response_body, status=200) responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) expected_course_response_body = self.content_payload expected_course_response_body[ "@odata.context"] = "$metadata#OcnCourses/$entity" responses.add(responses.POST, self.url_base + self.course_api_path, json=expected_course_response_body, status=200) sap_client = SAPSuccessFactorsAPIClient(self.enterprise_config) sap_client.create_content_metadata(self.content_payload) assert len(responses.calls) == 3 assert responses.calls[ 0].request.url == self.url_base + self.oauth_api_path assert responses.calls[ 1].request.url == self.url_base + self.oauth_api_path assert responses.calls[ 2].request.url == self.url_base + self.course_api_path
def __init__(self, enterprise_configuration): """ The base init function that initializes a SAPSuccessFactorsAPIClient for subsequent calls. Args: enterprise_configuration (SAPSuccessFactorsEnterpriseCustomerConfiguration): An enterprise customers's configuration model for connecting with SAP SuccessFactors """ self.enterprise_configuration = enterprise_configuration self.client = SAPSuccessFactorsAPIClient(enterprise_configuration)
def test_expired_access_token(self): expired_token_response_body = { "expires_in": 0, "access_token": self.access_token } responses.add( # pylint: disable=no-member responses.POST, # pylint: disable=no-member self.url_base + self.oauth_api_path, json=expired_token_response_body, status=200) 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) payload = { "ocnCourses": [{ "courseID": "TED1", "providerID": "TED", "status": "ACTIVE", "title": [{ "locale": "English", "value": "Can a computer write poetry?" }] }] } expected_course_response_body = payload expected_course_response_body[ "@odata.context"] = "$metadata#OcnCourses/$entity" responses.add( # pylint: disable=no-member responses.POST, # pylint: disable=no-member self.url_base + self.course_api_path, json=expected_course_response_body, status=200) expected_response = 200, json.dumps(expected_course_response_body) sap_client = SAPSuccessFactorsAPIClient(self.enterprise_config) actual_response = sap_client.send_course_import(payload) assert actual_response == expected_response assert len(responses.calls) == 3 # pylint: disable=no-member assert responses.calls[ 0].request.url == self.url_base + self.oauth_api_path # pylint: disable=no-member assert responses.calls[ 1].request.url == self.url_base + self.oauth_api_path # pylint: disable=no-member assert responses.calls[ 2].request.url == self.url_base + self.course_api_path # pylint: disable=no-member
def test_send_completion_status(self): 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) payload = { "userID": "abc123", "courseID": "course-v1:ColumbiaX+DS101X+1T2016", "providerID": "EDX", "courseCompleted": "true", "completedTimestamp": 1485283526, "instructorName": "Professor Professorson", "grade": "Pass" } expected_response_body = { "success": "true", "completion_status": payload } responses.add( # pylint: disable=no-member responses.POST, # pylint: disable=no-member self.url_base + self.completion_status_api_path, json=expected_response_body, status=200) 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) expected_response = 200, json.dumps(expected_response_body) sap_client = SAPSuccessFactorsAPIClient(self.enterprise_config) actual_response = sap_client.send_completion_status( self.user_type, json.dumps(payload)) assert actual_response == expected_response assert len(responses.calls) == 3 # pylint: disable=no-member assert responses.calls[ 0].request.url == self.url_base + self.oauth_api_path # pylint: disable=no-member assert responses.calls[ 1].request.url == self.url_base + self.oauth_api_path # pylint: disable=no-member expected_url = self.url_base + self.completion_status_api_path assert responses.calls[2].request.url == expected_url # pylint: disable=no-member
def test_sap_api_connection_error(self): """ ``create_content_metadata`` should raise ClientError when API request fails with a connection error. """ responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) expected_course_response_body = self.content_payload expected_course_response_body[ "@odata.context"] = "$metadata#OcnCourses/$entity" responses.add(responses.POST, self.url_base + self.course_api_path, body=requests.exceptions.RequestException()) with raises(ClientError): sap_client = SAPSuccessFactorsAPIClient(self.enterprise_config) sap_client.create_content_metadata(self.content_payload)
def test_content_import(self, client_method): responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) expected_course_response_body = self.content_payload expected_course_response_body[ "@odata.context"] = "$metadata#OcnCourses/$entity" responses.add(responses.POST, self.url_base + self.course_api_path, json=expected_course_response_body, status=200) sap_client = SAPSuccessFactorsAPIClient(self.enterprise_config) getattr(sap_client, client_method)(self.content_payload) assert len(responses.calls) == 2 assert responses.calls[ 0].request.url == self.url_base + self.oauth_api_path assert responses.calls[ 1].request.url == self.url_base + self.course_api_path
def test_init_no_config(self): with raises(ValueError): SAPSuccessFactorsAPIClient(None)