Commit cf7d7bd2 by Chris Dodge

Merge branch 'master' of github.com:MITx/mitx into fix/cdodge/studio-forum-improvements

parents 8fd85743 7e35bf19
...@@ -1586,7 +1586,8 @@ def import_course(request, org, course, name): ...@@ -1586,7 +1586,8 @@ def import_course(request, org, course, name):
shutil.move(r / fname, course_dir) shutil.move(r / fname, course_dir)
module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT, module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT,
[course_subdir], load_error_modules=False, static_content_store=contentstore(), target_location_namespace=Location(location)) [course_subdir], load_error_modules=False, static_content_store=contentstore(),
target_location_namespace=Location(location), draft_store=modulestore())
# we can blow this away when we're done importing. # we can blow this away when we're done importing.
shutil.rmtree(course_dir) shutil.rmtree(course_dir)
...@@ -1620,8 +1621,8 @@ def generate_export_course(request, org, course, name): ...@@ -1620,8 +1621,8 @@ def generate_export_course(request, org, course, name):
logging.debug('root = {0}'.format(root_dir)) logging.debug('root = {0}'.format(root_dir))
export_to_xml(modulestore('direct'), contentstore(), loc, root_dir, name) export_to_xml(modulestore('direct'), contentstore(), loc, root_dir, name, modulestore())
# filename = root_dir / name + '.tar.gz' #filename = root_dir / name + '.tar.gz'
logging.debug('tar file being generated at {0}'.format(export_file.name)) logging.debug('tar file being generated at {0}'.format(export_file.name))
tf = tarfile.open(name=export_file.name, mode='w:gz') tf = tarfile.open(name=export_file.name, mode='w:gz')
......
...@@ -225,7 +225,6 @@ function toggleSections(e) { ...@@ -225,7 +225,6 @@ function toggleSections(e) {
function editSectionPublishDate(e) { function editSectionPublishDate(e) {
e.preventDefault(); e.preventDefault();
$modal = $('.edit-subsection-publish-settings').show(); $modal = $('.edit-subsection-publish-settings').show();
$modal = $('.edit-subsection-publish-settings').show();
$modal.attr('data-id', $(this).attr('data-id')); $modal.attr('data-id', $(this).attr('data-id'));
$modal.find('.start-date').val($(this).attr('data-date')); $modal.find('.start-date').val($(this).attr('data-date'));
$modal.find('.start-time').val($(this).attr('data-time')); $modal.find('.start-time').val($(this).attr('data-time'));
......
...@@ -97,7 +97,7 @@ ...@@ -97,7 +97,7 @@
color: $blue; color: $blue;
&:hover, &:active { &:hover, &:active {
background: $blue-l3; background: $blue-l4;
color: $blue-s2; color: $blue-s2;
} }
......
...@@ -8,11 +8,11 @@ input[type="password"], ...@@ -8,11 +8,11 @@ input[type="password"],
textarea.text { textarea.text {
padding: 6px 8px 8px; padding: 6px 8px 8px;
@include box-sizing(border-box); @include box-sizing(border-box);
border: 1px solid $mediumGrey; border: 1px solid $gray-l2;
border-radius: 2px; border-radius: 2px;
@include linear-gradient($lightGrey, tint($lightGrey, 90%)); @include linear-gradient($gray-l5, $white);
background-color: $lightGrey; background-color: $gray-l5;
@include box-shadow(0 1px 2px rgba(0, 0, 0, .1) inset); @include box-shadow(inset 0 1px 2px $shadow-l1);
font-family: 'Open Sans', sans-serif; font-family: 'Open Sans', sans-serif;
font-size: 11px; font-size: 11px;
color: $baseFontColor; color: $baseFontColor;
...@@ -21,7 +21,7 @@ textarea.text { ...@@ -21,7 +21,7 @@ textarea.text {
&::-webkit-input-placeholder, &::-webkit-input-placeholder,
&:-moz-placeholder, &:-moz-placeholder,
&:-ms-input-placeholder { &:-ms-input-placeholder {
color: #979faf; color: $gray-l2;
} }
&:focus { &:focus {
...@@ -30,7 +30,72 @@ textarea.text { ...@@ -30,7 +30,72 @@ textarea.text {
} }
} }
// forms - specific // ====================
// forms - fields - not editable
.field.is-not-editable {
& label.is-focused {
color: $gray-d2;
}
label, input, textarea {
pointer-events: none;
}
}
// ====================
// field with error
.field.error {
input, textarea {
border-color: $red;
}
}
// ====================
// forms - additional UI
form {
.note {
@include box-sizing(border-box);
.title {
}
.copy {
}
// note with actions
&.has-actions {
@include clearfix();
.title {
}
.copy {
}
.list-actions {
}
}
}
.note-promotion {
}
}
// ====================
// forms - grandfathered
input.search { input.search {
padding: 6px 15px 8px 30px; padding: 6px 15px 8px 30px;
@include box-sizing(border-box); @include box-sizing(border-box);
...@@ -73,4 +138,4 @@ code { ...@@ -73,4 +138,4 @@ code {
background-color: #edf1f5; background-color: #edf1f5;
@include box-shadow(0 1px 2px rgba(0, 0, 0, 0.1) inset); @include box-shadow(0 1px 2px rgba(0, 0, 0, 0.1) inset);
font-family: Monaco, monospace; font-family: Monaco, monospace;
} }
\ No newline at end of file
...@@ -147,7 +147,7 @@ body.course.settings { ...@@ -147,7 +147,7 @@ body.course.settings {
} }
label { label {
@include font-size(14); @extend .t-copy-sub1;
@include transition(color, 0.15s, ease-in-out); @include transition(color, 0.15s, ease-in-out);
margin: 0 0 ($baseline/4) 0; margin: 0 0 ($baseline/4) 0;
font-weight: 400; font-weight: 400;
...@@ -161,7 +161,7 @@ body.course.settings { ...@@ -161,7 +161,7 @@ body.course.settings {
@include placeholder($gray-l4); @include placeholder($gray-l4);
@include font-size(16); @include font-size(16);
@include size(100%,100%); @include size(100%,100%);
padding: ($baseline/2); padding: ($baseline/2);
&.long { &.long {
} }
...@@ -212,7 +212,7 @@ body.course.settings { ...@@ -212,7 +212,7 @@ body.course.settings {
padding: $baseline; padding: $baseline;
&:last-child { &:last-child {
padding-bottom: $baseline; padding-bottom: $baseline;
} }
.actions { .actions {
...@@ -238,33 +238,36 @@ body.course.settings { ...@@ -238,33 +238,36 @@ body.course.settings {
} }
} }
// not editable fields
.field.is-not-editable {
& label.is-focused {
color: $gray-d2;
}
}
// field with error
.field.error {
input, textarea {
border-color: $red;
}
}
// specific fields - basic // specific fields - basic
&.basic { &.basic {
.list-input { .list-input {
@include clearfix(); @include clearfix();
padding: 0 ($baseline/2);
.field { .field {
margin-bottom: 0; margin-bottom: 0;
} }
} }
// course details that should appear more like content than elements to change
.field.is-not-editable {
label {
}
input, textarea {
@extend .t-copy-lead1;
@include box-shadow(none);
border: none;
background: none;
padding: 0;
margin: 0;
font-weight: 600;
}
}
#field-course-organization { #field-course-organization {
float: left; float: left;
width: flex-grid(2, 9); width: flex-grid(2, 9);
...@@ -281,6 +284,58 @@ body.course.settings { ...@@ -281,6 +284,58 @@ body.course.settings {
float: left; float: left;
width: flex-grid(5, 9); width: flex-grid(5, 9);
} }
// course link note
.note-promotion-courseURL {
@include box-shadow(0 2px 1px $shadow-l1);
@include border-radius(($baseline/5));
margin-top: ($baseline*1.5);
border: 1px solid $gray-l2;
padding: ($baseline/2) 0 0 0;
.title {
@extend .t-copy-sub1;
margin: 0 0 ($baseline/10) 0;
padding: 0 ($baseline/2);
.tip {
display: inline;
margin-left: ($baseline/4);
}
}
.copy {
padding: 0 ($baseline/2) ($baseline/2) ($baseline/2);
.link-courseURL {
@extend .t-copy-lead1;
&:hover {
}
}
}
.list-actions {
@include box-shadow(inset 0 1px 1px $shadow-l1);
border-top: 1px solid $gray-l2;
padding: ($baseline/2);
background: $gray-l5;
.action-primary {
@include blue-button();
@include font-size(13);
font-weight: 600;
.icon {
@extend .t-icon;
@include font-size(16);
display: inline-block;
vertical-align: middle;
}
}
}
}
} }
// specific fields - schedule // specific fields - schedule
...@@ -322,7 +377,7 @@ body.course.settings { ...@@ -322,7 +377,7 @@ body.course.settings {
} }
} }
} }
// specific fields - overview // specific fields - overview
#field-course-overview { #field-course-overview {
...@@ -468,7 +523,7 @@ body.course.settings { ...@@ -468,7 +523,7 @@ body.course.settings {
} }
} }
} }
.grade-specific-bar { .grade-specific-bar {
height: 50px !important; height: 50px !important;
} }
...@@ -479,7 +534,7 @@ body.course.settings { ...@@ -479,7 +534,7 @@ body.course.settings {
li { li {
position: absolute; position: absolute;
top: 0; top: 0;
height: 50px; height: 50px;
text-align: right; text-align: right;
@include border-radius(2px); @include border-radius(2px);
...@@ -600,8 +655,8 @@ body.course.settings { ...@@ -600,8 +655,8 @@ body.course.settings {
} }
#field-course-grading-assignment-shortname, #field-course-grading-assignment-shortname,
#field-course-grading-assignment-totalassignments, #field-course-grading-assignment-totalassignments,
#field-course-grading-assignment-gradeweight, #field-course-grading-assignment-gradeweight,
#field-course-grading-assignment-droppable { #field-course-grading-assignment-droppable {
width: flex-grid(2, 6); width: flex-grid(2, 6);
} }
...@@ -734,4 +789,4 @@ body.course.settings { ...@@ -734,4 +789,4 @@ body.course.settings {
.content-supplementary { .content-supplementary {
width: flex-grid(3, 12); width: flex-grid(3, 12);
} }
} }
\ No newline at end of file
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<form id="hls-form" enctype="multipart/form-data"> <form id="hls-form" enctype="multipart/form-data">
<section class="source-edit"> <section class="source-edit">
<textarea name="" data-metadata-name="source_code" class="source-edit-box hls-data" rows="8" cols="40">${metadata['source_code']|h}</textarea> <textarea name="" data-metadata-name="source_code" class="source-edit-box hls-data" rows="8" cols="40">${editable_metadata_fields['source_code']|h}</textarea>
</section> </section>
<div class="submit"> <div class="submit">
<button type="reset" class="hls-compile">Save &amp; Compile to edX XML</button> <button type="reset" class="hls-compile">Save &amp; Compile to edX XML</button>
......
...@@ -99,6 +99,7 @@ class CapaFields(object): ...@@ -99,6 +99,7 @@ class CapaFields(object):
seed = StringyInteger(help="Random seed for this student", scope=Scope.user_state) seed = StringyInteger(help="Random seed for this student", scope=Scope.user_state)
weight = StringyFloat(help="How much to weight this problem by", scope=Scope.settings) weight = StringyFloat(help="How much to weight this problem by", scope=Scope.settings)
markdown = String(help="Markdown source of this module", scope=Scope.settings) markdown = String(help="Markdown source of this module", scope=Scope.settings)
source_code = String(help="Source code for LaTeX and Word problems. This feature is not well-supported.", scope=Scope.settings)
class CapaModule(CapaFields, XModule): class CapaModule(CapaFields, XModule):
......
...@@ -118,8 +118,8 @@ class HtmlDescriptor(HtmlFields, XmlDescriptor, EditingDescriptor): ...@@ -118,8 +118,8 @@ class HtmlDescriptor(HtmlFields, XmlDescriptor, EditingDescriptor):
with system.resources_fs.open(filepath) as file: with system.resources_fs.open(filepath) as file:
html = file.read().decode('utf-8') html = file.read().decode('utf-8')
# Log a warning if we can't parse the file, but don't error # Log a warning if we can't parse the file, but don't error
if not check_html(html): if not check_html(html) and len(html) > 0:
msg = "Couldn't parse html in {0}.".format(filepath) msg = "Couldn't parse html in {0}, content = {1}".format(filepath, html)
log.warning(msg) log.warning(msg)
system.error_tracker("Warning: " + msg) system.error_tracker("Warning: " + msg)
...@@ -156,7 +156,8 @@ class HtmlDescriptor(HtmlFields, XmlDescriptor, EditingDescriptor): ...@@ -156,7 +156,8 @@ class HtmlDescriptor(HtmlFields, XmlDescriptor, EditingDescriptor):
resource_fs.makedir(os.path.dirname(filepath), recursive=True, allow_recreate=True) resource_fs.makedir(os.path.dirname(filepath), recursive=True, allow_recreate=True)
with resource_fs.open(filepath, 'w') as file: with resource_fs.open(filepath, 'w') as file:
file.write(self.data.encode('utf-8')) html_data = self.data.encode('utf-8')
file.write(html_data)
# write out the relative name # write out the relative name
relname = path(pathname).basename() relname = path(pathname).basename()
......
...@@ -3,7 +3,6 @@ from datetime import datetime ...@@ -3,7 +3,6 @@ from datetime import datetime
from . import ModuleStoreBase, Location, namedtuple_to_son from . import ModuleStoreBase, Location, namedtuple_to_son
from .exceptions import ItemNotFoundError from .exceptions import ItemNotFoundError
from .inheritance import own_metadata from .inheritance import own_metadata
import logging
DRAFT = 'draft' DRAFT = 'draft'
...@@ -107,7 +106,7 @@ class DraftModuleStore(ModuleStoreBase): ...@@ -107,7 +106,7 @@ class DraftModuleStore(ModuleStoreBase):
""" """
return wrap_draft(super(DraftModuleStore, self).clone_item(source, as_draft(location))) return wrap_draft(super(DraftModuleStore, self).clone_item(source, as_draft(location)))
def update_item(self, location, data): def update_item(self, location, data, allow_not_found=False):
""" """
Set the data in the item specified by the location to Set the data in the item specified by the location to
data data
...@@ -116,9 +115,13 @@ class DraftModuleStore(ModuleStoreBase): ...@@ -116,9 +115,13 @@ class DraftModuleStore(ModuleStoreBase):
data: A nested dictionary of problem data data: A nested dictionary of problem data
""" """
draft_loc = as_draft(location) draft_loc = as_draft(location)
draft_item = self.get_item(location) try:
if not getattr(draft_item, 'is_draft', False): draft_item = self.get_item(location)
self.clone_item(location, draft_loc) if not getattr(draft_item, 'is_draft', False):
self.clone_item(location, draft_loc)
except ItemNotFoundError, e:
if not allow_not_found:
raise e
return super(DraftModuleStore, self).update_item(draft_loc, data) return super(DraftModuleStore, self).update_item(draft_loc, data)
...@@ -164,7 +167,6 @@ class DraftModuleStore(ModuleStoreBase): ...@@ -164,7 +167,6 @@ class DraftModuleStore(ModuleStoreBase):
""" """
return super(DraftModuleStore, self).delete_item(as_draft(location)) return super(DraftModuleStore, self).delete_item(as_draft(location))
def get_parent_locations(self, location, course_id): def get_parent_locations(self, location, course_id):
'''Find all locations that are the parents of this location. Needed '''Find all locations that are the parents of this location. Needed
for path_to_location(). for path_to_location().
...@@ -178,6 +180,7 @@ class DraftModuleStore(ModuleStoreBase): ...@@ -178,6 +180,7 @@ class DraftModuleStore(ModuleStoreBase):
Save a current draft to the underlying modulestore Save a current draft to the underlying modulestore
""" """
draft = self.get_item(location) draft = self.get_item(location)
draft.cms.published_date = datetime.utcnow() draft.cms.published_date = datetime.utcnow()
draft.cms.published_by = published_by_id draft.cms.published_by = published_by_id
super(DraftModuleStore, self).update_item(location, draft._model_data._kvs._data) super(DraftModuleStore, self).update_item(location, draft._model_data._kvs._data)
...@@ -221,6 +224,6 @@ class DraftModuleStore(ModuleStoreBase): ...@@ -221,6 +224,6 @@ class DraftModuleStore(ModuleStoreBase):
# convert the dict - which is used for look ups - back into a list # convert the dict - which is used for look ups - back into a list
for key, value in to_process_dict.iteritems(): for key, value in to_process_dict.iteritems():
queried_children.append(value) queried_children.append(value)
return queried_children return queried_children
import logging import logging
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.inheritance import own_metadata from xmodule.modulestore.inheritance import own_metadata
from fs.osfs import OSFS from fs.osfs import OSFS
from json import dumps from json import dumps
def export_to_xml(modulestore, contentstore, course_location, root_dir, course_dir): def export_to_xml(modulestore, contentstore, course_location, root_dir, course_dir, draft_modulestore=None):
course = modulestore.get_item(course_location) course = modulestore.get_item(course_location)
...@@ -40,6 +39,24 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d ...@@ -40,6 +39,24 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d
policy = {'course/' + course.location.name: own_metadata(course)} policy = {'course/' + course.location.name: own_metadata(course)}
course_policy.write(dumps(policy)) course_policy.write(dumps(policy))
# export draft content
# NOTE: this code assumes that verticals are the top most draftable container
# should we change the application, then this assumption will no longer
# be valid
if draft_modulestore is not None:
draft_verticals = draft_modulestore.get_items([None, course_location.org, course_location.course,
'vertical', None, 'draft'])
if len(draft_verticals) > 0:
draft_course_dir = export_fs.makeopendir('drafts')
for draft_vertical in draft_verticals:
parent_locs = draft_modulestore.get_parent_locations(draft_vertical.location, course.location.course_id)
logging.debug('parent_locs = {0}'.format(parent_locs))
draft_vertical.xml_attributes['parent_sequential_url'] = Location(parent_locs[0]).url()
sequential = modulestore.get_item(Location(parent_locs[0]))
index = sequential.children.index(draft_vertical.location.url())
draft_vertical.xml_attributes['index_in_children_list'] = str(index)
draft_vertical.export_to_xml(draft_course_dir)
def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix=''): def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix=''):
query_loc = Location('i4x', course_location.org, course_location.course, category_type, None) query_loc = Location('i4x', course_location.org, course_location.course, category_type, None)
......
--- ---
metadata: metadata:
display_name: E-text Written in LaTeX display_name: E-text Written in LaTeX
source_processor_url: https://qisx.mit.edu:5443/latex2edx
source_code: | source_code: |
\subsection{Example of E-text in LaTeX} \subsection{Example of E-text in LaTeX}
......
--- ---
metadata: metadata:
display_name: Problem Written in LaTeX display_name: Problem Written in LaTeX
source_processor_url: https://studio-input-filter.mitx.mit.edu/latex2edx
source_code: | source_code: |
% Nearly any kind of edX problem can be authored using Latex as % Nearly any kind of edX problem can be authored using Latex as
% the source language. Write latex as usual, including equations. The % the source language. Write latex as usual, including equations. The
......
--- ---
metadata: metadata:
display_name: Problem with Adaptive Hint display_name: Problem with Adaptive Hint
source_processor_url: https://qisx.mit.edu:5443/latex2edx
source_code: | source_code: |
\subsection{Problem With Adaptive Hint} \subsection{Problem With Adaptive Hint}
......
...@@ -110,8 +110,7 @@ class XmlDescriptor(XModuleDescriptor): ...@@ -110,8 +110,7 @@ class XmlDescriptor(XModuleDescriptor):
'name', 'slug') 'name', 'slug')
metadata_to_strip = ('data_dir', metadata_to_strip = ('data_dir',
# cdodge: @TODO: We need to figure out a way to export out 'tabs' and 'grading_policy' which is on the course 'tabs', 'grading_policy', 'published_by', 'published_date',
'tabs', 'grading_policy', 'is_draft', 'published_by', 'published_date',
'discussion_blackouts', 'testcenter_info', 'discussion_blackouts', 'testcenter_info',
# VS[compat] -- remove the below attrs once everything is in the CMS # VS[compat] -- remove the below attrs once everything is in the CMS
'course', 'org', 'url_name', 'filename', 'course', 'org', 'url_name', 'filename',
...@@ -135,7 +134,7 @@ class XmlDescriptor(XModuleDescriptor): ...@@ -135,7 +134,7 @@ class XmlDescriptor(XModuleDescriptor):
'graded': bool_map, 'graded': bool_map,
'hide_progress_tab': bool_map, 'hide_progress_tab': bool_map,
'allow_anonymous': bool_map, 'allow_anonymous': bool_map,
'allow_anonymous_to_peers': bool_map 'allow_anonymous_to_peers': bool_map,
} }
......
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib.auth.models import User from factories import UserFactory, RegistrationFactory, UserProfileFactory
from student.models import Registration, UserProfile
import json import json
class LoginTest(TestCase): class LoginTest(TestCase):
''' '''
Test student.views.login_user() view Test student.views.login_user() view
''' '''
def setUp(self): def setUp(self):
# Create one user and save it to the database # Create one user and save it to the database
self.user = User.objects.create_user('test', 'test@edx.org', 'test_password') self.user = UserFactory.build(username='test', email='test@edx.org')
self.user.is_active = True self.user.set_password('test_password')
self.user.save() self.user.save()
# Create a registration for the user # Create a registration for the user
Registration().register(self.user) registration = RegistrationFactory(user=self.user)
registration.register(self.user)
registration.activate()
# Create a profile for the user # Create a profile for the user
UserProfile(user=self.user).save() UserProfileFactory(user=self.user)
# Create the test client # Create the test client
self.client = Client() self.client = Client()
...@@ -42,19 +43,17 @@ class LoginTest(TestCase): ...@@ -42,19 +43,17 @@ class LoginTest(TestCase):
response = self._login_response(unicode_email, 'test_password') response = self._login_response(unicode_email, 'test_password')
self._assert_response(response, success=True) self._assert_response(response, success=True)
def test_login_fail_no_user_exists(self): def test_login_fail_no_user_exists(self):
response = self._login_response('not_a_user@edx.org', 'test_password') response = self._login_response('not_a_user@edx.org', 'test_password')
self._assert_response(response, success=False, self._assert_response(response, success=False,
value='Email or password is incorrect') value='Email or password is incorrect')
def test_login_fail_wrong_password(self): def test_login_fail_wrong_password(self):
response = self._login_response('test@edx.org', 'wrong_password') response = self._login_response('test@edx.org', 'wrong_password')
self._assert_response(response, success=False, self._assert_response(response, success=False,
value='Email or password is incorrect') value='Email or password is incorrect')
def test_login_not_activated(self): def test_login_not_activated(self):
# De-activate the user # De-activate the user
self.user.is_active = False self.user.is_active = False
self.user.save() self.user.save()
...@@ -62,8 +61,7 @@ class LoginTest(TestCase): ...@@ -62,8 +61,7 @@ class LoginTest(TestCase):
# Should now be unable to login # Should now be unable to login
response = self._login_response('test@edx.org', 'test_password') response = self._login_response('test@edx.org', 'test_password')
self._assert_response(response, success=False, self._assert_response(response, success=False,
value="This account has not been activated") value="This account has not been activated")
def test_login_unicode_email(self): def test_login_unicode_email(self):
unicode_email = u'test@edx.org' + unichr(40960) unicode_email = u'test@edx.org' + unichr(40960)
...@@ -95,13 +93,13 @@ class LoginTest(TestCase): ...@@ -95,13 +93,13 @@ class LoginTest(TestCase):
try: try:
response_dict = json.loads(response.content) response_dict = json.loads(response.content)
except ValueError: except ValueError:
self.fail("Could not parse response content as JSON: %s" self.fail("Could not parse response content as JSON: %s"
% str(response.content)) % str(response.content))
if success is not None: if success is not None:
self.assertEqual(response_dict['success'], success) self.assertEqual(response_dict['success'], success)
if value is not None: if value is not None:
msg = ("'%s' did not contain '%s'" % msg = ("'%s' did not contain '%s'" %
(str(response_dict['value']), str(value))) (str(response_dict['value']), str(value)))
self.assertTrue(value in response_dict['value'], msg) self.assertTrue(value in response_dict['value'], msg)
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import json
from logging import getLogger
logger = getLogger(__name__)
class MockCommentServiceRequestHandler(BaseHTTPRequestHandler):
'''
A handler for Comment Service POST requests.
'''
protocol = "HTTP/1.0"
def do_POST(self):
'''
Handle a POST request from the client
Used by the APIs for comment threads, commentables, comments,
subscriptions, commentables, users
'''
# Retrieve the POST data into a dict.
# It should have been sent in json format
length = int(self.headers.getheader('content-length'))
data_string = self.rfile.read(length)
post_dict = json.loads(data_string)
# Log the request
logger.debug("Comment Service received POST request %s to path %s" %
(json.dumps(post_dict), self.path))
# Every good post has at least an API key
if 'api_key' in post_dict:
response = self.server._response_str
# Log the response
logger.debug("Comment Service: sending response %s" % json.dumps(response))
# Send a response back to the client
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(response)
else:
# Respond with failure
self.send_response(500, 'Bad Request: does not contain API key')
self.send_header('Content-type', 'text/plain')
self.end_headers()
return False
class MockCommentServiceServer(HTTPServer):
'''
A mock Comment Service server that responds
to POST requests to localhost.
'''
def __init__(self, port_num,
response={'username': 'new', 'external_id': 1}):
'''
Initialize the mock Comment Service server instance.
*port_num* is the localhost port to listen to
*response* is a dictionary that will be JSON-serialized
and sent in response to comment service requests.
'''
self._response_str = json.dumps(response)
handler = MockCommentServiceRequestHandler
address = ('', port_num)
HTTPServer.__init__(self, address, handler)
def shutdown(self):
'''
Stop the server and free up the port
'''
# First call superclass shutdown()
HTTPServer.shutdown(self)
# We also need to manually close the socket
self.socket.close()
import unittest
import threading
import json
import urllib2
from mock_cs_server import MockCommentServiceServer
from nose.plugins.skip import SkipTest
class MockCommentServiceServerTest(unittest.TestCase):
'''
A mock version of the Comment Service server that listens on a local
port and responds with pre-defined grade messages.
'''
def setUp(self):
# This is a test of the test setup,
# so it does not need to run as part of the unit test suite
# You can re-enable it by commenting out the line below
raise SkipTest
# Create the server
server_port = 4567
self.server_url = 'http://127.0.0.1:%d' % server_port
# Start up the server and tell it that by default it should
# return this as its json response
self.expected_response = {'username': 'user100', 'external_id': '4'}
self.server = MockCommentServiceServer(port_num=server_port,
response=self.expected_response)
# Start the server in a separate daemon thread
server_thread = threading.Thread(target=self.server.serve_forever)
server_thread.daemon = True
server_thread.start()
def tearDown(self):
# Stop the server, freeing up the port
self.server.shutdown()
def test_new_user_request(self):
"""
Test the mock comment service using an example
of how you would create a new user
"""
# Send a request
values = {'username': u'user100', 'api_key': 'TEST_API_KEY',
'external_id': '4', 'email': u'user100@edx.org'}
data = json.dumps(values)
headers = {'Content-Type': 'application/json', 'Content-Length': len(data)}
req = urllib2.Request(self.server_url + '/api/v1/users/4', data, headers)
# Send the request to the mock cs server
response = urllib2.urlopen(req)
# Receive the reply from the mock cs server
response_dict = json.loads(response.read())
# You should have received the response specified in the setup above
self.assertEqual(response_dict, self.expected_response)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment