class ResourceAdminTest(TestCase):
    def setUp(self):
        self.resource_admin = ResourceAdmin(Resource, AdminSite())
        self.user_admin = UserAdmin(User, AdminSite())

        self.organisation = OrganisationFactory.create()
        self.resource = ResourceFactory.create(organisation=self.organisation)
        self.resource_1 = ResourceFactory.create(
            organisation=self.organisation)
        self.resource_2 = ResourceFactory.create()

        self.user_without_organisation = UserFactory.create(is_staff=True)

        self.user_with_organisation = UserFactory.create(is_staff=True)
        self.user_with_organisation.approved_organisations.add(
            self.organisation)

    def test_permissions_with_no_organisation(self):
        self.user_admin.add_remove_permissions(self.user_without_organisation,
                                               Resource, 'change_resource')
        self.assertFalse(
            self.user_without_organisation.user_permissions.exists())

    def test_permissions_with_organisation(self):
        self.user_admin.add_remove_permissions(self.user_with_organisation,
                                               Resource, 'change_resource')
        content_type = ContentType.objects.get_for_model(Resource)
        permission = Permission.objects.get(content_type=content_type,
                                            codename='change_resource')
        permission_string = '{app_label}.{permission}'.format(
            app_label=permission.content_type.app_label,
            permission=permission.codename,
        )

        self.assertTrue(self.user_with_organisation.user_permissions.exists())
        self.assertTrue(
            self.user_with_organisation.has_perm(permission_string))

    def test_get_queryset(self):
        request = RequestFactory().request()
        request.user = self.user_with_organisation
        qs = self.resource_admin.get_queryset(request)

        self.assertEqual(qs.count(), 2)
class UserAdminTest(TestCase):

    def setUp(self):
        self.user_admin = UserAdmin(User, AdminSite())

        self.superuser = UserFactory.create(is_superuser=True)

        self.organisation = OrganisationFactory.create()
        self.organisation_1 = OrganisationFactory.create()

        self.user_with_org = UserFactory.create(is_staff=True)
        self.user_with_org.approved_organisations.add(self.organisation)
        self.user_with_org.approved_organisations.add(self.organisation_1)

        self.user_with_org_1 = UserFactory.create(is_staff=True)
        self.user_with_org_1.approved_organisations.add(self.organisation)

    def test_permissions_with_no_organisation(self):
        self.user_admin.add_remove_permissions(self.superuser, User, 'change_user')
        self.assertFalse(self.superuser.user_permissions.exists())

    def test_permissions_with_organisation(self):
        self.user_admin.add_remove_permissions(self.user_with_org, User, 'change_user')
        content_type = ContentType.objects.get_for_model(User)
        permission = Permission.objects.get(content_type=content_type, codename='change_user')
        permission_string = '{app_label}.{permission}'.format(
            app_label=permission.content_type.app_label,
            permission=permission.codename,
        )

        self.assertTrue(self.user_with_org.user_permissions.exists())
        self.assertTrue(self.user_with_org.has_perm(permission_string))

    def test_permissions_delete_organisation(self):
        self.user_with_org_1.approved_organisations.remove(self.organisation)
        self.user_admin.add_remove_permissions(self.user_with_org_1, User, 'change_user')

        self.assertFalse(self.user_with_org.user_permissions.exists())

    def test_get_queryset(self):
        request = RequestFactory().request()
        request.user = self.user_with_org
        qs = self.user_admin.get_queryset(request)

        self.assertEqual(qs.count(), 2)