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) store = StoreFactory(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') PickupDateFactory(store=store) PickupDateFactory(store=store, max_collectors=1, collectors=[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 pickup PickupDateFactory(store=store) # a fulfilled pickup PickupDateFactory(store=store, max_collectors=1, collectors=[user]) from_date, to_date = foodsaving.groups.emails.calculate_group_summary_dates( self.group) data = foodsaving.groups.emails.prepare_group_summary_data( self.group, from_date, to_date) self.assertEqual(data['pickups_done_count'], 1) self.assertEqual(data['pickups_missed_count'], 1) self.assertEqual(len(data['new_users']), 1) self.assertEqual(len(data['messages']), 2)
def test_only_notifies_active_group_members(self): self.group.add_member(UserFactory()) inactive_user = VerifiedUserFactory() self.group.add_member(inactive_user) self.group.groupmembership_set.filter(user=inactive_user).update(inactive_at=timezone.now()) mail.outbox = [] self.group.conversation.messages.create(author=self.author, content='foo') self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user.email])
def test_send_email_notifications(self): users = [VerifiedUserFactory() for _ in range(3)] [self.conversation.join(u) for u in users] mail.outbox = [] ConversationMessage.objects.create(author=self.user, conversation=self.conversation, content='asdf') actual_recipients = set(m.to[0] for m in mail.outbox) expected_recipients = set(u.email for u in users) self.assertEqual(actual_recipients, expected_recipients) self.assertEqual(len(mail.outbox), 3)
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() unverified_users = [UserFactory(language='en') for i in list(range(3))] for user in unverified_users: self.group.add_member(user)
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 test_exclude_bounced_addresses(self): bounce_user = VerifiedUserFactory() self.conversation.join(bounce_user) EmailEvent.objects.create(address=bounce_user.email, event='bounce', payload={}) mail.outbox = [] ConversationMessage.objects.create(author=self.user, conversation=self.conversation, content='asdf') self.assertEqual(len(mail.outbox), 0)
def test_collects_stats(self, write_points): a_few_days_ago = timezone.now() - relativedelta(days=4) store = StoreFactory(group=self.group) message_count = 10 pickups_missed_count = 5 feedback_count = 4 new_user_count = 3 pickups_done_count = 8 new_users = [VerifiedUserFactory() for _ in range(new_user_count)] user = new_users[0] with freeze_time(a_few_days_ago, tick=True): [self.group.add_member(u) for u in new_users] # a couple of messages [self.group.conversation.messages.create(author=user, content='hello') for _ in range(message_count)] # missed pickups [PickupDateFactory(store=store) for _ in range(pickups_missed_count)] # fullfilled pickups pickups = [ PickupDateFactory(store=store, max_collectors=1, collectors=[user]) for _ in range(pickups_done_count) ] # pickup feedback [FeedbackFactory(about=pickup, given_by=user) for pickup in pickups[:feedback_count]] write_points.reset_mock() send_summary_emails() write_points.assert_called_with([{ 'measurement': 'karrot.email.group_summary', 'tags': { 'group': str(self.group.id) }, 'fields': { 'value': 1, 'new_user_count': new_user_count, 'email_recipient_count': new_user_count, 'feedback_count': feedback_count, 'pickups_missed_count': pickups_missed_count, 'message_count': message_count, 'pickups_done_count': pickups_done_count, }, }])
def get_or_create_application(): application = GroupApplication.objects.order_by('?').first() if application is None: new_user = VerifiedUserFactory() group = random_group() if group.application_questions == '': group.application_questions = faker.text() group.save() application = GroupApplicationFactory( group=group, user=new_user, ) return application
def test_ignores_deleted_pickups(self): a_few_days_ago = timezone.now() - relativedelta(days=4) store = StoreFactory(group=self.group) user = VerifiedUserFactory(mail_verified=True) self.group.add_member(user) with freeze_time(a_few_days_ago, tick=True): # fulfilled, but deleted PickupDateFactory(store=store, max_collectors=1, collectors=[user], deleted=True) from_date, to_date = foodsaving.groups.emails.calculate_group_summary_dates(self.group) data = foodsaving.groups.emails.prepare_group_summary_data(self.group, from_date, to_date) self.assertEqual(data['pickups_done_count'], 0)
def test_not_exclude_bounced_addresses_if_too_old(self): bounce_user = VerifiedUserFactory() self.group.add_member(bounce_user) some_months_ago = timezone.now() - relativedelta(months=4) for _ in range(5): EmailEvent.objects.create(created_at=some_months_ago, address=bounce_user.email, event='bounce', payload={}) mail.outbox = [] ConversationMessage.objects.create(author=self.user, conversation=self.conversation, content='asdf') self.assertEqual(len(mail.outbox), 1)
def test_creates_one_email_for_one_language(self): n = 5 for i in list(range(n)): self.group.add_member(VerifiedUserFactory(language='en')) from_date, to_date = group_emails.calculate_group_summary_dates(self.group) context = group_emails.prepare_group_summary_data(self.group, from_date, to_date) emails = group_emails.prepare_group_summary_emails(self.group, context) self.assertEqual(len(emails), 1) expected_members = self.group.members.filter( groupmembership__in=GroupMembership.objects.active(). with_notification_type(GroupNotificationType.WEEKLY_SUMMARY) ).exclude(groupmembership__user__in=get_user_model().objects.unverified_or_ignored()) self.assertEqual(sorted(emails[0].to), sorted([member.email for member in expected_members])) self.assertNotIn(self.user_without_notifications.email, emails[0].to)
def setUpClass(cls): super().setUpClass() cls.user = UserFactory() cls.user2 = UserFactory() cls.verified_user = VerifiedUserFactory() cls.url = '/api/users/' cls.user_data = { 'email': faker.email(), 'password': faker.name(), 'display_name': faker.name(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } cls.group = GroupFactory(members=[cls.user, cls.user2]) cls.another_common_group = GroupFactory(members=[cls.user, cls.user2]) cls.user_in_another_group = UserFactory() cls.another_group = GroupFactory(members=[cls.user_in_another_group, ])
def setUp(self): self.applicant = VerifiedUserFactory() self.member = VerifiedUserFactory() self.group = GroupFactory(members=[self.member]) mail.outbox = []
def setUp(self): self.user = VerifiedUserFactory() self.group = GroupFactory(members=[self.user]) self.store = StoreFactory(group=self.group) self.pickup = PickupDateFactory(store=self.store, collectors=[self.user]) self.conversation = self.pickup.conversation
def setUp(self): self.user = UserFactory() self.verified_user = VerifiedUserFactory() self.url = '/api/auth/resend_verification/' mail.outbox = []
def setUp(self): self.verified_user = VerifiedUserFactory() self.another_user = VerifiedUserFactory() self.url = '/api/auth/user/' mail.outbox = []
class TestChangeEMail(APITestCase): def setUp(self): self.verified_user = VerifiedUserFactory() self.another_user = VerifiedUserFactory() self.url = '/api/auth/user/' mail.outbox = [] def test_change_succeeds(self): self.client.force_login(user=self.verified_user) self.assertTrue(self.verified_user.mail_verified) old_email = self.verified_user.email new_email = faker.email() # typical frontend use case of getting, modifying and sending data data = self.client.get(self.url).data data['email'] = new_email response = self.client.patch(self.url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(response.data['email'], old_email) self.assertEqual(len(mail.outbox), 2) self.assertIn('Your email address changed!', mail.outbox[0].subject) self.assertEqual(mail.outbox[0].to, [old_email], 'error: change notice sent to wrong address') self.assertIn('Please verify your email', mail.outbox[1].subject) self.assertEqual(mail.outbox[1].to, [new_email], 'error: verification request sent to wrong address') self.assertNotIn('Thank you for signing up', mail.outbox[1].body) self.verified_user.refresh_from_db() self.assertFalse(self.verified_user.mail_verified) self.assertEqual(self.verified_user.email, old_email) self.assertEqual(self.verified_user.unverified_email, new_email) self.verified_user.verify_mail() self.verified_user.refresh_from_db() self.assertTrue(self.verified_user.mail_verified) self.assertEqual(self.verified_user.email, new_email) response = self.client.get(self.url) self.assertEqual(response.data['email'], new_email) def test_change_to_existing_mail_fails(self): self.client.force_login(user=self.verified_user) data = {'email': self.another_user.email} response = self.client.patch(self.url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(len(mail.outbox), 0) def test_change_to_existing_similar_mail_fails(self): self.client.force_login(user=self.verified_user) similar_mail = self.another_user.email[0].swapcase( ) + self.another_user.email[1:] data = {'email': similar_mail} response = self.client.patch(self.url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['email'], ['Similar e-mail exists: ' + self.another_user.email]) self.assertEqual(len(mail.outbox), 0) def test_change_to_similar_mail_succeeds(self): self.client.force_login(user=self.verified_user) similar_mail = self.verified_user.email[0].swapcase( ) + self.verified_user.email[1:] data = {'email': similar_mail} response = self.client.patch(self.url, data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(mail.outbox), 2) def test_change_back_to_previous_address(self): self.client.force_login(user=self.verified_user) original = self.verified_user.email response = self.client.patch(self.url, {'email': faker.email()}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(mail.outbox), 2) mail.outbox.clear() response = self.client.patch(self.url, {'email': original}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(mail.outbox), 2) self.assertIn('Your email address changed!', mail.outbox[0].subject) self.assertEqual(response.data['email'], original) self.assertEqual(response.data['mail_verified'], False) def test_dont_change_email(self): self.client.force_login(user=self.verified_user) response = self.client.patch(self.url, {'email': self.verified_user.email}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['mail_verified'], True) self.assertEqual(len(mail.outbox), 0) def test_changing_sensitive_fields_is_forbidden(self): self.client.force_login(user=self.verified_user) response = self.client.get(self.url) original = response.data response = self.client.patch(self.url, {'unverified_email': faker.email()}) self.assertEqual(response.data['unverified_email'], original['unverified_email']) response = self.client.patch(self.url, {'mail_verified': False}) self.assertEqual(response.data['mail_verified'], True)
def setUp(self): self.user = VerifiedUserFactory() self.group = GroupFactory(members=[self.user]) self.conversation = Conversation.objects.get_or_create_for_target(self.group) self.conversation.join(self.user) self.participant = ConversationParticipant.objects.get(conversation=self.conversation, user=self.user)
class TestChangeEMail(APITestCase): def setUp(self): self.verified_user = VerifiedUserFactory() self.another_user = VerifiedUserFactory() self.old_email = self.verified_user.email self.new_email = faker.email() self.password = self.verified_user.display_name self.url = '/api/auth/email/' self.url_patch = '/api/auth/user/' mail.outbox = [] def test_patch_fails(self): self.client.force_login(user=self.verified_user) self.assertTrue(self.verified_user.mail_verified) # typical frontend use case of getting, modifying and sending data data = self.client.get(self.url_patch).data data['email'] = self.new_email response = self.client.patch(self.url_patch, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(self.verified_user.email, self.verified_user.unverified_email) self.assertEqual(response.data['email'], self.old_email) self.assertEqual(len(mail.outbox), 0) def test_change_succeeds(self): self.client.force_login(user=self.verified_user) self.assertTrue(self.verified_user.mail_verified) response = self.client.put(self.url, { 'password': self.password, 'new_email': self.new_email }) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(mail.outbox), 2) self.assertIn('Your email address changed', mail.outbox[0].subject) self.assertEqual(mail.outbox[0].to, [self.old_email], 'error: change notice sent to wrong address') self.assertIn('Please verify your email', mail.outbox[1].subject) self.assertEqual(mail.outbox[1].to, [self.new_email], 'error: verification request sent to wrong address') self.assertNotIn('Thank you for signing up', mail.outbox[1].body) self.verified_user.refresh_from_db() self.assertFalse(self.verified_user.mail_verified) self.assertEqual(self.verified_user.email, self.old_email) self.assertEqual(self.verified_user.unverified_email, self.new_email) self.verified_user.verify_mail() self.verified_user.refresh_from_db() self.assertTrue(self.verified_user.mail_verified) self.assertEqual(self.verified_user.email, self.new_email) response = self.client.get(self.url_patch) self.assertEqual(response.data['email'], self.new_email) def test_change_without_password_fails(self): self.client.force_login(user=self.verified_user) response = self.client.put(self.url, {'new_email': self.new_email}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) response = self.client.put(self.url, { 'password': '', 'new_email': self.new_email }) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(len(mail.outbox), 0) def test_change_with_wrong_password_fails(self): self.client.force_login(user=self.verified_user) response = self.client.put(self.url, { 'password': '******', 'new_email': self.new_email }) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(len(mail.outbox), 0) def test_change_to_existing_mail_fails(self): self.client.force_login(user=self.verified_user) response = self.client.put(self.url, { 'password': self.password, 'new_email': self.another_user.email }) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(len(mail.outbox), 0) def test_change_to_existing_similar_mail_fails(self): self.client.force_login(user=self.verified_user) similar_mail = self.another_user.email[0].swapcase( ) + self.another_user.email[1:] response = self.client.put(self.url, { 'password': self.password, 'new_email': similar_mail }) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['new_email'], ['Similar e-mail exists: ' + self.another_user.email]) self.assertEqual(len(mail.outbox), 0) def test_change_to_similar_mail_succeeds(self): self.client.force_login(user=self.verified_user) similar_mail = self.verified_user.email[0].swapcase( ) + self.verified_user.email[1:] response = self.client.put(self.url, { 'password': self.password, 'new_email': similar_mail }) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(mail.outbox), 2) def test_restore_email_succeeds(self): self.client.force_login(user=self.verified_user) response = self.client.put(self.url, { 'password': self.password, 'new_email': self.new_email }) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(mail.outbox), 2) mail.outbox.clear() response = self.client.put(self.url, { 'password': self.password, 'new_email': self.old_email }) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(mail.outbox), 0) self.assertEqual(self.verified_user.email, self.old_email) self.assertEqual(self.verified_user.unverified_email, self.old_email) self.assertTrue(self.verified_user.mail_verified) def test_dont_change_email(self): self.client.force_login(user=self.verified_user) response = self.client.put(self.url, { 'password': self.password, 'new_email': self.old_email }) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(mail.outbox), 0) def test_changing_sensitive_fields_is_forbidden(self): self.client.force_login(user=self.verified_user) response = self.client.get(self.url_patch) original = response.data response = self.client.patch(self.url_patch, {'unverified_email': faker.email()}) self.assertEqual(response.data['unverified_email'], original['unverified_email']) response = self.client.patch(self.url_patch, {'mail_verified': False}) self.assertEqual(response.data['mail_verified'], True) def test_resend_verification_email(self): self.client.force_login(user=self.verified_user) response = self.client.put(self.url, { 'password': self.password, 'new_email': self.new_email }) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) mail.outbox = [] resend_url = '/api/auth/email/resend_verification_code/' response = self.client.post(resend_url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(mail.outbox), 1) self.assertIn('Please verify your email', mail.outbox[0].subject) self.assertEqual(mail.outbox[0].to, [self.new_email]) self.assertNotIn('Thank you for signing up', mail.outbox[0].body)
def setUp(self): self.user = VerifiedUserFactory() self.user2 = VerifiedUserFactory()
def setUp(self): self.user = UserFactory() self.verified_user = VerifiedUserFactory() self.url = '/api/auth/verify_mail/'
def setUpTestData(cls): super().setUpTestData() cls.verified_user = VerifiedUserFactory() cls.another_user = VerifiedUserFactory() cls.url = '/api/users/' cls.user_url = cls.url + str(cls.verified_user.id) + '/'
def make_application(): applicant = VerifiedUserFactory() group = GroupFactory(members=[self.member]) GroupApplicationFactory(group=group, user=applicant)
def setUp(self): self.verified_user = VerifiedUserFactory( email='*****@*****.**') self.url = '/api/auth/reset_password/' mail.outbox = []
def setUp(self): self.user = VerifiedUserFactory() self.group = GroupFactory(members=[self.user]) self.conversation = self.group.conversation self.participant = ConversationParticipant.objects.get(conversation=self.conversation, user=self.user)