Skip to content

Commit

Permalink
refactored code to make names consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
hkethi002 committed Nov 1, 2017
1 parent 9cb4adb commit ef79dca
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 33 deletions.
12 changes: 6 additions & 6 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .handlers.reporthandler import ReportHandler
from .handlers.resolvehandler import ResolveHandler
from .handlers.roothandler import RootHandler
from .handlers.savedsearchhandler import SavedSearchHandler
from .handlers.savesearchhandler import SaveSearchHandler
from .handlers.schemahandler import SchemaHandler
from .handlers.userhandler import UserHandler
from .jobs.handlers import BatchHandler, JobsHandler, JobHandler, GearsHandler, GearHandler, RulesHandler, RuleHandler
Expand Down Expand Up @@ -111,10 +111,10 @@ def prefix(path, routes):
route('/dataexplorer/index/fields', DataExplorerHandler, h='index_field_names', m=['POST']),

# Search Saving
route('/savesearch', SavedSearchHandler, m=['POST']),
route('/savesearch', SavedSearchHandler, h='get_all', m=['GET']),
route('/savesearch/<sid:{cid}>', SavedSearchHandler, m=['GET','DELETE']),
route('/savesearch/<sid:{cid}>', SavedSearchHandler, h='replace_search', m=['POST']),
route('/savesearches', SaveSearchHandler, m=['POST']),
route('/savesearches', SaveSearchHandler, h='get_all', m=['GET']),
route('/savesearches/<sid:{cid}>', SaveSearchHandler, m=['GET','DELETE']),
route('/savesearches/<sid:{cid}>', SaveSearchHandler, h='replace_search', m=['POST']),

# Users

Expand Down Expand Up @@ -237,7 +237,7 @@ def prefix(path, routes):

# Collections / Projects

prefix('/<cont_name:collections|projects>', [
prefix('/<cont_name:collections|projects|savesearches>', [
prefix('/<cid:{cid}>', [
route('/<list_name:permissions>', PermissionsListHandler, m=['POST']),
route('/<list_name:permissions>/<_id:{uid}>', PermissionsListHandler, m=['GET', 'PUT', 'DELETE']),
Expand Down
1 change: 1 addition & 0 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def initialize_db():
# TODO review all indexes
db.users.create_index('api_key.key')
db.projects.create_index([('gid', 1), ('name', 1)])
db.savesearches.create_index('creator')
db.sessions.create_index('project')
db.sessions.create_index('uid')
db.sessions.create_index('created')
Expand Down
2 changes: 1 addition & 1 deletion api/dao/containerstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def inflate_job_info(self, analysis):
class SearchStorage(ContainerStorage):

def __init__(self):
super(SearchStorage, self).__init__('searches', use_object_id=True)
super(SearchStorage, self).__init__('savesearches', use_object_id=True)

def replace_el(self, search):
self.delete_el(search['_id'])
Expand Down
1 change: 1 addition & 0 deletions api/dao/containerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'project': 'projects',
'session': 'sessions',
'user': 'users',
'savesearch': 'savesearches',
}
PLURAL_TO_SINGULAR = {p: s for s, p in SINGULAR_TO_PLURAL.iteritems()}

Expand Down
1 change: 1 addition & 0 deletions api/handlers/listhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def initialize_list_configurations():
'acquisitions': copy.deepcopy(container_default_configurations),
'collections': copy.deepcopy(container_default_configurations),
'analyses': copy.deepcopy(container_default_configurations),
'savesearches': copy.deepcopy(container_default_configurations),
}
# preload the Storage instances for all configurations
for cont_name, cont_config in list_container_configurations.iteritems():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
log = config.log
storage = SearchStorage()

class SavedSearchHandler(base.RequestHandler):
class SaveSearchHandler(base.RequestHandler):

def __init__(self, request=None, response=None):
super(SavedSearchHandler, self).__init__(request, response)
super(SaveSearchHandler, self).__init__(request, response)

@require_login
def post(self):
payload = self.request.json_body
validators.validate_data(payload, 'search-input.json', 'input', 'POST')
payload['permissions'] = [{"_id": self.uid, "access": "admin"}]
payload['creator'] = self.uid
result = storage.create_el(payload)
if result.acknowledged:
if result.inserted_id:
Expand Down Expand Up @@ -49,10 +50,7 @@ def delete(self, sid):

def replace_search(self, sid):
payload = self.request.json_body
if payload.get('_id'):
del(payload['_id'])
if payload.get('permissions'):
del(payload['permissions'])
payload = self._scrub_replace(payload)
validators.validate_data(payload, 'search-input.json', 'input', 'POST')
payload['_id'] = bson.ObjectId(sid)
search = storage.get_container(sid)
Expand All @@ -64,3 +62,15 @@ def replace_search(self, sid):
if result.inserted_id:
return {'_id': result.inserted_id}
return {"hi" : "bye"}

def _scrub_replace(self, payload):
'''
Function to turn a search returned from a GET to a legal post/replace
'''
if payload.get('_id'):
del(payload['_id'])
if payload.get('permissions'):
del(payload['permissions'])
if payload.get('creator'):
del(payload['creator'])
return payload
2 changes: 1 addition & 1 deletion raml/api.raml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ resourceTypes:
/acquisitions: !include resources/acquisitions.raml
/projects: !include resources/projects.raml
/report: !include resources/report.raml
/savesearch: !include resources/savesearch.raml
/savesearches: !include resources/savesearch.raml
2 changes: 1 addition & 1 deletion raml/schemas/definitions/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"properties": {
"_id": {"$ref":"../definitions/objectid.json#"},
"label": {"$ref": "../definitions/container.json#/definitions/label"},
"uid": {"$ref":"../definitions/container.json#/definitions/uid"},
"creator": {"$ref":"../definitions/container.json#/definitions/uid"},
"created": {"$ref":"../definitions/created-modified.json#/definitions/created"},
"modified": {"$ref":"../definitions/created-modified.json#/definitions/modified"},
"permissions": {
Expand Down
14 changes: 7 additions & 7 deletions test/integration_tests/abao/abao_test_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ hooks.before("GET /devices/{DeviceId} -> 404", function(test, done) {
});

// Save Search Tests
hooks.before("POST /savesearch -> 200", function(test, done) {
hooks.before("POST /savesearches -> 200", function(test, done) {
test.request.body = {
"label": "Lable",
"search": {
Expand All @@ -1473,26 +1473,26 @@ hooks.before("POST /savesearch -> 200", function(test, done) {
};
done();
})
hooks.after("POST /savesearch -> 200", function(test, done) {
hooks.after("POST /savesearches -> 200", function(test, done) {
search_id = test.response.body['_id'];
done();
})

hooks.before("POST /savesearch -> 400", function(test, done) {
hooks.before("POST /savesearches -> 400", function(test, done) {
test.request.body = {
"not-label": "Label"
};
done();
})

hooks.before("GET /savesearch/{SearchId} -> 200", function(test, done) {
hooks.before("GET /savesearches/{SearchId} -> 200", function(test, done) {
test.request.params = {
SearchId: search_id
};
done();
})

hooks.before("POST /savesearch/{SearchId} -> 200", function(test, done) {
hooks.before("POST /savesearches/{SearchId} -> 200", function(test, done) {
test.request.params = {
SearchId: search_id
};
Expand All @@ -1506,7 +1506,7 @@ hooks.before("POST /savesearch/{SearchId} -> 200", function(test, done) {
done();
})

hooks.before("POST /savesearch/{SearchId} -> 400", function(test, done) {
hooks.before("POST /savesearches/{SearchId} -> 400", function(test, done) {
test.request.params = {
SearchId: search_id
};
Expand All @@ -1516,7 +1516,7 @@ hooks.before("POST /savesearch/{SearchId} -> 400", function(test, done) {
done();
})

hooks.before("DELETE /savesearch/{SearchId} -> 200", function(test, done) {
hooks.before("DELETE /savesearches/{SearchId} -> 200", function(test, done) {
test.request.params = {
SearchId: search_id
};
Expand Down
43 changes: 32 additions & 11 deletions test/integration_tests/python/test_savedsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,69 @@
def test_search_saving(as_admin, data_builder):

# Try posting a malformed search
r = as_admin.post('/savesearch', json={"not-label":"random-string"})
r = as_admin.post('/savesearches', json={"not-label":"random-string"})
assert r.status_code == 400

# Try getting a non-existent saved search
r = as_admin.get('/savesearch/000000000000000000000000')
r = as_admin.get('/savesearches/000000000000000000000000')
assert r.status_code == 404

# Save a search
r = as_admin.post('/savesearch', json={'label': 'search1', 'search': {'return_type': 'session'}})
r = as_admin.post('/savesearches', json={'label': 'search1', 'search': {'return_type': 'session'}})
assert r.ok
search = r.json()['_id']

# Get all searched user has access to
r = as_admin.get('/savesearch')
r = as_admin.get('/savesearches')
assert r.ok

# Get the saved search by id
r = as_admin.get('/savesearch/' + search)
r = as_admin.get('/savesearches/' + search)
assert r.ok
assert r.json()['label'] == 'search1'

# Malformed search replace
payload = {'label': 'good-label', 'search' : { 'not-return-type' : 'not-container'}}
r = as_admin.post('/savesearch/' + search, json=payload)
r = as_admin.post('/savesearches/' + search, json=payload)
assert r.status_code == 400

# Replace search
r = as_admin.get('/savesearch/' + search)
r = as_admin.get('/savesearches/' + search)
assert r.ok
assert r.json()['label'] == 'search1'
payload = r.json()
payload['label'] = 'newSearch'
r = as_admin.post('/savesearch/' + search, json=payload)
r = as_admin.post('/savesearches/' + search, json=payload)
assert r.ok
assert r.json()['_id'] == search
r = as_admin.get('/savesearch/' + search)
r = as_admin.get('/savesearches/' + search)
assert r.ok
assert r.json()['label'] == 'newSearch'

# Add permission to search
r = as_admin.post('/savesearches/' + search + '/permissions', json={'access': 'admin', '_id': '[email protected]'})
assert r.ok
r = as_admin.get('/savesearches/' + search)
assert r.ok
assert r.json()['permissions'][1]['_id'] == '[email protected]'

# Modify permission
r = as_admin.put('/savesearches/' + search + '/permissions/[email protected]', json={'access': 'ro'})
assert r.ok
r = as_admin.get('/savesearches/' + search)
assert r.ok
assert r.json()['permissions'][1]['access'] == 'ro'

# Remove permission
r = as_admin.delete('/savesearches/' + search + '/permissions/[email protected]')
assert r.ok
r = as_admin.get('/savesearches/' + search)
assert r.ok
assert len(r.json()['permissions']) == 1

# Delete saved search
r = as_admin.delete('/savesearch/' + search)
r = as_admin.delete('/savesearches/' + search)
assert r.ok
r = as_admin.get('/savesearch')
r = as_admin.get('/savesearches')
assert r.ok
assert len(r.json()) == 0

0 comments on commit ef79dca

Please sign in to comment.