Exemplo n.º 1
0
def _save_token(token, db_session=None, farm=None):
    logging.debug("Saving new token for farm: " + str(farm.id))
    token_in = FarmTokenCreate(farm_id=farm.id, **token)

    # Make sure we have a DB session and Farm object.
    if db_session is not None and farm is not None:
        # Update the farm token if it exists.
        if farm.token is not None:
            update_farm_token(db_session, farm.token, token_in)
        else:
            create_farm_token(db_session, token_in)
Exemplo n.º 2
0
def _save_token(token, db=None, farm=None):
    logging.debug("Saving new token for farm: " + str(farm.id))
    token_in = FarmTokenCreate(farm_id=farm.id, **token)

    # Make sure we have a DB session and Farm object.
    if db is not None and farm is not None:
        # Update the farm token if it exists.
        if farm.token is not None:
            update_farm_token(db, farm.token, token_in)
        else:
            create_farm_token(db, token_in)

        # Update the Farm.scope attribute based on what the server returned.
        if 'scope' in token:
            crud.farm.update_scope(db, farm=farm, scope=token['scope'])
Exemplo n.º 3
0
def test_create_farm_token(db: Session, test_farm):
    token = FarmTokenCreate(farm_id=test_farm.id,
                            access_token=random_lower_string(),
                            expires_in=random_lower_string(),
                            refresh_token=random_lower_string(),
                            expires_at=1581363344.0651991)

    # Check for existing token
    old_token = farm_token.get_farm_token(db, test_farm.id)
    if old_token is None:
        farm_token.create_farm_token(db, token=token)
    else:
        farm_token.update_farm_token(db, token=old_token, token_in=token)

    db_token = farm_token.get_farm_token(db, farm_id=test_farm.id)

    assert db_token.farm_id == token.farm_id == test_farm.id
    assert db_token.access_token == token.access_token
    assert db_token.expires_in == token.expires_in
    assert db_token.refresh_token == token.refresh_token
    assert float(db_token.expires_at) == token.expires_at
Exemplo n.º 4
0
def test_update_farm_token(test_farm):
    db_token = farm_token.get_farm_token(db_session, farm_id=test_farm.id)
    assert db_token is not None
    assert db_token.farm_id == test_farm.id

    token_changes = FarmTokenUpdate(id=db_token.id,
                                    farm_id=db_token.farm_id,
                                    access_token=None,
                                    expires_in=None,
                                    refresh_token=None,
                                    expires_at=None)
    new_token = farm_token.update_farm_token(db_session,
                                             token=db_token,
                                             token_in=token_changes)
    assert new_token.id == db_token.id
    assert new_token.farm_id == db_token.farm_id == test_farm.id

    # Check that the farm_token was reset.
    assert new_token.access_token is None
    assert new_token.expires_in is None
    assert new_token.refresh_token is None
    assert new_token.expires_at is None