<commit_before>from copy import deepcopy
from itertools import groupby
from operator import attrgetter
from random import choice

from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase

from karrot.groups.factories import GroupFactory
from karrot.groups.models import GroupStatus
from karrot.pickups.factories import PickupDateSeriesFactory, PickupDateFactory, FeedbackFactory
from karrot.pickups.models import to_range
from karrot.places.factories import PlaceFactory
from karrot.places.models import PlaceStatus
from karrot.tests.utils import ExtractPaginationMixin
from karrot.users.factories import UserFactory
from karrot.utils.tests.fake import faker


class TestPlacesAPI(APITestCase, ExtractPaginationMixin):
    @classmethod
    def setUpTestData(cls):
        cls.url = '/api/places/'

        # group with two members and one place
        cls.member = UserFactory()
        cls.member2 = UserFactory()
        cls.group = GroupFactory(members=[cls.member, cls.member2])
        cls.place = PlaceFactory(group=cls.group)
        cls.place_url = cls.url + str(cls.place.id) + '/'

        # not a member
        cls.user = UserFactory()

        # another place for above group
        cls.place_data = {
            'name': faker.name(),
            'description': faker.name(),
            'group': cls.group.id,
            'address': faker.address(),
            'latitude': faker.latitude(),
            'longitude': faker.longitude()
        }

        # another group
        cls.different_group = GroupFactory(members=[cls.member2])

    def setUp(self):
        self.group.refresh_from_db()

    def test_create_place(self):
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_place_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_place_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['name'], self.place_data['name'])

    def test_create_place_as_newcomer_fails(self):
        newcomer = UserFactory()
        self.group.groupmembership_set.create(user=newcomer)
        self.client.force_login(user=newcomer)
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_place_activates_group(self):
        self.group.status = GroupStatus.INACTIVE.value
        self.group.save()
        self.client.force_login(user=self.member)
        self.client.post(self.url, self.place_data, format='json')
        self.group.refresh_from_db()
        self.assertEqual(self.group.status, GroupStatus.ACTIVE.value)

    def test_create_place_with_short_name_fails(self):
        self.client.force_login(user=self.member)
        data = deepcopy(self.place_data)
        data['name'] = 's'
        response = self.client.post(self.url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_list_places(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_list_places_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 0)

    def test_list_places_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)

    def test_retrieve_places(self):
        response = self.client.get(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_retrieve_places_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.get(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_retrieve_places_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.get(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_patch_place(self):
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_patch_place_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_patch_place_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_edit_place_as_newcomer_fails(self):
        newcomer = UserFactory()
        self.group.groupmembership_set.create(user=newcomer)
        self.client.force_login(user=newcomer)
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_patch_place_activates_group(self):
        self.group.status = GroupStatus.INACTIVE.value
        self.group.save()
        self.client.force_login(user=self.member)
        self.client.patch(self.place_url, self.place_data, format='json')
        self.group.refresh_from_db()
        self.assertEqual(self.group.status, GroupStatus.ACTIVE.value)

    def test_valid_status(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, {'status': 'active'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_invalid_status(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, {'status': 'foobar'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_change_group_as_member_in_one(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, {'group': self.different_group.id}, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_change_group_as_member_in_both(self):
        self.client.force_login(user=self.member2)
        response = self.client.patch(self.place_url, {'group': self.different_group.id}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        response = self.client.patch(self.place_url, {'group': self.group.id}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_delete_places(self):
        response = self.client.delete(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_delete_places_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.delete(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_delete_places_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.delete(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_subscribe_and_get_conversation(self):
        self.client.force_login(user=self.member)
        response = self.client.post('/api/places/{}/subscription/'.format(self.place.id))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        response = self.client.get('/api/places/{}/conversation/'.format(self.place.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertIn(self.member.id, response.data['participants'])
        self.assertEqual(response.data['type'], 'place')
        self.assertEqual(len(response.data['participants']), 1)

        # post message in conversation
        data = {'conversation': response.data['id'], 'content': 'a nice message'}
        response = self.client.post('/api/messages/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_unsubscribe(self):
        self.place.placesubscription_set.create(user=self.member)<commit_msg>add a test for place.is_subscribed<commit_after>from copy import deepcopy
from itertools import groupby
from operator import attrgetter
from random import choice

from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase

from karrot.groups.factories import GroupFactory
from karrot.groups.models import GroupStatus
from karrot.pickups.factories import PickupDateSeriesFactory, PickupDateFactory, FeedbackFactory
from karrot.pickups.models import to_range
from karrot.places.factories import PlaceFactory
from karrot.places.models import PlaceStatus
from karrot.tests.utils import ExtractPaginationMixin
from karrot.users.factories import UserFactory
from karrot.utils.tests.fake import faker


class TestPlacesAPI(APITestCase, ExtractPaginationMixin):
    @classmethod
    def setUpTestData(cls):
        cls.url = '/api/places/'

        # group with two members and one place
        cls.member = UserFactory()
        cls.member2 = UserFactory()
        cls.group = GroupFactory(members=[cls.member, cls.member2])
        cls.place = PlaceFactory(group=cls.group)
        cls.place_url = cls.url + str(cls.place.id) + '/'

        # not a member
        cls.user = UserFactory()

        # another place for above group
        cls.place_data = {
            'name': faker.name(),
            'description': faker.name(),
            'group': cls.group.id,
            'address': faker.address(),
            'latitude': faker.latitude(),
            'longitude': faker.longitude()
        }

        # another group
        cls.different_group = GroupFactory(members=[cls.member2])

    def setUp(self):
        self.group.refresh_from_db()

    def test_create_place(self):
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_place_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_place_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['name'], self.place_data['name'])

    def test_create_place_as_newcomer_fails(self):
        newcomer = UserFactory()
        self.group.groupmembership_set.create(user=newcomer)
        self.client.force_login(user=newcomer)
        response = self.client.post(self.url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_place_activates_group(self):
        self.group.status = GroupStatus.INACTIVE.value
        self.group.save()
        self.client.force_login(user=self.member)
        self.client.post(self.url, self.place_data, format='json')
        self.group.refresh_from_db()
        self.assertEqual(self.group.status, GroupStatus.ACTIVE.value)

    def test_create_place_with_short_name_fails(self):
        self.client.force_login(user=self.member)
        data = deepcopy(self.place_data)
        data['name'] = 's'
        response = self.client.post(self.url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_list_places(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_list_places_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 0)

    def test_list_places_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)

    def test_retrieve_places(self):
        response = self.client.get(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_retrieve_places_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.get(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_retrieve_places_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.get(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_patch_place(self):
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_patch_place_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_patch_place_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_edit_place_as_newcomer_fails(self):
        newcomer = UserFactory()
        self.group.groupmembership_set.create(user=newcomer)
        self.client.force_login(user=newcomer)
        response = self.client.patch(self.place_url, self.place_data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_patch_place_activates_group(self):
        self.group.status = GroupStatus.INACTIVE.value
        self.group.save()
        self.client.force_login(user=self.member)
        self.client.patch(self.place_url, self.place_data, format='json')
        self.group.refresh_from_db()
        self.assertEqual(self.group.status, GroupStatus.ACTIVE.value)

    def test_valid_status(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, {'status': 'active'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_invalid_status(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, {'status': 'foobar'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_change_group_as_member_in_one(self):
        self.client.force_login(user=self.member)
        response = self.client.patch(self.place_url, {'group': self.different_group.id}, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_change_group_as_member_in_both(self):
        self.client.force_login(user=self.member2)
        response = self.client.patch(self.place_url, {'group': self.different_group.id}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        response = self.client.patch(self.place_url, {'group': self.group.id}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_delete_places(self):
        response = self.client.delete(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_delete_places_as_user(self):
        self.client.force_login(user=self.user)
        response = self.client.delete(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_delete_places_as_group_member(self):
        self.client.force_login(user=self.member)
        response = self.client.delete(self.place_url)
        self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_subscribe_and_get_conversation(self):
        self.client.force_login(user=self.member)
        response = self.client.post('/api/places/{}/subscription/'.format(self.place.id))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        response = self.client.get('/api/places/{}/'.format(self.place.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertTrue(response.data['is_subscribed'])

        response = self.client.get('/api/places/')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertTrue(response.data[0]['is_subscribed'])

        response = self.client.get('/api/places/{}/conversation/'.format(self.place.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
        self.assertIn(self.member.id, response.data['participants'])
        self.assertEqual(response.data['type'], 'place')
        self.assertEqual(len(response.data['participants']), 1)

        # post message in conversation
        data = {'conversation': response.data['id'], 'content': 'a nice message'}
        response = self.client.post('/api/messages/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_unsubscribe(self):
        self.place.placesubscription_set.create(user=self.member)
{'fraction_non_alphanumeric': 0.10987407447440956, 'fraction_numerical': 0.009566016321677567, 'mean_word_length': 3.6845569620253165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6186633', 'n_tokens_mistral': 5859, 'n_tokens_neox': 5631, 'n_words': 807}