class AuthTests(unittest.TestCase): """Make sure that users can only see the pages and such that they should be seeing. """ #TODO: can we make this a classmethod without SQLAlchemy complaining? def setUp(self): database.clean() self.client = application.test_client() self.user1 = user_datastore.create_user(email="*****@*****.**", password="******") self.user2 = user_datastore.create_user(email="*****@*****.**", password="******") db.session.commit() with self.client.session_transaction() as sess: sess["user_id"] = self.user1.get_id() sess["_fresh"] = True self.project = Project(name="Bars project", user=self.user2) self.project.save() file_handle, file_path = tempfile.mkstemp() file_handle = os.fdopen(file_handle, "r+") file_handle.write("foobar") self.file_path = os.path.join(file_path) self.document_file = DocumentFile(projects=[self.project], path=self.file_path) self.document_file.save() def test_list_projects(self): """Test to make sure that bar's projects aren't listed for foo. """ result = self.client.get("/projects/") assert "Bars project" not in result.data def test_view_project(self): """Test to make sure that foo can't see bar's project. """ result = self.client.get("/projects/" + str(self.project.id)) assert "Bars project" not in result.data def test_view_document(self): """Test to make sure that foo can't see bar's file. """ result = self.client.get("/projects/" + str(self.project.id) + "/documents/" + str(self.document_file.id)) assert "/uploads/" + str(self.document_file.id) not in result.data def test_get_document(self): """Test to make sure that foo can't get bar's file. """ result = self.client.get("/uploads/" + str(self.document_file.id)) with open(self.file_path) as test_file: assert result.data is not test_file.read()
class AuthTests(unittest.TestCase): """Make sure that users can only see the pages and such that they should be seeing. """ #TODO: can we make this a classmethod without SQLAlchemy complaining? def setUp(self): database.clean() self.client = application.test_client() self.user1 = user_datastore.create_user(email="*****@*****.**", password="******") self.user2 = user_datastore.create_user(email="*****@*****.**", password="******") db.session.commit() with self.client.session_transaction() as sess: sess["user_id"] = self.user1.get_id() sess["_fresh"] = True self.project = Project(name="Bars project") self.user2.add_project(self.project, role=ProjectsUsers.ROLE_ADMIN) file_handle, file_path = tempfile.mkstemp() file_handle = os.fdopen(file_handle, "r+") file_handle.write("foobar") self.file_path = os.path.join(file_path) self.document_file = DocumentFile(projects=[self.project], path=self.file_path) self.document_file.save() def test_list_projects(self): """Test to make sure that bar's projects aren't listed for foo. """ result = self.client.get("/projects/") assert "Bars project" not in result.data def test_view_project(self): """Test to make sure that foo can't see bar's project. """ result = self.client.get("/projects/" + str(self.project.id)) assert "Bars project" not in result.data def test_view_document(self): """Test to make sure that foo can't see bar's file. """ result = self.client.get("/projects/" + str(self.project.id) + "/documents/" + str(self.document_file.id)) assert "/uploads/" + str(self.document_file.id) not in result.data def test_get_document(self): """Test to make sure that foo can't get bar's file. """ result = self.client.get("/uploads/" + str(self.document_file.id)) with open(self.file_path) as test_file: assert result.data is not test_file.read()
def test_project_show_delete(self, mock_os): """Test file deletion. """ mock_os.path.isdir.return_value = False project = Project(name="test") db.session.add(project) self.user.add_project(project, role=ProjectsUsers.ROLE_ADMIN) project.save() pid = str(project.id) document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") db.session.add_all([document_file1, document_file2]) document_file1.save() document_file2.save() result = self.client.post(application.config["DELETE_ROUTE"], data={ "project_id": pid, "obj_type": "doc", "obj_id": document_file1.id }) assert '"obj_type": "doc",' in result.data assert '"obj_id": %s' % document_file1.id in result.data mock_os.remove.assert_any_call("/test-path/1.xml") # mock_os.remove.assert_any_call("/test-path/2.xml") assert mock_os.remove.call_count == 1
def test_get_file(self): """Run tests on the get_file view. """ file_handle, file_path = tempfile.mkstemp() file_handle = os.fdopen(file_handle, "r+") file_handle.write("foobar") project = Project(users=[self.user]) document_file = DocumentFile(path=file_path, projects=[project]) document_file.save() result = self.client.get(application.config["UPLOAD_ROUTE"] + "doc/%s" % document_file.id) with open(file_path) as test_file: assert result.data == file_handle.read()
def test_get_file(self): """Run tests on the get_file view. """ file_handle, file_path = tempfile.mkstemp() file_handle = os.fdopen(file_handle, "r+") file_handle.write("foobar") project = Project(user=self.user) document_file = DocumentFile(path=file_path, projects=[project]) document_file.save() result = self.client.get("/uploads/1") with open(file_path) as test_file: assert result.data == file_handle.read()
def test_logs(self): """Test to make sure that logs are being displayed. """ project1 = Project(name="log test project", path="/log-test-path") self.user.add_project(project1, role=ProjectsUsers.ROLE_ADMIN) logs = [ WarningLog(log_item="a", item_value="a", project=project1), InfoLog(log_item="b", item_value="b", project=project1), ErrorLog(log_item="c", item_value="c", project=project1) ] project1.document_files = [DocumentFile(path="foo")] db.session.add(project1) db.session.commit() result = self.client.get(application.config["PROJECT_ROUTE"] + str(project1.id)) print result.data assert "log test project" in result.data assert "processlog alert alert-warning" in result.data assert "processlog alert alert-warning hidden" not in result.data assert "processlog alert alert-info" in result.data assert "processlog alert alert-info hidden" not in result.data assert "processlog alert alert-danger" in result.data assert "processlog alert alert-danger hidden" not in result.data assert "<em>a</em>: a" in result.data assert "<em>b</em>: b" in result.data assert "<em>c</em>: c" in result.data
def test_process_processed_files(self): """Make sure that a project that's being processed or already processed can't be processed again. """ project1 = Project(name="foo", path="/test-path", status=Project.STATUS_PREPROCESSING) rel = self.user.add_project(project1, role=ProjectsUsers.ROLE_ADMIN) document_file = DocumentFile(path="foo/foo.xml") structure_file = StructureFile(path="foo/foo.json") project1.document_files = [document_file] project1.structure_files = [structure_file] project1.save() data = { "struc_id": structure_file.id, } result = self.client.post(application.config["PROCESS_ROUTE"] + str(project1.id), data=data) assert '"status": "OK"' not in result.data project1.status = Project.STATUS_DONE project1.save() result = self.client.post(application.config["PROCESS_ROUTE"] + str(project1.id), data=data) assert '"status": "OK"' not in result.data
def setUp(self, path, structure_file, input_file): """Set up some common variables. :param str path: The path that contains both the structure_file and input_file. :param str structure_file: The file with a JSON description of the XML structure. :param str input_file: The XML file to test. """ database.clean() self.path = path self.structure_file = path + structure_file self.input_file = path + input_file string_processor.project = Project() self.input_project = Project() self.input_project.document_files.append( DocumentFile(path=self.input_file)) self.input_project.save() with open(self.structure_file) as f: self.json = json.load(f) self.xml = etree.parse(self.input_file) self.extractor = StructureExtractor(self.input_project, self.structure_file, string_processor)
def test_document_show(self): """Test the detail document view. """ projxyz = Project(name="test project", path="/test-path/", user=self.user) docxyz = DocumentFile(path="/test-path/test-file.xml", projects=[projxyz]) docxyz.save() projxyz.save() #TODO: why is this necessary? why does sqlalchemy complain otherwise docid = docxyz.id result = self.client.get("/projects/1/documents/1") assert "/uploads/" + str(docid) in result.data assert "test-file.xml" in result.data
def setUpClass(cls): """Reset the DB and create a dummy project and document. """ database.clean() cls.client = application.test_client() user = User() db.session.add(user) db.session.commit() project = Project(name="Bars project", users=[user]) project.save() cls.file_handle, cls.file_path = tempfile.mkstemp() cls.file = os.fdopen(cls.file_handle, "r+") cls.file.write("foobar") cls.file_name = os.path.split(cls.file_path)[1] document_file = DocumentFile(projects=[project], path=cls.file_path) document_file.save()
def setUp(self): """Reset the DB and create a dummy project and document. """ database.clean() self.client = application.test_client() user = User() db.session.add(user) db.session.commit() project = Project(name="Bars project", user=user) project.save() self.file_handle, self.file_path = tempfile.mkstemp() self.file = os.fdopen(self.file_handle, "r+") self.file.write("foobar") self.file_name = os.path.split(self.file_path)[1] document_file = DocumentFile(projects=[project], path=self.file_path) document_file.save()
def test_project_show_process_no_perms(self): """Process files without proper permissions. """ project = Project(name="foo") rel = self.user.add_project(project, role=ProjectsUsers.ROLE_USER) document_file = DocumentFile(projects=[project], path="/foo/bar.xml") structure_file = StructureFile(project=project, path="/foo/bar.json") document_file.save() structure_file.save() project.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "0", "process-selection": [str(document_file.id)], "process-structure_file": str(structure_file.id) }) assert "You can't do that" in result.data
def test_project_show_bad_process(self, mock_process_files): """Test processing an unprocessable group of files. """ project = Project(name="test", user=self.user, path="/foo") project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") document_file1.save() document_file2.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "0", "process-selection": ["1", "2"] }) assert "must include exactly one" in result.data structure_file = StructureFile(project=project, path="/foo/bar.json") structure_file.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "0", "process-structure_file": [str(structure_file.id)] }) assert "at least one document" in result.data.lower()
def setUp(self): database.clean() self.client = application.test_client() self.user1 = user_datastore.create_user(email="*****@*****.**", password="******") self.user2 = user_datastore.create_user(email="*****@*****.**", password="******") db.session.commit() with self.client.session_transaction() as sess: sess["user_id"] = self.user1.get_id() sess["_fresh"] = True self.project = Project(name="Bars project") self.user2.add_project(self.project, role=ProjectsUsers.ROLE_ADMIN) file_handle, file_path = tempfile.mkstemp() file_handle = os.fdopen(file_handle, "r+") file_handle.write("foobar") self.file_path = os.path.join(file_path) self.document_file = DocumentFile(projects=[self.project], path=self.file_path) self.document_file.save()
def test_project_show_process_no_perms(self): """Delete files without proper permissions. """ project = Project(name="foo") rel = self.user.add_project(project, role=ProjectsUsers.ROLE_USER) document_file1 = DocumentFile(projects=[project], path="/foo/bar.xml") structure_file1 = StructureFile(project=project, path="/foo/bar.json") project.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "-1", "process-selection": ["1"] }) assert '"status": "OK",' not in result.data
def test_project_show(self): """Make sure project_show shows files. """ project = Project(name="test", users=[self.user]) project.save() document_file1 = DocumentFile(path="/test/doc1.xml", projects=[project]) document_file2 = DocumentFile(path="/test/doc2.xml", projects=[project]) document_file1.save() document_file2.save() result = self.client.get("/projects/1") assert "doc1.xml" in result.data assert "doc2.xml" in result.data assert application.config[ "UPLOAD_ROUTE"] + "doc/%s" % document_file1.id in result.data assert application.config[ "UPLOAD_ROUTE"] + "doc/%s" % document_file2.id in result.data
def test_project_show_bad_process(self, mock_process_files): """Test processing an unprocessable group of files. """ project = Project(name="test", path="/foo") rel = self.user.add_project(project, role=ProjectsUsers.ROLE_ADMIN) db.session.add(project) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") db.session.add_all([document_file1, document_file2]) document_file1.save() document_file2.save() result = self.client.post(application.config["PROCESS_ROUTE"] + str(project.id), data={"struc_id": 555}) assert '"status": "OK",' not in result.data
def test_project_show_process(self, mock_process_files): """Test processing a processable group of files. """ #TODO: why is this passing? project = Project(name="test", users=[self.user]) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.json") document_file1.save() document_file2.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "0", "process-selection": ["1", "2"] }) assert "Errors have occurred" not in result.data
def test_project_show_bad_delete(self): """Test a bad file delete request. """ project = Project(name="test", users=[self.user]) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") document_file1.save() document_file2.save() result = self.client.post( application.config["DELETE_ROUTE"], data={ "project_id": project.id, "obj_type": "doc", # missing the object id }) assert '"status": "OK"' not in result.data
def setUp(self): """Parse the brief example""" database.clean() self.path = "tests/data/plays/" self.structure_file = self.path + "structure.json" self.input_file = self.path + "brief_example.xml" self.input_project = Project() t.project = self.input_project self.input_project.document_files.append( DocumentFile(path=self.input_file)) self.input_project.save() with open(self.structure_file) as f: self.json = json.load(f) self.xml = etree.parse(self.input_file) self.extractor = StructureExtractor(self.input_project, self.structure_file, t)
def test_project_show(self): """Make sure project_show shows files. """ project = Project(name="test", user=self.user) project.save() document_file1 = DocumentFile(path="/test/doc1.xml", projects=[project]) document_file2 = DocumentFile(path="/test/doc2.xml", projects=[project]) document_file1.save() document_file2.save() result = self.client.get("/projects/1") assert "doc1.xml" in result.data assert "doc2.xml" in result.data assert "/projects/1/documents/1" in result.data assert "/projects/1/documents/2" in result.data
def test_project_show(self): """Make sure project_show shows files. """ project = Project(name="test", users=[self.user]) project.save() document_file1 = DocumentFile(path="/test/doc1.xml", projects=[project]) document_file2 = DocumentFile(path="/test/doc2.xml", projects=[project]) document_file1.save() document_file2.save() result = self.client.get("/projects/1") assert "doc1.xml" in result.data assert "doc2.xml" in result.data assert application.config["UPLOAD_ROUTE"] + "doc/%s" % document_file1.id in result.data assert application.config["UPLOAD_ROUTE"] + "doc/%s" % document_file2.id in result.data
def setUp(self): database.clean() self.client = application.test_client() self.user1 = user_datastore.create_user(email="*****@*****.**", password="******") self.user2 = user_datastore.create_user(email="*****@*****.**", password="******") db.session.commit() with self.client.session_transaction() as sess: sess["user_id"] = self.user1.get_id() sess["_fresh"] = True self.project = Project(name="Bars project", user=self.user2) self.project.save() file_handle, file_path = tempfile.mkstemp() file_handle = os.fdopen(file_handle, "r+") file_handle.write("foobar") self.file_path = os.path.join(file_path) self.document_file = DocumentFile(projects=[self.project], path=self.file_path) self.document_file.save()
def test_project_show_bad_delete(self): """Test a bad file delete request. """ project = Project(name="test", users=[self.user]) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") document_file1.save() document_file2.save() result = self.client.post(application.config["DELETE_ROUTE"], data={ "project_id": project.id, "obj_type": "doc", # missing the object id }) assert '"status": "OK"' not in result.data
def test_project_show_bad_process(self, mock_process_files): """Test processing an unprocessable group of files. """ project = Project(name="test", path="/foo") rel = self.user.add_project(project, role=ProjectsUsers.ROLE_ADMIN) db.session.add(project) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") db.session.add_all([document_file1, document_file2]) document_file1.save() document_file2.save() result = self.client.post(application.config["PROCESS_ROUTE"] + str(project.id), data={ "struc_id": 555 }) assert '"status": "OK",' not in result.data
def test_project_show_process(self, mock_process_files): """Test processing a processable group of files. """ project = Project(name="test", user=self.user) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.json") document_file1.save() document_file2.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "0", "process-selection": ["1", "2"] }) assert "Errors have occurred" not in result.data
def test_project_show_bad_delete(self): """Test a bad file delete request. """ project = Project(name="test", user=self.user) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") document_file1.save() document_file2.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "-1", }) assert "must select" in result.data assert "/projects/1/documents/1" in result.data assert "/projects/1/documents/2" in result.data
def test_project_show_delete(self, mock_os): """Test file deletion. """ mock_os.path.isdir.return_value = False project = Project(name="test", user=self.user) project.save() document_file1 = DocumentFile(projects=[project], path="/test-path/1.xml") document_file2 = DocumentFile(projects=[project], path="/test-path/2.xml") document_file1.save() document_file2.save() result = self.client.post("/projects/1", data={ "process-submitted": "true", "action": "-1", "process-selection": ["1", "2"] }) assert "no files in this project" in result.data mock_os.remove.assert_any_call(document_file1.path) mock_os.remove.assert_any_call(document_file2.path) assert mock_os.remove.call_count == 2
collection_name = "articles" if len(sys.argv) > 1 and sys.argv[1]: collection_name = sys.argv[1] collection_dir = os.path.join("tests", "data", collection_name) extension = ".xml" structure_file = os.path.join(collection_dir, "structure.json") database.reset() project = Project() project.save() user = User(email="test", password="******") user.add_project(project) user.save() files = [ f for f in os.listdir(collection_dir) if os.path.isfile(os.path.join(collection_dir, f)) ] for file_name in files: if os.path.splitext(file_name)[1] == extension: document_file = DocumentFile( path=os.path.join(collection_dir, file_name)) project.document_files.append(document_file) cp_run(collection_dir, structure_file, extension, project.id)