Skip to content

Commit

Permalink
Merge 34f0726 into 72ee7f4
Browse files Browse the repository at this point in the history
  • Loading branch information
ikreymer committed Mar 9, 2018
2 parents 72ee7f4 + 34f0726 commit 9e1a4a8
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 281 deletions.
118 changes: 111 additions & 7 deletions webrecorder/test/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
from webrecorder.models.usermanager import CLIUserManager

class TestAdminAPI(FullStackTests):
def test_cli_manager(self):
m = CLIUserManager()
@classmethod
def setup_class(cls):
super(TestAdminAPI, cls).setup_class()
cls.user_manager = CLIUserManager()

assert type(m.redis.keys('*')) is list

def test_admin_no_auth(self):
def test_no_auth_admin_users(self):
res = self.testapp.get('/api/v1/users')
# no permissions, redirect to _login
assert res.headers['Location'].endswith('_login')

def test_dashboard_no_auth(self):
def test_no_auth_admin_dashboard(self):
res = self.testapp.get('/api/v1/dashboard')
assert res.headers['Location'].endswith('_login')

def test_client_archives(self):
def test_no_auth_client_archives(self):
res = self.testapp.get('/api/v1/client_archives/')

assert len(list(res.json.keys())) == 25
Expand All @@ -27,4 +27,108 @@ def test_client_archives(self):
assert 'about' in value
assert 'prefix' in value

def test_create_login_admin(self):
self.user_manager.create_user('admin@example.com', 'adminuser', 'TestTest123', 'admin', 'Test Admin')
self.user_manager.create_user('user@example.com', 'test', 'TestTest456', 'archivist', 'Test User')

assert len(self.redis.keys('u:*:info')) == 3

def test_login_first_user(self):
params = {'username': 'adminuser',
'password': 'TestTest123',
}

res = self.testapp.post_json('/api/v1/login', params=params)
assert res.json['username'] == 'adminuser'
assert self.testapp.cookies['__test_sesh'] != ''

def test_api_roles(self):
res = self.testapp.get('/api/v1/user_roles')
assert set(res.json['roles']) == {'admin',
'beta-archivist',
'mounts-archivist',
'public-archivist',
'archivist'}

def test_get_api_defaults(self):
res = self.testapp.get('/api/v1/defaults')
assert res.json == {'defaults': {'max_anon_size': 1000000000, 'max_size': 1000000000}}

def test_set_api_defaults(self):
res = self.testapp.put_json('/api/v1/defaults', params={'max_size': 7000000000})
assert res.json == {'defaults': {'max_anon_size': 1000000000, 'max_size': 7000000000}}

# raw key, not converted to int
assert self.redis.hgetall('h:defaults') == {'max_anon_size': '1000000000', 'max_size': '7000000000'}

def test_api_users(self):
res = self.testapp.get('/api/v1/users')
assert [user['username'] for user in res.json['users']] == ['adminuser', 'test']
assert [user['role'] for user in res.json['users']] == ['admin', 'archivist']
assert [user['name'] for user in res.json['users']] == ['Test Admin', 'Test User']
assert [user['max_size'] for user in res.json['users']] == ['1000000000', '1000000000']

def test_api_temp_users(self):
res = self.testapp.get('/api/v1/temp-users')
assert [user['username'] for user in res.json['users']] == [self.anon_user]
assert [user.get('role') for user in res.json['users']] == [None]
assert [user.get('name') for user in res.json['users']] == [None]
assert [user['max_size'] for user in res.json['users']] == ['1000000000']

def test_api_create_user_errors(self):
params = {'email': 'another@example.com',
'username': 'test',
'password': 'TestTest',
'role': 'archivist2',
'name': 'Another User'}

res = self.testapp.post_json('/api/v1/users', params=params)

assert res.json == {'errors': ['Username already exists.',
'Not a valid role.',
'Passwords must match and be at least 8 characters long with '
'lowercase, uppercase, and either digits or symbols.']}


def test_api_create_user(self):
params = {'email': 'another@example.com',
'username': 'another',
'password': 'TestTest789',
'role': 'archivist',
'name': 'Another User'}

res = self.testapp.post_json('/api/v1/users', params=params)

assert res.json == {'first_coll': 'default-collection', 'user': 'another'}

def test_api_users_added(self):
res = self.testapp.get('/api/v1/users')
assert [user['username'] for user in res.json['users']] == ['adminuser', 'another', 'test']
assert [user['role'] for user in res.json['users']] == ['admin', 'archivist', 'archivist']
assert [user['name'] for user in res.json['users']] == ['Test Admin', 'Another User', 'Test User']
assert [user['max_size'] for user in res.json['users']] == ['1000000000', '7000000000', '1000000000']

def test_update_user_error_invalid_role_and_size(self):
params = {'role': 'beta-arc',
'max_size': '500000000',
'desc': 'Custom Desc'
}

res = self.testapp.put_json('/api/v1/users/test', params=params)
assert res.json == {'errors': ['Not a valid role.', 'max_size must be an int']}

def test_update_user(self):
params = {'role': 'beta-archivist',
'max_size': 200000000,
'desc': 'Custom Desc'
}

res = self.testapp.put_json('/api/v1/users/test', params=params)

assert res.json['user']['space_utilization'] == {'available': 200000000, 'total': 200000000, 'used': 0}
assert res.json['user']['role'] == 'beta-archivist'
assert res.json['user']['name'] == 'Test User'
assert res.json['user']['desc'] == 'Custom Desc'



12 changes: 11 additions & 1 deletion webrecorder/test/test_anon_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ class TestTempContent(FullStackTests):
'u:{user}:colls',
'n:colls:count',
'n:recs:count',
'h:defaults',
'h:roles',
'h:temp-usage',
]

POST_DEL_KEYS = [
'n:colls:count',
'n:recs:count',
'h:defaults',
'h:roles',
'h:temp-usage',
'q:del:nginx'
]

PAGE_STATS = {'rec': 'r:{rec}:<sesh_id>:stats:{url}',
'coll': 'c:{coll}:<sesh_id>:stats:{url}'
}
Expand Down Expand Up @@ -637,7 +647,7 @@ def test_anon_auto_delete(self):
sesh_redis.flushdb()

def assert_empty_keys():
assert set(self.redis.keys()) == set(['h:roles', 'h:temp-usage', 'n:colls:count', 'n:recs:count', 'q:del:nginx'])
assert set(self.redis.keys()) == set(self.POST_DEL_KEYS)
assert glob.glob(os.path.join(self.warcs_dir, 'temp$*')) == []

self.sleep_try(0.1, 10.0, assert_empty_keys)
Expand Down
2 changes: 1 addition & 1 deletion webrecorder/test/test_api_user_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def test_api_user_info_2(self):
user = res.json['user']

assert user['username'] == 'someuser'
assert user['description'] == 'New Description'
assert user['desc'] == 'New Description'

# collections not included
assert 'collections' not in user
Expand Down

0 comments on commit 9e1a4a8

Please sign in to comment.