async def test_is_new_course_initiates_rolling_update( setup_course_environ, setup_course_hook_environ): """ If the course is a new setup does it initiate a rolling update? """ local_authenticator = Authenticator(post_auth_hook=setup_course_hook) local_handler = mock_handler(RequestHandler, authenticator=local_authenticator) local_authentication = factory_auth_state_dict() response_args = { 'handler': local_handler.request, 'body': { 'is_new_setup': True } } with patch.object( JupyterHubAPI, 'add_student_to_jupyterhub_group', return_value=None) as mock_add_student_to_jupyterhub_group: with patch.object(JupyterHubAPI, 'add_user_to_nbgrader_gradebook', return_value=None): with patch.object( AsyncHTTPClient, 'fetch', side_effect=[ factory_http_response(**response_args), factory_http_response(**response_args), None, ], # noqa: E231 ) as mock_client: await setup_course_hook(local_authenticator, local_handler, local_authentication) assert mock_client.called mock_client.assert_any_call( 'http://setup-course:8000/rolling-update', headers={'Content-Type': 'application/json'}, body='', method='POST', ) mock_client.assert_any_call( 'http://setup-course:8000', headers={'Content-Type': 'application/json'}, body= '{"org": "test-org", "course_id": "intro101", "domain": "127.0.0.1"}', method='POST', )
async def test_setup_course_hook_calls_normalize_strings( auth_state_dict, setup_course_environ, setup_course_hook_environ): """ Does the setup_course_hook return normalized strings for the username and the course_id? """ local_authenticator = Authenticator(post_auth_hook=setup_course_hook) local_handler = mock_handler(RequestHandler, authenticator=local_authenticator) local_authentication = auth_state_dict with patch.object(LTIUtils, 'normalize_string', return_value='intro101') as mock_normalize_string: with patch.object(JupyterHubAPI, 'add_student_to_jupyterhub_group', return_value=None): with patch.object(JupyterHubAPI, 'add_user_to_nbgrader_gradebook', return_value=None): with patch.object(AsyncHTTPClient, 'fetch', return_value=factory_http_response( handler=local_handler.request)): _ = await setup_course_hook(local_authenticator, local_handler, local_authentication) assert mock_normalize_string.called
async def test_setup_course_hook_initialize_data_dict( setup_course_environ, setup_course_hook_environ): """ Is the data dictionary correctly initialized when properly setting the org env-var and and consistent with the course id value in the auth state? """ local_authenticator = Authenticator(post_auth_hook=setup_course_hook) local_handler = mock_handler(RequestHandler, authenticator=local_authenticator) local_authentication = factory_auth_state_dict() expected_data = { 'org': 'test-org', 'course_id': 'intro101', 'domain': '127.0.0.1', } with patch.object(JupyterHubAPI, 'add_student_to_jupyterhub_group', return_value=None): with patch.object(JupyterHubAPI, 'add_user_to_nbgrader_gradebook', return_value=None): with patch.object(AsyncHTTPClient, 'fetch', return_value=factory_http_response( handler=local_handler.request)): result = await setup_course_hook(local_authenticator, local_handler, local_authentication) assert expected_data['course_id'] == result['auth_state'][ 'course_id'] assert expected_data['org'] == os.environ.get( 'ORGANIZATION_NAME') assert expected_data['domain'] == local_handler.request.host
async def test_setup_course_hook_does_not_call_add_instructor_to_jupyterhub_group_when_role_is_learner( setup_course_environ, setup_course_hook_environ): """ Is the jupyterhub_api add instructor to jupyterhub group function not called when the user role is the learner role? """ local_authenticator = Authenticator(post_auth_hook=setup_course_hook) local_handler = mock_handler(RequestHandler, authenticator=local_authenticator) local_authentication = factory_auth_state_dict() with patch.object(JupyterHubAPI, 'add_student_to_jupyterhub_group', return_value=None): with patch.object(JupyterHubAPI, 'add_user_to_nbgrader_gradebook', return_value=None): with patch.object(JupyterHubAPI, 'add_instructor_to_jupyterhub_group', return_value=None ) as mock_add_instructor_to_jupyterhub_group: with patch.object( AsyncHTTPClient, 'fetch', return_value=factory_http_response( handler=local_handler.request), ): await setup_course_hook(local_authenticator, local_handler, local_authentication) assert not mock_add_instructor_to_jupyterhub_group.called
async def test_sender_calls__set_access_token_header_before_to_send_grades( self, lti_config_environ): sut = LTI13GradeSender('course-id', 'lab', { 'course_lineitems': 'canvas.docker.com/api/lti/courses/1/line_items' }) local_handler = mock_handler(RequestHandler) access_token_result = {'token_type': '', 'access_token': ''} line_item_result = { 'label': 'lab', 'id': 'line_item_url', 'scoreMaximum': 40 } with patch('illumidesk.grades.senders.get_lms_access_token', return_value=access_token_result) as mock_method: with patch.object( LTI13GradeSender, '_retrieve_grades_from_db', return_value=(lambda: 10, [{ 'score': 10, 'lms_user_id': 'id' }]), ): with patch.object( AsyncHTTPClient, 'fetch', side_effect=[ factory_http_response( handler=local_handler.request, body=[line_item_result]), factory_http_response( handler=local_handler.request, body=line_item_result), factory_http_response( handler=local_handler.request, body=[]), ], ): await sut.send_grades() assert mock_method.called
def http_async_httpclient_with_simple_response(request): """ Creates a patch of AsyncHttpClient.fetch method, useful when other tests are making http request """ local_handler = mock_handler(RequestHandler) test_request_body_param = request.param if hasattr(request, 'param') else { 'message': 'ok' } with patch.object( AsyncHTTPClient, 'fetch', return_value=factory_http_response(handler=local_handler.request, body=test_request_body_param), ): yield AsyncHTTPClient()
async def test_setup_course_hook_calls_add_instructor_to_jupyterhub_group_when_role_is_instructor( monkeypatch, setup_course_environ, setup_course_hook_environ): """ Is the jupyterhub_api add instructor to jupyterhub group function called when the user role is the instructor role? """ local_authenticator = Authenticator(post_auth_hook=setup_course_hook) local_handler = mock_handler(RequestHandler, authenticator=local_authenticator) local_authentication = factory_auth_state_dict(user_role='Instructor') with patch.object( JupyterHubAPI, 'add_instructor_to_jupyterhub_group', return_value=None) as mock_add_instructor_to_jupyterhub_group: with patch.object(AsyncHTTPClient, 'fetch', return_value=factory_http_response( handler=local_handler.request)): await setup_course_hook(local_authenticator, local_handler, local_authentication) assert mock_add_instructor_to_jupyterhub_group.called
async def test_setup_course_hook_raises_json_decode_error_without_client_fetch_response( monkeypatch, setup_course_environ, setup_course_hook_environ): """ Does the setup course hook raise a json decode error if the response form the setup course microservice is null or empty? """ local_authenticator = Authenticator(post_auth_hook=setup_course_hook) local_handler = mock_handler(RequestHandler, authenticator=local_authenticator) local_authentication = factory_auth_state_dict() with patch.object( JupyterHubAPI, 'add_student_to_jupyterhub_group', return_value=None) as mock_add_student_to_jupyterhub_group: with patch.object(JupyterHubAPI, 'add_user_to_nbgrader_gradebook', return_value=None): with patch.object(AsyncHTTPClient, 'fetch', return_value=factory_http_response( handler=local_handler.request, body=None)): with pytest.raises(JSONDecodeError): await setup_course_hook(local_authenticator, local_handler, local_authentication)