class AdminBase(): browser = SplinterWrapper.getBrowser() @classmethod def create_backend(cls, browser, name): SplinterWrapper.open("/admin/rapidsms/backend/add/") fill_form_and_submit(browser, {"id_name": name}, "_save") @classmethod def create_contact(cls, browser, name, gender, backend_name, identity, group): SplinterWrapper.open("/admin/rapidsms/contact/add/") form_data = { "id_name": name, "id_gender": gender, "id_birthdate_0": datetime.now().strftime("%Y-%m-%d"), "id_birthdate_1": "00:00:00", "id_reporting_location": ("%s" % REPORTING_LOCATION_ID_KAMAIBA), "id_connection_set-0-backend": backend_name, "id_connection_set-0-identity": identity, "id_groups": group } fill_form_and_submit(browser, form_data, "_save") @classmethod def create_group(cls, browser, name): SplinterWrapper.open("/admin/auth/group/add/") fill_form_and_submit(browser, {"id_name": name}, "_save") def create_user(self, name, group): SplinterWrapper.open("/admin/auth/user/add/") fill_form_and_submit(self.browser, {"id_username": name, "id_password1": name, "id_password2": name}, "_save") fill_form_and_submit(self.browser, {"id_groups": group}, "_save") @classmethod def change_users_group(cls, group_name): SplinterWrapper.open("/admin/auth/user") cls.browser.click_link_by_text("ureport") fill_form_and_submit(cls.browser, {"id_groups": group_name}, "_save") @classmethod def log_in_as_ureport(cls): SplinterWrapper.open('/accounts/login') if cls.browser.is_element_present_by_name("username"): cls.browser.fill("username", "ureport") cls.browser.fill("password", "ureport") cls.browser.find_by_css("input[type=submit]").first.click() def log_as_admin_and_visit(self, url): self.create_and_sign_in_admin("ureport", "ureport", url) def cleanup(self, url): self.open(url) if self.browser.is_element_present_by_id("action-toggle"): fill_form(self.browser, {"action-toggle": True}) fill_form_and_submit(self.browser, {"action": "delete_selected"}, "index", True, True) self.browser.find_by_value("Yes, I'm sure").first.click()
class AuthenticationTest(unittest.TestCase): browser = SplinterWrapper.getBrowser() def test_login_redirects_to_admin_dashboard_on_success(self): SplinterWrapper.open("/accounts/logout") SplinterWrapper.open("/accounts/login") fill_form(self.browser, { "username": "******", "password": "******" }, True, True) self.browser.find_by_value("Login").first.click() self.assertEqual(self.browser.is_text_present('Add New Poll'), True)
class PollFlowTest(unittest.TestCase,PollBase,PollAssertions): browser = SplinterWrapper.getBrowser() AdminBase.log_in_as_ureport() poll_id, question = PollBase.setup_poll(browser) opener = login_as_admin('ureport','ureport') @classmethod def setUpClass(cls): PollBase.start_poll(cls.browser,cls.poll_id) AdminBase.change_users_group("groupFT") @classmethod def cleanup(cls, url): SplinterWrapper.open(url) if cls.browser.is_element_present_by_id("action-toggle"): fill_form(cls.browser, {"action-toggle": True}) fill_form_and_submit(cls.browser, {"action": "delete_selected"}, "index", True, True) cls.browser.find_by_value("Yes, I'm sure").first.click() @classmethod def delete_poll(cls): delete_url = build_url('/polls/%s/delete/' % cls.poll_id) post(cls.opener, delete_url) def tearDown(self): responses = PollBase.get_poll_responses_ids(self.browser, self.poll_id) for response in responses: delete_responses_url = build_url('/polls/responses/%s/delete/' % response.value) post(self.opener, delete_responses_url) @classmethod def tearDownClass(cls): cls.delete_poll() cls.cleanup("/admin/rapidsms/connection/") cls.cleanup("/admin/rapidsms/backend/") cls.cleanup("/admin/rapidsms/contact/") cls.cleanup("/admin/auth/group/") SplinterWrapper.open('/account/logout') def test_that_poll_status_changes_when_started(self): SplinterWrapper.open("/poll_status/%s" % self.poll_id) self.assert_that_poll_start_date_is_not_none(self.poll_id) def test_that_poll_can_be_sent_out_to_contacts(self): self.assert_that_poll_question_are_sent_out_to_contacts(1, 'What is your name') def test_that_polls_can_be_responded(self): SplinterWrapper.open('/router/console/') number_of_responses = len(rows_of_table_by_class(self.browser, "messages module")) self.respond_to_the_started_poll("0794339344", "yes") self.assert_that_number_of_responses_increase_by(number_of_responses, 1) def test_that_polls_can_be_reopen(self): SplinterWrapper.open("/view_poll/%s" % self.poll_id) PollBase.close_poll(self.poll_id) self.browser.find_link_by_text('Reopen Poll').first.click() self.assert_that_poll_end_date_is_none(self.poll_id) def test_that_admin_is_able_to_add_new_poll(self): SplinterWrapper.open('/mypolls/%s' % self.poll_id) self.assert_that_page_has_add_poll_button() def test_should_show_the_status_page(self): SplinterWrapper.open("/poll_status/%s" % self.poll_id) self.assertEqual(self.browser.is_element_present_by_id('poll-details'), True) self.assertTrue(self.poll_id in self.browser.find_by_id("poll-details").first.text) self.assertEqual(self.browser.find_by_id('contact-count').text, "1") self.assertEqual(self.browser.find_by_id('category-count').text, "3") self.assertEqual(self.browser.find_by_id('is-yesno').text, "yes") def test_admin_can_search_for_ureporter(self): group_name = "groupFT" self.respond_to_the_started_poll("0794339344", "yes") SplinterWrapper.open( '/reporter/') self.search_by_ureporter_group("%s" % group_name) self.assertEquals(True, self.browser.is_text_present("0794339344")) def search_by_ureporter_group(self, group_name): element_list_matching_option = self.browser.find_option_by_text(group_name) self.browser.select("groups", element_list_matching_option.first.value) self.browser.click_link_by_partial_text("Update") return self
class PollResponsesTest(unittest.TestCase, PollAssertions): browser = SplinterWrapper.getBrowser() def tearDown(self): responses = PollBase.get_poll_responses_ids(self.browser, self.poll_id) for response in responses: delete_responses_url = build_url('/polls/responses/%s/delete/' % response.value) post(self.opener, delete_responses_url) PollBase.close_poll(self.poll_id) @classmethod def setUpClass(cls): AdminBase.log_in_as_ureport() cls.poll_id, cls.question = PollBase.setup_poll( cls.browser, question="This is a new poll.") AdminBase.change_users_group("groupFT") cls.opener = login_as_admin('ureport', 'ureport') @classmethod def cleanup(cls, url): SplinterWrapper.open(url) if cls.browser.is_element_present_by_id("action-toggle"): fill_form(cls.browser, {"action-toggle": True}) fill_form_and_submit(cls.browser, {"action": "delete_selected"}, "index", True, True) cls.browser.find_by_value("Yes, I'm sure").first.click() @classmethod def delete_poll(cls): delete_url = build_url('/polls/%s/delete/' % cls.poll_id) post(cls.opener, delete_url) @classmethod def tearDownClass(cls): cls.delete_poll() cls.cleanup("/admin/rapidsms/connection/") cls.cleanup("/admin/rapidsms/backend/") cls.cleanup("/admin/rapidsms/contact/") cls.cleanup("/admin/auth/group/") def test_that_poll_responses_are_shown_up_at_report_page(self): PollBase.start_poll(self.browser, self.poll_id) PollBase.respond_to_the_started_poll("0794339344", "yes") SplinterWrapper.open('/polls/%s/report/' % self.poll_id) self.assert_that_question_is(self.question) self.assert_the_number_of_participants_of_the_poll_is(1) self.assert_that_response_location_is("Kasese") self.assert_that_number_of_responses_is(1) def test_that_a_poll_response_can_be_reassigned_to_another_poll(self): second_poll_id = PollBase.create_poll( self.browser, name='Second Poll', type="Yes/No Question", question="Is the first poll working?", group="groupFT") PollBase.start_poll(self.browser, second_poll_id) PollBase.respond_to_the_started_poll("0794339344", "yes") PollBase.close_poll(second_poll_id) PollBase.reassign_poll_response(second_poll_id, self.poll_id) time.sleep(2) #Takes a time for a poll to be reassigned PollBase.start_poll(self.browser, self.poll_id) SplinterWrapper.open('/polls/%s/report/' % self.poll_id) self.assert_that_number_of_responses_is(1) def test_that_a_response_can_be_replied_to_an_ureporter(self): message = "Hello" PollBase.start_poll(self.browser, self.poll_id) number_of_responses = PollBase.respond_to_the_started_poll( "0794339344", "yes") self.assert_that_number_of_responses_increase_by( number_of_responses, 1) PollBase.reply_poll_to_an_ureporter(self.poll_id, message) self.assert_that_message_has_been_sent_out_to_ureporter(message)
class PollAssertions(): browser = SplinterWrapper.getBrowser() def assert_that_poll_start_date_is_not_none(self, poll_id): SplinterWrapper.open('/mypolls/%s' % poll_id) start_date = self.browser.find_by_xpath( '//*[@class="results"]/tbody/tr[2]/td[3]') date_today = date.today().strftime("%d/%m/%Y") self.assertEquals(start_date.text, date_today) def assert_that_poll_has_responses(self, poll): self.assertEquals(poll.responses.count(), 2) elements = self.browser.find_link_by_href('/%i/responses/' % poll.id) assert elements.first.value == 'Responses (%i)' % poll.responses.count( ) def assert_that_question_is(self, question): elements = self.browser.find_by_xpath('//*[@class="question"]') assert elements.first.value == question def assert_the_number_of_participants_of_the_poll_is( self, responses_count): elements = self.browser.find_by_xpath('//*[@class="participants"]') num_participants = elements.first.value.split(' ')[0] self.assertEquals(int(num_participants), responses_count) def assert_that_response_location_is(self, location): elements = self.browser.find_by_xpath('//*[@class="poll_table"]') tbody = elements.find_by_tag('tbody') trs = tbody.find_by_tag('tr') tds = trs.find_by_tag('td') self.assertEquals(tds.first.value, location) def assert_that_number_of_responses_is(self, responses_count): elements = self.browser.find_by_xpath('//*[@class="poll_table"]') tbody = elements.first.find_by_tag('tbody') tr = tbody.find_by_tag('tr').first tds = tr.find_by_tag('td') total = 0 for td in tds[1:]: total += int(td.value.split(' ')[0]) self.assertEquals(responses_count, total) def assert_that_poll_end_date_is_none(self, poll_id): SplinterWrapper.open('/mypolls/%s' % poll_id) elements = self.browser.find_by_xpath('//*[@class="results"]') tbody = elements.first.find_by_tag('tbody') trs = tbody.find_by_tag('tr') view_poll_link = '/view_poll/%s/' % poll_id for tr in trs: element = tr.find_by_xpath('//*[@href="%s"]' % view_poll_link).first if element is not None: start_date = date.today().strftime("%d/%m/%Y") self.assertTrue(tr.find_by_value(start_date) is not None) def assert_that_page_has_add_poll_button(self): self.assertTrue(self.browser.find_link_by_href('/createpoll/')) def assert_that_page_has_edit_poll_option(self, poll): element = self.browser.find_link_by_href('/view_poll/%i/' % poll.id) self.assertEqual(element.first.text, "Edit") def assert_that_page_has_report_poll_option(self, poll): element = self.browser.find_link_by_href('/polls/%i/report/' % poll.id) self.assertEqual(element.first.text, "Report") def assert_that_poll_question_are_sent_out_to_contacts( self, number_of_contact_for_poll, question): SplinterWrapper.open('/router/console') rows = rows_of_table_by_class(self.browser, 'messages module') total = 0 for row in rows: if row.find_by_tag('td').first.text == question: total += 1 self.assertEqual(total, number_of_contact_for_poll) def assert_that_number_of_responses_increase_by(self, number_of_responses, increment): SplinterWrapper.open('/router/console') rows_responses = rows_of_table_by_class(self.browser, "messages module") self.assertEqual(len(rows_responses), number_of_responses + increment) def assert_that_message_has_been_sent_out_to_ureporter(self, message): SplinterWrapper.open("/router/console") element = self.browser.find_by_xpath( "//table/tbody/tr/td[text()='%s']" % message) self.assertEquals(element.first.text, message)
class PollBase(PollAssertions): browser = SplinterWrapper.getBrowser() @classmethod def start_poll(cls, browser, poll_id): SplinterWrapper.open("/view_poll/%s " % poll_id) if len(browser.find_link_by_text('Start Poll')) > 0: browser.find_link_by_text('Start Poll').first.click() time.sleep(2) #Sending questions is an asynchronous process elif len(browser.find_link_by_text('Reopen Poll')) > 0: PollBase.reopen_poll(browser, poll_id) @classmethod def reopen_poll(cls, browser, poll_id): SplinterWrapper.open("/view_poll/%s " % poll_id) browser.find_link_by_text('Reopen Poll').first.click() @classmethod def close_poll(cls, poll_id): SplinterWrapper.open("/view_poll/%s" % poll_id) if len(cls.browser.find_link_by_text('Close Poll')) > 0: cls.browser.find_link_by_text('Close Poll').first.click() def get_poll(self, poll_id): return Poll.objects.get(id=poll_id) @classmethod def get_poll_responses_ids(cls, browser, poll_id): SplinterWrapper.open("/%s/responses/" % poll_id) return browser.find_by_xpath( '//input[@type="checkbox" and @name="results"]') @classmethod def respond_to_the_started_poll(cls, sender, message): SplinterWrapper.open('/router/console/') rows_responses = rows_of_table_by_class(cls.browser, "messages module") number_of_responses = len(rows_responses) form_data = {"text": message, "sender": sender} fill_form(cls.browser, form_data, True) cls.browser.find_by_css("input[type=submit]").first.click() return number_of_responses def get_poll_response_location(self, response): contact = response.message.connection.contact location = contact.reporting_location return location.name def get_first_poll_response_location(self, poll): responses = self.get_poll_responses(poll) return self.get_poll_response_location(responses[0]) def get_poll_responses(self, poll): responses = poll.responses.all() return responses @classmethod def create_poll(cls, browser, name, type, question, group): SplinterWrapper.open("/createpoll/") form_data = {"id_type": type, "id_name": name, "id_groups": group} browser.fill("question_en", question) fill_form(browser, form_data) browser.find_by_css(".buttons a").last.click() return browser.url.split('/')[-2] @classmethod def setup_poll(cls, browser, question="What is your name", number_prefix="079433934"): AdminBase.create_group(browser, "groupFT") AdminBase.create_backend(browser, "console") AdminBase.create_contact(browser, "FT1", "Male", "console", "%s4" % number_prefix, "groupFT") poll_id = PollBase.create_poll(browser, question, "Yes/No Question", question, "groupFT") return poll_id, question @classmethod def reassign_poll_response(cls, poll_id, second_poll_id): SplinterWrapper.open("/%s/responses/" % poll_id) responses_all = cls.browser.find_by_id("input_select_all").first responses_all.check() elements = cls.browser.find_by_id("id_poll") elements.first.find_by_value(second_poll_id).first._element.click() assign_link = cls.browser.find_link_by_text("Assign selected to poll") assign_link.click() @classmethod def reply_poll_to_an_ureporter(cls, poll_id, message): SplinterWrapper.open("/%s/responses/" % poll_id) responses_all = cls.browser.find_by_id("input_select_all").first responses_all.check() cls.browser.fill("text", message) reply_link = cls.browser.find_link_by_text("Reply to selected") reply_link.click()