def create_project(session, title='osf selenium test', tags=None, **kwargs): """Create a project for your current user through the OSF api. By default, projects will be given the `qatest` tag just in case deleting fails. If testing search, you will want to give the project no tags (or different tags). """ if tags is None: tags = ['qatest', os.environ['PYTEST_CURRENT_TEST']] node = client.Node(session=session) node.create(title=title, tags=tags, **kwargs) return node
def delete_project(session, guid, user=None): """Delete a single project. Simply pass in the guid""" if not user: user = current_user(session) nodes_url = user.relationships.nodes['links']['related']['href'] data = session.get(nodes_url) for node in data['data']: if node['id'] == guid: n = client.Node(id=node['id'], session=session) n.get() n.delete()
def get_existing_file(session, node_id=settings.PREFERRED_NODE): """Return the name of the first file in OSFStorage on a given node. Uploads a new file if one does not exist. """ node = client.Node(session=session, id=node_id) node.get() files_url = node.relationships.files['links']['related']['href'] data = session.get(files_url + 'osfstorage/') file = data['data'] if file: return data['data'][0]['attributes']['name'] else: return upload_fake_file(session, node)
def delete_all_user_projects(session, user=None): """Delete all of your user's projects that they have permission to delete except PREFERRED_NODE (if it's set). """ if not user: user = current_user(session) nodes_url = user.relationships.nodes['links']['related']['href'] data = session.get(nodes_url) for node in data['data']: if node['id'] != settings.PREFERRED_NODE: n = client.Node(id=node['id'], session=session) n.get() n.delete()
def delete_all_user_projects(session, user=None): """Delete all of your user's projects that they have permission to delete except PREFERRED_NODE (if it's set). """ if not user: user = current_user(session) nodes_url = user.relationships.nodes['links']['related']['href'] for _ in range(3): try: data = session.get(nodes_url) except requests.exceptions.HTTPError as exc: if exc.response.status_code == 502: logger.warning('502 Exception caught. Re-trying test') continue raise exc else: break else: logger.info('Max tries attempted') raise Exception('API not responding. Giving up.') nodes_failed = [] for node in data['data']: if node['id'] != settings.PREFERRED_NODE: n = client.Node(id=node['id'], session=session) try: n.get() n.delete() except Exception as exc: nodes_failed.append((node['id'], exc)) continue if nodes_failed: error_message_list = [] for error_tuple in nodes_failed: # Position [0] of error_tuple contains node_id # Position [1] of error_tuple contains the exception error_message = "node '{}' errored with exception: '{}'".format( error_tuple[0], error_tuple[1]) error_message_list.append(error_message) logger.error('\n'.join(error_message_list))
def get_node(session, node_id=settings.PREFERRED_NODE): return client.Node(session=session, id=node_id)