Пример #1
0
class SnippetAdminTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.model_admin = SnippetAdmin(Snippet, None)
        self.model_admin.admin_site = Mock()
        self.user = User.objects.get_or_create(username='******',
                                               email='*****@*****.**')[0]

    def test_save_as_published(self):
        request = self.factory.post('/',
                                    data={
                                        'name': 'test',
                                        'template': 'foo',
                                        'published': 'on',
                                        'ready_for_review': 'on',
                                        '_saveasnew': True
                                    })
        request.user = self.user

        with patch('snippets.base.admin.admin.ModelAdmin.change_view'
                   ) as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999, '', None)
            request = change_view_mock.call_args[0][0]
            self.assertTrue('published' not in request.POST)
            self.assertTrue('ready_for_review' not in request.POST)

    def test_normal_save_published(self):
        """Test that normal save doesn't alter 'published' attribute."""
        request = self.factory.post('/',
                                    data={
                                        'name': 'test',
                                        'template': 'foo',
                                        'ready_for_review': 'foo',
                                        'published': 'foo'
                                    })
        request.user = self.user

        with patch('snippets.base.admin.admin.ModelAdmin.change_view'
                   ) as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999, '', None)
            request = change_view_mock.call_args[0][0]
            self.assertEqual(request.POST['published'], 'foo')
            self.assertEqual(request.POST['ready_for_review'], 'foo')
Пример #2
0
class SnippetAdminTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.model_admin = SnippetAdmin(Snippet, None)
        self.model_admin.admin_site = Mock()
        self.user = User.objects.get_or_create(username='******', email='*****@*****.**')[0]

    def test_save_as_published(self):
        request = self.factory.post('/', data={
            'name': 'test',
            'template': 'foo',
            'published': 'on',
            'ready_for_review': 'on',
            '_saveasnew': True
        })
        request.user = self.user

        with patch('snippets.base.admin.admin.ModelAdmin.change_view') as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999, '', None)
            request = change_view_mock.call_args[0][0]
            self.assertTrue('published' not in request.POST)
            self.assertTrue('ready_for_review' not in request.POST)

    def test_normal_save_published(self):
        """Test that normal save doesn't alter 'published' attribute."""
        request = self.factory.post('/', data={
            'name': 'test',
            'template': 'foo',
            'ready_for_review': 'foo',
            'published': 'foo'
        })
        request.user = self.user

        with patch('snippets.base.admin.admin.ModelAdmin.change_view') as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999, '', None)
            request = change_view_mock.call_args[0][0]
            self.assertEqual(request.POST['published'], 'foo')
            self.assertEqual(request.POST['ready_for_review'], 'foo')
class SnippetAdminTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.model_admin = SnippetAdmin(Snippet, None)
        self.model_admin.admin_site = Mock()

    def _save_model(self, snippet, form):
        """Call SnippetAdmin.save_model for the given snippet instance."""
        request = self.factory.post('/url', {})
        form.is_valid()  # Generate cleaned_data.
        self.model_admin.save_model(request, snippet, form, True)

    def test_save_model_locales(self):
        """
        save_model should delete any locales that were removed from the snippet
        and save any locales that were added.
        """
        en_us = SnippetLocale(locale='en-us')
        fr = SnippetLocale(locale='fr')
        snippet = SnippetFactory.create(locale_set=[en_us, fr])
        data = {
            'name': 'test',
            'data': '{}',
            'template': snippet.template.id,
            'locales': ['en-us', 'de'],
            'priority': 0,
            'weight': 100,
        }

        form = SnippetAdminForm(data, instance=snippet)
        self._save_model(snippet, form)

        snippet = Snippet.objects.get(pk=snippet.pk)
        locales = (l.locale for l in snippet.locale_set.all())
        self.assertEqual(set(locales), set(('en-us', 'de')))

    def test_no_locales(self):
        """
        If the form being saved has no locale field, do not alter the snippet's
        locale.
        """
        en_us = SnippetLocale(locale='en-us')
        fr = SnippetLocale(locale='fr')
        snippet = SnippetFactory.create(locale_set=[en_us, fr])
        data = {
            'name': 'test',
            'data': '{}',
            'template': snippet.template.id,
            'priority': 0,
            'weight': 100,
        }

        # FormClass has no locale field.
        class FormClass(ModelForm):
            class Meta:
                model = Snippet
                fields = ('name', 'data', 'template', 'priority', 'weight')
        form = FormClass(data, instance=snippet)
        self._save_model(snippet, form)

        snippet = Snippet.objects.get(pk=snippet.pk)
        locales = (l.locale for l in snippet.locale_set.all())
        self.assertEqual(set(locales), set(('en-us', 'fr')))

    def test_save_as_disabled(self):
        request = self.factory.post('/', data={
            'name': 'test',
            'template': 'foo',
            'disabled': u'off',
            '_saveasnew': True
        })

        with patch('snippets.base.admin.BaseModelAdmin.change_view') as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999)
            request = change_view_mock.call_args[0][0]
            self.assertEqual(request.POST['disabled'], u'on')

    def test_normal_save_disabled(self):
        """Test that normal save doesn't alter 'disabled' attribute."""
        request = self.factory.post('/', data={
            'name': 'test',
            'template': 'foo',
            'disabled': u'foo'
        })

        with patch('snippets.base.admin.BaseModelAdmin.change_view') as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999)
            request = change_view_mock.call_args[0][0]
            self.assertEqual(request.POST['disabled'], u'foo')
Пример #4
0
class SnippetAdminTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.model_admin = SnippetAdmin(Snippet, None)
        self.model_admin.admin_site = Mock()

    def _save_model(self, snippet, form):
        """Call SnippetAdmin.save_model for the given snippet instance."""
        request = self.factory.post('/url', {})
        form.is_valid()  # Generate cleaned_data.
        self.model_admin.save_model(request, snippet, form, True)

    def test_save_model_locales(self):
        """
        save_model should delete any locales that were removed from the snippet
        and save any locales that were added.
        """
        en_us = SnippetLocale(locale='en-us')
        fr = SnippetLocale(locale='fr')
        snippet = SnippetFactory.create(locale_set=[en_us, fr])
        data = {
            'name': 'test',
            'data': '{}',
            'template': snippet.template.id,
            'locales': ['en-us', 'de'],
            'priority': 0,
            'weight': 100,
        }

        form = SnippetAdminForm(data, instance=snippet)
        self._save_model(snippet, form)

        snippet = Snippet.objects.get(pk=snippet.pk)
        locales = (l.locale for l in snippet.locale_set.all())
        eq_(set(locales), set(('en-us', 'de')))

    def test_no_locales(self):
        """
        If the form being saved has no locale field, do not alter the snippet's
        locale.
        """
        en_us = SnippetLocale(locale='en-us')
        fr = SnippetLocale(locale='fr')
        snippet = SnippetFactory.create(locale_set=[en_us, fr])
        data = {
            'name': 'test',
            'data': '{}',
            'template': snippet.template.id,
            'priority': 0,
            'weight': 100,
        }

        # FormClass has no locale field.
        class FormClass(ModelForm):
            class Meta:
                model = Snippet

        form = FormClass(data, instance=snippet)
        self._save_model(snippet, form)

        snippet = Snippet.objects.get(pk=snippet.pk)
        locales = (l.locale for l in snippet.locale_set.all())
        eq_(set(locales), set(('en-us', 'fr')))

    def test_save_as_disabled(self):
        request = self.factory.post('/',
                                    data={
                                        'name': 'test',
                                        'template': 'foo',
                                        'disabled': u'off',
                                        '_saveasnew': True
                                    })

        with patch('snippets.base.admin.BaseModelAdmin.change_view'
                   ) as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999)
            request = change_view_mock.call_args[0][0]
            eq_(request.POST['disabled'], u'on')

    def test_normal_save_disabled(self):
        """Test that normal save doesn't alter 'disabled' attribute."""
        request = self.factory.post('/',
                                    data={
                                        'name': 'test',
                                        'template': 'foo',
                                        'disabled': u'foo'
                                    })

        with patch('snippets.base.admin.BaseModelAdmin.change_view'
                   ) as change_view_mock:
            self.model_admin.change_view(request, 999)
            change_view_mock.assert_called_with(request, 999)
            request = change_view_mock.call_args[0][0]
            eq_(request.POST['disabled'], u'foo')