Beispiel #1
0
 def test_unknown_input_data_allowed(self):
     one = copy.deepcopy(_one_dict)
     one['foo'] = 'bar'
     addr = MailAddress(data=one, raise_on_unknown=False)
     out = addr.to_dict()
     self.assertIn('foo', out)
     self.assertEqual(out['foo'], one['foo'])
Beispiel #2
0
def verify_mail_address(state, proofing_user):
    """
    :param proofing_user: ProofingUser
    :param state: E-mail proofing state

    :type proofing_user: eduid_userdb.proofing.ProofingUser
    :type state: EmailProofingState

    :return: None

    """
    new_email = MailAddress(email=state.verification.email, application='email',
                            verified=True, primary=False)

    has_primary = proofing_user.mail_addresses.primary
    if has_primary is None:
        new_email.is_primary = True
    try:
        proofing_user.mail_addresses.add(new_email)
    except DuplicateElementViolation:
        proofing_user.mail_addresses.find(state.verification.email).is_verified = True
        if has_primary is None:
            proofing_user.mail_addresses.find(state.verification.email).is_primary = True

    mail_address_proofing = MailAddressProofing(proofing_user, created_by='email', mail_address=new_email.email,
                                                reference=state.reference, proofing_version='2013v1')
    if current_app.proofing_log.save(mail_address_proofing):
        save_and_sync_user(proofing_user)
        current_app.logger.info('Email address {!r} confirmed '
                                'for user {}'.format(state.verification.email, proofing_user))
        current_app.stats.count(name='email_verify_success', value=1)
        current_app.proofing_statedb.remove_state(state)
        current_app.logger.debug('Removed proofing state: {} '.format(state))
Beispiel #3
0
    def test_post_email_duplicate(self, mock_code_verification, mock_request_user_sync):
        response = self.browser.post('/new')
        self.assertEqual(response.status_code, 302)  # Redirect to token service

        mock_code_verification.return_value = u'123456'
        mock_request_user_sync.side_effect = self.request_user_sync
        eppn = self.test_user_data['eduPersonPrincipalName']
        email = '*****@*****.**'

        # Save unverified mail address for test user
        user = self.app.central_userdb.get_user_by_eppn(eppn)
        MailAddress(email=email, application='email', verified=False, primary=False)
        user.mail_addresses.add(MailAddress(email=email, application='email', verified=False, primary=False))
        self.app.central_userdb.save(user, check_sync=False)

        with self.session_cookie(self.browser, eppn) as client:
            with client.session_transaction() as sess:

                with self.app.test_request_context():
                    data = {
                        'email': email,
                        'verified': False,
                        'primary': False,
                        'csrf_token': sess.get_csrf_token()
                    }

                response2 = client.post('/new', data=json.dumps(data),  content_type=self.content_type_json)
                self.assertEqual(response2.status_code, 200)
                new_email_data = json.loads(response2.data)

                self.assertEqual(new_email_data['type'], 'POST_EMAIL_NEW_FAIL')
                self.assertEqual(new_email_data['payload']['error']['email'][0], 'emails.duplicated')
Beispiel #4
0
 def _add_2_emails_1_verified(self, user):
     verified = MailAddress.from_dict(
         dict(email='*****@*****.**', created_by='test', verified=True, primary=True)
     )
     verified2 = MailAddress.from_dict(
         dict(email='*****@*****.**', created_by='test', verified=False, primary=False)
     )
     user.mail_addresses.add(verified)
     user.mail_addresses.add(verified2)
def _add_mail_to_user(email, user):
    """
    Add an email address to a user.
    Part of set_email_verified() above.
    """
    new_email = MailAddress(email = email, application = 'dashboard',
                            verified = True, primary = False)
    if user.mail_addresses.primary is None:
        new_email.is_primary = True
    try:
        user.mail_addresses.add(new_email)
    except DuplicateElementViolation:
        user.mail_addresses.find(email).is_verified = True
Beispiel #6
0
def _add_mail_to_user(email, user):
    """
    Add an email address to a user.
    Part of set_email_verified() above.
    """
    new_email = MailAddress(email = email, application = 'dashboard',
                            verified = True, primary = False)
    if user.mail_addresses.primary is None:
        new_email.is_primary = True
    try:
        user.mail_addresses.add(new_email)
    except DuplicateElementViolation:
        user.mail_addresses.find(email).is_verified = True
Beispiel #7
0
def post_email(user, email, verified, primary):
    proofing_user = ProofingUser.from_user(user, current_app.private_userdb)
    current_app.logger.debug('Trying to save unconfirmed email {!r} '
                             'for user {}'.format(email, proofing_user))

    new_mail = MailAddress.from_dict(
        dict(email=email, created_by='email', verified=False, primary=False))

    try:
        proofing_user.mail_addresses.add(new_mail)
    except DuplicateElementViolation:
        return error_response(message=EmailMsg.dupe)

    try:
        save_and_sync_user(proofing_user)
    except UserOutOfSync:
        current_app.logger.debug('Couldnt save email {} for user {}, '
                                 'data out of sync'.format(
                                     email, proofing_user))
        return error_response(message=CommonMsg.out_of_sync)
    current_app.logger.info('Saved unconfirmed email {!r} '
                            'for user {}'.format(email, proofing_user))
    current_app.stats.count(name='email_save_unconfirmed_email', value=1)

    sent = send_verification_code(email, proofing_user)
    emails = {'emails': proofing_user.mail_addresses.to_list_of_dicts()}
    email_list = EmailListPayload().dump(emails)

    if not sent:
        return success_response(payload=email_list,
                                message=EmailMsg.added_and_throttled)

    current_app.stats.count(name='email_send_verification_code', value=1)

    return success_response(payload=email_list, message=EmailMsg.saved)
Beispiel #8
0
def post_email(user, email, verified, primary):
    proofing_user = ProofingUser.from_user(user, current_app.private_userdb)
    current_app.logger.debug('Trying to save unconfirmed email {!r} '
                             'for user {}'.format(email, proofing_user))

    new_mail = MailAddress(email=email,
                           application='email',
                           verified=False,
                           primary=False)

    try:
        proofing_user.mail_addresses.add(new_mail)
    except DuplicateElementViolation:
        return {'_status': 'error', 'message': 'emails.duplicated'}

    try:
        save_and_sync_user(proofing_user)
    except UserOutOfSync:
        current_app.logger.debug('Couldnt save email {} for user {}, '
                                 'data out of sync'.format(
                                     email, proofing_user))
        return {'_status': 'error', 'message': 'user-out-of-sync'}
    current_app.logger.info('Saved unconfirmed email {!r} '
                            'for user {}'.format(email, proofing_user))
    current_app.stats.count(name='email_save_unconfirmed_email', value=1)

    send_verification_code(email, proofing_user)
    current_app.stats.count(name='email_send_verification_code', value=1)

    emails = {
        'emails': proofing_user.mail_addresses.to_list_of_dicts(),
        'message': 'emails.save-success'
    }
    return EmailListPayload().dump(emails).data
Beispiel #9
0
    def add_success(self, emailform):
        newemail = self.schema.serialize(emailform)

        new_email = MailAddress(email=newemail['mail'],
                                application='dashboard',
                                verified=False,
                                primary=False)

        self.user = get_session_user(self.request)
        self.user.mail_addresses.add(new_email)
        try:
            self.context.save_dashboard_user(self.user)
        except UserOutOfSync:
            self.sync_user()

        else:
            message = _('Changes saved')
            self.request.session.flash(get_localizer(
                self.request).translate(message),
                                       queue='forms')

            send_verification_mail(self.request, newemail['mail'])

            second_msg = _(
                'A confirmation email has been sent to your email '
                'address. Please enter your confirmation code '
                '<a href="#" class="verifycode" '
                'data-identifier="${id}">here</a>.',
                mapping={'id': self.user.mail_addresses.count})
            self.request.session.flash(get_localizer(
                self.request).translate(second_msg),
                                       queue='forms')
def verify_mail_address(state, proofing_user):
    """
    :param proofing_user: ProofingUser
    :param state: E-mail proofing state

    :type proofing_user: eduid_userdb.proofing.ProofingUser
    :type state: EmailProofingState

    :return: None

    """
    new_email = MailAddress(email=state.verification.email,
                            application='email',
                            verified=True,
                            primary=False)

    has_primary = proofing_user.mail_addresses.primary
    if has_primary is None:
        new_email.is_primary = True
    try:
        proofing_user.mail_addresses.add(new_email)
    except DuplicateElementViolation:
        proofing_user.mail_addresses.find(
            state.verification.email).is_verified = True
        if has_primary is None:
            proofing_user.mail_addresses.find(
                state.verification.email).is_primary = True

    mail_address_proofing = MailAddressProofing(proofing_user,
                                                created_by='email',
                                                mail_address=new_email.email,
                                                reference=state.reference,
                                                proofing_version='2013v1')
    if current_app.proofing_log.save(mail_address_proofing):
        save_and_sync_user(proofing_user)
        current_app.logger.info('Email address {!r} confirmed '
                                'for user {}'.format(state.verification.email,
                                                     proofing_user))
        current_app.stats.count(name='email_verify_success', value=1)
        current_app.proofing_statedb.remove_state(state)
        current_app.logger.debug('Removed proofing state: {} '.format(state))
Beispiel #11
0
    def test_post_email_duplicate(self):
        eppn = self.test_user_data['eduPersonPrincipalName']
        email = '*****@*****.**'

        # Save unverified mail address for test user
        user = self.app.central_userdb.get_user_by_eppn(eppn)
        mail_address = MailAddress.from_dict(dict(email=email, created_by='email', verified=False, primary=False))
        user.mail_addresses.add(mail_address)
        self.app.central_userdb.save(user, check_sync=False)

        response = self._post_email()
        self.assertEqual(response.status_code, 200)
        new_email_data = json.loads(response.data)

        self.assertEqual(new_email_data['type'], 'POST_EMAIL_NEW_FAIL')
        self.assertEqual(new_email_data['payload']['error']['email'][0], 'emails.duplicated')
Beispiel #12
0
 def test_unknown_input_data(self):
     one = copy.deepcopy(_one_dict)
     one['foo'] = 'bar'
     with self.assertRaises(eduid_userdb.exceptions.UserHasUnknownData):
         MailAddress(data=one)