def enterprise_selection_page(request, user, next_url): """ Updates redirect url to enterprise selection page if user is associated with multiple enterprises otherwise return the next url. param: next_url(string): The URL to redirect to after multiple enterprise selection or in case the selection page is bypassed e.g when dealing with direct enrolment urls. """ redirect_url = next_url response = get_enterprise_learner_data_from_api(user) if response and len(response) > 1: redirect_url = reverse( 'enterprise_select_active') + '/?success_url=' + next_url # Check to see if next url has an enterprise in it. In this case if user is associated with # that enterprise, activate that enterprise and bypass the selection page. if re.match(ENTERPRISE_ENROLLMENT_URL_REGEX, urllib.parse.unquote(next_url)): enterprise_in_url = re.search(UUID4_REGEX, next_url).group(0) for enterprise in response: if enterprise_in_url == str( enterprise['enterprise_customer']['uuid']): is_activated_successfully = activate_learner_enterprise( request, user, enterprise_in_url) if is_activated_successfully: redirect_url = next_url break return redirect_url
def test_get_enterprise_learner_data_from_api(self, mock_api_client_class): user = mock.Mock(is_authenticated=True) mock_client = mock_api_client_class.return_value mock_client.fetch_enterprise_learner_data.return_value = { 'results': 'the-learner-data', } learner_data = get_enterprise_learner_data_from_api(user) assert 'the-learner-data' == learner_data mock_api_client_class.assert_called_once_with(user=user) mock_client.fetch_enterprise_learner_data.assert_called_once_with(user)