'Test a valid request from an authenticated user when configured to use Zendesk Custom Fields.
The response should have a 200 (success) status code, and a ticket with
the given information should have been submitted via the Zendesk API. When course_id is
present, it should be sent to Zendesk via a custom field, along with the enrollment mode
if the user has an active enrollment for that course. When course_id is blank or missing,
the request should still be processed successfully.'

@data(('course-v1:testOrg+testCourseNumber+testCourseRun', True), ('course-v1:testOrg+testCourseNumber+testCourseRun', False), ('', None), (None, None))
@unpack
@override_settings(ZENDESK_CUSTOM_FIELDS=TEST_ZENDESK_CUSTOM_FIELD_CONFIG)
def test_valid_request_auth_user_with_custom_fields(self, course_id, enrolled, zendesk_mock_class, datadog_mock):

    zendesk_mock_instance = zendesk_mock_class.return_value
    user = self._auth_user
    fields = self._auth_fields.copy()
    if (course_id is not None):
        fields['course_id'] = course_id
    ticket_id = 42
    zendesk_mock_instance.create_ticket.return_value = ticket_id
    zendesk_tags = ['LMS']
    datadog_tags = []
    zendesk_custom_fields = None
    if course_id:
        zendesk_tags.insert(0, course_id)
        datadog_tags.insert(0, 'course_id:{}'.format(course_id))
        zendesk_custom_fields = [{'id': TEST_ZENDESK_CUSTOM_FIELD_CONFIG['course_id'], 'value': course_id}]
        if (enrolled is not None):
            enrollment = CourseEnrollmentFactory.create(user=user, course_id=course_id, is_active=enrolled)
            if enrollment.is_active:
                zendesk_custom_fields.append({'id': TEST_ZENDESK_CUSTOM_FIELD_CONFIG['enrollment_mode'], 'value': enrollment.mode})
    ticket = self._build_zendesk_ticket(recipient=TEST_SUPPORT_EMAIL, name=user.profile.name, email=user.email, subject=fields['subject'], details=fields['details'], tags=zendesk_tags, custom_fields=zendesk_custom_fields)
    ticket_update = self._build_zendesk_ticket_update(TEST_REQUEST_HEADERS, user.username)
    self._test_success(user, fields)
    self._assert_zendesk_called(zendesk_mock_instance, ticket_id, ticket, ticket_update)
    self._assert_datadog_called(datadog_mock, datadog_tags)
