Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
a47f86e5
Commit
a47f86e5
authored
Sep 21, 2015
by
Nimisha Asthagiri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixup! checkpoint course_api test_forms
parent
d33e2e60
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
174 additions
and
45 deletions
+174
-45
lms/djangoapps/course_api/blocks/forms.py
+3
-3
lms/djangoapps/course_api/blocks/serializers.py
+1
-1
lms/djangoapps/course_api/blocks/tests/test_block_api.py
+0
-9
lms/djangoapps/course_api/blocks/tests/test_forms.py
+137
-0
lms/djangoapps/course_api/blocks/transformers/tests/test_student_view.py
+0
-1
lms/djangoapps/course_api/blocks/views.py
+1
-1
lms/djangoapps/discussion_api/tests/test_forms.py
+1
-30
openedx/core/djangoapps/util/test_forms.py
+31
-0
No files found.
lms/djangoapps/course_api/blocks/forms.py
View file @
a47f86e5
...
@@ -47,7 +47,7 @@ class BlockListGetForm(Form):
...
@@ -47,7 +47,7 @@ class BlockListGetForm(Form):
try
:
try
:
return
int
(
value
)
return
int
(
value
)
except
ValueError
:
except
ValueError
:
raise
ValidationError
(
"'{}' is not a valid depth value"
.
format
(
value
))
raise
ValidationError
(
"'{}' is not a valid depth value
.
"
.
format
(
value
))
def
clean_usage_key
(
self
):
def
clean_usage_key
(
self
):
usage_key
=
self
.
cleaned_data
[
'usage_key'
]
usage_key
=
self
.
cleaned_data
[
'usage_key'
]
...
@@ -56,7 +56,7 @@ class BlockListGetForm(Form):
...
@@ -56,7 +56,7 @@ class BlockListGetForm(Form):
usage_key
=
UsageKey
.
from_string
(
usage_key
)
usage_key
=
UsageKey
.
from_string
(
usage_key
)
usage_key
=
usage_key
.
replace
(
course_key
=
modulestore
()
.
fill_in_run
(
usage_key
.
course_key
))
usage_key
=
usage_key
.
replace
(
course_key
=
modulestore
()
.
fill_in_run
(
usage_key
.
course_key
))
except
InvalidKeyError
:
except
InvalidKeyError
:
raise
ValidationError
(
"'{}' is not a valid usage key"
.
format
(
unicode
(
usage_key
)))
raise
ValidationError
(
"'{}' is not a valid usage key
.
"
.
format
(
unicode
(
usage_key
)))
return
usage_key
return
usage_key
...
@@ -74,7 +74,7 @@ class BlockListGetForm(Form):
...
@@ -74,7 +74,7 @@ class BlockListGetForm(Form):
# validate and set user
# validate and set user
requested_username
=
cleaned_data
.
get
(
'user'
,
''
)
requested_username
=
cleaned_data
.
get
(
'user'
,
''
)
requesting_user
=
self
.
initial
[
'request
'
]
.
user
requesting_user
=
self
.
initial
[
'request
ing_user'
]
if
requesting_user
.
username
.
lower
()
==
requested_username
.
lower
():
if
requesting_user
.
username
.
lower
()
==
requested_username
.
lower
():
cleaned_data
[
'user'
]
=
requesting_user
cleaned_data
[
'user'
]
=
requesting_user
...
...
lms/djangoapps/course_api/blocks/serializers.py
View file @
a47f86e5
"""
"""
Serializers for
all
Course Blocks related return objects.
Serializers for Course Blocks related return objects.
"""
"""
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
rest_framework.reverse
import
reverse
from
rest_framework.reverse
import
reverse
...
...
lms/djangoapps/course_api/blocks/tests/test_block_api.py
deleted
100644 → 0
View file @
d33e2e60
"""
TODO
"""
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
class
TestCourseAPI
(
ModuleStoreTestCase
):
pass
lms/djangoapps/course_api/blocks/tests/test_forms.py
0 → 100644
View file @
a47f86e5
"""
Tests for Course Blocks forms
"""
import
ddt
from
django.http
import
Http404
,
QueryDict
from
unittest
import
TestCase
from
urllib
import
urlencode
from
rest_framework.exceptions
import
PermissionDenied
from
student.tests.factories
import
UserFactory
from
opaque_keys.edx.keys
import
UsageKey
from
openedx.core.djangoapps.util.test_forms
import
FormTestMixin
from
..forms
import
BlockListGetForm
@ddt.ddt
class
TestBlockListGetForm
(
FormTestMixin
,
TestCase
):
"""
Tests for BlockListGetForm
"""
FORM_CLASS
=
BlockListGetForm
def
setUp
(
self
):
self
.
student
=
UserFactory
.
create
()
self
.
student2
=
UserFactory
.
create
()
self
.
staff
=
UserFactory
.
create
(
is_staff
=
True
)
self
.
initial
=
{
'requesting_user'
:
self
.
student
}
usage_key_string
=
'block-v1:some_org+split_course+some_run+type@some_block_type+block@some_block_id'
self
.
form_data
=
QueryDict
(
urlencode
({
'user'
:
self
.
student
.
username
,
'usage_key'
:
usage_key_string
,
}),
mutable
=
True
,
)
self
.
cleaned_data
=
{
'block_counts'
:
[],
'depth'
:
0
,
'requested_fields'
:
{
'display_name'
,
'type'
},
'student_view_data'
:
[],
'usage_key'
:
UsageKey
.
from_string
(
usage_key_string
),
'user'
:
self
.
student
,
}
def
assert_raises_permission_denied
(
self
):
with
self
.
assertRaises
(
PermissionDenied
):
self
.
get_form
(
expected_valid
=
False
)
def
assert_raises_not_found
(
self
):
with
self
.
assertRaises
(
Http404
):
self
.
get_form
(
expected_valid
=
False
)
def
assert_equals_cleaned_data
(
self
):
form
=
self
.
get_form
(
expected_valid
=
True
)
self
.
assertEqual
(
form
.
cleaned_data
,
self
.
cleaned_data
)
def
test_basic
(
self
):
self
.
assert_equals_cleaned_data
()
#-- usage key
def
test_missing_usage_key
(
self
):
self
.
form_data
.
pop
(
'usage_key'
)
self
.
assert_error
(
'usage_key'
,
"This field is required."
)
def
test_invalid_usage_key
(
self
):
self
.
form_data
[
'usage_key'
]
=
'invalid_usage_key'
self
.
assert_error
(
'usage_key'
,
"'invalid_usage_key' is not a valid usage key."
)
#-- user
def
test_missing_user
(
self
):
self
.
form_data
.
pop
(
'user'
)
self
.
assert_raises_permission_denied
()
def
test_nonexistent_user_by_student
(
self
):
self
.
form_data
[
'user'
]
=
'non_existent_user'
self
.
assert_raises_permission_denied
()
def
test_nonexistent_user_by_staff
(
self
):
self
.
initial
=
{
'requesting_user'
:
self
.
staff
}
self
.
form_data
[
'user'
]
=
'non_existent_user'
self
.
assert_raises_not_found
()
def
test_other_user_by_student
(
self
):
self
.
form_data
[
'user'
]
=
self
.
student2
.
username
self
.
assert_raises_permission_denied
()
def
test_other_user_by_staff
(
self
):
self
.
initial
=
{
'requesting_user'
:
self
.
staff
}
self
.
get_form
(
expected_valid
=
True
)
#-- depth
def
test_depth_integer
(
self
):
self
.
form_data
[
'depth'
]
=
3
self
.
cleaned_data
[
'depth'
]
=
3
self
.
assert_equals_cleaned_data
()
def
test_depth_all
(
self
):
self
.
form_data
[
'depth'
]
=
'all'
self
.
cleaned_data
[
'depth'
]
=
None
self
.
assert_equals_cleaned_data
()
def
test_depth_invalid
(
self
):
self
.
form_data
[
'depth'
]
=
'not_an_integer'
self
.
assert_error
(
'depth'
,
"'not_an_integer' is not a valid depth value."
)
#-- requested fields
def
test_requested_fields
(
self
):
self
.
form_data
.
setlist
(
'requested_fields'
,
[
'graded'
,
'some_other_field'
])
self
.
cleaned_data
[
'requested_fields'
]
|=
{
'graded'
,
'some_other_field'
}
self
.
assert_equals_cleaned_data
()
@ddt.data
(
'block_counts'
,
'student_view_data'
)
def
test_higher_order_field
(
self
,
field_name
):
field_value
=
[
'block_type1'
,
'block_type2'
]
self
.
form_data
.
setlist
(
field_name
,
field_value
)
self
.
cleaned_data
[
field_name
]
=
field_value
self
.
cleaned_data
[
'requested_fields'
]
.
add
(
field_name
)
self
.
assert_equals_cleaned_data
()
def
test_combined_fields
(
self
):
# add requested fields
self
.
form_data
.
setlist
(
'requested_fields'
,
[
'field1'
,
'field2'
])
# add higher order fields
block_types_list
=
[
'block_type1'
,
'block_type2'
]
for
field_name
in
[
'block_counts'
,
'student_view_data'
]:
self
.
form_data
.
setlist
(
field_name
,
block_types_list
)
self
.
cleaned_data
[
field_name
]
=
block_types_list
# verify the requested_fields in cleaned_data includes all fields
self
.
cleaned_data
[
'requested_fields'
]
|=
{
'field1'
,
'field2'
,
'student_view_data'
,
'block_counts'
}
self
.
assert_equals_cleaned_data
()
lms/djangoapps/course_api/blocks/transformers/tests/test_student_view.py
View file @
a47f86e5
from
openedx.core.lib.block_cache.block_structure
import
BlockStructureFactory
from
openedx.core.lib.block_cache.block_structure
import
BlockStructureFactory
from
xmodule.modulestore.tests.factories
import
CourseFactory
,
ItemFactory
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
from
..student_view
import
StudentViewTransformer
from
..student_view
import
StudentViewTransformer
...
...
lms/djangoapps/course_api/blocks/views.py
View file @
a47f86e5
...
@@ -111,7 +111,7 @@ class CourseBlocks(DeveloperErrorViewMixin, ListAPIView):
...
@@ -111,7 +111,7 @@ class CourseBlocks(DeveloperErrorViewMixin, ListAPIView):
"""
"""
requested_params
=
request
.
GET
.
copy
()
requested_params
=
request
.
GET
.
copy
()
requested_params
.
update
({
'usage_key'
:
usage_key_string
})
requested_params
.
update
({
'usage_key'
:
usage_key_string
})
params
=
BlockListGetForm
(
requested_params
,
initial
=
{
'request
'
:
request
})
params
=
BlockListGetForm
(
requested_params
,
initial
=
{
'request
ing_user'
:
request
.
user
})
if
not
params
.
is_valid
():
if
not
params
.
is_valid
():
raise
ValidationError
(
params
.
errors
)
raise
ValidationError
(
params
.
errors
)
...
...
lms/djangoapps/discussion_api/tests/test_forms.py
View file @
a47f86e5
...
@@ -10,39 +10,10 @@ import ddt
...
@@ -10,39 +10,10 @@ import ddt
from
django.http
import
QueryDict
from
django.http
import
QueryDict
from
opaque_keys.edx.locator
import
CourseLocator
from
opaque_keys.edx.locator
import
CourseLocator
from
openedx.core.djangoapps.util.test_forms
import
FormTestMixin
from
discussion_api.forms
import
CommentListGetForm
,
ThreadListGetForm
from
discussion_api.forms
import
CommentListGetForm
,
ThreadListGetForm
class
FormTestMixin
(
object
):
"""A mixin for testing forms"""
def
get_form
(
self
,
expected_valid
):
"""
Return a form bound to self.form_data, asserting its validity (or lack
thereof) according to expected_valid
"""
form
=
self
.
FORM_CLASS
(
self
.
form_data
)
self
.
assertEqual
(
form
.
is_valid
(),
expected_valid
)
return
form
def
assert_error
(
self
,
expected_field
,
expected_message
):
"""
Create a form bound to self.form_data, assert its invalidity, and assert
that its error dictionary contains one entry with the expected field and
message
"""
form
=
self
.
get_form
(
expected_valid
=
False
)
self
.
assertEqual
(
form
.
errors
,
{
expected_field
:
[
expected_message
]})
def
assert_field_value
(
self
,
field
,
expected_value
):
"""
Create a form bound to self.form_data, assert its validity, and assert
that the given field in the cleaned data has the expected value
"""
form
=
self
.
get_form
(
expected_valid
=
True
)
self
.
assertEqual
(
form
.
cleaned_data
[
field
],
expected_value
)
class
PaginationTestMixin
(
object
):
class
PaginationTestMixin
(
object
):
"""A mixin for testing forms with pagination fields"""
"""A mixin for testing forms with pagination fields"""
def
test_missing_page
(
self
):
def
test_missing_page
(
self
):
...
...
openedx/core/djangoapps/util/test_forms.py
0 → 100644
View file @
a47f86e5
"""
Mixins for testing forms.
"""
class
FormTestMixin
(
object
):
"""A mixin for testing forms"""
def
get_form
(
self
,
expected_valid
):
"""
Return a form bound to self.form_data, asserting its validity (or lack
thereof) according to expected_valid
"""
form
=
self
.
FORM_CLASS
(
self
.
form_data
,
initial
=
getattr
(
self
,
'initial'
,
None
))
self
.
assertEqual
(
form
.
is_valid
(),
expected_valid
)
return
form
def
assert_error
(
self
,
expected_field
,
expected_message
):
"""
Create a form bound to self.form_data, assert its invalidity, and assert
that its error dictionary contains one entry with the expected field and
message
"""
form
=
self
.
get_form
(
expected_valid
=
False
)
self
.
assertEqual
(
form
.
errors
,
{
expected_field
:
[
expected_message
]})
def
assert_field_value
(
self
,
field
,
expected_value
):
"""
Create a form bound to self.form_data, assert its validity, and assert
that the given field in the cleaned data has the expected value
"""
form
=
self
.
get_form
(
expected_valid
=
True
)
self
.
assertEqual
(
form
.
cleaned_data
[
field
],
expected_value
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment