<commit_before># -*- coding: utf-8 -*-

"""
Akvo RSR is covered by the GNU Affero General Public License.

See more details in the license.txt file located at the root folder of the Akvo RSR module.
For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
"""

import base64
import json
import tempfile

from akvo.rsr.models import Project, User, ProjectUpdate

from django.conf import settings
from django.test import TestCase, Client

# Data for a WMF image file
WMF_DATA = (
    '\xd7\xcd\xc6\x9a\x00\x00\x00\x00\x00\x00\xd4\x00\xd4\x00\xb0\x04\x00\x00\x00\x00\xa1S\x01\x00\t\x00\x00\x03'
    'p\x00\x00\x00\x03\x00\x14\x00\x00\x00\x00\x00\x05\x00\x00\x00\x0c\x02\xd4\x00\xd4\x00\x05\x00\x00\x00\x0b\x02'
    '\x00\x00\x00\x00\x04\x00\x00\x00\x03\x01\x08\x00\x05\x00\x00\x00\x02\x01\x01\x00\x00\x00\x05\x00\x00\x00\x06\x01'
    '\x02\x00\x00\x00\x05\x00\x00\x00.\x01\x18\x00\x00\x00\x05\x00\x00\x00\t\x02\x00\x00\x00\x00\x05\x00\x00\x00'
    '\x04\x01\r\x00\x00\x00\x07\x00\x00\x00&\x06\x17\x00\x04\x00\x05\x00\x00\x00\x08\x00\x00\x00\xfa\x02\x00\x00'
    '\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\xfa\x02\x05\x00\x01\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00'
    '-\x01\x01\x00\x07\x00\x00\x00\xfc\x02\x01\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00-\x01\x02\x00\x04\x00'
    '\x00\x00\xf0\x01\x02\x00\x04\x00\x00\x00\xf0\x01\x01\x00\x04\x00\x00\x00\xf0\x01\x00\x00\x03\x00\x00\x00\x00\x00'
)


class RestProjectUpdateTestCase(TestCase):
    """Tests the project update REST endpoints."""

    def setUp(self):
        """
        For all tests, we at least need two projects and an update in the database. And a client.
        """

        # Create projects
        Project.objects.create(
            title="REST test project",
        ).publish()

        self.project = Project.objects.create(
            title="REST test project 2",
        )
        self.project.publish()

        # Create active (super)user
        self.user = User.objects.create_superuser(
            username="Super user REST",
            email="<EMAIL>",
            password="password",
        )
        self.user.is_active = True
        self.user.save()

        # Create update
        ProjectUpdate.objects.create(
            project=self.project,
            user=self.user,
            title="Update title",
        )

        self.c = Client(HTTP_HOST=settings.RSR_DOMAIN)

    def test_rest_project_update_project_filter(self):
        """
        Checks the REST project update endpoint with a project filter.
        """
        response = self.c.get('/rest/v1/project_update/', {'format': 'json', 'project__gte': 1})
        self.assertEqual(response.status_code, 200)

        content = json.loads(response.content)
        self.assertEqual(content['count'], 1)

        response = self.c.get('/rest/v1/project_update/',
                              {'format': 'json', 'project__title__exact': 'REST test project'})
        self.assertEqual(response.status_code, 200)

        content = json.loads(response.content)
        self.assertEqual(content['count'], 0)

        response = self.c.get('/rest/v1/project_update/',
                              {'format': 'json', 'project__partners': 1})
        self.assertEqual(response.status_code, 200)

        content = json.loads(response.content)
        self.assertEqual(content['count'], 0)
<commit_msg>[#3400] Use BaseTestCase for project update REST tests<commit_after># -*- coding: utf-8 -*-

"""
Akvo RSR is covered by the GNU Affero General Public License.

See more details in the license.txt file located at the root folder of the Akvo RSR module.
For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
"""

import base64
import json
import tempfile

from akvo.rsr.models import Organisation, Partnership, Project, ProjectUpdate
from akvo.rsr.tests.base import BaseTestCase

# Data for a WMF image file
WMF_DATA = (
    '\xd7\xcd\xc6\x9a\x00\x00\x00\x00\x00\x00\xd4\x00\xd4\x00\xb0\x04\x00\x00\x00\x00\xa1S\x01\x00\t\x00\x00\x03'
    'p\x00\x00\x00\x03\x00\x14\x00\x00\x00\x00\x00\x05\x00\x00\x00\x0c\x02\xd4\x00\xd4\x00\x05\x00\x00\x00\x0b\x02'
    '\x00\x00\x00\x00\x04\x00\x00\x00\x03\x01\x08\x00\x05\x00\x00\x00\x02\x01\x01\x00\x00\x00\x05\x00\x00\x00\x06\x01'
    '\x02\x00\x00\x00\x05\x00\x00\x00.\x01\x18\x00\x00\x00\x05\x00\x00\x00\t\x02\x00\x00\x00\x00\x05\x00\x00\x00'
    '\x04\x01\r\x00\x00\x00\x07\x00\x00\x00&\x06\x17\x00\x04\x00\x05\x00\x00\x00\x08\x00\x00\x00\xfa\x02\x00\x00'
    '\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\xfa\x02\x05\x00\x01\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00'
    '-\x01\x01\x00\x07\x00\x00\x00\xfc\x02\x01\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00-\x01\x02\x00\x04\x00'
    '\x00\x00\xf0\x01\x02\x00\x04\x00\x00\x00\xf0\x01\x01\x00\x04\x00\x00\x00\xf0\x01\x00\x00\x03\x00\x00\x00\x00\x00'
)


class RestProjectUpdateTestCase(BaseTestCase):
    """Tests the project update REST endpoints."""

    def setUp(self):
        """
        For all tests, we at least need two projects and an update in the database. And a client.
        """

        super(RestProjectUpdateTestCase, self).setUp()
        # Create active (super)user
        self.user = self.create_user("<EMAIL>", "password")
        self.org = Organisation.objects.create(name='org', long_name='org')
        self.make_employment(self.user, self.org, 'Users')

        # Create projects
        self.orphan_project = Project.objects.create(title="REST test project")
        self.orphan_project.publish()
        self.project = Project.objects.create(title="REST test project 2")
        self.project.publish()
        Partnership.objects.create(organisation=self.org, project=self.project)

        # Create update
        ProjectUpdate.objects.create(
            project=self.project,
            user=self.user,
            title="Update title",
        )

    def test_rest_project_update_project_filter(self):
        """
        Checks the REST project update endpoint with a project filter.
        """
        response = self.c.get('/rest/v1/project_update/', {'format': 'json', 'project__gte': 1})
        self.assertEqual(response.status_code, 200)

        content = json.loads(response.content)
        self.assertEqual(content['count'], 1)

        response = self.c.get('/rest/v1/project_update/',
                              {'format': 'json', 'project__title__exact': 'REST test project'})
        self.assertEqual(response.status_code, 200)

        content = json.loads(response.content)
        self.assertEqual(content['count'], 0)

        response = self.c.get('/rest/v1/project_update/',
                              {'format': 'json', 'project__partners': 1})
        self.assertEqual(response.status_code, 200)

        content = json.loads(response.content)
        self.assertEqual(content['count'], 0)

{'fraction_non_alphanumeric': 0.15227470298464213, 'fraction_numerical': 0.1248913358446827, 'mean_word_length': 2.5472764645426516, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7308795', 'n_tokens_mistral': 3196, 'n_tokens_neox': 2726, 'n_words': 385}