def setUp(self): self.maxDiff = None self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.activity_type = ActivityTypeFactory(group=self.group) self.archived_activity_type = ActivityTypeFactory(group=self.group, status='archived')
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.user = UserFactory() self.other_user = UserFactory() self.group = GroupFactory(members=[self.user, self.other_user]) self.other_group = GroupFactory(members=[self.user, self.other_user]) self.place = PlaceFactory(group=self.group) self.other_place = PlaceFactory(group=self.other_group)
def setUp(self): self.user = VerifiedUserFactory() self.author = VerifiedUserFactory() self.group = GroupFactory(members=[self.author, self.user]) mail.outbox = [] with suppressed_notifications(): self.message = self.group.conversation.messages.create(author=self.author, content='initial message')
def setUp(self): self.group = GroupFactory() self.user_without_notifications = VerifiedUserFactory(language='en') self.group.add_member(self.user_without_notifications) m = GroupMembership.objects.get(group=self.group, user=self.user_without_notifications) m.notification_types = [] m.save() # it should ignore unverified and inactive users so adding a random number # of them here should not change anything unverified_users = [ UserFactory(language='en') for _ in list(range(randint(2, 5))) ] for user in unverified_users: self.group.add_member(user) inactive_users = [ VerifiedUserFactory(language='en') for _ in list(range(randint(2, 5))) ] for user in inactive_users: membership = self.group.add_member(user) membership.inactive_at = timezone.now() membership.save()
def setUp(self): self.url = '/api/places/' # group with two members and one place self.member = UserFactory() self.member2 = UserFactory() self.group = GroupFactory(members=[self.member, self.member2]) self.place = PlaceFactory(group=self.group) self.place_url = self.url + str(self.place.id) + '/' # not a member self.user = UserFactory() # another place for above group self.place_data = { 'name': faker.name(), 'description': faker.name(), 'group': self.group.id, 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } # another group self.different_group = GroupFactory(members=[self.member2])
class GroupConversationReceiverPushTests(TransactionTestCase): def setUp(self): self.group = GroupFactory() self.user = UserFactory() self.author = UserFactory() self.group.add_member(self.user) self.group.add_member(self.author) self.token = faker.uuid4() self.content = faker.text() self.conversation = self.group.conversation # add a push subscriber self.subscription = PushSubscription.objects.create( user=self.user, token=self.token, platform=PushSubscriptionPlatform.ANDROID.value, ) def test_sends_to_push_subscribers(self, notify_subscribers): # add a message to the conversation ConversationMessage.objects.create(conversation=self.conversation, content=self.content, author=self.author) self.assertEqual(notify_subscribers.call_count, 2) kwargs = notify_subscribers.call_args_list[0][1] self.assertEqual(list(kwargs['subscriptions']), [self.subscription]) self.assertEqual(kwargs['fcm_options']['message_title'], self.group.name + ' / ' + self.author.display_name) self.assertEqual(kwargs['fcm_options']['message_body'], self.content)
class TestApplicationConversationModel(APITestCase): def setUp(self): self.applicant = VerifiedUserFactory() self.member = VerifiedUserFactory() self.group = GroupFactory(members=[self.member]) self.application = ApplicationFactory(group=self.group, user=self.applicant) self.conversation = Conversation.objects.get_for_target(self.application) def test_member_leaves_group(self): GroupMembership.objects.filter(user=self.member, group=self.group).delete() self.assertNotIn( self.member, self.conversation.participants.all(), ) def test_user_erased(self): self.applicant.refresh_from_db() # otherwise user.photo.name is None self.applicant.erase() self.application.refresh_from_db() self.assertEqual(self.application.status, ApplicationStatus.WITHDRAWN.value) def test_deleting_application_deletes_conversation(self): Application.objects.filter(user=self.applicant, group=self.group).delete() self.assertIsNone(Conversation.objects.get_for_target(self.application)) def test_sets_group(self): self.assertEqual(self.conversation.group, self.group) def test_withdraw_pending_application_when_user_joins_group(self): self.group.add_member(self.applicant) self.application.refresh_from_db() self.assertEqual(self.application.status, ApplicationStatus.WITHDRAWN.value)
def setUp(self): self.now = timezone.now() self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.series = PickupDateSeriesFactory(max_collectors=3, place=self.place)
def setUp(self): self.user = UserFactory() self.url = '/api/unsubscribe/{}/' self.user = UserFactory() self.group = GroupFactory(members=[self.user]) self.place = PlaceFactory(group=self.group) self.other_group = GroupFactory(members=[self.user])
def setUp(self): self.now = timezone.now() self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.series = ActivitySeriesFactory(max_participants=3, place=self.place)
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 create_group_with_members(self, member_count): self.members = [UserFactory() for _ in range(member_count)] self.group = GroupFactory(members=self.members) # trust threshold calculation ignores recently joined users, so we need to create users before that two_days_ago = timezone.now() - relativedelta(days=2) GroupMembership.objects.filter(group=self.group).update( created_at=two_days_ago)
def setUp(self): self.applicant = VerifiedUserFactory() self.member = VerifiedUserFactory() self.group = GroupFactory(members=[self.member]) self.application = ApplicationFactory(group=self.group, user=self.applicant) self.conversation = Conversation.objects.get_for_target( self.application)
def setUp(self): self.url = '/api/places/' # two groups one place self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.group2 = GroupFactory(members=[self.member]) self.place = PlaceFactory(group=self.group) self.place2 = PlaceFactory(group=self.group2)
def test_remove_trust_when_giver_leaves_group(self): editor = UserFactory() newcomer = UserFactory() group = GroupFactory(members=[editor], newcomers=[newcomer]) membership = GroupMembership.objects.get(user=newcomer, group=group) Trust.objects.create(membership=membership, given_by=editor) group.remove_member(editor) self.assertEqual(0, Trust.objects.filter(membership=membership).count())
def setUp(self): self.member = UserFactory() self.member2 = UserFactory() self.newcomer = UserFactory() self.group = GroupFactory(members=[self.member, self.member2], newcomers=[self.newcomer]) self.group2 = GroupFactory(members=[self.member]) # effectively disable throttling from karrot.invitations.api import InvitesPerDayThrottle InvitesPerDayThrottle.rate = '1000/min'
def test_do_not_remove_trust_in_other_groups(self): editor = UserFactory() newcomer = UserFactory() group = GroupFactory(members=[editor], newcomers=[newcomer]) membership = GroupMembership.objects.get(user=newcomer, group=group) other_group = GroupFactory(members=[editor]) Trust.objects.create(membership=membership, given_by=editor) other_group.remove_member(editor) self.assertEqual(1, Trust.objects.filter(membership=membership).count())
def test_removes_notification_when_leaving_group(self): user = UserFactory() group = GroupFactory(members=[user]) Notification.objects.all().delete() Notification.objects.create(user=user, type='foo', context={'group': group.id}) group.remove_member(user) self.assertEqual(Notification.objects.count(), 0)
def setUp(self): self.url = '/api/groups-info/' # some user self.user = UserFactory() # two groups with different members self.member = UserFactory() self.group = GroupFactory(members=[self.member]) self.member2 = UserFactory() self.group2 = GroupFactory(members=[self.member2]) self.empty_group = GroupFactory()
def setUp(self): self.url = '/api/pickup-dates/' self.oneWeekAgo = to_range(timezone.now() - relativedelta(weeks=1)) self.tooLongAgo = to_range( timezone.now() - relativedelta(days=settings.FEEDBACK_POSSIBLE_DAYS + 1), minutes=30) self.member = UserFactory() self.member2 = UserFactory() self.group = GroupFactory(members=[self.member, self.member2]) self.place = PlaceFactory(group=self.group) # not member (anymore) self.group2 = GroupFactory(members=[]) self.place2 = PlaceFactory(group=self.group2) self.pickupFeedbackPossible = PickupDateFactory(place=self.place, collectors=[ self.member, ], date=self.oneWeekAgo) # now the issues where no feedback can be given self.pickupUpcoming = PickupDateFactory(place=self.place, collectors=[ self.member, ]) self.pickupNotCollector = PickupDateFactory(place=self.place, date=self.oneWeekAgo) self.pickupTooLongAgo = PickupDateFactory(place=self.place, date=self.tooLongAgo) self.pickupFeedbackAlreadyGiven = PickupDateFactory( place=self.place, collectors=[ self.member, ], date=self.oneWeekAgo) self.feedback = FeedbackFactory(about=self.pickupFeedbackAlreadyGiven, given_by=self.member) self.pickupCollectorLeftGroup = PickupDateFactory(place=self.place2, collectors=[ self.member, ], date=self.oneWeekAgo) self.pickupDoneByAnotherUser = PickupDateFactory(place=self.place, collectors=[ self.member2, ], date=self.oneWeekAgo) PickupDateModel.objects.process_finished_pickup_dates()
def setUp(self): self.member = VerifiedUserFactory() self.affected_member = VerifiedUserFactory() self.group = GroupFactory(members=[self.member, self.affected_member]) self.issue = IssueFactory(group=self.group, created_by=self.member, affected_user=self.affected_member) # add notification type to send out emails for membership in self.group.groupmembership_set.all(): membership.add_notification_types( [GroupNotificationType.CONFLICT_RESOLUTION]) membership.save()
def setUp(self): self.normal_member = UserFactory() self.agreement_manager = UserFactory() self.group = GroupFactory(members=[self.normal_member, self.agreement_manager]) self.agreement = Agreement.objects.create(group=self.group, title=faker.text(), content=faker.text()) membership = GroupMembership.objects.get(group=self.group, user=self.agreement_manager) membership.roles.append(roles.GROUP_AGREEMENT_MANAGER) membership.save() # other group/agreement that neither user is part of self.other_group = GroupFactory() self.other_agreement = Agreement.objects.create( group=self.other_group, title=faker.text(), content=faker.text() )
def setUp(self): self.user = UserFactory() self.member = UserFactory() self.group = GroupFactory(members=[self.member], is_open=True) self.group_with_password = GroupFactory(password='******', is_open=True) self.join_password_url = '/api/groups/{}/join/'.format(self.group_with_password.id) self.url = '/api/groups/' self.group_data = { 'name': faker.name(), 'description': faker.text(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude(), 'timezone': 'Europe/Berlin' }
def test_creates_new_member_notification(self): member1 = UserFactory() member2 = UserFactory() group = GroupFactory(members=[member1, member2]) Notification.objects.all().delete() user = UserFactory() group.add_member(user, added_by=member1) notifications = Notification.objects.filter( type=NotificationType.NEW_MEMBER.value) # member1 doesn't get a notification, as they added the user self.assertEqual(notifications.count(), 1, notifications) self.assertEqual(notifications[0].user, member2) self.assertEqual(notifications[0].context['user'], user.id)
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_no_summary_email_if_no_activity_in_group(self, write_points): group = GroupFactory(members=[VerifiedUserFactory()]) with freeze_time( datetime.datetime(2018, 8, 19, 6, 0, 0, tzinfo=pytz.utc)): write_points.reset_mock() mail.outbox = [] send_summary_emails() self.assertEqual(len(mail.outbox), 0) write_points.assert_called_with([{ 'measurement': 'karrot.email.group_summary', 'tags': { 'group': str(group.id), 'group_status': 'active', }, 'fields': { 'value': 1, 'new_user_count': 0, 'email_recipient_count': 0, 'feedback_count': 0, 'activities_missed_count': 0, 'message_count': 0, 'activities_done_count': 0, 'has_activity': False, }, }])
def test_list_conversations_with_related_data_efficiently(self): user = UserFactory() group = GroupFactory(members=[user]) place = PlaceFactory(group=group) pickup = PickupDateFactory(place=place) application = ApplicationFactory(user=UserFactory(), group=group) issue = IssueFactory(group=group) conversations = [ t.conversation for t in (group, pickup, application, issue) ] [c.sync_users([user]) for c in conversations] [c.messages.create(content='hey', author=user) for c in conversations] ConversationMeta.objects.get_or_create(user=user) self.client.force_login(user=user) with self.assertNumQueries(13): response = self.client.get('/api/conversations/', {'group': group.id}, format='json') results = response.data['results'] self.assertEqual(len(results['conversations']), len(conversations)) self.assertEqual(results['pickups'][0]['id'], pickup.id) self.assertEqual(results['applications'][0]['id'], application.id) self.assertEqual(results['issues'][0]['id'], issue.id)
def setUp(self): self.user = VerifiedUserFactory() self.user2 = VerifiedUserFactory() self.group = GroupFactory(members=[self.user, self.user2]) self.conversation = self.group.conversation self.thread = self.conversation.messages.create(author=self.user, content='yay')
def test_clicked(self, write_points): user = UserFactory() group = GroupFactory(members=[user]) two_hours_ago = timezone.now() - relativedelta(hours=2) notification = Notification.objects.create( created_at=two_hours_ago, user=user, type=NotificationType.USER_BECAME_EDITOR.value, context={'group': group.id}, ) write_points.reset_mock() notification.clicked = True notification.save() write_points.assert_called_with([{ 'measurement': 'karrot.events', 'tags': { 'group': str(group.id), 'group_status': group.status, 'notification_type': NotificationType.USER_BECAME_EDITOR.value, }, 'fields': { 'notification_clicked': 1, 'notification_clicked_seconds': 60 * 60 * 2, }, }])