Exemple #1
0
 def test_subscribe_called_with_correct_arguments(self,
                                                  mock_get_mailchimp_api):
     list_name = 'foo'
     user = UserFactory()
     mock_client = mock.MagicMock()
     mock_get_mailchimp_api.return_value = mock_client
     mock_client.lists.list.return_value = {
         'data': [{
             'id': 1,
             'list_name': list_name
         }]
     }
     list_id = mailchimp_utils.get_list_id_from_name(list_name)
     mailchimp_utils.subscribe_mailchimp(list_name, user._id)
     handlers.celery_teardown_request()
     mock_client.lists.subscribe.assert_called_with(
         id=list_id,
         email={'email': user.username},
         merge_vars={
             'fname': user.given_name,
             'lname': user.family_name,
         },
         double_optin=False,
         update_existing=True,
     )
Exemple #2
0
 def test_subscribe_fake_email_does_not_throw_validation_error(self, mock_get_mailchimp_api):
     list_name = 'foo'
     user = UserFactory(username='******')
     mock_client = mock.MagicMock()
     mock_get_mailchimp_api.return_value = mock_client
     mock_client.lists.list.return_value = {'data': [{'id': 1, 'list_name': list_name}]}
     mock_client.lists.subscribe.side_effect = mailchimp.ValidationError
     mailchimp_utils.subscribe_mailchimp(list_name, user._id)
     handlers.celery_teardown_request()
     assert_false(user.mailchimp_mailing_lists[list_name])
Exemple #3
0
 def test_unsubscribe_called_with_correct_arguments(self, mock_get_mailchimp_api):
     list_name = 'foo'
     user = UserFactory()
     mock_client = mock.MagicMock()
     mock_get_mailchimp_api.return_value = mock_client
     mock_client.lists.list.return_value = {'data': [{'id': 2, 'list_name': list_name}]}
     list_id = mailchimp_utils.get_list_id_from_name(list_name)
     mailchimp_utils.unsubscribe_mailchimp_async(list_name, user._id)
     handlers.celery_teardown_request()
     mock_client.lists.unsubscribe.assert_called_with(id=list_id, email={'email': user.username}, send_goodbye=True)
Exemple #4
0
 def test_unsubscribe_called_with_correct_arguments(self, mock_get_mailchimp_api):
     list_name = 'foo'
     user = UserFactory()
     mock_client = mock.MagicMock()
     mock_get_mailchimp_api.return_value = mock_client
     mock_client.lists.list.return_value = {'data': [{'id': 2, 'list_name': list_name}]}
     list_id = mailchimp_utils.get_list_id_from_name(list_name)
     mailchimp_utils.unsubscribe_mailchimp_async(list_name, user._id)
     handlers.celery_teardown_request()
     mock_client.lists.unsubscribe.assert_called_with(id=list_id, email={'email': user.username}, send_goodbye=True)
Exemple #5
0
 def test_subscribe_fake_email_does_not_throw_validation_error(self, mock_get_mailchimp_api):
     list_name = 'foo'
     user = UserFactory(username='******')
     mock_client = mock.MagicMock()
     mock_get_mailchimp_api.return_value = mock_client
     mock_client.lists.list.return_value = {'data': [{'id': 1, 'list_name': list_name}]}
     mock_client.lists.subscribe.side_effect = mailchimp.ValidationError
     mailchimp_utils.subscribe_mailchimp(list_name, user._id)
     handlers.celery_teardown_request()
     user.reload()
     assert_false(user.mailchimp_mailing_lists[list_name])
Exemple #6
0
 def test_subscribe_called_with_correct_arguments(self, mock_get_mailchimp_api):
     list_name = 'foo'
     user = UserFactory()
     mock_client = mock.MagicMock()
     mock_get_mailchimp_api.return_value = mock_client
     mock_client.lists.list.return_value = {'data': [{'id': 1, 'list_name': list_name}]}
     list_id = mailchimp_utils.get_list_id_from_name(list_name)
     mailchimp_utils.subscribe_mailchimp(list_name, user._id)
     handlers.celery_teardown_request()
     mock_client.lists.subscribe.assert_called_with(
         id=list_id,
         email={'email': user.username},
         merge_vars={
             'fname': user.given_name,
             'lname': user.family_name,
         },
         double_optin=False,
         update_existing=True,
     )
Exemple #7
0
 def process_response(self, request, response):
     """Clear the celery task queue if the response status code is 400 or above"""
     celery_after_request(response, base_status_code_error=400)
     celery_teardown_request()
     return response
Exemple #8
0
 def process_exception(self, request, exception):
     """If an exception occurs, clear the celery task queue so process_response has nothing."""
     sentry_exception_handler(request=request)
     celery_teardown_request(error=True)
     return None
Exemple #9
0
def run_celery_tasks():
    yield
    celery_teardown_request()
Exemple #10
0
def run_celery_tasks():
    yield
    celery_teardown_request()
Exemple #11
0
    def test_merge(self, mock_get_mailchimp_api):
        other_user = factories.UserFactory()
        other_user.save()

        # define values for users' fields
        today = datetime.datetime.now()
        yesterday = today - datetime.timedelta(days=1)

        self.user.comments_viewed_timestamp['shared_gt'] = today
        other_user.comments_viewed_timestamp['shared_gt'] = yesterday
        self.user.comments_viewed_timestamp['shared_lt'] = yesterday
        other_user.comments_viewed_timestamp['shared_lt'] = today
        self.user.comments_viewed_timestamp['user'] = yesterday
        other_user.comments_viewed_timestamp['other'] = yesterday

        self.user.email_verifications = {'user': {'email': 'a'}}
        other_user.email_verifications = {'other': {'email': 'b'}}

        self.user.external_accounts = [factories.ExternalAccountFactory()]
        other_user.external_accounts = [factories.ExternalAccountFactory()]

        self.user.mailchimp_mailing_lists = {
            'user': True,
            'shared_gt': True,
            'shared_lt': False,
        }
        other_user.mailchimp_mailing_lists = {
            'other': True,
            'shared_gt': False,
            'shared_lt': True,
        }

        self.user.piwik_token = 'abc'
        other_user.piwik_token = 'def'

        self.user.security_messages = {
            'user': today,
            'shared': today,
        }
        other_user.security_messages = {
            'other': today,
            'shared': today,
        }

        self.user.system_tags = ['user', 'shared']
        other_user.system_tags = ['other', 'shared']

        self.user.watched = [factories.WatchConfigFactory()]
        other_user.watched = [factories.WatchConfigFactory()]

        self.user.save()
        other_user.save()

        # define expected behavior for ALL FIELDS of the User object
        default_to_master_user_fields = [
            '_id', 'date_confirmed', 'date_disabled', 'date_last_login',
            'date_registered', 'family_name', 'fullname', 'given_name',
            'is_claimed', 'is_invited', 'is_registered', 'jobs', 'locale',
            'merged_by', 'middle_names', 'password', 'piwik_token',
            'recently_added', 'schools', 'social', 'suffix', 'timezone',
            'username', 'mailing_lists', 'verification_key',
            '_affiliated_institutions', 'contributor_added_email_records'
        ]

        calculated_fields = {
            'comments_viewed_timestamp': {
                'user': yesterday,
                'other': yesterday,
                'shared_gt': today,
                'shared_lt': today,
            },
            'email_verifications': {
                'user': {
                    'email': 'a'
                },
                'other': {
                    'email': 'b'
                },
            },
            'emails': [
                self.user.username,
                other_user.username,
            ],
            'external_accounts': [
                self.user.external_accounts[0]._id,
                other_user.external_accounts[0]._id,
            ],
            'mailchimp_mailing_lists': {
                'user': True,
                'other': True,
                'shared_gt': True,
                'shared_lt': True,
            },
            'osf_mailing_lists': {
                'Open Science Framework Help': True
            },
            'security_messages': {
                'user': today,
                'other': today,
                'shared': today,
            },
            'system_tags': ['user', 'shared', 'other'],
            'unclaimed_records': {},
            'watched': [
                self.user.watched[0]._id,
                other_user.watched[0]._id,
            ],
        }

        # from the explicit rules above, compile expected field/value pairs
        expected = {}
        expected.update(calculated_fields)
        for key in default_to_master_user_fields:
            expected[key] = getattr(self.user, key)

        # ensure all fields of the user object have an explicit expectation
        assert_equal(
            set(expected.keys()),
            set(self.user._fields),
        )

        # mock mailchimp
        mock_client = mock.MagicMock()
        mock_get_mailchimp_api.return_value = mock_client
        mock_client.lists.list.return_value = {
            'data': [{
                'id': x,
                'list_name': list_name
            } for x, list_name in enumerate(self.user.mailchimp_mailing_lists)]
        }

        # perform the merge
        self.user.merge_user(other_user)
        self.user.save()
        handlers.celery_teardown_request()

        # check each field/value pair
        for k, v in expected.iteritems():
            assert_equal(
                getattr(self.user, k),
                v,
                # "{} doesn't match expectation".format(k)
            )

        # check fields set on merged user
        assert_equal(other_user.merged_by, self.user)

        assert_equal(
            0,
            models.Session.find(Q('data.auth_user_id', 'eq',
                                  other_user._id)).count())
Exemple #12
0
 def process_response(self, request, response):
     """Clear the celery task queue if the response status code is 400 or above"""
     celery_after_request(response, base_status_code_error=400)
     celery_teardown_request()
     return response
Exemple #13
0
 def process_exception(self, request, exception):
     """If an exception occurs, clear the celery task queue so process_response has nothing."""
     sentry_exception_handler(request=request)
     celery_teardown_request(error=True)
     return None
logging.disable(level=logging.INFO)


def main(dry_run=True):
    if dry_run:
        logger.warn("DRY RUN mode")
    pending_approval_drafts = DraftRegistration.find()
    need_approval_drafts = [
        draft
        for draft in pending_approval_drafts
        if draft.approval and draft.requires_approval and draft.approval.state == Sanction.UNAPPROVED
    ]

    for draft in need_approval_drafts:
        sanction = draft.approval
        try:
            if not dry_run:
                sanction.state = Sanction.APPROVED
                sanction._on_complete(None)
                sanction.save()
            logger.warn("Approved {0}".format(draft._id))
        except Exception as e:
            logger.error(e)


if __name__ == "__main__":
    dry_run = "dry" in sys.argv
    app = init_app(routes=False)
    main(dry_run=dry_run)
    celery_teardown_request()
Exemple #15
0
    def test_merge(self, mock_get_mailchimp_api):
        other_user = factories.UserFactory()
        other_user.save()

        # define values for users' fields
        today = datetime.datetime.now()
        yesterday = today - datetime.timedelta(days=1)

        self.user.comments_viewed_timestamp['shared_gt'] = today
        other_user.comments_viewed_timestamp['shared_gt'] = yesterday
        self.user.comments_viewed_timestamp['shared_lt'] = yesterday
        other_user.comments_viewed_timestamp['shared_lt'] = today
        self.user.comments_viewed_timestamp['user'] = yesterday
        other_user.comments_viewed_timestamp['other'] = yesterday

        self.user.email_verifications = {'user': {'email': 'a'}}
        other_user.email_verifications = {'other': {'email': 'b'}}

        self.user.notifications_configured = {'abc12': True}
        other_user.notifications_configured = {'123ab': True}

        self.user.external_accounts = [factories.ExternalAccountFactory()]
        other_user.external_accounts = [factories.ExternalAccountFactory()]

        self.user.mailchimp_mailing_lists = {
            'user': True,
            'shared_gt': True,
            'shared_lt': False,
        }
        other_user.mailchimp_mailing_lists = {
            'other': True,
            'shared_gt': False,
            'shared_lt': True,
        }

        self.user.security_messages = {
            'user': today,
            'shared': today,
        }
        other_user.security_messages = {
            'other': today,
            'shared': today,
        }

        self.user.system_tags = ['user', 'shared']
        other_user.system_tags = ['other', 'shared']

        self.user.watched = [factories.WatchConfigFactory()]
        other_user.watched = [factories.WatchConfigFactory()]

        self.user.save()
        other_user.save()

        # define expected behavior for ALL FIELDS of the User object
        default_to_master_user_fields = [
            '_id',
            'date_confirmed',
            'date_disabled',
            'date_last_login',
            'date_registered',
            'email_last_sent',
            'external_identity',
            'family_name',
            'fullname',
            'given_name',
            'is_claimed',
            'is_invited',
            'is_registered',
            'jobs',
            'locale',
            'merged_by',
            'middle_names',
            'password',
            'recently_added',
            'schools',
            'social',
            'suffix',
            'timezone',
            'username',
            'mailing_lists',
            'verification_key',
            'verification_key_v2',
            '_affiliated_institutions',
            'contributor_added_email_records',
            'requested_deactivation',
            'registered_by'
        ]

        calculated_fields = {
            'comments_viewed_timestamp': {
                'user': yesterday,
                'other': yesterday,
                'shared_gt': today,
                'shared_lt': today,
            },
            'email_verifications': {
                'user': {'email': 'a'},
                'other': {'email': 'b'},
            },
            'notifications_configured': {
                '123ab': True, 'abc12': True,
            },
            'emails': [
                self.user.username,
                other_user.username,
            ],
            'external_accounts': [
                self.user.external_accounts[0]._id,
                other_user.external_accounts[0]._id,
            ],
            'mailchimp_mailing_lists': {
                'user': True,
                'other': True,
                'shared_gt': True,
                'shared_lt': True,
            },
            'osf_mailing_lists': {
                'Open Science Framework Help': True
            },
            'security_messages': {
                'user': today,
                'other': today,
                'shared': today,
            },
            'system_tags': ['user', 'shared', 'other'],
            'unclaimed_records': {},
            'watched': [
                self.user.watched[0]._id,
                other_user.watched[0]._id,
            ],
        }

        # from the explicit rules above, compile expected field/value pairs
        expected = {}
        expected.update(calculated_fields)
        for key in default_to_master_user_fields:
            expected[key] = getattr(self.user, key)

        # ensure all fields of the user object have an explicit expectation
        assert_equal(
            set(expected.keys()),
            set(self.user._fields),
        )

        # mock mailchimp
        mock_client = mock.MagicMock()
        mock_get_mailchimp_api.return_value = mock_client
        mock_client.lists.list.return_value = {'data': [{'id': x, 'list_name': list_name} for x, list_name in enumerate(self.user.mailchimp_mailing_lists)]}

        # perform the merge
        self.user.merge_user(other_user)
        self.user.save()
        handlers.celery_teardown_request()

        # check each field/value pair
        for k, v in expected.iteritems():
            assert_equal(
                getattr(self.user, k),
                v,
                # "{} doesn't match expectation".format(k)
            )

        # check fields set on merged user
        assert_equal(other_user.merged_by, self.user)

        assert_equal(
            0,
            models.Session.find(
                Q('data.auth_user_id', 'eq', other_user._id)
            ).count()
        )
logging.disable(level=logging.INFO)


def main(dry_run=True):
    if dry_run:
        logger.warn('DRY RUN mode')
    pending_approval_drafts = DraftRegistration.find()
    need_approval_drafts = [
        draft for draft in pending_approval_drafts
        if draft.approval and draft.requires_approval
        and draft.approval.state == Sanction.UNAPPROVED
    ]

    for draft in need_approval_drafts:
        sanction = draft.approval
        try:
            if not dry_run:
                sanction.state = Sanction.APPROVED
                sanction._on_complete(None)
                sanction.save()
            logger.warn('Approved {0}'.format(draft._id))
        except Exception as e:
            logger.error(e)


if __name__ == '__main__':
    dry_run = 'dry' in sys.argv
    app = init_app(routes=False)
    main(dry_run=dry_run)
    celery_teardown_request()