def test_messages_from_response(self): response = self.authed_get('/awl_test_views/test_view_for_messages/') m = messages_from_response(response) self.assert_messages(m) # -- test handling with cookies # wipe out the context, so that the cookie that was set is what gets # processed response.context = None m = messages_from_response(response) self.assert_messages(m) # -- test handling bad response objects response.cookies.pop('messages') m = messages_from_response(response) self.assertEqual(0, len(m)) # object without context m = messages_from_response({}) self.assertEqual(0, len(m)) # object with context but empty class Dummy(object): pass d = Dummy() d.context = {} m = messages_from_response(d) self.assertEqual(0, len(m)) # object with context without messages key d.context = {'foo':'bar'} m = messages_from_response(d) self.assertEqual(0, len(m))
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 test_views(self): self.initiate() compile_filename, variables_filename = create_fakestrap(self.dir_name) version = Version.factory('v1', variables_filename, compile_filename) sheet = Sheet.factory('s1', version, SASS_FILE_CUSTOMIZED_DICT) output_dir = os.path.join(self.dir_name, 'output') os.mkdir(output_dir) with override_settings(BSEDITOR_DEPLOY_DIR=output_dir): # -------------------------- # -- test ajax_save_sheet expected = copy.copy(sheet.get_vars().custom_values) expected['body-bg'] = '#aaa' payload = { 'name': sheet.name, 'custom': expected, } data = { 'payload': json.dumps(payload), } response = self.authed_post( '/bseditor/ajax_save_sheet/%s/' % sheet.id, data) results = messages_from_response(response) self.assertEqual(1, len(results)) self.assertEqual('Saved & deployed %s' % sheet.filename, results[0][0]) self.assertEqual(messages.SUCCESS, results[0][1]) sheet = refetch(sheet) self.assertEqual(expected, sheet.get_vars().custom_values) # force an error when saving to check error handling (error # message should be added to existing message queue) with mock.patch('bseditor.views.Sheet.save') as patched: patched.side_effect = KeyError() response = self.silent_authed_post( '/bseditor/ajax_save_sheet/%s/' % sheet.id, data) results = messages_from_response(response) self.assertEqual(2, len(results)) self.assertEqual(messages.ERROR, results[1][1]) # -------------------------- # -- ajax_colour_value view payload = { 'sass_variable': 'gray-base', 'version': version.id, 'overrides': { 'gray-base': '#00f', }, } data = { 'payload': json.dumps(payload), } response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) self.assertTrue(result['success']) self.assertEqual('#00f', result['colours']['gray-base']) self.assertEqual('#00f', result['colours']['text-color']) # -- ajax_colour_value failure modes # bad source in Version, compile error should return success=False payload['overrides'] = {} data = { 'payload': json.dumps(payload), } t = version._store mess = ordered_json(version._store) mess['sections']['Colors']['components']['gray-base']['value'] = '$foo' version._store = json.dumps(mess) version.save() response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) self.assertFalse(result['success']) version._store = t version.save() # bad dependency in Version should be ignored t = version._store mess = ordered_json(version._store) mess['sections']['Scaffolding']['components']['text-color']['value'] = \ 'foo $gray-base' version._store = json.dumps(mess) version.save() response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) # text-color should not be in result because we messed it up self.assertTrue(result['success']) self.assertEqual(1, len(result['colours'].keys())) self.assertEqual('#000', result['colours']['gray-base']) version._store = t version.save() # missing overrides key in call, should return success=False del payload['overrides'] data = { 'payload': json.dumps(payload), } response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) self.assertFalse(result['success']) # -------------------------- # -- preview_css view preview = PreviewSheet.factory(sheet, SASS_FILE_OVERRIDES_DICT) response = self.authed_get('/bseditor/preview_css/%s/' % preview.id) self.assertEqual(EXPECTED_SASS_PREVIEW_FILE, response.content.decode('utf-8')) # -------------------------- # -- preview_sheet view response = self.authed_get('/bseditor/preview_sheet/%s/' % preview.id) self.assertTemplateUsed(response, 'bseditor/preview_sheet.html') # -------------------------- # -- ajax_save_preview view # remove any PreviewSheets before first test PreviewSheet.objects.filter(sheet=sheet).delete() payload = {} data = { 'payload': json.dumps(payload), } response = self.authed_post( '/bseditor/ajax_save_preview/%s/' % preview.id, data) result = json.loads(response.content.decode('utf-8')) previews = PreviewSheet.objects.filter(sheet=sheet) self.assertEqual(1, len(previews)) self.assertTrue(result['success']) self.assertEqual('/bseditor/preview_sheet/%s/' % previews[0].id, result['preview_url']) # force an error when saving to check error handling with mock.patch('bseditor.views.PreviewSheet.save') as patched: patched.side_effect = KeyError() response = self.authed_post( '/bseditor/ajax_save_preview/%s/' % sheet.id, data) results = messages_from_response(response) result = json.loads(response.content.decode('utf-8')) self.assertFalse(result['success'])
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 test_views(self): self.initiate() compile_filename, variables_filename = create_fakestrap(self.dir_name) version = Version.factory('v1', variables_filename, compile_filename) sheet = Sheet.factory('s1', version, SASS_FILE_CUSTOMIZED_DICT) output_dir = os.path.join(self.dir_name, 'output') os.mkdir(output_dir) with override_settings(BSEDITOR_DEPLOY_DIR=output_dir): # -------------------------- # -- test ajax_save_sheet expected = copy.copy(sheet.get_vars().custom_values) expected['body-bg'] = '#aaa' payload = { 'name':sheet.name, 'custom':expected, } data = { 'payload':json.dumps(payload), } response = self.authed_post( '/bseditor/ajax_save_sheet/%s/' % sheet.id, data) results = messages_from_response(response) self.assertEqual(1, len(results)) self.assertEqual('Saved & deployed %s' % sheet.filename, results[0][0]) self.assertEqual(messages.SUCCESS, results[0][1]) sheet = refetch(sheet) self.assertEqual(expected, sheet.get_vars().custom_values) # force an error when saving to check error handling (error # message should be added to existing message queue) with mock.patch('bseditor.views.Sheet.save') as patched: patched.side_effect = KeyError() response = self.silent_authed_post( '/bseditor/ajax_save_sheet/%s/' % sheet.id, data) results = messages_from_response(response) self.assertEqual(2, len(results)) self.assertEqual(messages.ERROR, results[1][1]) # -------------------------- # -- ajax_colour_value view payload = { 'sass_variable':'gray-base', 'version':version.id, 'overrides':{ 'gray-base':'#00f', }, } data = { 'payload':json.dumps(payload), } response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) self.assertTrue(result['success']) self.assertEqual('#00f', result['colours']['gray-base']) self.assertEqual('#00f', result['colours']['text-color']) # -- ajax_colour_value failure modes # bad source in Version, compile error should return success=False payload['overrides'] = {} data = { 'payload':json.dumps(payload), } t = version._store mess = ordered_json(version._store) mess['sections']['Colors']['components']['gray-base']['value'] = '$foo' version._store = json.dumps(mess) version.save() response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) self.assertFalse(result['success']) version._store = t version.save() # bad dependency in Version should be ignored t = version._store mess = ordered_json(version._store) mess['sections']['Scaffolding']['components']['text-color']['value'] = \ 'foo $gray-base' version._store = json.dumps(mess) version.save() response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) # text-color should not be in result because we messed it up self.assertTrue(result['success']) self.assertEqual(1, len(result['colours'].keys())) self.assertEqual('#000', result['colours']['gray-base']) version._store = t version.save() # missing overrides key in call, should return success=False del payload['overrides'] data = { 'payload':json.dumps(payload), } response = self.authed_post('/bseditor/ajax_colour_value/', data) result = json.loads(response.content.decode('utf-8')) self.assertFalse(result['success']) # -------------------------- # -- preview_css view preview = PreviewSheet.factory(sheet, SASS_FILE_OVERRIDES_DICT) response = self.authed_get('/bseditor/preview_css/%s/' % preview.id) self.assertEqual(EXPECTED_SASS_PREVIEW_FILE, response.content.decode('utf-8')) # -------------------------- # -- preview_sheet view response = self.authed_get('/bseditor/preview_sheet/%s/' % preview.id) self.assertTemplateUsed(response, 'bseditor/preview_sheet.html') # -------------------------- # -- ajax_save_preview view # remove any PreviewSheets before first test PreviewSheet.objects.filter(sheet=sheet).delete() payload = {} data = { 'payload':json.dumps(payload), } response = self.authed_post( '/bseditor/ajax_save_preview/%s/' % preview.id, data) result = json.loads(response.content.decode('utf-8')) previews = PreviewSheet.objects.filter(sheet=sheet) self.assertEqual(1, len(previews)) self.assertTrue(result['success']) self.assertEqual('/bseditor/preview_sheet/%s/' % previews[0].id, result['preview_url']) # force an error when saving to check error handling with mock.patch('bseditor.views.PreviewSheet.save') as patched: patched.side_effect = KeyError() response = self.authed_post( '/bseditor/ajax_save_preview/%s/' % sheet.id, data) results = messages_from_response(response) result = json.loads(response.content.decode('utf-8')) self.assertFalse(result['success'])