Skip to content

Commit

Permalink
Refs #79. ServiceQueryTests & UserQueryStats & UserQueryTests API adj…
Browse files Browse the repository at this point in the history
…ustments.
  • Loading branch information
SBriere committed Apr 7, 2022
1 parent 142c947 commit b4bb89b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,9 @@ def query_test(self, test_id: int = None, test_uuid: str = None):
else:
return None

if not test:
return None

test_session = self.query_session(test.id_session)
if not test_session:
# No access to asset session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def post(self):
test_name = test_type.test_type_key
else:
test_name = test_type.test_name
test_name += ' [' + str(test_date.year) + '.' + \
str(TeraTest.count_with_filters({'id_session': test_info['id_session']})) + ']'
test_name += ' #' + \
str(TeraTest.count_with_filters({'id_session': test_info['id_session']}))
test_info['test_name'] = test_name

# Check if the service can create/update that test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def get_session_stats(user_access: DBManagerTeraUserAccess, item_id: int) -> dic
'devices_total_count': len(ses.session_devices),
'assets_total_count': len(ses.session_assets),
'events_total_count': len(ses.session_events),
'tests_total_count': 0
'tests_total_count': len(ses.session_tests)
}
return stats

Expand Down
35 changes: 30 additions & 5 deletions teraserver/python/modules/FlaskModule/API/user/UserQueryTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from modules.LoginModule.LoginModule import user_multi_auth, current_user
from modules.FlaskModule.FlaskModule import user_api_ns as api
from opentera.db.models.TeraTest import TeraTest
from opentera.db.models.TeraService import TeraService
from sqlalchemy import exc

from modules.DatabaseModule.DBManager import DBManager
from opentera.redis.RedisVars import RedisVars
Expand All @@ -25,6 +25,9 @@
get_parser.add_argument('full', type=inputs.boolean, help='Also include names of sessions, users, services, ... in the '
'reply')

delete_parser = api.parser()
delete_parser.add_argument('id', type=int, help='Test type ID to delete', required=True)


class UserQueryTests(Resource):

Expand Down Expand Up @@ -116,8 +119,30 @@ def post(self):
'Test service)'), 501

@user_multi_auth.login_required
@api.doc(description='Delete test.',
responses={501: 'Unable to delete test from here'})
@api.expect(delete_parser)
@api.doc(description='Delete a specific test',
responses={200: 'Success',
403: 'Logged user can\'t delete test',
500: 'Database error.'})
def delete(self):
return gettext('Test deletion must be done directly into a service (such as '
'Test service)'), 501
user_access = DBManager.userAccess(current_user)

args = delete_parser.parse_args(strict=True)
id_todel = args['id']

# Check if current user can delete
if not user_access.query_test(id_todel):
return gettext('Forbidden'), 403

# If we are here, we are allowed to delete. Do so.
try:
TeraTest.delete(id_todel=id_todel)
except exc.SQLAlchemyError as e:
import sys
print(sys.exc_info())
self.module.logger.log_error(self.module.module_name,
UserQueryTests.__name__,
'delete', 500, 'Database error', e)
return gettext('Database error'), 500

return '', 200
2 changes: 2 additions & 0 deletions teraserver/python/opentera/db/models/TeraSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def to_json(self, ignore_fields=None, minimal=False):

# Append session stats
from opentera.db.models.TeraAsset import TeraAsset
from opentera.db.models.TeraTest import TeraTest
rval['session_assets_count'] = TeraAsset.get_count({'id_session': self.id_session})
rval['session_tests_count'] = TeraTest.get_count({'id_session': self.id_session})
# rval['session_has_device_data'] = len(TeraDeviceData.get_data_for_session(self.id_session)) > 0
return rval

Expand Down
5 changes: 3 additions & 2 deletions teraserver/python/opentera/db/models/TeraTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class TeraTest(db.Model, BaseModel):
test_service = db.relationship("TeraService")
test_test_type = db.relationship('TeraTestType')

def from_json(self, json, ignore_fields=None):
def from_json(self, json_data, ignore_fields=None):
if ignore_fields is None:
ignore_fields = []

ignore_fields.extend(['test_session', 'test_device', 'test_user', 'test_participant', 'test_service',
'test_test_type'])

super().from_json(json, ignore_fields)
super().from_json(json_data, ignore_fields)

def to_json(self, ignore_fields=None, minimal=False):
if ignore_fields is None:
Expand All @@ -55,6 +55,7 @@ def to_json(self, ignore_fields=None, minimal=False):
test_json = super().to_json(ignore_fields=ignore_fields)
if not minimal:
test_json['test_session_name'] = self.test_session.session_name
test_json['test_test_type_name'] = self.test_test_type.test_type_name
if self.id_device:
test_json['test_device'] = self.test_device.device_name
if self.id_user:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ def test_post_as_admin(self):
self.assertEqual(response.status_code, 501)

def test_delete_as_admin(self):
response = self._delete_with_user_http_auth(username='admin', password='admin', params={'id_todel': 1},
response = self._delete_with_user_http_auth(username='admin', password='admin', params={'id': 44},
client=self.test_client)
self.assertEqual(response.status_code, 501)
self.assertEqual(response.status_code, 403)

response = self._delete_with_user_http_auth(username='admin', password='admin', params={'id': 1},
client=self.test_client)
self.assertEqual(response.status_code, 200)

def test_query_session_tests_as_admin_token_only(self):
payload = {'id_session': 2, 'with_urls': True, 'with_only_token': True}
Expand Down

0 comments on commit b4bb89b

Please sign in to comment.