Skip to content
Snippets Groups Projects
Commit 403b6719 authored by Sofiya Semenova's avatar Sofiya Semenova
Browse files

Management command to create GDPR test user

parent 26ab5959
No related branches found
No related tags found
No related merge requests found
"""
Create a user with GDPR P1 PII for manual testing.
Enrolls the user in the DemoX course.
Optionally takes in username, email, and course UUID arguments.
"""
from __future__ import unicode_literals
from datetime import datetime
from uuid import uuid4
from pytz import UTC
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from consent.models import DataSharingConsent
from enterprise.models import (
EnterpriseCourseEnrollment,
EnterpriseCustomer,
EnterpriseCustomerUser,
PendingEnterpriseCustomerUser,
)
from entitlements.models import CourseEntitlement, CourseEntitlementSupportDetail
from integrated_channels.sap_success_factors.models import SapSuccessFactorsLearnerDataTransmissionAudit
from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification
from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.course_groups.models import UnregisteredLearnerCohortAssignments, CourseUserGroup
from openedx.core.djangoapps.profile_images.images import create_profile_images
from openedx.core.djangoapps.profile_images.tests.helpers import make_image_file
from student.models import (
CourseEnrollment,
PendingEmailChange,
UserProfile,
CourseEnrollmentAllowed
)
from ...models import UserOrgTag
class Command(BaseCommand):
"""
Implementation of the create_user_gdpr_testing command.
"""
def add_arguments(self, parser):
parser.add_argument(
'-u', '--username',
nargs=1,
required=False,
help='Username'
)
parser.add_argument(
'-e', '--email',
nargs=1,
required=False,
help='Email'
)
parser.add_argument(
'-c', '--course',
nargs=1,
required=False,
help='Course UUID'
)
def handle(self, *args, **options):
"""
Execute the command.
"""
username = options['username'] if options['username'] else 'gdpr_test_user'
email = options['email'] if options['email'] else 'gdpr_test_user@example.com'
course_uuid = options['course'] if options['course'] else uuid4().hex
user, __ = User.objects.get_or_create(
username=username,
)
user_info = {
'email': email,
'first_name': "GDPR",
'last_name': "Test",
'is_active': True,
'password': 'gdpr test password'
}
for field, value in user_info.items():
setattr(user, field, value)
user.save()
# UserProfile
profile_image_uploaded_date = datetime(2018, 5, 3, tzinfo=UTC)
user_profile, __ = UserProfile.objects.get_or_create(
user=user
)
user_profile_info = {
'name': 'gdpr test name',
'meta': 'gdpr test meta',
'location': 'gdpr test location',
'year_of_birth': 1950,
'gender': 'gdpr test gender',
'mailing_address': 'gdpr test mailing address',
'city': 'Boston',
'country': 'US',
'bio': 'gdpr test bio',
'profile_image_uploaded_at': profile_image_uploaded_date
}
for field, value in user_profile_info.items():
setattr(user_profile, field, value)
user_profile.save()
# Profile images
with make_image_file() as image_file:
create_profile_images(
image_file,
{10: "ten.jpg"}
)
# DataSharingConsent
enterprise_customer, __ = EnterpriseCustomer.objects.get_or_create( # pylint: disable=no-member
name='test gdpr enterprise customer',
active=True,
branding_configuration=None,
catalog=None,
enable_audit_enrollment=False,
enable_data_sharing_consent=False,
enforce_data_sharing_consent='at_enrollment',
replace_sensitive_sso_username=True,
site_id=1
)
DataSharingConsent.objects.get_or_create(
username=username,
enterprise_customer_id=enterprise_customer.uuid
)
# Sapsf data transmission
enterprise_customer_user, __ = EnterpriseCustomerUser.objects.get_or_create(
user_id=user.id,
enterprise_customer_id=enterprise_customer.uuid
)
audit, __ = EnterpriseCourseEnrollment.objects.get_or_create(
enterprise_customer_user=enterprise_customer_user
)
SapSuccessFactorsLearnerDataTransmissionAudit.objects.get_or_create(
enterprise_course_enrollment_id=audit.id,
completed_timestamp=10
)
# PendingEnterpriseCustomerUser
PendingEnterpriseCustomerUser.objects.get_or_create(
user_email=user.email,
enterprise_customer_id=enterprise_customer.uuid
)
# EntitlementSupportDetail
course_entitlement, __ = CourseEntitlement.objects.get_or_create(
user_id=user.id,
course_uuid=course_uuid
)
CourseEntitlementSupportDetail.objects.get_or_create(
support_user=user,
comments='test comments',
entitlement_id=course_entitlement.id
)
# Misc. models that may contain PII of this user
SoftwareSecurePhotoVerification.objects.get_or_create(
user=user,
name='gdpr test',
face_image_url='https://fake_image_url.com',
photo_id_image_url='gdpr_test',
photo_id_key='gdpr_test'
)
PendingEmailChange.objects.get_or_create(
user=user,
activation_key=uuid4().hex
)
UserOrgTag.objects.get_or_create(
user=user
)
course_id = CourseKey.from_string("course-v1:edX+DemoX+Demo_Course")
# Objects linked to the user via their original email
CourseEnrollmentAllowed.objects.get_or_create(
email=user.email
)
course_user_group, __ = CourseUserGroup.objects.get_or_create(
name='test course user group',
course_id=course_id
)
UnregisteredLearnerCohortAssignments.objects.get_or_create(
email=user.email,
course_user_group_id=course_user_group.id
)
# Enroll the user in a course
CourseEnrollment.objects.get_or_create(
course_id=course_id,
user_id=user.id,
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment