Skip to content
Snippets Groups Projects
Commit 6356e177 authored by Harsha Kethineni's avatar Harsha Kethineni
Browse files

tested new functionality of access log reports

parent 7a5119c2
No related branches found
No related tags found
No related merge requests found
......@@ -16,24 +16,24 @@ from ..web import base
EIGHTEEN_YEARS_IN_SEC = 18 * 365.25 * 24 * 60 * 60
BYTES_IN_MEGABYTE = float(1<<20)
ACCESS_LOG_FIELDS = [
"context.session.label",
"context.project.id",
"context.subject.label",
"context.ticket_id",
"_id",
"access_type",
"context.acquisition.id",
"context.acquisition.label",
"timestamp",
"access_type",
"context.group.id",
"request_method",
"context.subject.id",
"request_path",
"context.group.label",
"context.project.id",
"context.project.label",
"origin.id",
"_id",
"context.session.id",
"origin.type"
"context.session.label",
"context.subject.id",
"context.subject.label",
"context.ticket_id",
"origin.id",
"origin.type",
"request_method",
"request_path",
"timestamp"
]
class APIReportException(Exception):
......@@ -71,6 +71,7 @@ class ReportHandler(base.RequestHandler):
for doc in report.build():
writer.writerow(doc)
# Need to close and reopen file to flush buffer into file
csv_file.close()
self.response.app_iter = open(os.path.join(tempdir.name, 'acceslog.csv'), 'r')
self.response.headers['Content-Type'] = 'text/csv'
......@@ -504,7 +505,7 @@ class AccessLogReport(Report):
if limit < 1:
raise APIReportParamsException('Limit must be an integer greater than 0.')
for access_type in access_types:
if access_type not in ['user_login', 'view_container']:
if access_type not in ['user_login', 'view_container', 'download_file']:
raise APIReportParamsException('Not a valid access type')
self.start_date = start_date
......
......@@ -2,7 +2,6 @@ import calendar
import copy
import datetime
# create timestamps for report filtering
today = datetime.datetime.today()
ts_format = '{:%Y-%m-%dT%H:%M:%S+00:00}'
......@@ -93,7 +92,7 @@ def test_project_report(data_builder, as_admin, as_user):
assert len(project_report['projects']) == 2
def test_access_log_report(with_user, as_user, as_admin):
def test_access_log_report(data_builder, with_user, as_user, as_admin):
# try to get access log report as user
r = as_user.get('/report/accesslog')
assert r.status_code == 403
......@@ -135,6 +134,50 @@ def test_access_log_report(with_user, as_user, as_admin):
assert len(accesslog) == 1
assert accesslog[0]['access_type'] == 'user_login'
# get access log report of certain subject
project = data_builder.create_project()
r = as_admin.post('/sessions', json={
'project': project,
'label': 'test-accesslog-session',
'timestamp': '1979-01-01T00:00:00+00:00',
'subject': {'code': 'compliant5'}
})
assert r.ok
session = r.json()['_id']
r = as_admin.get('/sessions/' + session)
assert r.ok
session = r.json()['_id']
r = as_admin.get('/sessions/' + session)
assert r.ok
r = as_admin.get('/report/accesslog', params={'subject': 'compliant5'})
assert r.ok
for count,log in enumerate(r.json(), start = 1):
assert log.get('context', {}).get('subject', {}).get('label') == 'compliant5'
assert count == 2
r = as_admin.delete('/sessions/' + session)
data_builder.delete_project(project, recursive=True)
# get access log report of certain access types
r = as_admin.get('/report/accesslog', params={'access_types': ['user_login', 'view_container']})
assert r.ok
ul, vc = False, False
# test that each item in log is either view_container or user_login
for log in r.json():
assert log.get('access_type') in ['user_login', 'view_container']
if log.get('access_type') == 'user_login':
ul = True
elif log.get('access_type') == 'view_container':
vc = True
assert ul and vc
# Download .csv file
r = as_admin.get('/report/accesslog', params={'csv': 'true'})
assert r.ok
r.content[0][:3] == '_id'
def test_usage_report(data_builder, file_form, as_user, as_admin):
# try to get usage report as user
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment