def test_user_get_user_key_by_id(self): usr = User('*****@*****.**') usr.set_nickname('Somebodyguki') usr.set_user_id('SomeUserid1234guki') key = ps.add_user(usr) self.assertEqual(key,ps.get_user_key_by_id("SomeUserid1234guki"))
def test_e_permissions_modify_user_element_permissions(self): key = ps.add_element_permissions(ndb.Key("uk","muep"),ndb.Key("ek","muep"),True) ps.modify_user_element_permissions(ndb.Key("uk","muep"),ndb.Key("ek","muep"), False) retrieved = ps.get_element_permissions_by_key(key) self.assertFalse(retrieved.access)
def test_user_get_user_by_key_succeed(self): usr = User('*****@*****.**') usr.set_nickname('Somebodyguks') usr.set_user_id('SomeUserid1234guks') key = ps.add_user(usr) self.assertEqual(ps.get_user_by_key(key).email(), usr.email())
def test_user_remove_user_by_key_succeed(self): usr = User('*****@*****.**') usr.set_nickname('Somebodyruks') usr.set_user_id('SomeUserid1234ruks') ukey = ps.add_user(usr) self.assertTrue(ps.remove_user_by_key(ukey))
def test_file_rename_file_by_key_success(self): key = ps.add_file( FileInfo("testFilernfks",ndb.Key("someKey","rnfks") ) ) self.assertTrue(ps.rename_file_by_key(key,"testFilernfks2")) fileobj = ps.get_file_by_key(key) self.assertEqual(fileobj.file_name,"testFilernfks2")
def test_permissions_revoke_all_by_user_key(self): uk = ndb.Key("uk","rauk") ps.add_file_permissions(ndb.Key("fk","rauk1"),uk,Permissions(True,True,True)) ps.add_file_permissions(ndb.Key("fk","rauk1"),uk,Permissions(True,True,True)) ps.add_file_permissions(ndb.Key("fk","rauk1"),uk,Permissions(True,True,True)) ps.revoke_all_by_user_key(uk) with self.assertRaises(StopIteration): ps.get_user_permissions_list(uk).next()
def test_e_permissions_revoke_element_permissions_element_key(self): ek = ndb.Key("ek","repek") ps.add_element_permissions(ndb.Key("uk","repek1"),ek,True) ps.add_element_permissions(ndb.Key("uk","repek2"),ek,True) ps.add_element_permissions(ndb.Key("uk","repek3"),ek,True) ps.revoke_element_permissions_element_key(ek) with self.assertRaises(StopIteration): ps.get_element_permissions_by_element_key(ek).next()
def test_e_permissions_revoke_element_permissions_user_key(self): uk = ndb.Key("uk","repuk") ps.add_element_permissions(uk,ndb.Key("ek","repuk1"),True) ps.add_element_permissions(uk,ndb.Key("ek","repuk1"),True) ps.add_element_permissions(uk,ndb.Key("ek","repuk1"),True) ps.revoke_element_permissions_user_key(uk) with self.assertRaises(StopIteration): ps.get_element_permissions_by_user_key(uk).next()
def test_permissions_revoke_all_by_file_key(self): fk = ndb.Key("fk","rafk") ps.add_file_permissions(fk,ndb.Key("uk","rafk1"),Permissions(True,True,True)) ps.add_file_permissions(fk,ndb.Key("uk","rafk2"),Permissions(True,True,True)) ps.add_file_permissions(fk,ndb.Key("uk","rafk3"),Permissions(True,True,True)) ps.revoke_all_by_file_key(fk) with self.assertRaises(StopIteration): ps.get_file_permissions_list(fk).next()
def file_list_json(request): app_access = __check_app_access() if app_access is not None: return app_access authed_user = auth.get_current_user() user_key = ps.get_user_key_by_id(authed_user.user_id()) #TODO: This shouldn't be here - a generic method in APIPermissions would be nice. # 'your files' file_list = [] lst = ps.get_user_permissions_list(user_key) temp_group = [] if lst is not None: for perm in lst: list_entry = {} file_entry = ps.get_file_by_key(perm.file_key) if file_entry is not None: temp_file = ds.get_file_info(file_entry.file_name) if temp_file is None: continue list_entry.update({ 'permissions' : 'yes', 'friendlyName' : file_entry.friendly_name, 'colour' : perm.colour, 'starred' : perm.starred } ) temp_file.filename = temp_file.filename.rpartition('/')[2] list_entry.update( { 'filename' : temp_file.filename, 'size' : temp_file.st_size, 'hash' : temp_file.etag, 'timestamp' : temp_file.st_ctime }) temp_group.append(list_entry) if len(temp_group) > 0: file_list.append({ 'catagory' : 'owned', 'heading' : 'Your Files', 'type' : 'files', 'data' : temp_group }) if len(file_list) == 0: file_list.append({ 'catagory' : 'notice', 'type' : 'html', # TODO: move this line out 'data' : '<a data-toggle="modal" data-target="#uploadModal" class="list-group-item">No files - Click here to upload one</a>' }) return HttpResponse(json.dumps(file_list), content_type="application/json")
def test_e_permissions_get_element_permissions_by_key(self): user_key = ndb.Key("uk","gepk") element_key = ndb.Key("ek","gepk") key = ps.add_element_permissions(user_key,element_key,True) obj = ps.get_element_permissions_by_key(key) self.assertEqual(obj.user_key, user_key) self.assertEqual(obj.element_key, element_key) self.assertEqual(obj.access, True)
def test_permissions_modify_file_permissions_by_key(self): permission = Permissions(True,True,True) key = ps.add_file_permissions(ndb.Key("fk","mfpk"),ndb.Key("uk","mfpk"),permission) new_permissions = Permissions(False,False,False) ps.modify_file_permissions_by_key(key, new_permissions) retrieved = ps.get_permissions_by_key(key) self.assertEqual(new_permissions.read, retrieved.read) self.assertEqual(new_permissions.write, retrieved.write) self.assertEqual(new_permissions.full_control, retrieved.full_control)
def test_user_modify_user_by_id_succeed(self): usr = User('*****@*****.**') usr.set_nickname('Somebodymids') usr.set_user_id('SomeUserid1234mids') ps.add_user(usr) newusr = User('*****@*****.**') newusr.set_nickname('SomebodyElsemids') newusr.set_user_id('SomeUotherId3878mids') key = ps.modify_user_by_id('SomeUserid1234mids',newusr) self.assertEqual(ps.get_user_by_key(key).email(), newusr.email())
def test_permissions_get_permissions_by_key(self): user_key = ndb.Key("uk","gpbk") file_key = ndb.Key("fk","gpbk") perms = Permissions(True,True,True) key = ps.add_file_permissions(file_key,user_key,perms) obj = ps.get_permissions_by_key(key) self.assertEqual(obj.user_key, user_key) self.assertEqual(obj.file_key, file_key) self.assertEqual(obj.read, perms.read) self.assertEqual(obj.write, perms.write) self.assertEqual(obj.full_control, perms.full_control)
def test_file_get_files_by_owner_key_sucess(self): ps.add_file( FileInfo("testFile1",ndb.Key("someKey","gfuks") ) ) ps.add_file( FileInfo("testFile2",ndb.Key("someKey","gfuks") ) ) ps.add_file( FileInfo("testFile3",ndb.Key("someKey","gfuks") ) ) ps.add_file( FileInfo("testFile4",ndb.Key("someKey","gfuks") ) ) iterate = ps.get_file_by_owner_key(ndb.Key("someKey","gfuks")) for fileobj in iterate: self.assertEqual(fileobj.owner_key, ndb.Key("someKey","gfuks"))
def __check_app_access(redir = False): authed_user = auth.get_current_user() if authed_user is None: return __unauthed_response(redir) else: elem = ps.get_element_key_by_ref('canLogIn') if elem is not None: user_key = ps.get_user_key_by_id(authed_user.user_id()) if user_key is not None: perm = ps.get_user_element_permissions(user_key, elem) if perm is not None: if perm.access: return None return __not_signed_up_response(redir)
def test_user_add_user(self): usr = User('*****@*****.**') usr.set_nickname('Somebodyau') usr.set_user_id('SomeUserid1234au') ret = ps.add_user(usr) self.assertTrue(isinstance(ret, ndb.Key))
def analysis_status_json(request): authed_user = auth.get_current_user() if authed_user is None: return __unauthed_response() user_key = ps.get_user_key_by_id(authed_user.user_id()) response_part = { 'backoff' : 0, #tells the client to back off for a given amount of time (milliseconds) (This is added to the client's constant poll interval) 'giveup' : True, #True instructs the client to stop polling - useful for situations such as unauthed requests where polling will never result in the user being shown a graph 'done' : False #True indicates that the analysis has finished and that the user can be redirected to the new image } try: file_req = json.loads(request.raw_post_data) except ValueError: response_part.update({'error' : 'Invalid request payload.'}) return HttpResponse(json.dumps(response_part), content_type="application/json") if 'filename' not in file_req: response_part.update({'error' : 'Incomplete request.'}) return HttpResponse(json.dumps(response_part), content_type="application/json") chk_file_name = DATA_BUCKET + '/' + file_req['filename'] file_entry = ps.get_file_by_name(chk_file_name) logging.info(file_entry) if file_entry is None: response_part.update( { 'error' : 'File or gate not recognised.', 'giveup' : False } ) return HttpResponse(json.dumps(response_part), content_type="application/json") else: fp_entry = ps.get_user_file_permissions(file_entry.key, user_key) if fp_entry is None: response_part.update( { 'error' : 'Permission denied.' } ) return HttpResponse(json.dumps(response_part), content_type="application/json") name = file_req['filename'] is_done = ds.check_exists(GRAPH_BUCKET + '/' + name + '.png', None) #Prevent redirecting before the view is ready is_done &= ds.check_exists(DATA_BUCKET + '/' + name, None) #Prevent redirecting before gating info is ready is_done &= ds.check_exists(INFO_BUCKET + '/' + name + '.txt', None) response_part.update( { 'done' : is_done, 'giveup' : False } ) return HttpResponse(json.dumps(response_part), content_type="application/json")
def test_e_permissions_get_element_permissions_by_user_key(self): uk = ndb.Key("uk","gepuk") ps.add_element_permissions(uk,ndb.Key("ek","gepuk1"),True) ps.add_element_permissions(uk,ndb.Key("ek","gepuk2"),True) ps.add_element_permissions(uk,ndb.Key("ek","gepuk3"),True) elements = ps.get_element_permissions_by_user_key(uk) for element in elements: self.assertEqual(element.user_key, uk)
def test_e_permissions_get_element_permissions_by_element_key(self): ek = ndb.Key("ek","gepek") ps.add_element_permissions(ndb.Key("uk","gepek1"),ek,True) ps.add_element_permissions(ndb.Key("uk","gepek2"),ek,True) ps.add_element_permissions(ndb.Key("uk","gepek3"),ek,True) elements = ps.get_element_permissions_by_element_key(ek) for element in elements: self.assertEqual(element.element_key, ek)
def test_permissions_get_user_permissions_list(self): uk = ndb.Key("uk","gupl") ps.add_file_permissions(ndb.Key("fk","gupl1"),uk,Permissions(True,True,True)) ps.add_file_permissions(ndb.Key("fk","gupl2"),uk,Permissions(True,True,True)) ps.add_file_permissions(ndb.Key("fk","gupl3"),uk,Permissions(True,True,True)) permissions = ps.get_file_permissions_list(uk) for permission in permissions: self.assertEqual(permission.user_key, uk)
def test_permissions_get_file_permissions_list(self): fk = ndb.Key("fk","gfpl") ps.add_file_permissions(fk,ndb.Key("uk","gfpl1"),Permissions(True,True,True)) ps.add_file_permissions(fk,ndb.Key("uk","gfpl2"),Permissions(True,True,True)) ps.add_file_permissions(fk,ndb.Key("uk","gfpl3"),Permissions(True,True,True)) permissions = ps.get_file_permissions_list(fk) for permission in permissions: self.assertEqual(permission.file_key, fk)
def fetch_file(path, type, friendly): # TODO: Need protection against hack such as ../ buffer = ds.open(path) if buffer: file = buffer.read() # TODO: Maybe transform the httpresponse to streaminghttpresponse in case the graph is really large and to improve efficiency response = HttpResponse(file, content_type=type) # Get filename. if friendly: path = path.rpartition('/')[0] + '/' + ps.get_file_by_name(path).friendly_name # Construct and send response response['Content-Disposition'] = 'attachment; filename="' + path + '"' return response else: return HttpResponseNotFound('<h1>404 : ' + path + ' not found</h1>')
def tool(request): authed_user = auth.get_current_user() if authed_user is None: return __unauthed_response() try: gate_info = json.loads(request.raw_post_data) print gate_info except ValueError: return HttpResponse(simplejson.dumps(gt.generate_gating_feedback('fail', 'Invalid request payload')), content_type="application/json") ## \todo This should probably iterate over list if (('points' not in gate_info) or ('tool' not in gate_info) or ('filename' not in gate_info)): return HttpResponse(simplejson.dumps(gt.generate_gating_feedback('fail', 'Incomplete gate parameters')), content_type="application/json") if (gate_info['points']): if not isinstance(gate_info['points'], list): return HttpResponse(simplejson.dumps(gt.generate_gating_feedback('fail', 'Invalid points list')), content_type="application/json") else: # Normalise false value to None gate_info.update( { 'points' : None } ) file_entry = ps.get_file_by_name(DATA_BUCKET + '/' + gate_info['filename']) if file_entry is None: return HttpResponse(simplejson.dumps(gt.generate_gating_feedback('fail', 'File not found')), content_type="application/json") gate_info.update( { 'axes' : { 'x' : file_entry.axis_a, 'y' : file_entry.axis_b } } ) tool = gt.AVAILABLE_TOOLS.get(gate_info['tool'], gt.no_such_tool) ## first two arguments passed for compatibility tool_response = tool(gate_info) ## Load balance instances in the background. background.run(instances.balance, 0) json_response = simplejson.dumps(tool_response); return HttpResponse(json_response, content_type="application/json")
def generate_gating_feedback(status, message, new_graph_name = None, existing_name = None, new_axis_a = "FSC-A", new_axis_b = "SSC-A"): if new_graph_name is not None: ## Authenticate and get user authed_user = auth.get_current_user() user_key = ps.get_user_key_by_id(authed_user.user_id()) ## Get previous file permissions. previous_file = ps.get_file_by_name(DATA_BUCKET + existing_name) previous_permissions = ps.get_user_file_permissions(previous_file.key, user_key) if isinstance(new_graph_name, list): new_graph_names = new_graph_name else: new_graph_names = [new_graph_name] # Overwrite new_graph_name for return dictionary new_graph_name = new_graph_names[0] logging.info(new_graph_name) logging.info(new_graph_names) for new_name in new_graph_names: new_file = FileInfo(file_name = DATA_BUCKET + new_name, owner_key = user_key, friendly_name = previous_file.friendly_name + '-gate', prev_file_key = previous_file.key, axis_a = new_axis_a, axis_b = new_axis_b ) file_key = ps.add_file(new_file) ps.add_file_permissions(file_key, user_key, Permissions ( previous_permissions.read, previous_permissions.write, previous_permissions.full_control ), previous_permissions.colour, False) return { 'status': status, 'message': message, 'url': reverse('get_graph', args=[new_graph_name]), 'graphName' : new_graph_name }
def signup_handler(request): authed_user = auth.get_current_user() if authed_user is None: return __unauthed_response() user_key = ps.get_user_key_by_id(authed_user.user_id()) if user_key is None: user_key = ps.add_user(authed_user) response_part = { 'success' : False } try: file_req = json.loads(request.raw_post_data) except ValueError: response_part.update({'error' : 'Invalid request payload.'}) return HttpResponse(json.dumps(response_part), content_type="application/json") if 'action' not in file_req: response_part.update({'error' : 'Incomplete request.'}) return HttpResponse(json.dumps(response_part), content_type="application/json") elem_key = ps.get_element_key_by_ref('canLogIn') if elem_key is None: elem_key = ps.add_element('canLogIn') user_elem = ps.get_user_element_permissions(user_key, elem_key) if user_elem is not None: response_part.update({'error' : 'Already signed up.'}) return HttpResponse(json.dumps(response_part), content_type="application/json") else: ps.add_element_permissions(user_key, elem_key, True) response_part.update({'success' : True}) return HttpResponse(json.dumps(response_part), content_type="application/json")
def test_user_remove_user_by_key_fail(self): badKey = "asdfasdf" self.assertFalse(ps.remove_user_by_key(badKey))
def test_e_permissions_get_user_element_permissions_key(self): user_key = ndb.Key("uk","guepk") element_key = ndb.Key("ek","guepk") key = ps.add_element_permissions(user_key,element_key,True) self.assertEqual(key,ps.get_user_element_permissions_key(user_key,element_key))
def test_user_remove_user_by_id_fail(self): self.assertFalse(ps.remove_user_by_id('userThatDoesntExist'))
def test_e_permissions_revoke_element_permissions_by_key(self): key = ps.add_element_permissions(ndb.Key("uk","repk"),ndb.Key("ek","repk"),True) ps.revoke_element_permissions_by_key(key) self.assertIsNone(ps.get_element_permissions_by_key(key))