Commit 5d7171e3 by Jonathan Piacenti

Tests written.

parent 275ee7a9
...@@ -6,7 +6,7 @@ from django.template import Template, Context ...@@ -6,7 +6,7 @@ from django.template import Template, Context
import pkg_resources import pkg_resources
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, List, String, Dict from xblock.fields import Scope, String, Dict, List
from xblock.fragment import Fragment from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader from xblockutils.resources import ResourceLoader
...@@ -101,7 +101,6 @@ class PollBlock(XBlock): ...@@ -101,7 +101,6 @@ class PollBlock(XBlock):
""" """
if not context: if not context:
context = {} context = {}
js_template = self.resource_string( js_template = self.resource_string(
'/public/handlebars/results.handlebars') '/public/handlebars/results.handlebars')
...@@ -258,11 +257,23 @@ class PollBlock(XBlock): ...@@ -258,11 +257,23 @@ class PollBlock(XBlock):
# workbench while developing your XBlock. # workbench while developing your XBlock.
@staticmethod @staticmethod
def workbench_scenarios(): def workbench_scenarios():
"""A canned scenario for display in the workbench.""" """
Canned scenarios for display in the workbench.
"""
return [ return [
("PollBlock", ("Default Poll",
"""<vertical_demo> """
<poll/> <vertical_demo>
</vertical_demo> <poll />
</vertical_demo>
"""),
("Customized Poll",
"""
<vertical_demo>
<poll url_name="poll_functions" question="## How long have you been studying with us?"
feedback="### Thank you&#10;&#10;for being a valued student."
tally="{'long': 20, 'short': 29, 'not_saying': 15, 'longer' : 35}"
answers="[['long', 'A very long time'], ['short', 'Not very long'], ['not_saying', 'I shall not say'], ['longer', 'Longer than you']]"/>
</vertical_demo>
"""), """),
] ]
\ No newline at end of file
{{ js_template|safe }} {{ js_template|safe }}
<div class="poll_block"> <div class="poll-block">
{# If no form is present, the Javascript will load the results instead. #} {# If no form is present, the Javascript will load the results instead. #}
{% if not choice %} {% if not choice %}
<form> <form>
{{question|safe}} <div class="poll-question-container">
{{question|safe}}
</div>
<ul class="poll-answers"> <ul class="poll-answers">
{% for key, answer in answers %} {% for key, answer in answers %}
<li class="poll-answer"> <li class="poll-answer">
......
...@@ -17,7 +17,7 @@ function PollBlock(runtime, element) { ...@@ -17,7 +17,7 @@ function PollBlock(runtime, element) {
url: tallyURL, url: tallyURL,
data: JSON.stringify({}), data: JSON.stringify({}),
success: function (data) { success: function (data) {
element.innerHTML = resultsTemplate(data); $('div.poll-block', element).html(resultsTemplate(data));
} }
}) })
} }
......
from xblockutils.base_test import SeleniumBaseTest
class PollBaseTest(SeleniumBaseTest):
default_css_selector = 'div.poll-block'
module_name = __name__
\ No newline at end of file
...@@ -3,13 +3,25 @@ Tests to verify a default poll XBlock is a functional demo. ...@@ -3,13 +3,25 @@ Tests to verify a default poll XBlock is a functional demo.
Deeper investigation should be tested in test_poll_functions. Deeper investigation should be tested in test_poll_functions.
""" """
from xblockutils.base_test import SeleniumBaseTest from selenium.common.exceptions import NoSuchElementException
from .base_test import PollBaseTest
class TestDefaults(SeleniumBaseTest): class TestDefaults(PollBaseTest):
def test_default_poll(self): def test_default_poll(self):
""" """
Verifies that a default poll loads, that it can be voted on, and that Verifies that a default poll loads, that it can be voted on, and that
the tally displays afterward. Verifies that the feedback section does the tally displays afterward. Verifies that the feedback section does
not load since it is not enabled by default. not load since it is not enabled by default.
""" """
\ No newline at end of file self.go_to_page('Defaults')
button = self.browser.find_element_by_css_selector('input[type=radio]')
button.click()
submit = self.browser.find_element_by_css_selector('input[name="poll-submit"]')
submit.click()
# Should now be on the results page.
self.assertEqual(self.browser.find_element_by_css_selector('.poll-percent-display').text, '100%')
# No feedback section.
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback')
\ No newline at end of file
""" """
Tests to make sure that markdown is both useful and secure. Tests to make sure that markdown is both useful and secure.
""" """
from .base_test import PollBaseTest
from xblockutils.base_test import SeleniumBaseTest
class MarkdownTestCase(PollBaseTest):
class MarkdownTestCase(SeleniumBaseTest):
def test_question_markdown(self): def test_question_markdown(self):
pass self.go_to_page("Markdown")
self.assertEqual(
self.browser.find_element_by_css_selector('.poll-question-container').text,
"""This is a test
This is only a ><test
One
Two
Three
First
Second
Third
We shall find out if markdown is respected.
"I have not yet begun to code.\"""")
def test_feedback_markdown(self): def test_feedback_markdown(self):
pass self.go_to_page("Markdown")
\ No newline at end of file self.browser.find_element_by_css_selector('input[type=radio]').click()
self.browser.find_element_by_css_selector('input[name="poll-submit"]').click()
self.assertEqual(
self.browser.find_element_by_css_selector('.poll-feedback').text,
"""This is some feedback
This is a link
This is also a link.
This is a paragraph with emphasized and bold text, and both.""")
\ No newline at end of file
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
Tests a realistic, configured Poll to make sure that everything works as it Tests a realistic, configured Poll to make sure that everything works as it
should. should.
""" """
from xblockutils.base_test import SeleniumBaseTest from selenium.common.exceptions import NoSuchElementException
from .base_test import PollBaseTest
class TestPollFunctions(SeleniumBaseTest): class TestPollFunctions(PollBaseTest):
def test_first_load(self): def test_first_load(self):
""" """
Checks first load. Checks first load.
...@@ -14,20 +15,59 @@ class TestPollFunctions(SeleniumBaseTest): ...@@ -14,20 +15,59 @@ class TestPollFunctions(SeleniumBaseTest):
not showing, that the submit button is disabled, and that it is enabled not showing, that the submit button is disabled, and that it is enabled
when a choice is selected. when a choice is selected.
""" """
self.go_to_page('Poll Functions')
answer_elements = self.browser.find_elements_by_css_selector('label.poll-answer')
answers = [element.text for element in answer_elements]
self.assertEqual(['A very long time', 'Not very long', 'I shall not say', 'Longer than you'], answers)
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback')
submit_button = self.browser.find_element_by_css_selector('input[name=poll-submit]')
self.assertFalse(submit_button.is_enabled())
answer_elements[0].click()
# When an answer is selected, make sure submit is enabled.
self.assertTrue(submit_button.is_enabled())
def test_poll_submission(self): def test_poll_submission(self):
""" """
Verify that the user can submit his or her vote, that the vote Verify that the user can submit his or her vote and that the vote counts.
influences the tally, and that feedback is shown after the vote.
Also check that feedback is displayed afterward.
""" """
self.go_to_page('Poll Functions')
answer_elements = self.browser.find_elements_by_css_selector('label.poll-answer')
# 'Not very long'
answer_elements[1].click()
self.browser.find_element_by_css_selector('input[name=poll-submit]').click()
self.assertTrue(self.browser.find_element_by_css_selector('.poll-feedback').text,
"Thank you\nfor being a valued student.")
def test_no_duplicate_vote(self): self.assertEqual(self.browser.find_element_by_css_selector('.poll-footnote').text,
'Results gathered from 100 respondent(s).')
self.assertFalse(self.browser.find_element_by_css_selector('input[name=poll-submit]').is_enabled())
def test_submit_not_enabled_on_revisit(self):
""" """
Verify that revisiting the page does not re-enable the submit button. Verify that revisiting the page post-vote does not re-enable the submit button.
""" """
self.go_to_page('Poll Functions')
def test_no_ballot_stuffing(self): answer_elements = self.browser.find_elements_by_css_selector('label.poll-answer')
"""
Verify that the server rejects a well crafted attempt to force an # Not very long
additional vote. answer_elements[1].click()
"""
\ No newline at end of file self.browser.find_element_by_css_selector('input[name=poll-submit]').click()
submit_button = self.browser.find_element_by_css_selector('input[name=poll-submit]')
self.assertFalse(submit_button.is_enabled())
self.go_to_page('Poll Functions')
self.assertFalse(self.browser.find_element_by_css_selector('input[name=poll-submit]').is_enabled())
\ No newline at end of file
<vertical_demo> <vertical_demo>
<poll url_name="markdown" question="## This is a test <poll url_name="markdown" question="## This is a test&#10;&#10;&lt;h1&gt;This is only a &amp;gt;&amp;lt;test&lt;/h1&gt;&#10;&#10;* One&#10;* Two&#10;* Three&#10;&#10;1. First&#10;2. Second&#10;3. Third&#10;&#10;We shall find out if markdown is respected.&#10;&#10;&gt; &quot;I have not yet begun to code.&quot;" feedback="### This is some feedback&#10;&#10;[This is a link](http://www.example.com)&#10;&#10;&lt;a href=&quot;http://www.example.com&quot; target=&quot;_blank&quot;&gt;This is also a link.&lt;/a&gt;&#10;&#10;This is a paragraph with *emphasized* and **bold** text, and **_both_**." />
&lt;h1&gt;This is only a &amp;gt;&amp;lt;test&lt;/h1&gt;
* One
* Two
* Three
1. First
2. Second.
3. Third
We shall find out if markdown is respected.
&gt; &quot;I have not yet begun to code.&quot;" feedback="### This is some feedback
[This is a link](http://www.example.com)
&lt;a href=&quot;http://www.example.com&quot; target=&quot;_blank&quot;&gt;This is also a link.&lt;/a&gt;
This is a paragraph with *emphasized* and **bold** text, and **_both_**." />
</vertical_demo> </vertical_demo>
\ No newline at end of file
<vertical_demo> <vertical_demo>
<poll url_name="poll_functions" question="## How long have you been studying with us?" <poll tally="{'long': 20, 'short': 29, 'not_saying': 15, 'longer' : 35}"
feedback="### Thank you&#10;&#10;for being a valued student." question="## How long have you been studying with us?"
tally="{'long': 0, 'short': 0, 'not_saying': 1}" answers="[['long', 'A very long time'], ['short', 'Not very long'], ['not_saying', 'I shall not say'], ['longer', 'Longer than you']]"
answers="[[u'long', u'A very long time.'], [u'short', u'Not very long'], [u'not_saying', u'I shall not say.']]"/> feedback="### Thank you&#10;&#10;for being a valued student."/>
</vertical_demo> </vertical_demo>
\ No newline at end of file
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