utils.py 1.86 KB
Newer Older
1 2 3 4
'''
Utilities for contentstore tests
'''

5 6 7 8
import json

from student.models import Registration
from django.contrib.auth.models import User
David Baumgold committed
9
from django.test.client import Client
10
from django.test.utils import override_settings
David Baumgold committed
11 12 13

from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
14
from contentstore.tests.modulestore_config import TEST_MODULESTORE
15

Calen Pennington committed
16

17 18 19 20
def parse_json(response):
    """Parse response, which is assumed to be json"""
    return json.loads(response.content)

Calen Pennington committed
21

22 23 24 25
def user(email):
    """look up a user by email"""
    return User.objects.get(email=email)

Calen Pennington committed
26

27 28 29
def registration(email):
    """look up registration object by email"""
    return Registration.objects.get(user__email=email)
David Baumgold committed
30 31


32
@override_settings(MODULESTORE=TEST_MODULESTORE)
David Baumgold committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
class CourseTestCase(ModuleStoreTestCase):
    def setUp(self):
        """
        These tests need a user in the DB so that the django Test Client
        can log them in.
        They inherit from the ModuleStoreTestCase class so that the mongodb collection
        will be cleared out before each test case execution and deleted
        afterwards.
        """
        uname = 'testuser'
        email = 'test+courses@edx.org'
        password = 'foo'

        # Create the use so we can log them in.
        self.user = User.objects.create_user(uname, email, password)

        # Note that we do not actually need to do anything
        # for registration if we directly mark them active.
        self.user.is_active = True
        # Staff has access to view all courses
        self.user.is_staff = True
        self.user.save()

        self.client = Client()
        self.client.login(username=uname, password=password)

        self.course = CourseFactory.create(
            org='MITx',
            number='999',
            display_name='Robot Super Course',
        )