lundi 25 septembre 2017

Django: http_permissions interfering with tests

I have a view that list records. If I'm already login, I can access it with no problems. If have not login yet and try to access the page, I get redirected to the login page. So far so good!

But during my tests I have the following issue: I am able to successfully login, but when I try to execute a get to list the records, I get a 403 (Permission denied) and a 302 (redirect, likely to login page). I don't understand what is going on. I know http_permissions is interfering with the tests, as I have commented the http_permissions and the test passed. Do I have to grant this http_permissions to the test user? Anyone can cast some light on this? Here is my code:

teleconsultoria/models.py

...
class Bibliografia(BaseModel):
    ativa = models.BooleanField(u'Ativa', default=True)
    link = models.CharField(u'Link', blank=True, max_length=2000,)
    nome = models.CharField(u'Nome', blank=False, max_length=255,)
    arquivo = SizeRestrictedFileField(
        u'PDF da Bibliografia', upload_to='bibliografia', blank=True, null=True,
    )   

    class Meta:
        verbose_name = u'Bibliografia'
        verbose_name_plural = u'Bibliografias'
        permissions = ( 
            ('ver_bibliografia', u'Pode ver bibliografia'),
            ('criar_bibliografia', u'Pode criar bibliografia'),
            ('alterar_bibliografia', u'Pode alterar bibliografia'),
            ('excluir_bibliografia', u'Pode excluir bibliografia'),
        )
...

teleconsultoria/views.py

...
class BibliografiaListView(ModelListView):                                                                    
    model = Bibliografia                                                                                      
    app_name = 'teleconsultoria'                                                                              
    table = BibliografiaTable                                                                                 
    search_fields = ('id', 'ativa', 'nome', 'link')                                                           
    http_permissions = ('teleconsultoria.ver_bibliografia',)                                                  

    def get_queryset(self, request):                                                                          
        return self.model.objects.order_by('id')                                                              

...

teleconsultoria/tests.py

def test_ver_lista_bibliografias_se_existirem_bibliografias(self):
    Bibliografia.objects.create(**{'nome': 'Bibliografia 1'})
    Bibliografia.objects.create(**{'nome': 'Bibliografia 2'})
    Bibliografia.objects.create(**{'nome': 'Bibliografia 3'})
    response = self.client.get('/teleconsultoria/bibliografia/')
    self.assertIn('3 Bibliografias', response.content)

AssertionError: '3 Bibliografias' not found in ''

Aucun commentaire:

Enregistrer un commentaire