def test_parse_link(self): url, text = parse_link("") self.assertEqual("", url) self.assertEqual("", text) url, text = parse_link('before <a href="/foo/bar.html">Stuff</a> after') self.assertEqual("/foo/bar.html", url) self.assertEqual("Stuff", text)
def test_parse_link(self): url, text = parse_link('') self.assertEqual('', url) self.assertEqual('', text) url, text = parse_link( 'before <a href="/foo/bar.html">Stuff</a> after') self.assertEqual('/foo/bar.html', url) self.assertEqual('Stuff', text)
def _assert_question_fields(self, html, q_url, q_text, r_url): q_link, r_link = html.split("|") url, text = parse_link(q_link) self.assertIn(q_url, url) self.assertIn(q_text, text) url, text = parse_link(r_link) self.assertIn(r_url, url) self.assertIn("Reorder", text)
def _assert_question_fields(self, html, q_url, q_text, r_url): q_link, r_link = html.split('|') url, text = parse_link(q_link) self.assertIn(q_url, url) self.assertIn(q_text, text) url, text = parse_link(r_link) self.assertIn(r_url, url) self.assertIn('Reorder', text)
def test_without_urls(self): # what actions are available depend on whether the dform.urls.py file # is used or not, and need to be able to change if it isn't there; it # is always there when testing, so we'll need to use mock to force the # code to execute self.initiate() survey_admin = SurveyAdmin(Survey, self.site) version_admin = SurveyVersionAdmin(SurveyVersion, self.site) group_admin = AnswerGroupAdmin(AnswerGroup, self.site) survey, _, tx, _, _, _, _, _, _ = sample_survey() with patch('dform.admin.reverse') as mock_reverse: mock_reverse.side_effect = fake_reverse html = self.field_value(survey_admin, survey, 'show_actions') actions = html.split(',') self.assertEqual(1, len(actions)) url, text = parse_link(html) self.assertEqual(text, 'Edit Survey') # -- SurveyVersion of same test result = self.field_value(version_admin, survey.latest_version, 'show_actions') self.assertEqual(1, len(actions)) url, text = parse_link(html) self.assertEqual(text, 'Edit Survey') # -- repeat with locked survey answer = 'answer and stuff and things' ag = AnswerGroup.objects.create( survey_version=survey.latest_version) survey.answer_question(tx, ag, answer) html = self.field_value(survey_admin, survey, 'show_actions') actions = html.split(',') self.assertEqual(1, len(actions)) url, text = parse_link(html) self.assertEqual(text, 'New Version') # -- SurveyVersion of same test result = self.field_value(version_admin, survey.latest_version, 'show_actions') self.assertEqual('', result) # -- check actions on AG result = self.field_value(group_admin, ag, 'show_actions') self.assertEqual('', result)
def visit_admin_link(self, admin_model, instance, field_name, response_code=200, headers={}): """This method is used for testing links that are in the change list view of the django admin. For the given instance and field name, the HTML link tags in the column are parsed for a URL and then invoked with :class:`AdminToolsMixin.authed_get`. :param admin_model: Instance of a :class:`admin.ModelAdmin` object that is responsible for displaying the change list :param instance: Object instance that is the row in the admin change list :param field_name: Name of the field/column to containing the HTML link to get a URL from to visit :param response_code: Expected HTTP status code resulting from the call. The value of this is asserted. Defaults to 200. :param headers: Optional dictionary of headers to send in the request :returns: Django test ``Response`` object :raises AttributeError: If the column does not contain a URL that can be parsed """ html = self.field_value(admin_model, instance, field_name) url, text = parse_link(html) if not url: raise AttributeError('href could not be parsed from *%s*' % html) return self.authed_get(url, response_code=response_code, headers=headers)
def test_without_urls(self): # what actions are available depend on whether the dform.urls.py file # is used or not, and need to be able to change if it isn't there; it # is always there when testing, so we'll need to use mock to force the # code to execute self.initiate() survey_admin = SurveyAdmin(Survey, self.site) version_admin = SurveyVersionAdmin(SurveyVersion, self.site) group_admin = AnswerGroupAdmin(AnswerGroup, self.site) survey, fields = create_survey() with patch("dform.admin.reverse") as mock_reverse: mock_reverse.side_effect = fake_reverse html = self.field_value(survey_admin, survey, "show_actions") actions = html.split(",") self.assertEqual(2, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, "Edit Survey") url, text = parse_link(actions[1]) self.assertEqual(text, "Show Links") # -- SurveyVersion of same test result = self.field_value(version_admin, survey.latest_version, "show_actions") self.assertEqual(2, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, "Edit Survey") url, text = parse_link(actions[1]) self.assertEqual(text, "Show Links") # -- repeat with locked survey answer = "answer and stuff and things" ag = AnswerGroup.factory(survey_version=survey.latest_version) survey.answer_question(fields["text"], ag, answer) html = self.field_value(survey_admin, survey, "show_actions") actions = html.split(",") self.assertEqual(2, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, "New Version") url, text = parse_link(actions[1]) self.assertEqual(text, "Show Links") # -- SurveyVersion of same test html = self.field_value(version_admin, survey.latest_version, "show_actions") actions = html.split(",") self.assertEqual(1, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, "Show Links") # -- check actions on AG result = self.field_value(group_admin, ag, "show_actions") self.assertEqual("", result)
def test_admin_obj_mixin(self): # setup the admin site and id self.initiate() inner_admin = InnerAdmin(Inner, self.site) outer_admin = OuterAdmin(Outer, self.site) n1 = Nested.objects.create(name='n1') i1 = Inner.objects.create(name='i1', nested=n1) o1 = Outer.objects.create(name='o1', inner=i1) # check the basic __str__ named link from Inner to Nested html = self.field_value(inner_admin, i1, 'show_nested') url, text = parse_link(html) self.assertEqual('Nested(id=1 n1)', text) self.assertEqual('/admin/tests/nested/?id__exact=1', url) # check the template based name link of Inner from Outer html = self.field_value(outer_admin, o1, 'show_inner') url, text = parse_link(html) self.assertEqual('Inner.id=1', text) self.assertEqual('/admin/tests/inner/?id__exact=1', url) # check the double dereferenced Nested from Outer html = self.field_value(outer_admin, o1, 'show_nested') url, text = parse_link(html) self.assertEqual('Nested(id=1 n1)', text) self.assertEqual('/admin/tests/nested/?id__exact=1', url) # check the title got set correctly label = label_for_field('show_inner', o1, outer_admin) self.assertEqual(label, 'My Inner') # check that empty values work properly o2 = Outer.objects.create(name='o2') result = self.field_value(outer_admin, o2, 'show_nested') self.assertEqual('', result) i2 = Inner.objects.create(name='i2') o2.inner = i2 o2.save() result = self.field_value(outer_admin, o2, 'show_nested') self.assertEqual('', result)
def test_answer(self): self.initiate() answer_admin = AnswerAdmin(Answer, self.site) answer_group_admin = AnswerGroupAdmin(AnswerGroup, self.site) survey = Survey.objects.create(name='survey') q1 = survey.add_question(Text, '1st question') # create a basic AnswerGroup and test its results ag1 = AnswerGroup.objects.create(survey_version=survey.latest_version) result = self.field_value(answer_group_admin, ag1, 'show_data') self.assertEqual('', result) html = self.field_value(answer_group_admin, ag1, 'show_questions') url, text = parse_link(html) self.assertEqual('1 Question', text) result = self.field_value(answer_group_admin, ag1, 'show_answers') self.assertEqual('', result) # add a question and check for change q2 = survey.add_question(Text, '2nd question') html = self.field_value(answer_group_admin, ag1, 'show_questions') url, text = parse_link(html) self.assertEqual('2 Questions', text) # add an answer and check for change answer = survey.answer_question(q1, ag1, 'stuff') html = self.field_value(answer_group_admin, ag1, 'show_answers') url, text = parse_link(html) self.assertEqual('1 Answer', text) # add another answer answer = survey.answer_question(q2, ag1, 'stuff') html = self.field_value(answer_group_admin, ag1, 'show_answers') url, text = parse_link(html) self.assertEqual('2 Answers', text)
def test_answer(self): self.initiate() answer_group_admin = AnswerGroupAdmin(AnswerGroup, self.site) survey = Survey.factory(name='survey') q1 = survey.add_question(Text, '1st question') # create a basic AnswerGroup and test its results ag1 = AnswerGroup.factory(survey_version=survey.latest_version) result = self.field_value(answer_group_admin, ag1, 'show_data') self.assertEqual('', result) html = self.field_value(answer_group_admin, ag1, 'show_questions') url, text = parse_link(html) self.assertEqual('1 Question', text) result = self.field_value(answer_group_admin, ag1, 'show_answers') self.assertEqual('', result) # add a question and check for change q2 = survey.add_question(Text, '2nd question') html = self.field_value(answer_group_admin, ag1, 'show_questions') url, text = parse_link(html) self.assertEqual('2 Questions', text) # add an answer and check for change survey.answer_question(q1, ag1, 'stuff') html = self.field_value(answer_group_admin, ag1, 'show_answers') url, text = parse_link(html) self.assertEqual('1 Answer', text) # add another answer survey.answer_question(q2, ag1, 'stuff') html = self.field_value(answer_group_admin, ag1, 'show_answers') url, text = parse_link(html) self.assertEqual('2 Answers', text)
def test_answer(self): self.initiate() answer_group_admin = AnswerGroupAdmin(AnswerGroup, self.site) survey = Survey.factory(name="survey") q1 = survey.add_question(Text, "1st question") # create a basic AnswerGroup and test its results ag1 = AnswerGroup.factory(survey_version=survey.latest_version) result = self.field_value(answer_group_admin, ag1, "show_data") self.assertEqual("", result) html = self.field_value(answer_group_admin, ag1, "show_questions") url, text = parse_link(html) self.assertEqual("1 Question", text) result = self.field_value(answer_group_admin, ag1, "show_answers") self.assertEqual("", result) # add a question and check for change q2 = survey.add_question(Text, "2nd question") html = self.field_value(answer_group_admin, ag1, "show_questions") url, text = parse_link(html) self.assertEqual("2 Questions", text) # add an answer and check for change survey.answer_question(q1, ag1, "stuff") html = self.field_value(answer_group_admin, ag1, "show_answers") url, text = parse_link(html) self.assertEqual("1 Answer", text) # add another answer survey.answer_question(q2, ag1, "stuff") html = self.field_value(answer_group_admin, ag1, "show_answers") url, text = parse_link(html) self.assertEqual("2 Answers", text)
def _assert_link(self, html, url, text): u, t = parse_link(html) self.assertIn(url, u) self.assertIn(text, t)
def test_without_urls(self): # what actions are available depend on whether the dform.urls.py file # is used or not, and need to be able to change if it isn't there; it # is always there when testing, so we'll need to use mock to force the # code to execute self.initiate() survey_admin = SurveyAdmin(Survey, self.site) version_admin = SurveyVersionAdmin(SurveyVersion, self.site) group_admin = AnswerGroupAdmin(AnswerGroup, self.site) survey, fields = create_survey() with patch('dform.admin.reverse') as mock_reverse: mock_reverse.side_effect = fake_reverse html = self.field_value(survey_admin, survey, 'show_actions') actions = html.split(',') self.assertEqual(2, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, 'Edit Survey') url, text = parse_link(actions[1]) self.assertEqual(text, 'Show Links') # -- SurveyVersion of same test result = self.field_value(version_admin, survey.latest_version, 'show_actions') self.assertEqual(2, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, 'Edit Survey') url, text = parse_link(actions[1]) self.assertEqual(text, 'Show Links') # -- repeat with locked survey answer = 'answer and stuff and things' ag = AnswerGroup.factory(survey_version=survey.latest_version) survey.answer_question(fields['text'], ag, answer) html = self.field_value(survey_admin, survey, 'show_actions') actions = html.split(',') self.assertEqual(2, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, 'New Version') url, text = parse_link(actions[1]) self.assertEqual(text, 'Show Links') # -- SurveyVersion of same test html = self.field_value(version_admin, survey.latest_version, 'show_actions') actions = html.split(',') self.assertEqual(1, len(actions)) url, text = parse_link(actions[0]) self.assertEqual(text, 'Show Links') # -- check actions on AG result = self.field_value(group_admin, ag, 'show_actions') self.assertEqual('', result)
def assert_version_link(self, link): url, text = parse_link(link) self.assertEqual("New Version", text) self.authed_get(url, response_code=302)
def assert_change_link(self, link): url, text = parse_link(link) self.assertEqual("Answer Survey", text) response = self.authed_get(url) self.assertTemplateUsed(response, "dform/survey.html")
def assert_sample_link(self, link): url, text = parse_link(link) self.assertEqual("View Sample", text) response = self.authed_get(url) self.assertTemplateUsed(response, "dform/survey.html")
def assert_show_link(self, link): url, text = parse_link(link) self.assertEqual("Show Links", text) response = self.authed_get(url) self.assertTemplateUsed(response, "dform/links_survey.html")
def assert_edit_link(self, link): url, text = parse_link(link) self.assertEqual("Edit Survey", text) response = self.authed_get(url) self.assertTemplateUsed(response, "dform/edit_survey.html")
def assert_change_link(self, link): url, text = parse_link(link) self.assertEqual('Answer Survey', text) response = self.authed_get(url) self.assertTemplateUsed(response, 'dform/survey.html')
def assert_version_link(self, link): url, text = parse_link(link) self.assertEqual('New Version', text) self.authed_get(url, response_code=302)
def assert_show_link(self, link): url, text = parse_link(link) self.assertEqual('Show Links', text) response = self.authed_get(url) self.assertTemplateUsed(response, 'dform/links_survey.html')
def test_models(self): self.initiate() compile_filename, variables_filename = create_fakestrap(self.dir_name) version = Version.factory("v1", variables_filename, compile_filename) # verify load/compile worked self.assertEqual(version._store, VARS_JSON) # -- test Version Admin version_admin = VersionAdmin(Version, self.site) links = self.field_value(version_admin, version, "show_actions") show, create = links.split(",") # test Show Vars link url, text = parse_link(show) self.assertEqual("Show Variables", text) expected = ordered_json(version.get_vars().base_to_json()) response = self.authed_get(url) result = ordered_json(response.content.decode("utf-8")) self.assertEqual(expected, result) # test Create Sheet link url, text = parse_link(create) self.assertEqual("Create Sheet", text) self.authed_get(url, response_code=302) sheets = Sheet.objects.all() self.assertEqual(1, len(sheets)) self.assertEqual(version, sheets[0].version) self.assertEqual("new sheet", sheets[0].name) # do it again, check the sheet name incrementer self.authed_get(url, response_code=302) sheets = Sheet.objects.all() self.assertEqual(2, len(sheets)) self.assertEqual("new sheet 1", sheets[1].name) # -- test Sheet output_dir = os.path.join(self.dir_name, "output") os.mkdir(output_dir) with override_settings(BSEDITOR_DEPLOY_DIR=output_dir, BSEDITOR_TRACK_LAST_COMPILE=True): sheet = Sheet.factory("s1", version, SASS_FILE_CUSTOMIZED_DICT) self.assertEqual("s1.css", sheet.filename) # check states before we deploy sheet_admin = SheetAdmin(Sheet, self.site) result = self.field_value(sheet_admin, sheet, "show_filedate") self.assertEqual("<i>no file</i>", result) expected = os.path.join(output_dir, "s1.css") self.assertEqual(expected, sheet.full_filename) self.assertEqual(None, sheet.last_deploy) # -- test deployment sheet.deploy() # check states after deploy result = self.field_value(sheet_admin, sheet, "show_filedate") self.assertEqual(sheet.last_deploy, result) # check last compile record is there fname = os.path.join(output_dir, "last_compile.txt") os.path.isfile(fname) # verify the created CSS file fname = os.path.join(output_dir, "s1.css") with open(fname) as f: result = f.read() self.assertEqual(EXPECTED_SASS_FILE, result) self.assertNotEqual(None, sheet.last_deploy) # -- test deployment hooks hook = "bseditor.tests.test_models.fake_deploy_hook" with override_settings(BSEDITOR_COLLECT_ON_DEPLOY=True): # check with custom hook with override_settings(BSEDITOR_COLLECT_HOOK=hook): with self.assertRaises(GotHereError): sheet.deploy() # check with default hook # call_command = 'django.core.management.call_command' call_command = "bseditor.models.call_command" with mock.patch(call_command) as patched: sheet.deploy() self.assertTrue(patched.called) self.assertEqual(patched.call_args, (("collectstatic", "--noinput"),)) # -- test templatetag t = """ {% load bseditortags %} {% sheetpath 's1' %} """ template = Template(t) result = template.render(Context({})).strip() self.assertEqual("/static/%s" % sheet.filename, result) t = ( """ {%% load bseditortags %%} {%% sheetpath %s %%} """ % sheet.id ) template = Template(t) result = template.render(Context({})).strip() self.assertEqual("/static/%s" % sheet.filename, result) # -- test SheetAdmin links = self.field_value(sheet_admin, sheet, "show_actions") edit, preview, deploy = links.split(",") # test Edit link url, text = parse_link(edit) self.assertEqual("Edit", text) response = self.authed_get(url) self.assertTemplateUsed(response, "bseditor/edit_sheet.html") # test Preview link self.assertEqual(0, PreviewSheet.objects.count()) url, text = parse_link(preview) self.assertEqual("Preview", text) self.authed_get(url, response_code=302) previews = PreviewSheet.objects.all() self.assertEqual(1, len(previews)) self.assertEqual(sheet, previews[0].sheet) # do it again, last time created it, this time should fetch it self.authed_get(url, response_code=302) previews = PreviewSheet.objects.all() self.assertEqual(1, len(previews)) self.assertEqual(sheet, previews[0].sheet) # test Deploy link url, text = parse_link(deploy) self.assertEqual("Deploy", text) response = self.authed_get(url, follow=True) results = messages_from_response(response) self.assertEqual(1, len(results)) self.assertEqual("Deployed s1.css", results[0][0]) self.assertEqual(messages.SUCCESS, results[0][1]) # retest with simulated problem with mock.patch("bseditor.views.Sheet.deploy") as patched: patched.side_effect = KeyError() response = self.silent_authed_get(url, follow=True) results = messages_from_response(response) self.assertEqual(1, len(results)) self.assertEqual(messages.ERROR, results[0][1]) # -- test PreviewSheet preview = PreviewSheet.factory(sheet, SASS_FILE_OVERRIDES_DICT) self.assertEqual(EXPECTED_SASS_PREVIEW_FILE, preview.content()) # -- misc coverage tests Sheet.factory("s2", version) version.get_vars() str(version) str(sheet) str(preview)
def assert_edit_link(self, link): url, text = parse_link(link) self.assertEqual('Edit Survey', text) response = self.authed_get(url) self.assertTemplateUsed(response, 'dform/edit_survey.html')
def test_models(self): self.initiate() compile_filename, variables_filename = create_fakestrap(self.dir_name) version = Version.factory('v1', variables_filename, compile_filename) # verify load/compile worked self.assertEqual(version._store, VARS_JSON) # -- test Version Admin version_admin = VersionAdmin(Version, self.site) links = self.field_value(version_admin, version, 'show_actions') show, create = links.split(',') # test Show Vars link url, text = parse_link(show) self.assertEqual('Show Variables', text) expected = ordered_json(version.get_vars().base_to_json()) response = self.authed_get(url) result = ordered_json(response.content.decode('utf-8')) self.assertEqual(expected, result) # test Create Sheet link url, text = parse_link(create) self.assertEqual('Create Sheet', text) self.authed_get(url, response_code=302) sheets = Sheet.objects.all() self.assertEqual(1, len(sheets)) self.assertEqual(version, sheets[0].version) self.assertEqual('new sheet', sheets[0].name) # do it again, check the sheet name incrementer self.authed_get(url, response_code=302) sheets = Sheet.objects.all() self.assertEqual(2, len(sheets)) self.assertEqual('new sheet 1', sheets[1].name) # -- test Sheet output_dir = os.path.join(self.dir_name, 'output') os.mkdir(output_dir) with override_settings(BSEDITOR_DEPLOY_DIR=output_dir, BSEDITOR_TRACK_LAST_COMPILE=True): sheet = Sheet.factory('s1', version, SASS_FILE_CUSTOMIZED_DICT) self.assertEqual('s1.css', sheet.filename) # check states before we deploy sheet_admin = SheetAdmin(Sheet, self.site) result = self.field_value(sheet_admin, sheet, 'show_filedate') self.assertEqual('<i>no file</i>', result) expected = os.path.join(output_dir, 's1.css') self.assertEqual(expected, sheet.full_filename) self.assertEqual(None, sheet.last_deploy) # -- test deployment sheet.deploy() # check states after deploy result = self.field_value(sheet_admin, sheet, 'show_filedate') self.assertEqual(sheet.last_deploy, result) # check last compile record is there fname = os.path.join(output_dir, 'last_compile.txt') os.path.isfile(fname) # verify the created CSS file fname = os.path.join(output_dir, 's1.css') with open(fname) as f: result = f.read() self.assertEqual(EXPECTED_SASS_FILE, result) self.assertNotEqual(None, sheet.last_deploy) # -- test deployment hooks hook = 'bseditor.tests.test_models.fake_deploy_hook' with override_settings(BSEDITOR_COLLECT_ON_DEPLOY=True): # check with custom hook with override_settings(BSEDITOR_COLLECT_HOOK=hook): with self.assertRaises(GotHereError): sheet.deploy() # check with default hook #call_command = 'django.core.management.call_command' call_command = 'bseditor.models.call_command' with mock.patch(call_command) as patched: sheet.deploy() self.assertTrue(patched.called) self.assertEqual(patched.call_args, (('collectstatic', '--noinput'), )) # -- test templatetag t = """ {% load bseditortags %} {% sheetpath 's1' %} """ template = Template(t) result = template.render(Context({})).strip() self.assertEqual('/static/%s' % sheet.filename, result) t = """ {%% load bseditortags %%} {%% sheetpath %s %%} """ % sheet.id template = Template(t) result = template.render(Context({})).strip() self.assertEqual('/static/%s' % sheet.filename, result) # -- test SheetAdmin links = self.field_value(sheet_admin, sheet, 'show_actions') edit, preview, deploy = links.split(',') # test Edit link url, text = parse_link(edit) self.assertEqual('Edit', text) response = self.authed_get(url) self.assertTemplateUsed(response, 'bseditor/edit_sheet.html') # test Preview link self.assertEqual(0, PreviewSheet.objects.count()) url, text = parse_link(preview) self.assertEqual('Preview', text) self.authed_get(url, response_code=302) previews = PreviewSheet.objects.all() self.assertEqual(1, len(previews)) self.assertEqual(sheet, previews[0].sheet) # do it again, last time created it, this time should fetch it self.authed_get(url, response_code=302) previews = PreviewSheet.objects.all() self.assertEqual(1, len(previews)) self.assertEqual(sheet, previews[0].sheet) # test Deploy link url, text = parse_link(deploy) self.assertEqual('Deploy', text) response = self.authed_get(url, follow=True) results = messages_from_response(response) self.assertEqual(1, len(results)) self.assertEqual('Deployed s1.css', results[0][0]) self.assertEqual(messages.SUCCESS, results[0][1]) # retest with simulated problem with mock.patch('bseditor.views.Sheet.deploy') as patched: patched.side_effect = KeyError() response = self.silent_authed_get(url, follow=True) results = messages_from_response(response) self.assertEqual(1, len(results)) self.assertEqual(messages.ERROR, results[0][1]) # -- test PreviewSheet preview = PreviewSheet.factory(sheet, SASS_FILE_OVERRIDES_DICT) self.assertEqual(EXPECTED_SASS_PREVIEW_FILE, preview.content()) # -- misc coverage tests Sheet.factory('s2', version) version.get_vars() str(version) str(sheet) str(preview)
def assert_sample_link(self, link): url, text = parse_link(link) self.assertEqual('View Sample', text) response = self.authed_get(url) self.assertTemplateUsed(response, 'dform/survey.html')