mardi 28 avril 2015

Django Unit Test: "You don't have permission to change anything" when Client.post

I am having a problem when I run a unit test using the Client object and trying to post to an create view on admin.

When I do post(uri, data, follow=True) on a create view, it doesn't save anything and re-render the form with a message that says: "You don't have permission to change anything".

Here is the code:

class ProductTests(TestCase):

    def setUp(self):
        user = User.objects.create_superuser('admin', '', 'admin')
        self.client = Client(enforce_csrf_checks=True)
        self.uom = UOM.objects.get(pk=1)

    def test_consumable_add(self):
        login = self.client.login(username='admin', password='admin')

        uri = construct_xadmin_url(Consumable(), 'add')
        response = self.client.post(uri, {'name': 'Consumable',
                                          'uom': self.uom.pk},
                                    follow=True)

        #f = open('file.html', 'w')
        #f.write(response.content)
        #f.close()

        self.assertContains(response=response,text='was added success')

        c = Consumable.objects.all().last()
        self.assertIsNotNone(c, 'Consumable not added')
        self.assertTrue(c.tags.count() > 0, 'Consumable has not tags', )

    def tearDown(self):
        self.client.logout()

When I run this code by hand inside shell, it works perfectly.

What could be happening?

I am using Django 1.6, django-nose and django-xadmin.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire