<commit_before>"""
Test cases to cover CourseEnrollmentAllowed models (and its feature) with APPSEMBLER_MULTI_TENANT_EMAILS.
"""

import json
from mock import patch
from rest_framework import status
from xmodule.modulestore.tests.factories import CourseFactory

from django.urls import reverse
from django.contrib.auth.models import User
from student.models import CourseEnrollment, CourseEnrollmentAllowed
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from student.tests.factories import UserFactory

from .test_utils import with_organization_context, lms_multi_tenant_test
from openedx.core.djangoapps.appsembler.api.tests.factories import (
    CourseOverviewFactory,
    OrganizationCourseFactory,
    UserOrganizationMappingFactory,
)


@lms_multi_tenant_test
@patch.dict('django.conf.settings.FEATURES', {'SKIP_EMAIL_VALIDATION': True})
class TestCourseEnrollmentAllowedMultitenant(ModuleStoreTestCase):
    """
    Unit tests for the CourseEnrollmentAllowed model and related features.
    """

    RED = 'red1'
    BLUE = 'blue2'

    OMAR_EMAIL = '<EMAIL>'
    JOHN_EMAIL = '<EMAIL>'
    PASSWORD = '<PASSWORD>'

    def create_org_course(self, org):
        """
        Create course with organization link.
        """
        course = CourseFactory.create()
        overview = CourseOverviewFactory.create(id=course.id)
        OrganizationCourseFactory(organization=org, course_id=str(overview.id))
        return overview

    def invite(self, org, course_id, email):
        """
        Invite a user by email via the Tahoe Enrollment API endpoint.
        """
        client = self.client_class()
        caller = UserFactory.create(password=self.PASSWORD)
        UserOrganizationMappingFactory(user=caller,
                                       organization=org,
                                       is_amc_admin=True)
        client.login(username=caller.username, password=self.PASSWORD)
        url = reverse('tahoe-api:v1:enrollments-list')
        body = json.dumps({
            'action': 'enroll',
            'auto_enroll': True,
            'identifiers': [email],
            'email_learners': True,
            'courses': [str(course_id)],
        })
        response = client.post(url, data=body, content_type='application/json')
        assert 'error' not in response.content
        assert response.status_code == status.HTTP_201_CREATED, response.content
        return response

    def register_user(self, color, email, password):
        """
        Register a user via the registration form API endpoint.
        """
        url = reverse('user_api_registration')
        response = self.client.post(url, {
            'email': email,
            'name': 'Ali',
            'username': 'ali_{}'.format(color),
            'password': password,
            'honor_code': 'true',
        })
        assert response.status_code == status.HTTP_200_OK, response.content
        user = User.objects.get(email=email)
        return user<commit_msg>fix encoding issues in TestCourseEnrollmentAllowedMultitenant<commit_after>"""
Test cases to cover CourseEnrollmentAllowed models (and its feature) with APPSEMBLER_MULTI_TENANT_EMAILS.
"""

import json
from mock import patch
from rest_framework import status
from xmodule.modulestore.tests.factories import CourseFactory

from django.urls import reverse
from django.contrib.auth.models import User
from student.models import CourseEnrollment, CourseEnrollmentAllowed
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from student.tests.factories import UserFactory

from .test_utils import with_organization_context, lms_multi_tenant_test
from openedx.core.djangoapps.appsembler.api.tests.factories import (
    CourseOverviewFactory,
    OrganizationCourseFactory,
    UserOrganizationMappingFactory,
)


@lms_multi_tenant_test
@patch.dict('django.conf.settings.FEATURES', {'SKIP_EMAIL_VALIDATION': True})
class TestCourseEnrollmentAllowedMultitenant(ModuleStoreTestCase):
    """
    Unit tests for the CourseEnrollmentAllowed model and related features.
    """

    RED = 'red1'
    BLUE = 'blue2'

    OMAR_EMAIL = '<EMAIL>'
    JOHN_EMAIL = '<EMAIL>'
    PASSWORD = '<PASSWORD>'

    def create_org_course(self, org):
        """
        Create course with organization link.
        """
        course = CourseFactory.create()
        overview = CourseOverviewFactory.create(id=course.id)
        OrganizationCourseFactory(organization=org, course_id=str(overview.id))
        return overview

    def invite(self, org, course_id, email):
        """
        Invite a user by email via the Tahoe Enrollment API endpoint.
        """
        client = self.client_class()
        caller = UserFactory.create(password=<PASSWORD>)
        UserOrganizationMappingFactory(user=caller,
                                       organization=org,
                                       is_amc_admin=True)
        client.login(username=caller.username, password=<PASSWORD>)
        url = reverse('tahoe-api:v1:enrollments-list')
        body = json.dumps({
            'action': 'enroll',
            'auto_enroll': True,
            'identifiers': [email],
            'email_learners': True,
            'courses': [str(course_id)],
        })
        response = client.post(url, data=body, content_type='application/json')
        content = response.content.decode('utf-8')
        assert 'error' not in content
        assert response.status_code == status.HTTP_201_CREATED, content
        return response

    def register_user(self, color, email, password):
        """
        Register a user via the registration form API endpoint.
        """
        url = reverse('user_api_registration')
        response = self.client.post(url, {
            'email': email,
            'name': 'Ali',
            'username': 'ali_{}'.format(color),
            'password': password,
            'honor_code': 'true',
        })
        content = response.content.decode('utf-8')
        assert response.status_code == status.HTTP_200_OK, content
        user = User.objects.get(email=email)
        return user
{'fraction_non_alphanumeric': 0.09544787077826726, 'fraction_numerical': 0.0032631750693424702, 'mean_word_length': 2.8650693568726355, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3321382', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1683, 'n_words': 385}