예제 #1
0
    def move(self):
        a = self.klass.objects.create(name='a', group='y')
        self.klass.objects.create(name='b', group='y')
        d = self.klass.objects.create(name='d', group='y')
        c = self.klass.objects.create(name='c', group='y')

        # test a simple move
        #import pudb; pudb.set_trace()
        c.rank -= 1
        c.save()
        self.assertValues(a.grouped_filter(), 'a,b,c,d')

        # test an out of bounds move
        d = refetch(d)
        d.rank += 5
        d.save()
        d = refetch(d)
        self.assertEqual(4, d.rank)
        self.assertValues(a.grouped_filter(), 'a,b,c,d')

        # test moving in the list
        a = refetch(a)
        a.rank += 2
        a.save()
        self.assertValues(a.grouped_filter(), 'b,c,a,d')

        a = refetch(a)
        a.rank = 0
        a.save()
        a = refetch(a)
        self.assertEqual(1, a.rank)
        self.assertValues(a.grouped_filter(), 'a,b,c,d')
예제 #2
0
    def repack(self):
        a = self.klass.objects.create(name='a', group='y')
        b = self.klass.objects.create(name='b', group='y')
        c = self.klass.objects.create(name='c', group='y')
        d = self.klass.objects.create(name='d', group='y')

        b.delete()
        a.repack()
        a = refetch(a)
        self.assertEqual(1, a.rank)
        c = refetch(c)
        self.assertEqual(2, c.rank)
        d = refetch(d)
        self.assertEqual(3, d.rank)
        self.assertValues(a.grouped_filter(), 'a,c,d')
예제 #3
0
    def admin(self):
        self.initiate()

        a = self.klass.objects.create(name='a', group='y')
        b = self.klass.objects.create(name='b', group='y')
        c = self.klass.objects.create(name='c', group='y')

        rank_admin = RankAdmin(self.klass, self.site)

        self.assertEqual('', self.field_value(rank_admin, a, 'move_up'))
        self.assertNotEqual('', self.field_value(rank_admin, b, 'move_up'))
        self.assertNotEqual('', self.field_value(rank_admin, c, 'move_up'))

        self.assertNotEqual('', self.field_value(rank_admin, a, 'move_down'))
        self.assertNotEqual('', self.field_value(rank_admin, b, 'move_down'))
        self.assertEqual('', self.field_value(rank_admin, c, 'move_down'))

        # use the view to move b up one
        headers = {
            'HTTP_REFERER':'/admin/',
        }
        self.visit_admin_link(rank_admin, b, 'move_up', response_code=302, 
            headers=headers)
        self.assertValues(a.grouped_filter(), 'b,a,c')

        # use the view to move a down one
        a = refetch(a)
        self.visit_admin_link(rank_admin, a, 'move_down', response_code=302,
            headers=headers)
        self.assertValues(a.grouped_filter(), 'b,c,a')
예제 #4
0
 def test_validating_mixin(self):
     # Validator overrides full_clean() to increment its counter,
     # full_clean() is called on every save, so after creation and save the
     # value should be 2
     v = Validator.objects.create()
     v.save()
     v = refetch(v)
     self.assertEqual(2, v.counter)
예제 #5
0
    def test_on_success(self):
        survey = Survey.factory(name="test")
        version = survey.latest_version

        with self.assertRaises(AttributeError):
            version.on_success()

        with self.settings(DFORM_SUCCESS_REDIRECT="/one/"):
            self.assertEqual("/one/", version.on_success())

            survey.success_redirect = "/two/"
            survey.save()
            survey = refetch(survey)
            version = refetch(version)
            self.assertEqual("/two/", version.on_success())

            version.success_redirect = "/three/"
            self.assertEqual("/three/", version.on_success())
예제 #6
0
    def test_on_success(self):
        survey = Survey.objects.create(name='test')
        version = survey.latest_version

        with self.assertRaises(AttributeError):
            version.on_success()

        with self.settings(DFORM_SUCCESS_REDIRECT='/one/'):
            self.assertEqual('/one/', version.on_success())

            survey.success_redirect = '/two/'
            survey.save()
            survey = refetch(survey)
            version = refetch(version)
            self.assertEqual('/two/', version.on_success())

            version.success_redirect = '/three/'
            self.assertEqual('/three/', version.on_success())
예제 #7
0
    def test_on_success(self):
        survey = Survey.factory(name='test')
        version = survey.latest_version

        with self.assertRaises(AttributeError):
            version.on_success()

        with self.settings(DFORM_SUCCESS_REDIRECT='/one/'):
            self.assertEqual('/one/', version.on_success())

            survey.success_redirect = '/two/'
            survey.save()
            survey = refetch(survey)
            version = refetch(version)
            self.assertEqual('/two/', version.on_success())

            version.success_redirect = '/three/'
            self.assertEqual('/three/', version.on_success())
예제 #8
0
    def test_refetch(self):
        link = Link.objects.create(url='url', text='text')
        link.text = 'foo'

        link = refetch(link)
        self.assertEqual('url', link.url)
        self.assertEqual('text', link.text)

        link.text = 'foo'
        link = refetch_for_update(link)
        self.assertEqual('url', link.url)
        self.assertEqual('text', link.text)
    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'])
예제 #10
0
 def test_counter(self):
     count = Counter.objects.create(name='foo')
     Counter.increment('foo')
     count = refetch(count)
     self.assertEqual(1, count.value)
    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'])