'Verify the method returns a hash of a user\'s current enrollments.'

def test_generate_enrollment_status_hash(self):

    self.assertIsNone(CourseEnrollment.generate_enrollment_status_hash(AnonymousUser()))
    expected = hashlib.md5(self.user.username).hexdigest()
    self.assertEqual(CourseEnrollment.generate_enrollment_status_hash(self.user), expected)
    self.assert_enrollment_status_hash_cached(self.user, expected)
    enrollment_mode = 'verified'
    course_id = self.course.id
    enrollment = CourseEnrollmentFactory.create(user=self.user, course_id=course_id, mode=enrollment_mode, is_active=False)
    self.assertEqual(CourseEnrollment.generate_enrollment_status_hash(self.user), expected)
    self.assert_enrollment_status_hash_cached(self.user, expected)
    enrollment.is_active = True
    enrollment.save()
    expected = '{username}&{course_id}={mode}'.format(username=self.user.username, course_id=str(course_id).lower(), mode=enrollment_mode.lower())
    expected = hashlib.md5(expected).hexdigest()
    self.assertEqual(CourseEnrollment.generate_enrollment_status_hash(self.user), expected)
    self.assert_enrollment_status_hash_cached(self.user, expected)
    CourseEnrollmentFactory.create(user=self.user)
    enrollments = CourseEnrollment.enrollments_for_user(self.user).order_by(Lower('course_id'))
    hash_elements = [self.user.username]
    hash_elements += ['{course_id}={mode}'.format(course_id=str(enrollment.course_id).lower(), mode=enrollment.mode.lower()) for enrollment in enrollments]
    expected = hashlib.md5('&'.join(hash_elements)).hexdigest()
    self.assertEqual(CourseEnrollment.generate_enrollment_status_hash(self.user), expected)
    self.assert_enrollment_status_hash_cached(self.user, expected)
