def test_has_access_to_application(user_with_profile, adg_admin, user_type, expected_result, super_user): """ Tests `has_access_to_application` returns expected result for different types of users. """ bu = BusinessLineFactory() application = UserApplicationFactory() user = user_with_profile if user_type == 'bu_admin': application.business_line = bu application.save() group = GroupFactory(name=bu.title) user_with_profile.groups.add(group) elif user_type == 'other_bu_admin': bu2 = BusinessLineFactory() group = GroupFactory(name=bu2.title) user_with_profile.groups.add(group) elif user_type == 'adg_admin': user = adg_admin elif user_type == 'superuser': user = super_user assert has_access_to_application(user, application) == expected_result
def test_application_listing_view_get_queryset(search_query, omni_status, bu_status, business_unit, adg_admin): """ Test that the filtering of the applications works correctly in the `get_quertset` method of the listing view """ for _, application_type in UserApplication.STATUS_CHOICES: expected_applications = [] if search_query: user_with_queried_name = UserFactory() UserProfileFactory(user=user_with_queried_name, name=search_query) application_with_test_user = UserApplicationFactory( user=user_with_queried_name, status=application_type) ApplicationHubFactory(user=application_with_test_user.user, is_written_application_completed=True) expected_applications.append(application_with_test_user) if omni_status: application_with_omni_status = UserApplicationFactory( status=application_type) ApplicationHubFactory( user=application_with_omni_status.user, program_prereq_courses_status=int(omni_status[0]), is_written_application_completed=True, ) expected_applications.append(application_with_omni_status) if bu_status: application_with_bu_status = UserApplicationFactory( status=application_type) ApplicationHubFactory( user=application_with_bu_status.user, bu_prereq_courses_status=int(bu_status[0]), is_written_application_completed=True, ) expected_applications.append(application_with_bu_status) if business_unit: query_business_unit = BusinessLineFactory(title=business_unit[0]) application_with_query_bu = UserApplicationFactory( status=application_type, business_line=query_business_unit) ApplicationHubFactory(user=application_with_query_bu.user, is_written_application_completed=True) expected_applications = [application_with_query_bu] listing_view = ApplicationListingView( application_type=application_type, query=search_query, filter_by_business_line=DEFAULT_BUSINESS_LINE_FILTER, omni_courses_query_values=omni_status, bu_courses_query_values=bu_status, business_line_query=business_unit, is_user_adg_admin=is_superuser_or_adg_admin(adg_admin)) assert list(listing_view.get_queryset()) == list(expected_applications)
def test_webinar_invite_emails_for_subsequent_publish_for_invite_cohort_field( initial_invited_cohort, subsequent_invited_cohort, expected_cohort, business_line, mocker, webinar ): # pylint: disable=protected-access """ Test that the webinar invitation emails are sent correctly for all the recipients related to `invited_user_cohort` field when a webinar is published after the first time """ mocker.patch('openedx.adg.lms.webinars.models.send_webinar_update_emails') mock_zoom_client = mocker.patch.object(ZoomClient, 'create_webinar') mocker.patch.object(ZoomClient, 'add_panelists') mock_send_webinar_emails = mocker.patch('openedx.adg.lms.webinars.helpers.send_webinar_emails') mock_zoom_client.return_value = { 'join_url': webinar.meeting_link, 'id': webinar.id, } user_a = UserFactory() UserApplicationFactory(business_line=BusinessLineFactory(title=BUSINESS_LINE), user=user_a) user_b = UserFactory() UserApplicationFactory(business_line=business_line, user=user_b) webinar._loaded_values = { 'title': webinar.title, 'description': webinar.description, 'start_time': webinar.start_time, 'is_published': True, 'invited_user_cohort': initial_invited_cohort, 'alternative_hosts': '', 'panelists': '', 'invites_by_email_address': '' } webinar.invited_user_cohort = subsequent_invited_cohort webinar.is_published = False webinar.publish() all_user_emails = list(User.objects.exclude(email='').values_list('email', flat=True)) selected_bu_emails = list(BusinessLine.user_emails_for_business_line(BUSINESS_LINE)) expected_emails = { NO_EMAILS: [], INVITE_ALL_USERS: all_user_emails, BUSINESS_LINE: selected_bu_emails, ALL_USERS_EXCEPT_BUSINESS_LINE_USERS: list(set(all_user_emails).difference(set(selected_bu_emails))) } if expected_cohort == NO_EMAILS: mock_send_webinar_emails.assert_not_called() else: template, _, actual_email_addresses = mock_send_webinar_emails.call_args.args assert template == MandrillClient.WEBINAR_CREATED assert sorted(expected_emails[expected_cohort]) == sorted(actual_email_addresses)
def test_webinar_invite_emails_for_initial_publish( alternative_hosts, panelists, invites_by_email_address, invited_user_cohort, expected, business_line, mocker ): """ Test that the webinar invitation emails are sent correctly to all the correct recipients when a webinar is published for the first time """ mocker.patch( 'openedx.adg.lms.webinars.models.Webinar.get_platform_and_non_platform_team_members', return_value=(set(extract_emails_from_string(alternative_hosts) + extract_emails_from_string(panelists)), set()) ) mocker.patch('openedx.adg.lms.webinars.models.send_webinar_update_emails') mock_zoom_client = mocker.patch.object(ZoomClient, 'create_webinar') mock_send_webinar_emails = mocker.patch('openedx.adg.lms.webinars.helpers.send_webinar_emails') user_a = UserFactory(email='*****@*****.**') UserApplicationFactory(business_line=BusinessLineFactory(title=BUSINESS_LINE), user=user_a) user_b = UserFactory(email='*****@*****.**') UserApplicationFactory(business_line=business_line, user=user_b) webinar = WebinarFactory( alternative_hosts=alternative_hosts, panelists=panelists, invites_by_email_address=invites_by_email_address, invited_user_cohort=invited_user_cohort, is_published=False ) mock_zoom_client.return_value = { 'join_url': webinar.meeting_link, 'id': webinar.id, } webinar.publish() expected_emails = set(expected) if invited_user_cohort == INVITE_ALL_USERS: expected_emails = expected_emails.union(set(User.objects.exclude(email='').values_list('email', flat=True))) else: expected_emails = expected_emails.union(set(BusinessLine.user_emails_for_business_line(invited_user_cohort))) if expected or invited_user_cohort: template, _, actual_email_addresses = mock_send_webinar_emails.call_args.args assert template == MandrillClient.WEBINAR_CREATED assert sorted(list(expected_emails)) == sorted(actual_email_addresses) else: mock_send_webinar_emails.assert_not_called()
def test_send_user_info_to_mailchimp_application_fields( mocker, mailchimp_handlers): """ Assert that `send_user_info_to_mailchimp` called with appropriate params and sync with mailchimp task is called specific number of times when application is created, `update_fields` contains UserApplication field which is required at mailchimp and is not called when `update_fields` is empty. """ mock_task = mocker.patch.object(mailchimp_handlers, 'task_send_user_info_to_mailchimp') user_application = UserApplicationFactory() user_email = user_application.user.email base_user_json = { 'email_address': user_email, 'status_if_new': 'subscribed', } user_json = { **base_user_json, 'merge_fields': get_userapplication_merge_fields(user_application) } user_application.organization = 'dummy org' user_application.save(update_fields=('organization', )) user_app_updated_json = { **base_user_json, 'merge_fields': get_userapplication_merge_fields(user_application) } user_application.organization = 'dummy org2' user_application.save() assert mock_task.delay.call_count == 4 mock_task.delay.assert_any_call(user_email, user_json) mock_task.delay.assert_any_call(user_email, user_app_updated_json)
def test_user_is_qualified_for_bu_prereq_courses(user_client): """ Tests `is_user_qualified_for_bu_prereq_courses` returns `True` for user with application and application_hub and has passed program prerequisites. """ user, _ = user_client UserApplicationFactory(user=user) ApplicationHubFactory( user=user, program_prereq_courses_status=ApplicationHub.COMPLETED) assert is_user_qualified_for_bu_prereq_courses(user)
def submitted_accepted_applications(): """ Fixtures that returns 3 accepted applications that have their written applications submitted """ accepted_applications = UserApplicationFactory.create_batch( 3, status=UserApplication.ACCEPTED) for application in accepted_applications: ApplicationHub(user=application.user ).submit_written_application_for_current_date() return accepted_applications
def submitted_waitlisted_applications(): """ Fixtures that returns 4 waitlisted applications that have their written applications submitted """ waitlisted_applications = UserApplicationFactory.create_batch( 4, status=UserApplication.WAITLIST) for application in waitlisted_applications: ApplicationHub(user=application.user ).submit_written_application_for_current_date() return waitlisted_applications
def submitted_open_applications(): """ Fixtures that returns 5 open applications that have their written applications submitted """ open_applications = UserApplicationFactory.create_batch( 5, status=UserApplication.OPEN) for application in open_applications: ApplicationHub(user=application.user ).submit_written_application_for_current_date() return open_applications
def create_applications(number_of_applications, target_date): """ Creates applications equal to the `number_of_applications` with `submission_date` set to `target_date` sent as argument. Arguments: number_of_applications (int): Depicts the number of applications to be created target_date (datetime): Represents the date for application submission """ user_applications = UserApplicationFactory.create_batch(number_of_applications) users = factory.Iterator([users_application.user for users_application in user_applications]) ApplicationHubFactory.create_batch(number_of_applications, user=users, submission_date=target_date)
def test_get_invitee_recipients_for_invitation_emails( old_invites_by_email, new_invites_by_email, old_cohort, new_cohort, cohort_users, is_already_published, registered_emails, expected_emails, webinar ): """ Tests that `get_invitee_recipients_for_invitation_emails` returns expected invitee emails. """ old_webinar_data = { 'title': webinar.title, 'description': webinar.description, 'start_time': webinar.start_time, 'is_published': is_already_published, 'panelists': '', 'alternative_hosts': '', 'invites_by_email_address': old_invites_by_email, 'invited_user_cohort': old_cohort } webinar._loaded_values = old_webinar_data # pylint: disable=protected-access webinar.invites_by_email_address = new_invites_by_email webinar.invited_user_cohort = new_cohort webinar.save() BusinessLineFactory(title=old_cohort) business_line = BusinessLineFactory(title=new_cohort) if new_cohort != INVITE_ALL_USERS: for email in cohort_users: user = UserFactory(email=email) UserApplicationFactory(business_line=business_line, user=user) else: for email in cohort_users: UserFactory(email=email) for email in registered_emails: WebinarRegistrationFactory(email=email, webinar=webinar) actual_team_emails = webinar.get_invitee_recipients_for_invitation_emails(False) if new_cohort == INVITE_ALL_USERS: expected_emails.append(webinar.created_by.email) assert sorted(list(actual_team_emails)) == sorted(expected_emails)
def test_get_user_emails_of_invited_cohort(cohort, business_line): """ Tests that `get_user_emails_of_invited_cohort` returns expected emails for a given cohort. """ users = UserFactory.create_batch(5) if cohort == 'business_line': cohort = business_line.title for user in users: UserApplicationFactory(business_line=business_line, user=user) actual_user_emails = Webinar.get_user_emails_of_invited_cohort(cohort) expected_user_emails = [user.email for user in users] assert sorted(expected_user_emails) == sorted(actual_user_emails)
def test_get_submitted_applications(is_superuser, group_name, expected_count): """ Test `get_submitted_applications` function for different types of admins. """ user_1 = UserFactory() user_2 = UserFactory() business_line_1 = BusinessLineFactory(title=BUSINESS_LINE_TEST_NAME_1) business_line_2 = BusinessLineFactory(title=BUSINESS_LINE_TEST_NAME_2) ApplicationHubFactory(user=user_1, is_written_application_completed=True) ApplicationHubFactory(user=user_2, is_written_application_completed=True) UserApplicationFactory(user=user_1, business_line=business_line_1) UserApplicationFactory(user=user_2, business_line=business_line_2) if is_superuser: admin_user = UserFactory(is_staff=True, is_superuser=True) else: admin_user = UserFactory(is_staff=True, groups=[GroupFactory(name=group_name)]) submitted_applications = get_submitted_applications(admin_user) assert submitted_applications.count() == expected_count
def test_get_business_line_prereq_courses(user_client, courses): """ Tests `get_business_line_prereq_courses` returns valid courses for a user. """ user, _ = user_client UserApplicationFactory(user=user) ApplicationHubFactory( user=user, program_prereq_courses_status=ApplicationHub.COMPLETED) MultilingualCourseFactory( course=courses['test_course1'], multilingual_course_group=(MultilingualCourseGroupFactory( is_common_business_line_prerequisite=True))) courses_list = get_business_line_prereq_courses(user) assert courses_list == [courses['test_course1']]
def test_get_courses_from_course_groups(user_client, courses): """ Tests `get_courses_from_course_groups` returns a valid list of courses. """ user, _ = user_client UserApplicationFactory(user=user) ApplicationHubFactory(user=user) course_group = MultilingualCourseGroupFactory( is_common_business_line_prerequisite=True) MultilingualCourseFactory(course=courses['test_course1'], multilingual_course_group=course_group) catalog_courses = get_courses_from_course_groups([course_group], user) assert catalog_courses == [courses['test_course1']]
def test_get_application_hub_instructions(written_application, omni_courses, bu_courses, omni_courses_started, bu_courses_started, congrats, msg, inst, is_application_waitlisted, is_application_accepted, user): """ Test the congratulation messages and instructions for each requirement completion till application submission. """ application_hub = ApplicationHubFactory(user=user) user_application = UserApplicationFactory(user=user) if written_application: application_hub.submit_written_application_for_current_date() if omni_courses: application_hub.set_program_prereq_courses_status( ApplicationHub.COMPLETED) if bu_courses: application_hub.set_bu_prereq_courses_status(ApplicationHub.COMPLETED) if is_application_waitlisted: user_application.set_status(UserApplication.WAITLIST) elif is_application_accepted: user_application.set_status(UserApplication.ACCEPTED) congrats = congrats.format(username=user.username) actual_messages = get_application_hub_instructions(application_hub, omni_courses_started, bu_courses_started) expected_messages = { 'congrats': congrats, 'message': msg, 'instruction': inst } assert actual_messages == expected_messages
def user_application(request): """ Create user application, this fixture can be passed as a parameter to other pytests or fixtures """ return UserApplicationFactory(user=UserFactory())