class TestHistoryAPIActivityForInactivePlace(APITestCase, ExtractPaginationMixin): def setUp(self): self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group, status='archived') self.activity = ActivityFactory(place=self.place, date=to_range(timezone.now() - relativedelta(days=1))) self.activity.add_participant(self.member) ActivityFactory(place=self.place, date=to_range(timezone.now() - relativedelta(days=1), minutes=30)) Activity.objects.process_finished_activities() def test_no_activity_done_for_inactive_place(self): self.client.force_login(self.member) response = self.get_results(history_url, {'typus': 'ACTIVITY_DONE'}) self.assertEqual(len(response.data), 0) def test_no_activity_missed_for_inactive_place(self): self.client.force_login(self.member) response = self.get_results(history_url, {'typus': 'ACTIVITY_MISSED'}) self.assertEqual(len(response.data), 0)
def test_leave_group(self): place = PlaceFactory(group=self.group) activity = ActivityFactory(place=place, participants=[self.member, self.user], date=to_range(timezone.now() + relativedelta(weeks=1))) past_activity = ActivityFactory(place=place, participants=[ self.member, ], date=to_range(timezone.now() - relativedelta(weeks=1))) unrelated_activity = ActivityFactory( date=to_range(timezone.now() + relativedelta(weeks=1)), participants=[ self.member, ], ) GroupMembership.objects.create(group=unrelated_activity.place.group, user=self.member) self.client.force_login(user=self.member) response = self.client.post('/api/groups/{}/leave/'.format( self.group.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(activity.participants.get_queryset().filter( id=self.member.id).exists()) self.assertTrue(past_activity.participants.get_queryset().filter( id=self.member.id).exists()) self.assertTrue(unrelated_activity.participants.get_queryset().filter( id=self.member.id).exists())
class TestHistoryAPIWithDoneActivity(APITestCase, ExtractPaginationMixin): def setUp(self): self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place, date=to_range(timezone.now() - relativedelta(days=1))) self.activity.add_participant(self.member) Activity.objects.process_finished_activities() def test_activity_done(self): self.client.force_login(self.member) response = self.get_results(history_url) self.assertEqual(response.data[0]['typus'], 'ACTIVITY_DONE') self.assertLess(parse(response.data[0]['date']), timezone.now() - relativedelta(hours=22)) def test_filter_activity_done(self): self.client.force_login(self.member) response = self.get_results(history_url, {'typus': 'ACTIVITY_DONE'}) self.assertEqual(response.data[0]['typus'], 'ACTIVITY_DONE') response = self.get_results( history_url, {'typus': 'GROUP_JOIN'}) # unrelated event should give no result self.assertEqual(len(response.data), 0)
def setUp(self): self.url = '/api/activities/' # activity for group with one member and one place self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity_type = ActivityTypeFactory(group=self.group) self.activity = ActivityFactory(activity_type=self.activity_type, place=self.place) # and another place + group + activity self.group2 = GroupFactory(members=[self.member]) self.place2 = PlaceFactory(group=self.group2) self.activity_type2 = ActivityTypeFactory(group=self.group2) self.activity2 = ActivityFactory(activity_type=self.activity_type2, place=self.place2) # an activity series self.series = ActivitySeriesFactory(activity_type=self.activity_type, place=self.place) # another activity series self.series2 = ActivitySeriesFactory(activity_type=self.activity_type, place=self.place)
def setUp(self): super().setUp() self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place, participants=[self.member])
def make_activity_in_group(self, group): place = PlaceFactory(group=group) new_users = [VerifiedUserFactory() for _ in range(self.new_user_count)] user = new_users[0] a_few_days_ago = timezone.now() - relativedelta(days=4) with freeze_time(a_few_days_ago, tick=True): [group.add_member(u) for u in new_users] # a couple of messages [ group.conversation.messages.create(author=user, content='hello') for _ in range(self.message_count) ] # missed activities [ ActivityFactory(place=place) for _ in range(self.activities_missed_count) ] # fullfilled activities activities = [ ActivityFactory(place=place, max_participants=1, participants=[user]) for _ in range(self.activities_done_count) ] # activity feedback [ FeedbackFactory(about=activity, given_by=user) for activity in activities[:self.feedback_count] ]
def test_create_activity_upcoming_notifications(self): users = [UserFactory() for _ in range(3)] group = GroupFactory(members=users) place = PlaceFactory(group=group) in_one_hour = to_range(timezone.now() + relativedelta(hours=1)) activity1 = ActivityFactory(place=place, date=in_one_hour, participants=users) in_two_hours = to_range(timezone.now() + relativedelta(hours=1)) ActivityFactory(place=place, date=in_two_hours, participants=users) Notification.objects.all().delete() create_activity_upcoming_notifications.call_local() notifications = Notification.objects.filter(type=NotificationType.ACTIVITY_UPCOMING.value) self.assertEqual(notifications.count(), 6) self.assertEqual(set(n.user.id for n in notifications), set(user.id for user in users)) activity1_user1_participant = ActivityParticipant.objects.get(user=users[0], activity=activity1) activity1_user1_notification = next( n for n in notifications if n.context['activity_participant'] == activity1_user1_participant.id ) self.assertEqual( activity1_user1_notification.context, { 'group': group.id, 'place': place.id, 'activity': activity1.id, 'activity_participant': activity1_user1_participant.id, } ) self.assertEqual(activity1_user1_notification.expires_at, activity1.date.start)
def test_activity_active_within(self): ActivityFactory(place=self.place, date=to_range(timezone.now() - timedelta(days=2)), participants=[self.user]) ActivityFactory( place=self.place, date=to_range(timezone.now() - timedelta(days=9)), participants=[self.other_user] ) memberships = self.group.groupmembership_set.activity_active_within(days=7) self.assertEqual(memberships.count(), 1)
def setUp(self): self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place) self.activity_url = '/api/activities/{}/'.format(self.activity.id) self.series = ActivitySeriesFactory(place=self.place) self.series_url = '/api/activity-series/{}/'.format(self.series.id)
def setUp(self): self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place, date=to_range(timezone.now() - relativedelta(days=1))) self.activity.add_participant(self.member) Activity.objects.process_finished_activities()
def setUp(self): self.url = '/api/activities/' # activity for group with one member and one place self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.activity_type = ActivityTypeFactory(group=self.group) self.active_place = PlaceFactory(group=self.group, status='active') self.inactive_place = PlaceFactory(group=self.group, status='created') ActivityFactory(activity_type=self.activity_type, place=self.active_place) ActivityFactory(activity_type=self.activity_type, place=self.inactive_place)
def test_cannot_delete_with_activities(self): self.client.force_login(user=self.member) activity_type = self.activity_types[0] activity = ActivityFactory(activity_type=activity_type) response = self.client.delete( f'/api/activity-types/{activity_type.id}/') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data) # make sure we can delete it if we get rid of the activity activity.delete() response = self.client.delete( f'/api/activity-types/{activity_type.id}/') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.data)
def test_deletes_activity_upcoming_notification(self): user = UserFactory() group = GroupFactory(members=[user]) place = PlaceFactory(group=group) in_one_hour = to_range(timezone.now() + relativedelta(hours=1)) activity = ActivityFactory(place=place, date=in_one_hour, participants=[user]) Notification.objects.all().delete() create_activity_upcoming_notifications.call_local() activity.remove_participant(user) notifications = Notification.objects.filter( type=NotificationType.ACTIVITY_UPCOMING.value) self.assertEqual(notifications.count(), 0)
def test_unsubscribe_from_activity_conversation(self): activity = ActivityFactory(place=self.place, participants=[self.user]) participant = activity.conversation.conversationparticipant_set.filter( user=self.user) self.assertFalse(participant.get().muted) unsubscribe_from_all_conversations_in_group(self.user, self.group) self.assertTrue(participant.get().muted)
def create_empty_activity(self, delta, place=None): if place is None: place = self.place return ActivityFactory( place=place, date=to_range(timezone.localtime() + delta), max_participants=1, )
def test_join_activity_without_max_participants_as_member(self): self.client.force_login(user=self.member) p = ActivityFactory(activity_type=self.activity_type, max_participants=None, place=self.place) response = self.client.post('/api/activities/{}/add/'.format(p.id)) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
def test_group_summary_data(self): a_couple_of_weeks_ago = timezone.now() - relativedelta(weeks=3) a_few_days_ago = timezone.now() - relativedelta(days=4) place = PlaceFactory(group=self.group) old_user = VerifiedUserFactory(mail_verified=True) user = VerifiedUserFactory(mail_verified=True) # should not be included in summary email with freeze_time(a_couple_of_weeks_ago, tick=True): self.group.add_member(old_user) self.group.conversation.messages.create(author=old_user, content='old message') ActivityFactory(place=place) ActivityFactory(place=place, max_participants=1, participants=[old_user]) # should be included in summary email with freeze_time(a_few_days_ago, tick=True): self.group.add_member(user) # a couple of messages self.group.conversation.messages.create(author=user, content='hello') self.group.conversation.messages.create(author=user, content='whats up') # a missed activity ActivityFactory(place=place) # a fulfilled activity ActivityFactory(place=place, max_participants=1, participants=[user]) from_date, to_date = karrot.groups.emails.calculate_group_summary_dates( self.group) data = karrot.groups.emails.prepare_group_summary_data( self.group, from_date, to_date) self.assertEqual(data['activities_done_count'], 1) self.assertEqual(data['activities_missed_count'], 1) self.assertEqual(len(data['new_users']), 1) self.assertEqual(len(data['messages']), 2)
def setUp(self): self.first_member = UserFactory() self.second_member = UserFactory() self.third_member = UserFactory() self.group = GroupFactory( members=[self.first_member, self.second_member, self.third_member]) self.place = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place, participants=[self.first_member])
def setUp(self): self.user = VerifiedUserFactory() self.other_user = VerifiedUserFactory() self.group = GroupFactory(members=[self.user, self.other_user]) self.place = PlaceFactory(group=self.group, subscribers=[self.user, self.other_user]) self.activity = ActivityFactory(place=self.place) self.subscriptions = [ PushSubscription.objects.create(user=self.user, token='', platform=PushSubscriptionPlatform.ANDROID.value) ]
def setUpTestData(cls): cls.url = '/api/activities/' # activity for group with one member and one place cls.member = UserFactory() cls.second_member = UserFactory() cls.group = GroupFactory(members=[cls.member, cls.second_member]) cls.place = PlaceFactory(group=cls.group) cls.activity_type = ActivityTypeFactory(group=cls.group) cls.archived_activity_type = ActivityTypeFactory(group=cls.group, status='archived') cls.activity = ActivityFactory(activity_type=cls.activity_type, place=cls.place) cls.activity_url = cls.url + str(cls.activity.id) + '/' cls.join_url = cls.activity_url + 'add/' cls.leave_url = cls.activity_url + 'remove/' cls.conversation_url = cls.activity_url + 'conversation/' # not a member of the group cls.user = UserFactory() # another activity for above place cls.activity_data = { 'activity_type': cls.activity_type.id, 'date': to_range(timezone.now() + relativedelta(days=2)).as_list(), 'max_participants': 5, 'place': cls.place.id } # past activity cls.past_activity_data = { 'activity_type': cls.activity_type.id, 'date': to_range(timezone.now() - relativedelta(days=1)).as_list(), 'max_participants': 5, 'place': cls.place.id } cls.past_activity = ActivityFactory( activity_type=cls.activity_type, place=cls.place, date=to_range(timezone.now() - relativedelta(days=1))) cls.past_activity_url = cls.url + str(cls.past_activity.id) + '/' cls.past_join_url = cls.past_activity_url + 'add/' cls.past_leave_url = cls.past_activity_url + 'remove/'
def test_creates_no_activity_upcoming_notification_when_too_far_in_future(self): user = UserFactory() group = GroupFactory(members=[user]) place = PlaceFactory(group=group) in_one_day = to_range(timezone.now() + relativedelta(days=1)) ActivityFactory(place=place, date=in_one_day, participants=[user]) Notification.objects.all().delete() create_activity_upcoming_notifications.call_local() notifications = Notification.objects.filter(type=NotificationType.ACTIVITY_UPCOMING.value) self.assertEqual(notifications.count(), 0)
def test_tags_for_activity_conversation(self): group = GroupFactory() place = PlaceFactory(group=group) activity = ActivityFactory(place=place) tags = conversation_tags(activity.conversation) self.assertEqual( tags, { 'type': 'activity', 'group': str(group.id), 'group_status': group.status, })
def setUp(self): self.url = '/api/feedback/' # create a group with a user and two places self.participant = UserFactory() self.participant2 = UserFactory() self.group = GroupFactory( members=[self.participant, self.participant2]) self.group2 = GroupFactory( members=[self.participant, self.participant2]) self.place = PlaceFactory(group=self.group) self.place2 = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place, date=to_range(timezone.now() - relativedelta(days=1))) self.activity2 = ActivityFactory(place=self.place2, date=to_range(timezone.now() - relativedelta(days=1))) # create a feedback data self.feedback_get = { 'given_by': self.participant, 'about': self.activity, 'weight': 1, 'comment': 'asfjk' } self.feedback_get2 = { 'given_by': self.participant2, 'about': self.activity2, 'weight': 2, 'comment': 'bsfjk' } # create 2 instances of feedback self.feedback = Feedback.objects.create(**self.feedback_get) self.feedback2 = Feedback.objects.create(**self.feedback_get2) # transforms the user into a participant self.activity.add_participant(self.participant) self.activity2.add_participant(self.participant) self.activity2.add_participant(self.participant2)
def test_activity_message_title(self): author = UserFactory() group = GroupFactory(members=[author], timezone='Europe/Berlin') place = PlaceFactory(group=group) activity = ActivityFactory(place=place, participants=[author], date=to_range(parse('2018-11-11T20:00:00Z'))) conversation = Conversation.objects.get_or_create_for_target(activity) message = conversation.messages.create(author=author, content='bla') title = get_message_title(message, 'en') self.assertEqual( title, '{} Sunday 9:00 PM / {}'.format(activity.activity_type.get_translated_name(), author.display_name) )
def test_creates_activity_disabled_notification_and_deletes_activity_upcoming_notification( self): user1, user2 = UserFactory(), UserFactory() group = GroupFactory(members=[user1, user2]) place = PlaceFactory(group=group) in_one_hour = to_range(timezone.now() + relativedelta(hours=1)) activity = ActivityFactory(place=place, date=in_one_hour, participants=[user1, user2]) Notification.objects.all().delete() create_activity_upcoming_notifications.call_local() activity.last_changed_by = user2 activity.is_disabled = True activity.save() activity_upcoming_notifications = Notification.objects.filter( type=NotificationType.ACTIVITY_UPCOMING.value) self.assertEqual(activity_upcoming_notifications.count(), 0) activity_disabled_notifications = Notification.objects.filter( type=NotificationType.ACTIVITY_DISABLED.value) self.assertEqual(activity_disabled_notifications.count(), 1) self.assertEqual(activity_disabled_notifications[0].user, user1) context = activity_disabled_notifications[0].context self.assertEqual(context['group'], group.id) self.assertEqual(context['activity'], activity.id) self.assertEqual(context['place'], place.id)
class FinishedActivityReceiverTest(WSTestCase): def setUp(self): super().setUp() self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity = ActivityFactory(place=self.place, participants=[self.member]) def test_receive_history_and_notification(self): self.activity.date = to_range(timezone.now() - relativedelta(days=1)) self.activity.save() Notification.objects.all().delete() client = self.connect_as(self.member) Activity.objects.process_finished_activities() messages_by_topic = client.messages_by_topic response = messages_by_topic['history:history'][0] self.assertEqual(response['payload']['typus'], 'ACTIVITY_DONE') response = messages_by_topic['notifications:notification'][0] self.assertEqual(response['payload']['type'], 'feedback_possible') status_messages = messages_by_topic['status'] self.assertEqual(len(status_messages), 2) self.assertEqual(status_messages[0]['payload'], {'unseen_notification_count': 1}) self.assertEqual( status_messages[1]['payload'], {'groups': { self.group.id: { 'feedback_possible_count': 1 } }}) self.assertEqual(len(client.messages), 4, client.messages)
def test_place_statistics_as_average(self): user = UserFactory() self.client.force_login(user=user) group = GroupFactory(members=[user]) place = PlaceFactory(group=group) response = self.client.get('/api/places/{}/statistics/'.format( place.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, { 'feedback_count': 0, 'feedback_weight': 0, 'activities_done': 0, }) one_day_ago = to_range(timezone.now() - relativedelta(days=1)) users = [UserFactory() for _ in range(9)] activities = [ ActivityFactory( place=place, date=one_day_ago, participants=users, is_done=True, ) for _ in range(3) ] feedback = [ FeedbackFactory(about=choice(activities), given_by=u) for u in users ] # calculate weight from feedback feedback.sort(key=attrgetter('about.id')) weight = 0 for _, fs in groupby(feedback, key=attrgetter('about.id')): len_list = [f.weight for f in fs] weight += float(sum(len_list)) / len(len_list) weight = round(weight) response = self.client.get('/api/places/{}/statistics/'.format( place.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data, { 'feedback_count': len(feedback), 'feedback_weight': weight, 'activities_done': len(activities), })
def create_user_activity(self, delta, place=None, **kwargs): if place is None: place = self.place activity = ActivityFactory( place=place, date=to_range(timezone.localtime() + delta), **kwargs, ) activity.add_participant(self.user) activity.save() return activity
def test_creates_activity_enabled_notification(self): user1, user2 = UserFactory(), UserFactory() group = GroupFactory(members=[user1, user2]) place = PlaceFactory(group=group) activity = ActivityFactory(place=place, participants=[user1, user2]) Notification.objects.all().delete() activity.last_changed_by = user2 activity.is_disabled = True activity.save() activity.is_disabled = False activity.save() activity_enabled_notifications = Notification.objects.filter( type=NotificationType.ACTIVITY_ENABLED.value) self.assertEqual(activity_enabled_notifications.count(), 1) self.assertEqual(activity_enabled_notifications[0].user, user1) context = activity_enabled_notifications[0].context self.assertEqual(context['group'], group.id) self.assertEqual(context['activity'], activity.id) self.assertEqual(context['place'], place.id)
def create_not_full_activity(self, delta, place=None): if place is None: place = self.place activity = ActivityFactory( place=place, date=to_range(timezone.localtime() + delta), max_participants=2, ) activity.add_participant(self.other_user) activity.save() return activity