Exemple #1
0
def handler_from_email(email_address):
    if '@mit.edu' in email_address:
        user, domain = email_address.split('@')
        email_address = user + '@exchange.mit.edu'

    provider_name = provider_from_address(email_address)
    return handler_from_provider(provider_name)
Exemple #2
0
def handler_from_email(email_address):
    if '@mit.edu' in email_address:
        user, domain = email_address.split('@')
        email_address = user + '@exchange.mit.edu'

    provider_name = provider_from_address(email_address)
    return handler_from_provider(provider_name)
Exemple #3
0
def get_provider(email):
    output = {}    
    output['email'] = email
    output['provider'] = provider_from_address(email)
    if output['provider'] in providers:
        output['provider_info'] = providers[output['provider']]
    return jsonify(output)
Exemple #4
0
def test_sending(client):
    # because of T478 these tests don't pass on iCloud
    if provider_from_address(client.email_address) == "icloud":
        print "Sending disabled for iCloud because of their sent behavior"
        return
    # Create a message and send it to ourselves
    subject = "%s (Self Send Test)" % strftime("%Y-%m-%d %H:%M:%S")
    draft = client.drafts.create(to=[{"email": client.email_address}],
                                 subject=subject,
                                 body=subject + "Test email.")

    body = random_words(sig=client.email_address.split('@')[0])

    draft = client.drafts.create(to=[{"email": client.email_address}],
                                 subject=subject,
                                 body=body)
    draft.send()
    wait_for_send(client, subject)

    # Archive the message
    thread = client.threads.where(subject=subject, tag='inbox').first()
    thread.archive()
    wait_for_archive(client, thread.id)

    # Trash the message (Raises notimplementederror)
    # remove False guard when working
    if False:
        client.threads.first().trash()
        wait_for_trash(client, thread.id)
Exemple #5
0
def get_handler(email_address):
    register_backends()

    provider = provider_from_address(email_address)
    auth_mod = AUTH_MOD_FOR.get(provider)

    if auth_mod is None:
        raise NotSupportedError('Inbox currently only supports Gmail and Yahoo.')
        sys.exit(1)

    return auth_mod
Exemple #6
0
def get_handler(email_address):
    register_backends()

    provider = provider_from_address(email_address)
    auth_mod = AUTH_MOD_FOR.get(provider)

    if auth_mod is not None:
        return auth_mod

    # Try as EAS
    auth_mod = AUTH_MOD_FOR.get('EAS', None)
    if auth_mod is not None:
        return auth_mod

    raise NotSupportedError('Inbox does not support the email provider.')
Exemple #7
0
def get_handler(email_address):
    register_backends()

    provider = provider_from_address(email_address)
    auth_mod = AUTH_MOD_FOR.get(provider)

    if auth_mod is not None:
        return auth_mod

    # Try as EAS
    auth_mod = AUTH_MOD_FOR.get('EAS', None)
    if auth_mod is not None:
        return auth_mod

    raise NotSupportedError('Inbox does not support the email provider.')
Exemple #8
0
def wait_for_send(client, subject):
    thread_query = client.threads.where(subject=subject)

    threads = thread_query.all()

    if not threads:
        return False
    if provider_from_address(client.email_address) not in ['unknown', 'eas']:
        # Reconciliation doesn't seem to quite work on EAS because the
        # X-INBOX-ID header is stripped?
        assert len(threads) == 1, \
            "Warning: Number of threads for unique subject is > 1!"

    tags = [t['name'] for thread in threads for t in thread.tags]
    return True if ("sent" in tags and "inbox" in tags) else False
def wait_for_send(client, subject):
    thread_query = client.threads.where(subject=subject)

    threads = thread_query.all()

    if not threads:
        return False
    if provider_from_address(client.email_address) not in ['unknown', 'eas']:
        # Reconciliation doesn't seem to quite work on EAS because the
        # X-INBOX-ID header is stripped?
        assert len(threads) == 1, \
            "Warning: Number of threads for unique subject is > 1!"

    tags = [t['name'] for thread in threads for t in thread.tags]
    return True if ("sent" in tags and "inbox" in tags) else False
Exemple #10
0
    def f(*args, **kwargs):
        for email, password in passwords:
            # FIXME: Don't create the account if it's already created. --cg3
            with session_scope() as db_session:
                create_account(db_session, email, password)

            client = InboxTestClient(email)
            wait_for_auth(client)

            # wait for sync to start. tests rely on things setup at beginning
            # of sync (e.g. folder hierarchy)
            wait_for_sync_start(client)
            start_time = time()
            fn(client, *args, **kwargs)
            format_test_result(fn.__name__, provider_from_address(email),
                               email, start_time)
Exemple #11
0
def confim_oauth_user():
    response = {}
    encoder = APIEncoder()
    data = request.get_json(force=True)
    email_address = data.get('email_address')
    token = data.get('token')
    target = data.get('target', 0)

    if not email_address:
        response['error'] = 'Missing key - "email_address"!'
        return encoder.jsonify(response)
    if not token:
        response['error'] = 'Missing key - "token"!'
        return encoder.jsonify(response)

    shard_id = target << 48

    with session_scope(shard_id) as db_session:
        account = db_session.query(Account).filter_by(
            email_address=email_address).first()
        if account is None:
            response['error'] = 'Don\'t have this account!'
            return encoder.jsonify(response)
        auth_info = {}
        provider = provider_from_address(email_address)
        auth_info['provider'] = provider
        auth_handler = handler_from_provider(provider)
        try:
            auth_response = auth_handler._get_authenticated_user(token)
            auth_response['contacts'] = True
            auth_response['events'] = True
            auth_info.update(auth_response)
        except OAuthError:
            response['error'] = "Invalid authorization code, try again..."
            return encoder.jsonify(response)
        account = auth_handler.update_account(account, auth_info)
        try:
            if auth_handler.verify_account(account):
                db_session.add(account)
                db_session.commit()
                response['data'] = 'OK. Authenticated account for {}'.format(
                    email_address)
        except NotSupportedError as e:
            response['error'] = str(e)
            return encoder.jsonify(response)
    return encoder.jsonify(response)
Exemple #12
0
def create_account(db_session, email_address, response):
    provider_name = provider_from_address(email_address)
    if provider_name == "unknown":
        raise NotSupportedError('Inbox does not support the email provider.')

    try:
        account = db_session.query(GenericAccount).filter_by(
            email_address=email_address).one()
    except sqlalchemy.orm.exc.NoResultFound:
        namespace = Namespace()
        account = GenericAccount(namespace=namespace)

    account.email_address = response['email']
    account.password = response['password']
    account.date = datetime.datetime.utcnow()
    account.provider = provider_name

    return account
Exemple #13
0
def create_account(db_session, email_address, response):
    provider_name = provider_from_address(email_address)
    if provider_name == "unknown":
        raise NotSupportedError('Inbox does not support the email provider.')

    try:
        account = db_session.query(GenericAccount).filter_by(
            email_address=email_address).one()
    except sqlalchemy.orm.exc.NoResultFound:
        namespace = Namespace()
        account = GenericAccount(namespace=namespace)

    account.email_address = response['email']
    account.password = response['password']
    account.date = datetime.datetime.utcnow()
    account.provider = provider_name

    return account
Exemple #14
0
def create_account(db_session, email, password):
    provider = provider_from_address(email)
    auth_handler = handler_from_email(email)
    # Special-case Gmail and Outlook, because we need to provide an oauth token
    # and not merely a password.
    if provider == 'gmail':
        token = google_auth(email, password)
        account = create_gmail_account(db_session, email, token, False)
    elif provider == 'outlook':
        token = outlook_auth(email, password)
        account = create_outlook_account(db_session, email, token, False)
    else:
        response = {"email": email, "password": password}
        account = auth_handler.create_account(db_session, email, response)

    auth_handler.verify_account(account)

    db_session.add(account)
    db_session.commit()
def test_provider_resolution(mock_dns_resolver):
    mock_dns_resolver._load_records(
        "tests/data/general_test_provider_resolution.json")
    test_cases = [
        ("*****@*****.**", "unknown"),
        ("*****@*****.**", "unknown"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "yahoo"),
        ("*****@*****.**", "yahoo"),
        ("*****@*****.**", "microsoft"),
        ("*****@*****.**", "microsoft"),
        ("*****@*****.**", "aol"),
        ("*****@*****.**", "aol"),
        ("*****@*****.**", "aol"),
        ("*****@*****.**", "microsoft"),
        ("*****@*****.**", "fastmail"),
        ("*****@*****.**", "fastmail"),
        ("*****@*****.**", "fastmail"),
        ("*****@*****.**", "hover"),
        ("*****@*****.**", "yahoo"),
        ("*****@*****.**", "yandex"),
        ("*****@*****.**", "zimbra"),
        ("*****@*****.**", "icloud"),
        ("*****@*****.**", "icloud"),
        ("*****@*****.**", "gmx"),
        ("*****@*****.**", "gandi"),
        ("*****@*****.**", "gandi"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "microsoft"),
        ("*****@*****.**", "microsoft"),
        ("*****@*****.**", "unknown"),
        ("*****@*****.**", "bluehost"),
    ]
    for email, expected_provider in test_cases:
        assert (provider_from_address(
            email, lambda: mock_dns_resolver) == expected_provider)

    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("notanemail", lambda: mock_dns_resolver)
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("not@anemail", lambda: mock_dns_resolver)
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("notanemail.com", lambda: mock_dns_resolver)
Exemple #16
0
def create_account(db_session, email, password):
    provider = provider_from_address(email)
    auth_handler = handler_from_email(email)
    # Special-case Gmail and Outlook, because we need to provide an oauth token
    # and not merely a password.
    if provider == 'gmail':
        token = google_auth(email, password)
        account = create_gmail_account(db_session, email, token, False)
    elif provider == 'outlook':
        token = outlook_auth(email, password)
        account = create_outlook_account(db_session, email, token, False)
    else:
        response = {"email": email, "password": password}
        account = auth_handler.create_account(db_session, email, response)

    auth_handler.verify_account(account)

    db_session.add(account)
    db_session.commit()
    return account
Exemple #17
0
def test_provider_resolution(mock_dns_resolver):
    mock_dns_resolver._load_records('inbox', 'test/data/general_test_provider_resolution.json')
    test_cases = [
        ('*****@*****.**', 'unknown'),
        ('*****@*****.**', 'unknown'),
        ('*****@*****.**', 'gmail'),
        ('*****@*****.**', 'gmail'),
        ('*****@*****.**', 'yahoo'),
        ('*****@*****.**', 'yahoo'),
        ('*****@*****.**', 'outlook'),
        ('*****@*****.**', 'outlook'),
        ('*****@*****.**', 'aol'),
        ('*****@*****.**', 'aol'),
        ('*****@*****.**', 'aol'),
        ('*****@*****.**', 'eas'),
        ('*****@*****.**', 'fastmail'),
        ('*****@*****.**', 'fastmail'),
        ('*****@*****.**', 'fastmail'),
        ('*****@*****.**', 'hover'),
        ('*****@*****.**', 'yahoo'),
        ('*****@*****.**', 'yandex'),
        ('*****@*****.**', 'zimbra'),
        ('*****@*****.**', 'icloud'),
        ('*****@*****.**', 'icloud'),
        ('*****@*****.**', 'gmx'),
        ('*****@*****.**', 'gandi'),
        ('*****@*****.**', 'gandi'),
        ('*****@*****.**', 'gmail'),
        ('*****@*****.**', 'gmail'),
        ('*****@*****.**', 'eas'),
        ('*****@*****.**', 'eas'),
        ('*****@*****.**', 'unknown'),
        ('*****@*****.**', 'bluehost'),
    ]
    for email, expected_provider in test_cases:
        assert provider_from_address(email, lambda: mock_dns_resolver) == expected_provider

    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail', lambda: mock_dns_resolver)
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail', lambda: mock_dns_resolver)
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com', lambda: mock_dns_resolver)
def test_provider_resolution(mock_dns_resolver):
    mock_dns_resolver._load_records("tests/data/general_test_provider_resolution.json")
    test_cases = [
        ("*****@*****.**", "unknown"),
        ("*****@*****.**", "unknown"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "yahoo"),
        ("*****@*****.**", "yahoo"),
        ("*****@*****.**", "outlook"),
        ("*****@*****.**", "outlook"),
        ("*****@*****.**", "aol"),
        ("*****@*****.**", "aol"),
        ("*****@*****.**", "aol"),
        ("*****@*****.**", "eas"),
        ("*****@*****.**", "fastmail"),
        ("*****@*****.**", "fastmail"),
        ("*****@*****.**", "fastmail"),
        ("*****@*****.**", "hover"),
        ("*****@*****.**", "yahoo"),
        ("*****@*****.**", "yandex"),
        ("*****@*****.**", "zimbra"),
        ("*****@*****.**", "icloud"),
        ("*****@*****.**", "icloud"),
        ("*****@*****.**", "gmx"),
        ("*****@*****.**", "gandi"),
        ("*****@*****.**", "gandi"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "gmail"),
        ("*****@*****.**", "eas"),
        ("*****@*****.**", "eas"),
        ("*****@*****.**", "unknown"),
        ("*****@*****.**", "bluehost"),
    ]
    for email, expected_provider in test_cases:
        assert provider_from_address(email, lambda: mock_dns_resolver) == expected_provider

    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("notanemail", lambda: mock_dns_resolver)
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("not@anemail", lambda: mock_dns_resolver)
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("notanemail.com", lambda: mock_dns_resolver)
Exemple #19
0
def create_account(db_session, email, password):
    provider = provider_from_address(email)
    auth_handler = handler_from_provider(provider)
    # Special-case Gmail and Outlook, because we need to provide an oauth token
    # and not merely a password.
    response = {'email': email}
    if provider == 'gmail':
        code = google_auth(email, password)
        response = auth_handler._get_authenticated_user(code)
    elif provider == 'outlook':
        code = outlook_auth(email, password)
        response = auth_handler._get_authenticated_user(code)
    else:
        response = {"email": email, "password": password}

    account = auth_handler.create_account(db_session, email, response)
    auth_handler.verify_account(account)

    db_session.add(account)
    db_session.commit()
    return account
Exemple #20
0
def main(email_address, reauth, target, provider):
    """ Auth an email account. """
    preflight()

    maybe_enable_rollbar()

    shard_id = target << 48

    with session_scope(shard_id) as db_session:
        account = (db_session.query(Account).filter_by(
            email_address=email_address).first())
        if account is not None and not reauth:
            sys.exit("Already have this account!")

        if not provider:
            provider = provider_from_address(email_address)

            # Resolve unknown providers into either custom IMAP or EAS.
            if provider == "unknown":
                is_imap = raw_input(
                    "IMAP account? [Y/n] ").strip().lower() != "n"
                provider = "custom" if is_imap else "eas"

        auth_handler = handler_from_provider(provider)
        account_data = auth_handler.interactive_auth(email_address)

        if reauth:
            account = auth_handler.update_account(account, account_data)
        else:
            account = auth_handler.create_account(account_data)

        try:
            if auth_handler.verify_account(account):
                db_session.add(account)
                db_session.commit()
        except NotSupportedError as e:
            sys.exit(str(e))

    print("OK. Authenticated account for {}".format(email_address))
Exemple #21
0
def create_account(db_session, email, password):
    provider = provider_from_address(email)
    auth_handler = handler_from_provider(provider)
    # Special-case Gmail and Outlook, because we need to provide an oauth token
    # and not merely a password.
    response = {"email": email}
    if provider == "gmail":
        code = google_auth(email, password)
        response = auth_handler._get_authenticated_user(code)
    elif provider == "outlook":
        code = outlook_auth(email, password)
        response = auth_handler._get_authenticated_user(code)
    else:
        response = {"email": email, "password": password}

    account = auth_handler.create_account(email, response)
    auth_handler.verify_account(account)
    account.throttled = False
    account.sync_host = platform.node()
    account.desired_sync_host = platform.node()
    db_session.add(account)
    db_session.commit()
    return account
Exemple #22
0
def provider_from_email():
    g.parser.add_argument('email',
                          required=True,
                          type=bounded_str,
                          location='args')
    args = strict_parse_args(g.parser, request.args)

    try:
        provider_name = provider_from_address(args['email'])
        provider_info = providers[
            provider_name] if provider_name != 'unknown' else 'unknown'

        resp = simplejson.dumps({
            'provider_name': provider_name,
            'provider_info': provider_info
        })

        return make_response((resp, 200, {'Content-Type': 'application/json'}))
    except NotSupportedError as e:
        resp = simplejson.dumps({
            'message': str(e),
            'type': 'custom_api_error'
        })
        return make_response((resp, 400, {'Content-Type': 'application/json'}))
def test_provider_resolution():
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'gmx'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'unknown'

    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com')

    try:
        # Registering a new provider
        assert provider_from_address('*****@*****.**') == 'unknown'
        providers.register_info(
            'example', {
                "type": "generic",
                "imap": ("mail.example.net", 993),
                "smtp": ("smtp.example.net", 587),
                "auth": "password",
                "domains": ["example.com"],
                "mx_servers": ["mx.example.net"]
            })
        assert provider_from_address('*****@*****.**') == 'example'

        # Registering some filters
        def aol_filter(info, provider, email):
            info['domains'].append('example.net')

        def wildcard_filter(info, provider, email):
            if provider == 'zimbra':
                info['domains'].append('example.org')

        assert provider_from_address('*****@*****.**') == 'unknown'
        assert provider_from_address('*****@*****.**') == 'unknown'
        providers.register_info_filter('aol', aol_filter)
        providers.register_info_filter(None, wildcard_filter)
        assert provider_from_address('*****@*****.**') == 'aol'
        assert provider_from_address('*****@*****.**') == 'zimbra'

        # Modifying provider info based on the email address
        def email_address_filter(info, provider, email):
            if email == '*****@*****.**':
                info['imap'] = ('imap2.example.com', 994)

        orig_imap = tuple(providers['yahoo']['imap'])

        assert (providers.lookup_info(
            'yahoo', '*****@*****.**')['imap'] == orig_imap)
        assert (providers.lookup_info(
            'yahoo', '*****@*****.**')['imap'] == orig_imap)
        providers.register_info_filter(None, email_address_filter)
        assert (providers.lookup_info(
            'yahoo',
            '*****@*****.**')['imap'] == ('imap2.example.com', 994))
        assert (providers.lookup_info(
            'yahoo', '*****@*****.**')['imap'] == orig_imap)
    finally:
        providers.reset()
def test_provider_resolution():
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'gmx'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'eas'
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com')

    try:
        # Registering a new provider
        assert provider_from_address('*****@*****.**') == 'unknown'
        providers.register_info('example', {
            "type": "generic",
            "imap": ("mail.example.net", 993),
            "smtp": ("smtp.example.net", 587),
            "auth": "password",
            "domains": ["example.com"],
            "mx_servers": ["mx.example.net"]
        })
        assert provider_from_address('*****@*****.**') == 'example'

        # Registering some filters
        def aol_filter(info, provider, email):
            info['domains'].append('example.net')

        def wildcard_filter(info, provider, email):
            if provider == 'zimbra':
                info['domains'].append('example.org')

        assert provider_from_address('*****@*****.**') == 'unknown'
        assert provider_from_address('*****@*****.**') == 'unknown'
        providers.register_info_filter('aol', aol_filter)
        providers.register_info_filter(None, wildcard_filter)
        assert provider_from_address('*****@*****.**') == 'aol'
        assert provider_from_address('*****@*****.**') == 'zimbra'

        # Modifying provider info based on the email address
        def email_address_filter(info, provider, email):
            if email == '*****@*****.**':
                info['imap'] = ('imap2.example.com', 994)
        orig_imap = tuple(providers['yahoo']['imap'])

        assert (providers.lookup_info('yahoo', '*****@*****.**')['imap'] ==
                orig_imap)
        assert (providers.lookup_info('yahoo', '*****@*****.**')['imap'] ==
                orig_imap)
        providers.register_info_filter(None, email_address_filter)
        assert (providers.lookup_info('yahoo', '*****@*****.**')['imap'] ==
                ('imap2.example.com', 994))
        assert (providers.lookup_info('yahoo', '*****@*****.**')['imap'] ==
                orig_imap)
    finally:
        providers.reset()
def test_provider_resolution():
    assert provider_from_address("*****@*****.**") == "unknown"
    assert provider_from_address("*****@*****.**") == "unknown"
    assert provider_from_address("*****@*****.**") == "gmail"
    assert provider_from_address("*****@*****.**") == "yahoo"
    assert provider_from_address("*****@*****.**") == "yahoo"
    assert provider_from_address("*****@*****.**") == "outlook"
    assert provider_from_address("*****@*****.**") == "outlook"
    assert provider_from_address("*****@*****.**") == "aol"
    assert provider_from_address("*****@*****.**") == "aol"
    assert provider_from_address("*****@*****.**") == "aol"
    assert provider_from_address("*****@*****.**") == "eas"
    assert provider_from_address("*****@*****.**") == "fastmail"
    assert provider_from_address("*****@*****.**") == "fastmail"
    assert provider_from_address("*****@*****.**") == "fastmail"
    assert provider_from_address("*****@*****.**") == "icloud"
    assert provider_from_address("*****@*****.**") == "icloud"
    assert provider_from_address("*****@*****.**") == "gmx"
    assert provider_from_address("*****@*****.**") == "gandi"
    assert provider_from_address("*****@*****.**") == "gandi"
    assert provider_from_address("*****@*****.**") == "eas"
    assert provider_from_address("*****@*****.**") == "gmail"
    assert provider_from_address("*****@*****.**") == "gmail"
    assert provider_from_address("*****@*****.**") == "eas"
    assert provider_from_address("*****@*****.**") == "eas"
    assert provider_from_address("*****@*****.**") == "unknown"

    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("notanemail")
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("not@anemail")
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address("notanemail.com")

    try:
        # Registering a new provider
        assert provider_from_address("*****@*****.**") == "unknown"
        providers.register_info(
            "example",
            {
                "type": "generic",
                "imap": ("mail.example.net", 993),
                "smtp": ("smtp.example.net", 587),
                "auth": "password",
                "domains": ["example.com"],
                "mx_servers": ["mx.example.net"],
            },
        )
        assert provider_from_address("*****@*****.**") == "example"

        # Registering some filters
        def aol_filter(info, provider, email):
            info["domains"].append("example.net")

        def wildcard_filter(info, provider, email):
            if provider == "zimbra":
                info["domains"].append("example.org")

        assert provider_from_address("*****@*****.**") == "unknown"
        assert provider_from_address("*****@*****.**") == "unknown"
        providers.register_info_filter("aol", aol_filter)
        providers.register_info_filter(None, wildcard_filter)
        assert provider_from_address("*****@*****.**") == "aol"
        assert provider_from_address("*****@*****.**") == "zimbra"

        # Modifying provider info based on the email address
        def email_address_filter(info, provider, email):
            if email == "*****@*****.**":
                info["imap"] = ("imap2.example.com", 994)

        orig_imap = tuple(providers["yahoo"]["imap"])

        assert providers.lookup_info("yahoo", "*****@*****.**")["imap"] == orig_imap
        assert providers.lookup_info("yahoo", "*****@*****.**")["imap"] == orig_imap
        providers.register_info_filter(None, email_address_filter)
        assert providers.lookup_info("yahoo", "*****@*****.**")["imap"] == ("imap2.example.com", 994)
        assert providers.lookup_info("yahoo", "*****@*****.**")["imap"] == orig_imap
    finally:
        providers.reset()
Exemple #26
0
def test_provider_resolution():
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'gmx'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gandi'
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com')
Exemple #27
0
def handler_from_email(email_address):
    return handler_from_provider(provider_from_address(email_address))
Exemple #28
0
def add_new_user():
    response = {}
    encoder = APIEncoder()

    data = request.get_json(force=True)
    email_address = data.get('email_address')
    password = data.get('password')
    auth_details = data.get('auth_details')
    reauth = data.get('reauth')
    target = data.get('target', 0)

    if not email_address:
        response['error'] = 'Missing key - "email_address"!'
        return encoder.jsonify(response)

    shard_id = target << 48

    with session_scope(shard_id) as db_session:
        account = db_session.query(Account).filter_by(
            email_address=email_address).first()
        if account is not None and not reauth:
            response['error'] = 'Already have this account!'
            return encoder.jsonify(response)

        auth_info = {}
        provider = provider_from_address(email_address)
        if 'gmail' in provider:
            auth_handler = handler_from_provider(provider)
            response['oauth_url'] = auth_handler.get_oauth_url(email_address)
            response['links'] = {'confirm_url': request.url + '/confirm_oauth'}
            namespace = Namespace()
            account = GmailAccount(namespace=namespace)
            account.sync_should_run = False
            account.refresh_token = '_placeholder_'
            account.email_address = email_address
        else:
            if not password:
                response['error'] = 'Missing key - "password"!'
                return encoder.jsonify(response)
            auth_info['email'] = email_address
            auth_info['password'] = password
            if provider != 'unknown':
                auth_handler = handler_from_provider(provider)
                auth_info['provider'] = provider
                try:
                    if reauth:
                        account = auth_handler.update_account(
                            account, auth_info)
                    else:
                        account = auth_handler.create_account(
                            email_address, auth_info)
                except Exception as e:
                    response['error'] = e.msg
            else:
                auth_info['provider'] = 'custom'
                auth_handler = handler_from_provider('custom')
                if not auth_details:
                    auth_info.update(
                        try_fill_config_data(email_address, password))
                else:
                    auth_info.update(auth_details)
                try:
                    if reauth:
                        account = auth_handler.update_account(
                            account, auth_info)
                    else:
                        account = auth_handler.create_account(
                            email_address, auth_info)
                except Exception as e:
                    response['error'] = str(e)
            try:
                auth_handler.verify_account(account)
                response['data'] = 'OK. Authenticated account for {}'.format(
                    email_address)
            except Exception as e:
                response['error'] = str(e)
        db_session.add(account)
        db_session.commit()
    return encoder.jsonify(response)
Exemple #29
0
def test_provider_resolution():
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'hover'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yandex'
    assert provider_from_address('*****@*****.**') == 'zimbra'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'gmx'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'bluehost'

    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com')
def test_provider_resolution():
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'gmx'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gandi'
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com')

    # Register a new provider
    try:
        providers.register_info('example', {
            "type": "generic",
            "imap": ("mail.example.net", 993),
            "smtp": ("smtp.example.net", 587),
            "auth": "password",
            "domains": ["example.com"],
            "mx_servers": ["mx.example.net"]
        })
        assert provider_from_address('*****@*****.**') == 'example'
    finally:
        providers.reset()

    # Register a filter
    try:
        def my_filter(name, info):
            info['domains'].append('example.net')
        assert provider_from_address('*****@*****.**') == 'unknown'
        providers.register_info_filter('aol', my_filter)
        assert provider_from_address('*****@*****.**') == 'aol'
    finally:
        providers.reset()
Exemple #31
0
def test_provider_resolution():
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'unknown'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'yahoo'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'outlook'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'aol'
    assert provider_from_address('*****@*****.**') == 'gmail'
    assert provider_from_address('*****@*****.**') == 'eas'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'fastmail'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'icloud'
    assert provider_from_address('*****@*****.**') == 'gmx'
    assert provider_from_address('*****@*****.**') == 'gandi'
    assert provider_from_address('*****@*****.**') == 'gandi'
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('not@anemail')
    with pytest.raises(InvalidEmailAddressError):
        provider_from_address('notanemail.com')

    # Register a new provider
    try:
        providers.register_info(
            'example', {
                "type": "generic",
                "imap": ("mail.example.net", 993),
                "smtp": ("smtp.example.net", 587),
                "auth": "password",
                "domains": ["example.com"],
                "mx_servers": ["mx.example.net"]
            })
        assert provider_from_address('*****@*****.**') == 'example'
    finally:
        providers.reset()

    # Register a filter
    try:

        def my_filter(name, info):
            info['domains'].append('example.net')

        assert provider_from_address('*****@*****.**') == 'unknown'
        providers.register_info_filter('aol', my_filter)
        assert provider_from_address('*****@*****.**') == 'aol'
    finally:
        providers.reset()