'Tests whether the user has permission to create a thread with certain
group_id values.
If there is no group, user cannot create a thread.
Else if group is None or set, and the course is not cohorted and/or the
role is a student, user can create a thread.'

@ddt.data(*itertools.product([FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA, FORUM_ROLE_STUDENT], [True, False], [True, False], ['no_group_set', 'group_is_none', 'group_is_set']))
@ddt.unpack
def test_group_id(self, role_name, course_is_cohorted, topic_is_cohorted, data_group_state):

    cohort_course = CourseFactory.create(discussion_topics={'Test    Topic': {'id': 'test_topic'}}, cohort_config={'cohorted': course_is_cohorted, 'cohorted_discussions': (['test_topic'] if topic_is_cohorted else [])})
    CourseEnrollmentFactory.create(user=self.user, course_id=cohort_course.id)
    if course_is_cohorted:
        cohort = CohortFactory.create(course_id=cohort_course.id, users=[self.user])
    role = Role.objects.create(name=role_name, course_id=cohort_course.id)
    role.users = [self.user]
    self.register_post_thread_response({'username': self.user.username})
    data = self.minimal_data.copy()
    data['course_id'] = unicode(cohort_course.id)
    if (data_group_state == 'group_is_none'):
        data['group_id'] = None
    elif (data_group_state == 'group_is_set'):
        if course_is_cohorted:
            data['group_id'] = (cohort.id + 1)
        else:
            data['group_id'] = 1
    expected_error = ((data_group_state in ['group_is_none', 'group_is_set']) and ((not course_is_cohorted) or (role_name == FORUM_ROLE_STUDENT)))
    try:
        create_thread(self.request, data)
        self.assertFalse(expected_error)
        actual_post_data = httpretty.last_request().parsed_body
        if (data_group_state == 'group_is_set'):
            self.assertEqual(actual_post_data['group_id'], [str(data['group_id'])])
        elif ((data_group_state == 'no_group_set') and course_is_cohorted and topic_is_cohorted):
            self.assertEqual(actual_post_data['group_id'], [str(cohort.id)])
        else:
            self.assertNotIn('group_id', actual_post_data)
    except ValidationError as ex:
        if (not expected_error):
            self.fail('Unexpected    validation    error:    {}'.format(ex))
