Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
    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()
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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()
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
    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()
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    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()
Ejemplo n.º 13
0
    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()
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
    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
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
0
    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
Ejemplo n.º 20
0
    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
Ejemplo n.º 21
0
    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
Ejemplo n.º 22
0
    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
Ejemplo n.º 23
0
    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
Ejemplo n.º 24
0
    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