def test_invalid_token_during_connect(db, patch_access_token_getter,
                                      account_with_single_auth_creds):
    account_id = account_with_single_auth_creds.id

    patch_access_token_getter.revoke_refresh_token(
        account_with_single_auth_creds.auth_credentials[0].refresh_token)
    account_with_single_auth_creds.verify_all_credentials()
    assert len(account_with_single_auth_creds.valid_auth_credentials) == 0
    g_token_manager.clear_cache(account_with_single_auth_creds)

    # connect_account() takes an /expunged/ account object
    # that has the necessary relationships eager-loaded
    object_session(account_with_single_auth_creds).expunge(
        account_with_single_auth_creds)
    assert not object_session(account_with_single_auth_creds)

    account = db.session.query(GmailAccount).options(
        joinedload(GmailAccount.auth_credentials)).get(
        account_id)
    db.session.expunge(account)
    assert not object_session(account)

    g = GmailAuthHandler('gmail')

    with pytest.raises(OAuthError):
        g.connect_account(account)

    invalid_account = db.session.query(GmailAccount).get(account_id)
    for auth_creds in invalid_account.auth_credentials:
        assert not auth_creds.is_valid
def test_invalid_token_during_connect(db, patch_access_token_getter,
                                      account_with_single_auth_creds):
    account_id = account_with_single_auth_creds.id

    patch_access_token_getter.revoke_refresh_token(
        account_with_single_auth_creds.auth_credentials[0].refresh_token)
    account_with_single_auth_creds.verify_all_credentials()
    assert len(account_with_single_auth_creds.valid_auth_credentials) == 0
    g_token_manager.clear_cache(account_with_single_auth_creds)

    # connect_account() takes an /expunged/ account object
    # that has the necessary relationships eager-loaded
    object_session(account_with_single_auth_creds).expunge(
        account_with_single_auth_creds)
    assert not object_session(account_with_single_auth_creds)

    account = db.session.query(GmailAccount).options(
        joinedload(GmailAccount.auth_credentials)).get(
        account_id)
    db.session.expunge(account)
    assert not object_session(account)

    g = GmailAuthHandler('gmail')

    with pytest.raises(OAuthError):
        g.connect_account(account)

    invalid_account = db.session.query(GmailAccount).get(account_id)
    for auth_creds in invalid_account.auth_credentials:
        assert not auth_creds.is_valid
예제 #3
0
def get_auth_handler(monkeypatch, folders):
    g = GmailAuthHandler('gmail')

    def mock_connect(x, y, z):
        return ConnectionStub()

    g.connect_account = mock_connect
    monkeypatch.setattr(GmailCrispinClient, 'folder_names', lambda x: folders)
    return g
def get_auth_handler(monkeypatch, folders):
    g = GmailAuthHandler('gmail')

    def mock_connect(a):
        return ConnectionStub()

    g.connect_account = mock_connect
    monkeypatch.setattr(GmailCrispinClient, 'folder_names',
                        lambda x: folders)
    return g
예제 #5
0
def test_verify_account(db, patched_gmail_client):
    handler = GmailAuthHandler('gmail')
    handler.connect_account = lambda account: None

    # Create an account with sync_email=True
    account = handler.create_account(settings['email'], settings)
    db.session.add(account)
    db.session.commit()
    assert account.sync_email == True
    # Verify an exception is raised if there is an email settings error.
    with pytest.raises(ImapSupportDisabledError):
        handler.verify_account(account)

    # Create an account with sync_email=True
    updated_settings = copy.deepcopy(settings)
    updated_settings['email'] = '*****@*****.**'
    updated_settings['sync_email'] = False
    account = handler.create_account(updated_settings['email'], updated_settings)
    db.session.add(account)
    db.session.commit()
    assert account.sync_email == False
    # Verify an exception is NOT raised if there is an email settings error.
    account = handler.verify_account(account)
예제 #6
0
def test_verify_account(db, patched_gmail_client):
    handler = GmailAuthHandler('gmail')
    handler.connect_account = lambda account: None

    # Create an account with sync_email=True
    account = handler.create_account(settings['email'], settings)
    db.session.add(account)
    db.session.commit()
    assert account.sync_email is True
    # Verify an exception is raised if there is an email settings error.
    with pytest.raises(ImapSupportDisabledError):
        handler.verify_account(account)

    # Create an account with sync_email=True
    updated_settings = copy.deepcopy(settings)
    updated_settings['email'] = '*****@*****.**'
    updated_settings['sync_email'] = False
    account = handler.create_account(updated_settings['email'], updated_settings)
    db.session.add(account)
    db.session.commit()
    assert account.sync_email is False
    # Verify an exception is NOT raised if there is an email settings error.
    account = handler.verify_account(account)
예제 #7
0
def test_successful_reauth_resets_sync_state(monkeypatch, db):
    monkeypatch.setattr('inbox.auth.gmail.GmailCrispinClient', mock.Mock())
    handler = GmailAuthHandler('gmail')
    handler.connect_account = lambda account: mock.Mock()

    account = handler.create_account(settings['email'], settings)
    assert handler.verify_account(account) is True
    # Brand new accounts have `sync_state`=None.
    assert account.sync_state is None
    db.session.add(account)
    db.session.commit()

    # Pretend account sync starts, and subsequently the password changes,
    # causing the account to be in `sync_state`='invalid'.
    account.mark_invalid()
    db.session.commit()
    assert account.sync_state == 'invalid'

    # Verify the `sync_state` is reset to 'running' on a successful "re-auth".
    account = handler.update_account(account, settings)
    assert handler.verify_account(account) is True
    assert account.sync_state == 'running'
    db.session.add(account)
    db.session.commit()
예제 #8
0
def test_successful_reauth_resets_sync_state(monkeypatch, db):
    monkeypatch.setattr('inbox.auth.gmail.GmailCrispinClient', mock.Mock())
    handler = GmailAuthHandler('gmail')
    handler.connect_account = lambda account: mock.Mock()

    account = handler.create_account(settings['email'], settings)
    assert handler.verify_account(account) is True
    # Brand new accounts have `sync_state`=None.
    assert account.sync_state is None
    db.session.add(account)
    db.session.commit()

    # Pretend account sync starts, and subsequently the password changes,
    # causing the account to be in `sync_state`='invalid'.
    account.mark_invalid()
    db.session.commit()
    assert account.sync_state == 'invalid'

    # Verify the `sync_state` is reset to 'running' on a successful "re-auth".
    account = handler.update_account(account, settings)
    assert handler.verify_account(account) is True
    assert account.sync_state == 'running'
    db.session.add(account)
    db.session.commit()