From f4ae0e0cae633f5a4b9e89e5a4cac178f8b24044 Mon Sep 17 00:00:00 2001 From: Nate Hardison Date: Tue, 21 May 2013 16:33:30 -0700 Subject: [PATCH] Test generation of chat settings Ensure that the chat connection settings are generated properly for the template context. --- lms/djangoapps/courseware/tests/test_views.py | 23 +++++++++++++ lms/djangoapps/courseware/views.py | 32 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index a5efe744a8..7d494df4cb 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -124,3 +124,26 @@ class ViewsTestCase(TestCase): self.assertContains(result, expected_end_text) else: self.assertNotContains(result, "Classes End") + + def test_chat_settings(self): + mock_user = MagicMock() + mock_user.username = "johndoe" + + mock_course = MagicMock() + mock_course.id = "a/b/c" + + # Stub this out in the case that it's not in the settings + domain = "jabber.edx.org" + settings.JABBER_DOMAIN = domain + + chat_settings = views.chat_settings(mock_course, mock_user) + + # Test the proper format of all chat settings + self.assertEquals(chat_settings['domain'], domain) + self.assertEquals(chat_settings['room'], "a-b-c_class") + self.assertEquals(chat_settings['username'], "johndoe@%s" % domain) + + # TODO: this needs to be changed once we figure out how to + # generate/store a real password. + self.assertEquals(chat_settings['password'], "johndoe@%s" % domain) + diff --git a/lms/djangoapps/courseware/views.py b/lms/djangoapps/courseware/views.py index 360eb143a5..9dc9f77bbe 100644 --- a/lms/djangoapps/courseware/views.py +++ b/lms/djangoapps/courseware/views.py @@ -235,6 +235,30 @@ def update_timelimit_module(user, course_id, model_data_cache, timelimit_descrip return context +def chat_settings(course, user): + """ + Returns a dict containing the settings required to connect to a + Jabber chat server and room. + """ + return { + 'domain': settings.JABBER_DOMAIN, + + # Jabber doesn't like slashes, so replace with dashes + 'room': "{ID}_class".format(ID=course.id.replace('/', '-')), + + 'username': "{USER}@{DOMAIN}".format( + USER=user.username, DOMAIN=settings.JABBER_DOMAIN + ), + + # TODO: clearly this needs to be something other than the username + # should also be something that's not necessarily tied to a + # particular course + 'password': "{USER}@{DOMAIN}".format( + USER=user.username, DOMAIN=settings.JABBER_DOMAIN + ), + } + + @login_required @ensure_csrf_cookie @cache_control(no_cache=True, no_store=True, must_revalidate=True) @@ -300,13 +324,7 @@ def index(request, course_id, chapter=None, section=None, } if course.show_chat: - context['chat'] = { - 'domain': settings.JABBER_DOMAIN, - 'room': "{ID}_class".format(ID=course.id.replace('/', '-')), # Jabber doesn't like /s - 'username': "{USER}@{DOMAIN}".format(USER=user.username, DOMAIN=settings.JABBER_DOMAIN), - # TODO: clearly this needs to be something other than the username - 'password': "{USER}@{DOMAIN}".format(USER=user.username, DOMAIN=settings.JABBER_DOMAIN), - } + context['chat'] = chat_settings(course, user) chapter_descriptor = course.get_child_by(lambda m: m.url_name == chapter) if chapter_descriptor is not None: