Ejemplo n.º 1
0
def test_create_access_token(expiration, default_expiration, initialized_db, app_config):
  user = model.user.get_user('devtable')
  expiration_date = datetime.now() + convert_to_timedelta(expiration) if expiration else None
  with patch.dict(_config.app_config, {}, clear=True):
    app_config['APP_SPECIFIC_TOKEN_EXPIRATION'] = default_expiration
    if expiration:
      exp_token = create_token(user, 'Some token', expiration=expiration_date)
      assert exp_token.expiration == expiration_date
    else:
      exp_token = create_token(user, 'Some token')
      assert (exp_token.expiration is None) == (default_expiration is None)
Ejemplo n.º 2
0
def test_expiring_soon(initialized_db):
  user = model.user.get_user('devtable')

  # Create some tokens.
  create_token(user, 'Some token')
  exp_token = create_token(user, 'Some expiring token', datetime.now() + convert_to_timedelta('1d'))
  create_token(user, 'Some other token', expiration=datetime.now() + convert_to_timedelta('2d'))

  # Get the token expiring soon.
  expiring_soon = get_expiring_tokens(user, convert_to_timedelta('25h'))
  assert expiring_soon
  assert len(expiring_soon) == 1
  assert expiring_soon[0].id == exp_token.id

  expiring_soon = get_expiring_tokens(user, convert_to_timedelta('49h'))
  assert expiring_soon
  assert len(expiring_soon) == 2
Ejemplo n.º 3
0
def test_expiring_soon(initialized_db):
    user = model.user.get_user("devtable")

    # Create some tokens.
    create_token(user, "Some token")
    exp_token = create_token(user, "Some expiring token",
                             datetime.now() + convert_to_timedelta("1d"))
    create_token(user,
                 "Some other token",
                 expiration=datetime.now() + convert_to_timedelta("2d"))

    # Get the token expiring soon.
    expiring_soon = get_expiring_tokens(user, convert_to_timedelta("25h"))
    assert expiring_soon
    assert len(expiring_soon) == 1
    assert expiring_soon[0].id == exp_token.id

    expiring_soon = get_expiring_tokens(user, convert_to_timedelta("49h"))
    assert expiring_soon
    assert len(expiring_soon) == 2
Ejemplo n.º 4
0
def test_access_token(initialized_db):
  user = model.user.get_user('devtable')

  # Create a token.
  token = create_token(user, 'Some token')
  assert token.last_accessed is None

  # Lookup the token.
  token = access_valid_token(get_full_token_string(token))
  assert token.last_accessed is not None

  # Revoke the token.
  revoke_token(token)

  # Ensure it cannot be accessed
  assert access_valid_token(get_full_token_string(token)) is None
Ejemplo n.º 5
0
def test_gc(expiration, initialized_db):
  user = model.user.get_user('devtable')

  expiration_date = None
  is_expired = False
  if expiration:
    if expiration[0] == '-':
      is_expired = True
      expiration_date = datetime.now() - convert_to_timedelta(expiration[1:])
    else:
      expiration_date = datetime.now() + convert_to_timedelta(expiration)

  # Create a token.
  token = create_token(user, 'Some token', expiration=expiration_date)

  # GC tokens.
  gc_expired_tokens(timedelta(seconds=0))

  # Ensure the token was GCed if expired and not if it wasn't.
  assert (access_valid_token(get_full_token_string(token)) is None) == is_expired