2017-12-06 22:06:03 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2017-04-05 18:01:55 +00:00
|
|
|
from mock import patch
|
|
|
|
|
2018-02-23 21:45:16 +00:00
|
|
|
from data.database import EmailConfirmation, User, DeletedNamespace
|
2017-12-22 21:45:49 +00:00
|
|
|
from data.model.user import create_user_noverify, validate_reset_code, get_active_users
|
2018-02-23 21:45:16 +00:00
|
|
|
from data.model.user import mark_namespace_for_deletion, delete_namespace_via_marker
|
2018-05-09 11:52:28 +00:00
|
|
|
from data.model.user import create_robot, lookup_robot, list_namespace_robots
|
|
|
|
from data.model.user import InvalidRobotException
|
2017-12-06 22:06:03 +00:00
|
|
|
from util.timedeltastring import convert_to_timedelta
|
2018-02-23 21:45:16 +00:00
|
|
|
from data.queue import WorkQueue
|
2017-04-24 17:49:29 +00:00
|
|
|
from test.fixtures import *
|
2017-04-05 18:01:55 +00:00
|
|
|
|
2017-04-24 17:49:29 +00:00
|
|
|
def test_create_user_with_expiration(initialized_db):
|
2017-04-05 18:01:55 +00:00
|
|
|
with patch('data.model.config.app_config', {'DEFAULT_TAG_EXPIRATION': '1h'}):
|
|
|
|
user = create_user_noverify('foobar', 'foo@example.com', email_required=False)
|
|
|
|
assert user.removed_tag_expiration_s == 60 * 60
|
2017-12-06 22:06:03 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('token_lifetime, time_since', [
|
|
|
|
('1m', '2m'),
|
|
|
|
('2m', '1m'),
|
|
|
|
('1h', '1m'),
|
|
|
|
])
|
|
|
|
def test_validation_code(token_lifetime, time_since, initialized_db):
|
|
|
|
user = create_user_noverify('foobar', 'foo@example.com', email_required=False)
|
|
|
|
created = datetime.now() - convert_to_timedelta(time_since)
|
|
|
|
confirmation = EmailConfirmation.create(user=user, pw_reset=True, created=created)
|
|
|
|
|
|
|
|
with patch('data.model.config.app_config', {'USER_RECOVERY_TOKEN_LIFETIME': token_lifetime}):
|
|
|
|
result = validate_reset_code(confirmation.code)
|
|
|
|
expect_success = convert_to_timedelta(token_lifetime) >= convert_to_timedelta(time_since)
|
|
|
|
assert expect_success == (result is not None)
|
2017-12-22 21:45:49 +00:00
|
|
|
|
2018-05-15 13:01:20 +00:00
|
|
|
|
2017-12-22 21:45:49 +00:00
|
|
|
@pytest.mark.parametrize('disabled', [
|
|
|
|
(True),
|
|
|
|
(False),
|
|
|
|
])
|
2018-05-15 13:01:20 +00:00
|
|
|
@pytest.mark.parametrize('deleted', [
|
|
|
|
(True),
|
|
|
|
(False),
|
|
|
|
])
|
|
|
|
def test_get_active_users(disabled, deleted, initialized_db):
|
|
|
|
# Delete a user.
|
|
|
|
deleted_user = model.user.get_user('public')
|
|
|
|
queue = WorkQueue('testgcnamespace', lambda db: db.transaction())
|
|
|
|
mark_namespace_for_deletion(deleted_user, [], queue)
|
|
|
|
|
|
|
|
users = get_active_users(disabled=disabled, deleted=deleted)
|
|
|
|
deleted_found = [user for user in users if user.id == deleted_user.id]
|
|
|
|
assert bool(deleted_found) == (deleted and disabled)
|
|
|
|
|
2017-12-22 21:45:49 +00:00
|
|
|
for user in users:
|
|
|
|
if not disabled:
|
|
|
|
assert user.enabled
|
2018-02-23 21:45:16 +00:00
|
|
|
|
2018-05-15 13:01:20 +00:00
|
|
|
|
2018-02-23 21:45:16 +00:00
|
|
|
def test_mark_namespace_for_deletion(initialized_db):
|
|
|
|
def create_transaction(db):
|
|
|
|
return db.transaction()
|
|
|
|
|
|
|
|
# Create a user and then mark it for deletion.
|
|
|
|
user = create_user_noverify('foobar', 'foo@example.com', email_required=False)
|
|
|
|
|
2018-05-09 11:52:28 +00:00
|
|
|
# Add some robots.
|
|
|
|
create_robot('foo', user)
|
|
|
|
create_robot('bar', user)
|
|
|
|
|
|
|
|
assert lookup_robot('foobar+foo') is not None
|
|
|
|
assert lookup_robot('foobar+bar') is not None
|
|
|
|
assert len(list(list_namespace_robots('foobar'))) == 2
|
|
|
|
|
2018-02-23 21:45:16 +00:00
|
|
|
# Mark the user for deletion.
|
|
|
|
queue = WorkQueue('testgcnamespace', create_transaction)
|
|
|
|
mark_namespace_for_deletion(user, [], queue)
|
|
|
|
|
|
|
|
# Ensure the older user is still in the DB.
|
2018-05-09 11:52:28 +00:00
|
|
|
older_user = User.get(id=user.id)
|
|
|
|
assert older_user.username != 'foobar'
|
|
|
|
|
|
|
|
# Ensure the robots are deleted.
|
|
|
|
with pytest.raises(InvalidRobotException):
|
|
|
|
assert lookup_robot('foobar+foo')
|
|
|
|
|
|
|
|
with pytest.raises(InvalidRobotException):
|
|
|
|
assert lookup_robot('foobar+bar')
|
|
|
|
|
|
|
|
assert len(list(list_namespace_robots(older_user.username))) == 0
|
2018-02-23 21:45:16 +00:00
|
|
|
|
|
|
|
# Ensure we can create a user with the same namespace again.
|
|
|
|
new_user = create_user_noverify('foobar', 'foo@example.com', email_required=False)
|
|
|
|
assert new_user.id != user.id
|
|
|
|
|
|
|
|
# Ensure the older user is still in the DB.
|
|
|
|
assert User.get(id=user.id).username != 'foobar'
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_namespace_via_marker(initialized_db):
|
|
|
|
def create_transaction(db):
|
|
|
|
return db.transaction()
|
|
|
|
|
|
|
|
# Create a user and then mark it for deletion.
|
|
|
|
user = create_user_noverify('foobar', 'foo@example.com', email_required=False)
|
|
|
|
|
|
|
|
# Mark the user for deletion.
|
|
|
|
queue = WorkQueue('testgcnamespace', create_transaction)
|
|
|
|
marker_id = mark_namespace_for_deletion(user, [], queue)
|
|
|
|
|
|
|
|
# Delete the user.
|
|
|
|
delete_namespace_via_marker(marker_id, [])
|
|
|
|
|
|
|
|
# Ensure the user was actually deleted.
|
|
|
|
with pytest.raises(User.DoesNotExist):
|
|
|
|
User.get(id=user.id)
|
|
|
|
|
|
|
|
with pytest.raises(DeletedNamespace.DoesNotExist):
|
|
|
|
DeletedNamespace.get(id=marker_id)
|