Files
edx-platform/openedx/tests/completion_integration/test_views.py
Feanil Patel 9cf2f9f298 Run 2to3 -f future . -w
This will remove imports from __future__ that are no longer needed.

https://docs.python.org/3.5/library/2to3.html#2to3fixer-future
2019-12-30 10:35:30 -05:00

273 lines
9.3 KiB
Python

# -*- coding: utf-8 -*-
"""
Test models, managers, and validators.
"""
import ddt
from completion import waffle
from completion.test_utils import CompletionWaffleTestMixin
from django.urls import reverse
from rest_framework.test import APIClient
import six
from openedx.core.djangolib.testing.utils import skip_unless_lms
from student.tests.factories import CourseEnrollmentFactory, UserFactory
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
@ddt.ddt
@skip_unless_lms
class CompletionBatchTestCase(CompletionWaffleTestMixin, ModuleStoreTestCase):
"""
Test that BlockCompletion.objects.submit_batch_completion has the desired
semantics.
"""
ENROLLED_USERNAME = 'test_user'
UNENROLLED_USERNAME = 'unenrolled_user'
COURSE_KEY = 'course-v1:TestX+101+Test'
BLOCK_KEY = 'block-v1:TestX+101+Test+type@problem+block@Test_Problem'
# And for old mongo:
COURSE_KEY_DEPRECATED = 'TestX/201/Test'
BLOCK_KEY_DEPRECATED = 'i4x://TestX/201/problem/Test_Problem'
def setUp(self):
"""
Create the test data.
"""
super(CompletionBatchTestCase, self).setUp()
self.url = reverse('completion:v1:completion-batch')
# Enable the waffle flag for all tests
self.override_waffle_switch(True)
# Create course
self.course = CourseFactory.create(
org='TestX', number='101', display_name='Test',
default_store=ModuleStoreEnum.Type.split,
)
self.assertEqual(six.text_type(self.course.id), self.COURSE_KEY)
self.problem = ItemFactory.create(
parent=self.course, category="problem", display_name="Test Problem", publish_item=False,
)
self.assertEqual(six.text_type(self.problem.location), self.BLOCK_KEY)
# And an old mongo course:
self.course_deprecated = CourseFactory.create(
org='TestX', number='201', display_name='Test',
default_store=ModuleStoreEnum.Type.mongo,
)
self.assertEqual(six.text_type(self.course_deprecated.id), self.COURSE_KEY_DEPRECATED)
self.problem_deprecated = ItemFactory.create(
parent=self.course_deprecated, category="problem", display_name="Test Problem",
)
self.assertEqual(six.text_type(self.problem_deprecated.location), self.BLOCK_KEY_DEPRECATED)
# Create users
self.staff_user = UserFactory(is_staff=True)
self.enrolled_user = UserFactory(username=self.ENROLLED_USERNAME)
self.unenrolled_user = UserFactory(username=self.UNENROLLED_USERNAME)
# Enrol one user in the course
CourseEnrollmentFactory.create(user=self.enrolled_user, course_id=self.course.id)
CourseEnrollmentFactory.create(user=self.enrolled_user, course_id=self.course_deprecated.id)
# Login the enrolled user by for all tests
self.client = APIClient()
self.client.force_authenticate(user=self.enrolled_user)
def test_enable_completion_tracking(self):
"""
Test response when the waffle switch is disabled (default).
"""
with waffle.waffle().override(waffle.ENABLE_COMPLETION_TRACKING, False):
response = self.client.post(self.url, {'username': self.ENROLLED_USERNAME}, format='json')
self.assertEqual(response.data, {
"detail":
"BlockCompletion.objects.submit_batch_completion should not be called when the feature is disabled."
})
self.assertEqual(response.status_code, 400)
@ddt.data(
# Valid submission
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 200, {'detail': 'ok'}
),
# Valid submission (old mongo)
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY_DEPRECATED,
'blocks': {
BLOCK_KEY_DEPRECATED: 1.0,
}
}, 200, {'detail': 'ok'}
),
# Blocks list can be empty, though it's a no-op
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': [],
}, 200, {"detail": "ok"}
),
# Course must be a valid key
(
{
'username': ENROLLED_USERNAME,
'course_key': "not:a:course:key",
'blocks': {
BLOCK_KEY: 1.0,
}
}, 400, {"detail": "Invalid learning context key: not:a:course:key"}
),
# Block must be a valid key
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': {
'not:a:block:key': 1.0,
}
}, 400, {"detail": "Invalid block key: not:a:block:key"}
),
# Block not in course
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': {
'block-v1:TestX+101+OtherCourse+type@problem+block@other': 1.0,
}
},
400,
{
"detail": (
u"Block with key: 'block-v1:TestX+101+OtherCourse+type@problem+block@other' "
u"is not in context {}".format(COURSE_KEY)
)
}
),
# Course key is required
(
{
'username': ENROLLED_USERNAME,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 400, {"detail": "Key 'course_key' not found."}
),
# Blocks is required
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY,
}, 400, {"detail": "Key 'blocks' not found."}
),
# Ordinary users can only update their own completions
(
{
'username': UNENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 403, {"detail": "You do not have permission to perform this action."}
),
# Username is required
(
{
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 403, {"detail": 'You do not have permission to perform this action.'}
),
# Course does not exist
(
{
'username': ENROLLED_USERNAME,
'course_key': 'course-v1:TestX+101+Test2',
'blocks': {
BLOCK_KEY: 1.0,
}
}, 400, {"detail": "User is not enrolled in course."}
),
)
@ddt.unpack
def test_batch_submit(self, payload, expected_status, expected_data):
"""
Test the batch submission response for student users.
"""
response = self.client.post(self.url, payload, format='json')
self.assertEqual(response.data, expected_data)
self.assertEqual(response.status_code, expected_status)
@ddt.data(
# Staff can submit completion on behalf of other users
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 200, {'detail': 'ok'}
),
# Staff can submit completion on behalf of other users (old mongo)
(
{
'username': ENROLLED_USERNAME,
'course_key': COURSE_KEY_DEPRECATED,
'blocks': {
BLOCK_KEY_DEPRECATED: 1.0,
}
}, 200, {'detail': 'ok'}
),
# User must be enrolled in the course
(
{
'username': UNENROLLED_USERNAME,
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 400, {"detail": "User is not enrolled in course."}
),
# Username is required
(
{
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 400, {"detail": "Key 'username' not found."}
),
# User must not exist
(
{
'username': 'doesntexist',
'course_key': COURSE_KEY,
'blocks': {
BLOCK_KEY: 1.0,
}
}, 404, {"detail": 'User matching query does not exist.'}
),
)
@ddt.unpack
def test_batch_submit_staff(self, payload, expected_status, expected_data):
"""
Test the batch submission response when logged in as a staff user.
"""
self.client.force_authenticate(user=self.staff_user)
response = self.client.post(self.url, payload, format='json')
self.assertEqual(response.data, expected_data)
self.assertEqual(response.status_code, expected_status)