'Tests that the client can get and set group permissions through the test_resource_permissions view.'

def test_resource_permissions(self):

    self.assertTrue(self.client.login(username='admin', password='admin'))
    layer = Layer.objects.all()[0]
    document = Document.objects.all()[0]
    map_obj = Map.objects.all()[0]
    layer.set_default_permissions()
    document.set_default_permissions()
    map_obj.set_default_permissions()
    objects = (layer, document, map_obj)
    for obj in objects:
        response = self.client.get(reverse('resource_permissions', kwargs=dict(resource_id=obj.id)))
        self.assertEqual(response.status_code, 200)
        js = json.loads(response.content)
        permissions = js.get('permissions', dict())
        if (isinstance(permissions, unicode) or isinstance(permissions, str)):
            permissions = json.loads(permissions)
        expected_permissions = {}
        if settings.DEFAULT_ANONYMOUS_DOWNLOAD_PERMISSION:
            expected_permissions.setdefault(u'anonymous', []).append(u'download_resourcebase')
        if settings.DEFAULT_ANONYMOUS_VIEW_PERMISSION:
            expected_permissions.setdefault(u'anonymous', []).append(u'view_resourcebase')
        self.assertDictEqual(permissions.get('groups'), expected_permissions)
        permissions = {'groups': {'bar': ['change_resourcebase']}, 'users': {'admin': ['change_resourcebase']}}
        response = self.client.post(reverse('resource_permissions', kwargs=dict(resource_id=obj.id)), data=json.dumps(permissions), content_type='application/json')
        self.assertEqual(response.status_code, 200)
        response = self.client.get(reverse('resource_permissions', kwargs=dict(resource_id=obj.id)))
        js = json.loads(response.content)
        permissions = js.get('permissions', dict())
        if (isinstance(permissions, unicode) or isinstance(permissions, str)):
            permissions = json.loads(permissions)
        self.assertDictEqual(permissions['groups'], {'bar': ['change_resourcebase']})
        permissions = {'users': {'admin': ['change_resourcebase']}}
        response = self.client.post(reverse('resource_permissions', kwargs=dict(resource_id=obj.id)), data=json.dumps(permissions), content_type='application/json')
        self.assertEqual(response.status_code, 200)
        response = self.client.get(reverse('resource_permissions', kwargs=dict(resource_id=obj.id)))
        js = json.loads(response.content)
        permissions = js.get('permissions', dict())
        if (isinstance(permissions, unicode) or isinstance(permissions, str)):
            permissions = json.loads(permissions)
        self.assertDictEqual(permissions['groups'], {})
