helpers.py 4.8 KB
Newer Older
1 2 3 4
import json

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
5
from django.test.client import RequestFactory
6 7 8 9 10 11 12

from student.models import Registration

from django.test import TestCase


def check_for_get_code(self, code, url):
13 14 15
    """
    Check that we got the expected code when accessing url via GET.
    Returns the HTTP response.
16

17
    `self` is a class that subclasses TestCase.
18

19
    `code` is a status code for HTTP responses.
20

21 22 23 24 25 26 27
    `url` is a url pattern for which we have to test the response.
    """
    resp = self.client.get(url)
    self.assertEqual(resp.status_code, code,
                     "got code %d for url '%s'. Expected code %d"
                     % (resp.status_code, url, code))
    return resp
28 29 30


def check_for_post_code(self, code, url, data={}):
31 32 33 34
    """
    Check that we got the expected code when accessing url via POST.
    Returns the HTTP response.
    `self` is a class that subclasses TestCase.
35

36
    `code` is a status code for HTTP responses.
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    `url` is a url pattern for which we want to test the response.
    """
    resp = self.client.post(url, data)
    self.assertEqual(resp.status_code, code,
                     "got code %d for url '%s'. Expected code %d"
                     % (resp.status_code, url, code))
    return resp


def get_request_for_user(user):
    """Create a request object for user."""

    request = RequestFactory()
    request.user = user
    request.META = {}
    request.is_secure = lambda: True
    request.get_host = lambda: "edx.org"
    return request
56 57 58


class LoginEnrollmentTestCase(TestCase):
59 60 61 62
    """
    Provides support for user creation,
    activation, login, and course enrollment.
    """
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    def setup_user(self):
        """
        Create a user account, activate, and log in.
        """
        self.email = 'foo@test.com'
        self.password = 'bar'
        self.username = 'test'
        self.create_account(self.username,
                            self.email, self.password)
        self.activate_user(self.email)
        self.login(self.email, self.password)

    # ============ User creation and login ==============

    def login(self, email, password):
        """
        Login, check that the corresponding view's response has a 200 status code.
        """
81 82
        resp = self.client.post(reverse('login'),
                                {'email': email, 'password': password})
83 84 85 86 87 88
        self.assertEqual(resp.status_code, 200)
        data = json.loads(resp.content)
        self.assertTrue(data['success'])

    def logout(self):
        """
89 90
        Logout; check that the HTTP response code indicates redirection
        as expected.
91 92
        """
        # should redirect
93
        check_for_get_code(self, 302, reverse('logout'))
94 95 96 97 98

    def create_account(self, username, email, password):
        """
        Create the account and check that it worked.
        """
99
        resp = check_for_post_code(self, 200, reverse('create_account'), {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            'username': username,
            'email': email,
            'password': password,
            'name': 'username',
            'terms_of_service': 'true',
            'honor_code': 'true',
        })
        data = json.loads(resp.content)
        self.assertEqual(data['success'], True)
        # Check both that the user is created, and inactive
        self.assertFalse(User.objects.get(email=email).is_active)

    def activate_user(self, email):
        """
        Look up the activation key for the user, then hit the activate view.
        No error checking.
        """
        activation_key = Registration.objects.get(user__email=email).activation_key
        # and now we try to activate
119
        check_for_get_code(self, 200, reverse('activate', kwargs={'key': activation_key}))
120 121 122 123 124 125
        # Now make sure that the user is now actually activated
        self.assertTrue(User.objects.get(email=email).is_active)

    def enroll(self, course, verify=False):
        """
        Try to enroll and return boolean indicating result.
126
        `course` is an instance of CourseDescriptor.
127
        `verify` is an optional boolean parameter specifying whether we
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        want to verify that the student was successfully enrolled
        in the course.
        """
        resp = self.client.post(reverse('change_enrollment'), {
            'enrollment_action': 'enroll',
            'course_id': course.id,
        })
        result = resp.status_code == 200
        if verify:
            self.assertTrue(result)
        return result

    def unenroll(self, course):
        """
        Unenroll the currently logged-in user, and check that it worked.
143
        `course` is an instance of CourseDescriptor.
144
        """
145 146
        check_for_post_code(self, 200, reverse('change_enrollment'), {'enrollment_action': 'unenroll',
                                                                      'course_id': course.id})