course_helpers.py 2.43 KB
Newer Older
1
# pylint: disable=missing-docstring
2

3
import urllib
4

5
from django.contrib.auth.models import User
6 7
from lettuce import world

8
from student.models import CourseEnrollment
9
from xmodule.contentstore.django import _CONTENTSTORE
10
from xmodule.modulestore.django import clear_existing_modulestores, modulestore
11

12

13
@world.absorb
14
def create_user(uname, password):
15 16 17 18 19

    # If the user already exists, don't try to create it again
    if len(User.objects.filter(username=uname)) > 0:
        return

20
    portal_user = world.UserFactory.build(username=uname, email=uname + '@edx.org')
21
    portal_user.set_password(password)
22 23 24 25 26 27
    portal_user.save()

    registration = world.RegistrationFactory(user=portal_user)
    registration.register(portal_user)
    registration.activate()

28
    world.UserProfileFactory(user=portal_user)
29 30 31


@world.absorb
32
def log_in(username='robot', password='test', email='robot@edx.org', name="Robot"):
33
    """
34
    Use the auto_auth feature to programmatically log the user in
35
    """
36
    url = '/auto_auth'
37
    params = {'username': username, 'password': password, 'email': email, 'full_name': name}
38
    url += "?" + urllib.urlencode(params)
39
    world.visit(url)
40

41 42 43 44
    # Save the user info in the world scenario_dict for use in the tests
    user = User.objects.get(username=username)
    world.scenario_dict['USER'] = user

45 46

@world.absorb
47
def register_by_course_key(course_key, username='robot', password='test', is_staff=False):
48 49
    create_user(username, password)
    user = User.objects.get(username=username)
50 51
    # Note: this flag makes the user global staff - that is, an edX employee - not a course staff.
    # See courseware.tests.factories for StaffFactory and InstructorFactory.
52
    if is_staff:
53 54
        user.is_staff = True
        user.save()
55
    CourseEnrollment.enroll(user, course_key)
56

57

58
@world.absorb
59
def enroll_user(user, course_key):
60 61 62 63 64
    # Activate user
    registration = world.RegistrationFactory(user=user)
    registration.register(user)
    registration.activate()
    # Enroll them in the course
65
    CourseEnrollment.enroll(user, course_key)
66

67

68 69 70 71 72 73 74
@world.absorb
def clear_courses():
    # Flush and initialize the module store
    # Note that if your test module gets in some weird state
    # (though it shouldn't), do this manually
    # from the bash shell to drop it:
    # $ mongo test_xmodule --eval "db.dropDatabase()"
75 76 77
    modulestore()._drop_database()  # pylint: disable=protected-access
    _CONTENTSTORE.clear()
    clear_existing_modulestores()