Ejemplo n.º 1
0
 def test_import_permissions(self):
     assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
     another_user = '******', 'UV20', 'uv20'
     assert add_user(another_user[0], another_user[1], another_user[2])
     with self.app.test_client() as test_client:
         import_operation, token = self._create_operation(test_client,
                                                          self.userdata,
                                                          path="import")
         user = get_user(self.userdata[0])
         another = get_user(another_user[0])
         fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
         fm.add_bulk_permission(import_operation.id, user, [another.id],
                                "viewer")
         current_operation, token = self._create_operation(test_client,
                                                           self.userdata,
                                                           path="current")
         response = test_client.post('/import_permissions',
                                     data={
                                         "token": token,
                                         "import_op_id":
                                         import_operation.id,
                                         "current_op_id":
                                         current_operation.id
                                     })
         assert response.status_code == 200
         data = json.loads(response.data.decode('utf-8'))
         # creator is not listed
         assert data["success"] is True
Ejemplo n.º 2
0
 def _save_content(self, operation, userdata=None):
     if userdata is None:
         userdata = self.userdata
     user = get_user(userdata[0])
     fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
     fm.save_file(operation.id, "content1", user)
     return fm, user
Ejemplo n.º 3
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)

        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        self.user = get_user(self.userdata[0])
        assert self.user is not None
        assert add_user('UV20@uv20', 'UV20', 'uv20')
        self.user_2 = get_user('UV20@uv20')
Ejemplo n.º 4
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)
        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.operation_name = "XYZ"
        self.description = "Template"
        self.userdata_0 = 'UV0@uv0', 'UV0', 'uv0'
        self.userdata_1 = "UV1@uv1", "UV1", "UV1"
        self.userdata_2 = "UV2@v2", "V2", "v2"

        assert add_user(self.userdata_0[0], self.userdata_0[1], self.userdata_0[2])
        assert add_operation(self.operation_name, self.description)
        assert add_user_to_operation(path=self.operation_name, emailid=self.userdata_0[0])
        self.user = User(self.userdata_0[0], self.userdata_0[1], self.userdata_0[2])
Ejemplo n.º 5
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)

        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV11@uv11', 'UV11', 'uv11'
        self.userdata2 = 'UV12@uv12', 'UV12', 'uv12'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        assert add_user(self.userdata2[0], self.userdata2[1], self.userdata2[2])

        self.user = get_user(self.userdata[0])
        self.user2 = get_user(self.userdata2[0])
        assert self.user is not None
        self.file_message_counter = [0] * 2
        self._example_data()
Ejemplo n.º 6
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'
        self.anotheruserdata = 'UV20@uv20', 'UV20', 'uv20'
        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        self.user = get_user(self.userdata[0])
        assert self.user is not None
        assert add_user(self.anotheruserdata[0], self.anotheruserdata[1],
                        self.anotheruserdata[2])
        self.anotheruser = get_user(self.anotheruserdata[0])
        assert add_user('UV30@uv30', 'UV30', 'uv30')
        self.vieweruser = get_user('UV30@uv30')
        assert add_user('UV40@uv40', 'UV40', 'uv40')
        self.collaboratoruser = get_user('UV40@uv40')

        self._example_data()
Ejemplo n.º 7
0
def setup_managers(app):
    """
    takes app as parameter to extract config data,
    initializes ChatManager, FileManager, SocketManager and return them
    #ToDo return socketio and integrate socketio.cm = ChatManager()
    similarly for FileManager and SocketManager(already done for this)
    """

    cm = ChatManager()
    fm = FileManager(app.config["MSCOLAB_DATA_DIR"])
    sm = SocketsManager(cm, fm)
    # sockets related handlers
    socketio.on_event('connect', sm.handle_connect)
    socketio.on_event('start', sm.handle_start_event)
    socketio.on_event('disconnect', sm.handle_disconnect)
    socketio.on_event('chat-message', sm.handle_message)
    socketio.on_event('edit-message', sm.handle_message_edit)
    socketio.on_event('delete-message', sm.handle_message_delete)
    socketio.on_event('file-save', sm.handle_file_save)
    socketio.on_event('add-user-to-room', sm.join_creator_to_room)
    socketio.sm = sm
    return socketio, cm, fm
Ejemplo n.º 8
0
class Test_Files(TestCase):
    render_templates = False

    def create_app(self):
        app = APP
        app.config[
            'SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
        app.config['MSCOLAB_DATA_DIR'] = mscolab_settings.MSCOLAB_DATA_DIR
        app.config['UPLOAD_FOLDER'] = mscolab_settings.UPLOAD_FOLDER
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        app.config["TESTING"] = True
        app.config['LIVESERVER_TIMEOUT'] = 10
        app.config['LIVESERVER_PORT'] = 0
        return app

    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)

        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV11@uv11', 'UV11', 'uv11'
        self.userdata2 = 'UV12@uv12', 'UV12', 'uv12'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        assert add_user(self.userdata2[0], self.userdata2[1],
                        self.userdata2[2])

        self.user = get_user(self.userdata[0])
        self.user2 = get_user(self.userdata2[0])
        assert self.user is not None
        self.file_message_counter = [0] * 2
        self._example_data()

    def tearDown(self):
        pass

    def test_create_operation(self):
        with self.app.test_client():
            # test for blank character in path
            assert self.fm.create_operation('test path', 'test desc.',
                                            self.user) is False
            # test for normal path
            assert self.fm.create_operation('test_path', 'test desc.',
                                            self.user) is True
            # test for '/' in path
            assert self.fm.create_operation('test/path', 'sth',
                                            self.user) is False
            # check file existence
            assert os.path.exists(
                os.path.join(mscolab_settings.MSCOLAB_DATA_DIR,
                             'test_path')) is True
            # check creation in db
            p = Operation.query.filter_by(path="test_path").first()
            assert p is not None
            # check permission for author
            perms = Permission.query.filter_by(op_id=p.id,
                                               access_level="creator").all()
            assert len(perms) == 1
            assert perms[0].u_id == self.user.id

    def test_operations(self):
        with self.app.test_client():
            operations = self.fm.list_operations(self.user)
            assert len(operations) == 0
            assert self.fm.create_operation('test_path', 'test desc.',
                                            self.user) is True
            operations = self.fm.list_operations(self.user)
            assert len(operations) == 1

    def test_is_creator(self):
        with self.app.test_client():
            assert self.fm.create_operation('test_path', 'test desc.',
                                            self.user) is True
            op_id = get_recent_op_id(self.fm, self.user)
            u_id = self.user.id
            assert self.fm.is_creator(u_id, op_id) is True
            undefined_op_id = 123
            assert self.fm.is_creator(u_id, undefined_op_id) is False
            no_perm_op_id = 2
            assert self.fm.is_creator(u_id, no_perm_op_id) is False

    def test_file_save(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation77")
            assert self.fm.save_file(operation.id, "beta", self.user)
            assert self.fm.get_file(operation.id, self.user) == "beta"
            assert self.fm.save_file(operation.id, "gamma", self.user)
            assert self.fm.get_file(operation.id, self.user) == "gamma"
            # check if change is saved properly
            changes = self.fm.get_all_changes(operation.id, self.user)
            assert len(changes) == 2

    def test_undo(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation7", content="alpha")
            assert self.fm.save_file(operation.id, "beta", self.user)
            assert self.fm.save_file(operation.id, "gamma", self.user)
            changes = Change.query.filter_by(op_id=operation.id).all()
            assert changes is not None
            assert changes[0].id == 1
            assert self.fm.undo(changes[0].id, self.user) is True
            assert len(self.fm.get_all_changes(operation.id, self.user)) == 3
            assert "beta" in self.fm.get_file(operation.id, self.user)

    def test_get_operation(self):
        with self.app.test_client():
            self._create_operation(flight_path="operation9")
            op_id = get_recent_op_id(self.fm, self.user)
            assert self.fm.get_file(op_id, self.user) is not False
            user2 = User.query.filter_by(emailid=self.userdata2[0]).first()
            assert self.fm.get_file(op_id, user2) is False

    def test_authorized_users(self):
        with self.app.test_client():
            self._create_operation(flight_path="operation10",
                                   content=self.content1)
            op_id = get_recent_op_id(self.fm, self.user)
            assert len(self.fm.get_authorized_users(op_id)) == 1

    def test_modify_operation(self):
        with self.app.test_client():
            self._create_operation(flight_path="path")
            op_id = get_recent_op_id(self.fm, self.user)
            # testing for wrong characters in path like ' ', '/'
            assert self.fm.update_operation(op_id, 'path', 'dummy wrong',
                                            self.user) is False
            assert self.fm.update_operation(op_id, 'path', 'dummy/wrong',
                                            self.user) is False
            assert self.fm.update_operation(op_id, 'path', 'dummy',
                                            self.user) is True
            assert os.path.exists(
                os.path.join(mscolab_settings.MSCOLAB_DATA_DIR, 'dummy'))
            assert self.fm.update_operation(op_id, 'description', 'dummy',
                                            self.user) is True

    def test_delete_operation(self):
        with self.app.test_client():
            self._create_operation(flight_path="f3")
            op_id = get_recent_op_id(self.fm, self.user)
            assert self.fm.delete_file(op_id, self.user2) is False
            assert self.fm.delete_file(op_id, self.user) is True
            assert self.fm.delete_file(op_id, self.user) is False
            permissions = Permission.query.filter_by(op_id=op_id).all()
            assert len(permissions) == 0
            operations_db = Operation.query.filter_by(id=op_id).all()
            assert len(operations_db) == 0
            changes = Change.query.filter_by(op_id=op_id).all()
            assert len(changes) == 0
            messages = Message.query.filter_by(op_id=op_id).all()
            assert len(messages) == 0

    def _example_data(self):
        self.content1 = """\
    <?xml version="1.0" encoding="utf-8"?>
      <FlightTrack>
        <Name>new flight track (1)</Name>
        <ListOfWaypoints>
          <Waypoint flightlevel="0.0" lat="55.15" location="B" lon="-23.74">
            <Comments>Takeoff</Comments>
          </Waypoint>
          <Waypoint flightlevel="350" lat="42.99" location="A" lon="-12.1">
            <Comments></Comments>
          </Waypoint>
          <Waypoint flightlevel="380.0" lat="52.785" location="Shannon" lon="-8.925">
            <Comments>Dive</Comments>
          </Waypoint>
          <Waypoint flightlevel="400.0" lat="48.08" location="EDMO" lon="11.28">
            <Comments></Comments>
          </Waypoint>
          <Waypoint flightlevel="0.0" lat="63.74" location="C" lon="1.73">
            <Comments>Landing</Comments>
          </Waypoint>
        </ListOfWaypoints>
      </FlightTrack>"""
        self.content2 = """\
    <?xml version="1.0" encoding="utf-8"?>
      <FlightTrack>
        <Name>new flight track (1)</Name>
        <ListOfWaypoints>
          <Waypoint flightlevel="0.0" lat="55.15" location="B" lon="-23.74">
            <Comments>Takeoff</Comments>
          </Waypoint>
          <Waypoint flightlevel="350" lat="42.99" location="A" lon="-12.1">
            <Comments></Comments>
          </Waypoint>
          </ListOfWaypoints>
      </FlightTrack>"""

    def _create_operation(self,
                          flight_path="firstflight",
                          user=None,
                          content=None):
        if user is None:
            user = self.user
        self.fm.create_operation(flight_path,
                                 f"info about {flight_path}",
                                 user,
                                 content=content)
        operation = Operation.query.filter_by(path=flight_path).first()
        return flight_path, operation
Ejemplo n.º 9
0
class Test_Files(TestCase):
    render_templates = False

    def create_app(self):
        app = APP
        app.config[
            'SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
        app.config['MSCOLAB_DATA_DIR'] = mscolab_settings.MSCOLAB_DATA_DIR
        app.config['UPLOAD_FOLDER'] = mscolab_settings.UPLOAD_FOLDER
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        app.config["TESTING"] = True
        app.config['LIVESERVER_TIMEOUT'] = 10
        app.config['LIVESERVER_PORT'] = 0
        return app

    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)

        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        self.user = get_user(self.userdata[0])
        assert self.user is not None
        assert add_user('UV20@uv20', 'UV20', 'uv20')
        self.user_2 = get_user('UV20@uv20')

    def tearDown(self):
        pass

    def test_create_operation(self):
        with self.app.test_client():
            flight_path = "f3"
            operation = Operation.query.filter_by(path=flight_path).first()
            assert operation is None
            assert self.fm.create_operation(flight_path, "f3 test example",
                                            self.user)
            operation = Operation.query.filter_by(path=flight_path).first()
            assert operation.id is not None
            assert operation.path == "f3"

    def test_list_operations(self):
        with self.app.test_client():
            operations = ["alpha", "beta", "gamma"]
            for fp in operations:
                assert self.fm.create_operation(fp, f"{fp} test example",
                                                self.user)
            assert len(self.fm.list_operations(self.user)) == 3
            assert len(self.fm.list_operations(self.user_2)) == 0
            fps = self.fm.list_operations(self.user)
            all_operations = [fp['path'] for fp in fps]
            assert operations == all_operations

    def test_get_operation_details(self):
        with self.app.test_client():
            description = "test example"
            flight_path, operation = self._create_operation(
                flight_path="V1", description=description)
            details = self.fm.get_operation_details(operation.id, self.user)
            assert details["description"] == description
            assert details["path"] == flight_path
            assert details["id"] == operation.id

    def test_get_authorized_users(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V1")
            users = self.fm.get_authorized_users(operation.id)
            assert users[0] == {'username': '******', 'access_level': 'creator'}

    def test_fetch_users_without_permission(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V2")
            assert self.fm.fetch_users_without_permission(
                operation.id, self.user_2.id) is False
            without_permission = self.fm.fetch_users_without_permission(
                operation.id, self.user.id)
            # ToDo after seeding removed use absolut comparison
            assert without_permission[-1] == [
                self.user_2.username, self.user_2.id
            ]

    def test_fetch_users_with_permission(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V3")
            assert self.fm.fetch_users_with_permission(operation.id,
                                                       self.user_2.id) is False
            # we look in the query only on others than creator
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == []

    def test_add_bulk_permissions(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V4")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == []
            self.fm.add_bulk_permission(operation.id, self.user,
                                        [self.user_2.id], "viewer")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == [[
                self.user_2.username, 'viewer', self.user_2.id
            ]]

    def test_modify_bulk_permissions(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V5")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == []
            self.fm.add_bulk_permission(operation.id, self.user,
                                        [self.user_2.id], "viewer")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == [[
                self.user_2.username, 'viewer', self.user_2.id
            ]]
            self.fm.modify_bulk_permission(operation.id, self.user,
                                           [self.user_2.id], "collaborator")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == [[
                self.user_2.username, 'collaborator', self.user_2.id
            ]]

    def test_delete_bulk_permissions(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V6")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == []
            self.fm.add_bulk_permission(operation.id, self.user,
                                        [self.user_2.id], "viewer")
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == [[
                self.user_2.username, 'viewer', self.user_2.id
            ]]
            assert self.fm.delete_bulk_permission(operation.id, self.user,
                                                  [self.user_2.id])
            with_permission = self.fm.fetch_users_with_permission(
                operation.id, self.user.id)
            assert with_permission == []

    def test_import_permissions(self):
        with self.app.test_client():
            flight_path, operation_1 = self._create_operation(flight_path="V7")
            with_permission = self.fm.fetch_users_with_permission(
                operation_1.id, self.user.id)
            assert with_permission == []
            self.fm.add_bulk_permission(operation_1.id, self.user,
                                        [self.user_2.id], "viewer")

            flight_path, operation_2 = self._create_operation(flight_path="V8")
            with_permission = self.fm.fetch_users_with_permission(
                operation_2.id, self.user.id)
            assert with_permission == []

            self.fm.import_permissions(operation_1.id, operation_2.id,
                                       self.user.id)
            with_permission = self.fm.fetch_users_with_permission(
                operation_2.id, self.user.id)
            assert with_permission == [[
                self.user_2.username, 'viewer', self.user_2.id
            ]]

    def test_update_operation(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V9")
            new_flight_path = 'NEW_V9'
            assert self.fm.update_operation(operation.id, 'path',
                                            new_flight_path, self.user)
            operation = Operation.query.filter_by(path=new_flight_path).first()
            assert operation.path == new_flight_path
            data = fs.open_fs(self.fm.data_dir)
            assert data.exists(new_flight_path)
            new_description = "my new description"
            assert self.fm.update_operation(operation.id, 'description',
                                            new_description, self.user)
            operation = Operation.query.filter_by(path=new_flight_path).first()
            assert operation.description == new_description

    def test_delete_file(self):
        # ToDo rename to operation
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V10")
            assert operation.path == flight_path
            assert self.fm.delete_file(operation.id, self.user)
            operation = Operation.query.filter_by(path=flight_path).first()
            assert operation is None

    def test_get_all_changes(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V11")
            assert self.fm.save_file(operation.id, "content1", self.user)
            assert self.fm.save_file(operation.id, "content2", self.user)
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            assert len(all_changes) == 2

    def test_get_change_content(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V12",
                                                            content='initial')
            assert self.fm.save_file(operation.id, "content1", self.user)
            assert self.fm.save_file(operation.id, "content2", self.user)
            assert self.fm.save_file(operation.id, "content3", self.user)
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            previous_change = self.fm.get_change_content(all_changes[2]["id"])
            assert previous_change == "content1"
            previous_change = self.fm.get_change_content(all_changes[1]["id"])
            assert previous_change == "content2"

    def test_set_version_name(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="V13",
                                                            content='initial')
            assert self.fm.save_file(operation.id, "content1", self.user)
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            ch_id = all_changes[-1]["id"]
            self.fm.set_version_name(ch_id, operation.id, self.user.id,
                                     "berlin")
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            version_name = all_changes[-1]["version_name"]
            assert version_name == "berlin"

    def _create_operation(self,
                          flight_path="firstflight",
                          description="example",
                          user=None,
                          content=None):
        if user is None:
            user = self.user
        self.fm.create_operation(flight_path,
                                 description,
                                 user,
                                 content=content)
        operation = Operation.query.filter_by(path=flight_path).first()
        return flight_path, operation
Ejemplo n.º 10
0
class Test_FileManager(TestCase):
    render_templates = False

    def create_app(self):
        app = APP
        app.config[
            'SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
        app.config['MSCOLAB_DATA_DIR'] = mscolab_settings.MSCOLAB_DATA_DIR
        app.config['UPLOAD_FOLDER'] = mscolab_settings.UPLOAD_FOLDER
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        app.config["TESTING"] = True
        app.config['LIVESERVER_TIMEOUT'] = 10
        app.config['LIVESERVER_PORT'] = 0
        return app

    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'
        self.anotheruserdata = 'UV20@uv20', 'UV20', 'uv20'
        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        self.user = get_user(self.userdata[0])
        assert self.user is not None
        assert add_user(self.anotheruserdata[0], self.anotheruserdata[1],
                        self.anotheruserdata[2])
        self.anotheruser = get_user(self.anotheruserdata[0])
        assert add_user('UV30@uv30', 'UV30', 'uv30')
        self.vieweruser = get_user('UV30@uv30')
        assert add_user('UV40@uv40', 'UV40', 'uv40')
        self.collaboratoruser = get_user('UV40@uv40')

        self._example_data()

    def tearDown(self):
        pass

    def test_create_operation(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="famous")
            assert operation.path == flight_path
            assert self.fm.create_operation(flight_path, "something to know",
                                            self.user) is False
            flight_path, operation = self._create_operation(
                flight_path="example_flight_path", content=self.content1)
            assert operation.path == flight_path

    def test_get_operation_details(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path='operation2')
            pd = self.fm.get_operation_details(operation.id, self.user)
            assert pd['description'] == operation.description
            assert pd['path'] == operation.path
            assert pd['id'] == operation.id

    def test_list_operations(self):
        with self.app.test_client():
            self.fm.create_operation("first", "info about first", self.user)
            self.fm.create_operation("second", "info about second", self.user)
            expected_result = [{
                'access_level': 'creator',
                'category': 'default',
                'description': 'info about first',
                'op_id': 1,
                'path': 'first'
            }, {
                'access_level': 'creator',
                'category': 'default',
                'description': 'info about second',
                'op_id': 2,
                'path': 'second'
            }]
            assert self.fm.list_operations(self.user) == expected_result

    def test_is_admin(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path='third')
            assert self.fm.is_admin(self.user.id, operation.id)

    def test_is_collaborator(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path='fourth')
            assert self.anotheruser.id is not None
            self.fm.add_bulk_permission(operation.id, self.user,
                                        [self.anotheruser.id], "collaborator")
            assert self.fm.is_collaborator(self.anotheruser.id, operation.id)

    def test_auth_type(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(flight_path="aa")
            assert self.fm.auth_type(self.user.id,
                                     operation.id) != "collaborator"
            assert self.fm.auth_type(self.user.id, operation.id) == "creator"

    def test_update_operation(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path='operation3')
            rename_to = "operation03"
            self.fm.update_operation(operation.id, "path", rename_to,
                                     self.user)
            ren_operation = Operation.query.filter_by(path=rename_to).first()
            assert ren_operation.id == operation.id
            assert ren_operation.path == rename_to

    def test_delete_file(self):
        # Todo rename "file" to operation
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path='operation4')
            assert self.fm.delete_file(operation.id, self.user)
            assert Operation.query.filter_by(path=flight_path).first() is None

    def test_get_authorized_users(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path='operation5')
            assert self.fm.get_authorized_users(operation.id) == [{
                'access_level':
                'creator',
                'username':
                self.userdata[1]
            }]

    def test_save_file(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation6", content=self.content1)
            # nothing changed
            assert self.fm.save_file(operation.id, self.content1,
                                     self.user) is False
            assert self.fm.save_file(operation.id, self.content2, self.user)

    def test_get_file(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation7")
            assert self.fm.get_file(
                operation.id,
                self.user).startswith('<?xml version="1.0" encoding="utf-8"?>')

    def test_get_all_changes(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation8")
            assert self.fm.get_all_changes(operation.id, self.user) == []
            assert self.fm.save_file(operation.id, self.content1, self.user)
            assert self.fm.save_file(operation.id, self.content2, self.user)
            changes = self.fm.get_all_changes(operation.id, self.user)
            assert len(changes) == 2

    def test_get_change_content(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation8")
            assert self.fm.get_all_changes(operation.id, self.user) == []
            assert self.fm.save_file(operation.id, self.content1, self.user)
            assert self.fm.save_file(operation.id, self.content2, self.user)
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            assert self.fm.get_change_content(
                all_changes[1]["id"]) == self.content1

    def test_set_version_name(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation8")
            assert self.fm.get_all_changes(operation.id, self.user) == []
            assert self.fm.save_file(operation.id, self.content1, self.user)
            assert self.fm.save_file(operation.id, self.content2, self.user)
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            assert self.fm.set_version_name(all_changes[1]["id"], operation.id,
                                            self.user.id, "THIS")

    def test_undo(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation8")
            assert self.fm.get_all_changes(operation.id, self.user) == []
            assert self.fm.save_file(operation.id, self.content1, self.user)
            assert self.fm.save_file(operation.id, self.content2, self.user)
            all_changes = self.fm.get_all_changes(operation.id, self.user)
            assert self.fm.undo(all_changes[1]["id"], self.user)

    def test_fetch_users_without_permission(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation9")
            assert len(
                self.fm.fetch_users_without_permission(operation.id,
                                                       self.user.id)) == 3

    def test_fetch_users_with_permission(self):
        with self.app.test_client():
            flight_path, operation = self._create_operation(
                flight_path="operation9")
            assert self.fm.fetch_users_with_permission(operation.id,
                                                       self.user.id) == []

    def test_import_permission(self):
        with self.app.test_client():
            flight_path10, operation10 = self._create_operation(
                flight_path="operation10")
            flight_path11, operation11 = self._create_operation(
                flight_path="operation11")
            flight_path12, operation12 = self._create_operation(
                flight_path="operation12", user=self.anotheruser)
            flight_path13, operation13 = self._create_operation_with_users(
                flight_path="operation13")
            flight_path14, operation14 = self._create_operation_with_users(
                flight_path="operation14")
            flight_path15, operation15 = self._create_operation_with_opposite_permissions(
                flight_path="operation15")
            # equal permissions, nothing to do
            result = (False, None, 'Permissions are already given')
            assert self.fm.import_permissions(operation10.id, operation11.id,
                                              self.user.id) == result
            # no admin rights
            result = (False, None, 'Not an admin of this operation')
            assert self.fm.import_permissions(operation10.id, operation12.id,
                                              self.user.id) == result
            # not a member
            result = (False, None, 'Not a member of this operation')
            assert self.fm.import_permissions(operation12.id, operation10.id,
                                              self.user.id) == result
            # we add to op8 all users of op11
            result = (True, {
                'add_users': [self.vieweruser.id, self.collaboratoruser.id],
                'delete_users': [],
                'modify_users': []
            }, 'success')
            assert self.fm.import_permissions(operation13.id, operation10.id,
                                              self.user.id) == result
            # we remove all users from op8 which are not in op9
            result = (True, {
                'add_users': [],
                'delete_users': [self.vieweruser.id, self.collaboratoruser.id],
                'modify_users': []
            }, 'success')
            assert self.fm.import_permissions(operation11.id, operation10.id,
                                              self.user.id) == result
            # we modify access level
            result = (True, {
                'add_users': [],
                'delete_users': [],
                'modify_users': [self.vieweruser.id, self.collaboratoruser.id]
            }, 'success')
            assert self.fm.import_permissions(operation15.id, operation14.id,
                                              self.user.id) == result

    def _example_data(self):
        self.content1 = """\
<?xml version="1.0" encoding="utf-8"?>
  <FlightTrack>
    <Name>new flight track (1)</Name>
    <ListOfWaypoints>
      <Waypoint flightlevel="0.0" lat="55.15" location="B" lon="-23.74">
        <Comments>Takeoff</Comments>
      </Waypoint>
      <Waypoint flightlevel="350" lat="42.99" location="A" lon="-12.1">
        <Comments></Comments>
      </Waypoint>
      <Waypoint flightlevel="380.0" lat="52.785" location="Shannon" lon="-8.925">
        <Comments>Dive</Comments>
      </Waypoint>
      <Waypoint flightlevel="400.0" lat="48.08" location="EDMO" lon="11.28">
        <Comments></Comments>
      </Waypoint>
      <Waypoint flightlevel="0.0" lat="63.74" location="C" lon="1.73">
        <Comments>Landing</Comments>
      </Waypoint>
    </ListOfWaypoints>
  </FlightTrack>"""
        self.content2 = """\
<?xml version="1.0" encoding="utf-8"?>
  <FlightTrack>
    <Name>new flight track (1)</Name>
    <ListOfWaypoints>
      <Waypoint flightlevel="0.0" lat="55.15" location="B" lon="-23.74">
        <Comments>Takeoff</Comments>
      </Waypoint>
      <Waypoint flightlevel="350" lat="42.99" location="A" lon="-12.1">
        <Comments></Comments>
      </Waypoint>
      </ListOfWaypoints>
  </FlightTrack>"""

    def _create_operation(self,
                          flight_path="firstflight",
                          user=None,
                          content=None):
        if user is None:
            user = self.user
        self.fm.create_operation(flight_path,
                                 f"info about {flight_path}",
                                 user,
                                 content=content)
        operation = Operation.query.filter_by(path=flight_path).first()
        return flight_path, operation

    def _create_operation_with_users(self,
                                     flight_path="firstflight",
                                     user=None,
                                     content=None):
        if user is None:
            user = self.user
        self.fm.create_operation(flight_path,
                                 f"info about {flight_path}",
                                 user,
                                 content=content)
        operation = Operation.query.filter_by(path=flight_path).first()
        self.fm.add_bulk_permission(operation.id, self.user,
                                    [self.vieweruser.id], "viewer")
        self.fm.add_bulk_permission(operation.id, self.user,
                                    [self.collaboratoruser.id], "collaborator")
        return flight_path, operation

    def _create_operation_with_opposite_permissions(self,
                                                    flight_path="firstflight",
                                                    user=None,
                                                    content=None):
        if user is None:
            user = self.user
        self.fm.create_operation(flight_path,
                                 f"info about {flight_path}",
                                 user,
                                 content=content)
        operation = Operation.query.filter_by(path=flight_path).first()
        self.fm.add_bulk_permission(operation.id, self.user,
                                    [self.vieweruser.id], "collaborator")
        self.fm.add_bulk_permission(operation.id, self.user,
                                    [self.collaboratoruser.id], "viewer")
        return flight_path, operation
Ejemplo n.º 11
0
class Test_Seed(TestCase):
    render_templates = False

    def create_app(self):
        app = APP
        app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
        app.config['MSCOLAB_DATA_DIR'] = mscolab_settings.MSCOLAB_DATA_DIR
        app.config['UPLOAD_FOLDER'] = mscolab_settings.UPLOAD_FOLDER
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        app.config["TESTING"] = True
        app.config['LIVESERVER_TIMEOUT'] = 10
        app.config['LIVESERVER_PORT'] = 0
        return app

    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)
        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.operation_name = "XYZ"
        self.description = "Template"
        self.userdata_0 = 'UV0@uv0', 'UV0', 'uv0'
        self.userdata_1 = "UV1@uv1", "UV1", "UV1"
        self.userdata_2 = "UV2@v2", "V2", "v2"

        assert add_user(self.userdata_0[0], self.userdata_0[1], self.userdata_0[2])
        assert add_operation(self.operation_name, self.description)
        assert add_user_to_operation(path=self.operation_name, emailid=self.userdata_0[0])
        self.user = User(self.userdata_0[0], self.userdata_0[1], self.userdata_0[2])

    def tearDown(self):
        pass

    def test_add_operation(self):
        with self.app.test_client():
            assert add_operation("a1", "description")
            operation = Operation.query.filter_by(path="a1").first()
            assert operation.id > 0

    def test_delete_operation(self):
        with self.app.test_client():
            assert add_operation("todelete", "description")
            operation = Operation.query.filter_by(path="todelete").first()
            assert operation.id > 0
            assert delete_operation("todelete")
            operation = Operation.query.filter_by(path="todelete").first()
            assert operation is None

    def test_add_all_users_default_operation_viewer(self):
        with self.app.test_client():
            assert add_user(self.userdata_1[0], self.userdata_1[1], self.userdata_1[2])
            # viewer
            add_all_users_default_operation(path='XYZ', description="Operation to keep all users",
                                            access_level='viewer')
            expected_result = [{'access_level': 'viewer', 'category': 'default',
                                'description': 'Template', 'op_id': 7, 'path': 'XYZ'}]
            user = User.query.filter_by(emailid=self.userdata_1[0]).first()
            assert user is not None
            result = self.fm.list_operations(user)
            # we don't care here for op_id
            expected_result[0]['op_id'] = result[0]['op_id']
            assert result == expected_result

    def test_add_all_users_default_operation_collaborator(self):
        with self.app.test_client():
            # collaborator
            assert add_user(self.userdata_1[0], self.userdata_1[1], self.userdata_1[2])
            add_all_users_default_operation(path='XYZ', description="Operation to keep all users",
                                            access_level='collaborator')
            expected_result = [{'access_level': 'collaborator', 'category': 'default',
                                'description': 'Template', 'op_id': 7, 'path': 'XYZ'}]
            user = User.query.filter_by(emailid=self.userdata_1[0]).first()
            assert user is not None
            result = self.fm.list_operations(user)
            # we don't care here for op_id
            expected_result[0]['op_id'] = result[0]['op_id']
            assert result == expected_result

    def test_add_all_users_default_operation_creator(self):
        with self.app.test_client():
            assert add_user(self.userdata_1[0], self.userdata_1[1], self.userdata_1[2])
            # creator
            add_all_users_default_operation(path='XYZ', description="Operation to keep all users",
                                            access_level='creator')
            expected_result = [{'access_level': 'creator', 'category': 'default',
                                'description': 'Template', 'op_id': 7, 'path': 'XYZ'}]
            user = User.query.filter_by(emailid=self.userdata_1[0]).first()
            result = self.fm.list_operations(user)
            # we don't care here for op_id
            expected_result[0]['op_id'] = result[0]['op_id']
            assert result == expected_result

    def test_add_all_users_default_operation_creator_unknown_operation(self):
        with self.app.test_client():
            assert add_user(self.userdata_1[0], self.userdata_1[1], self.userdata_1[2])
            # creator added to new operation
            add_all_users_default_operation(path='UVXYZ', description="Operation to keep all users",
                                            access_level='creator')
            expected_result = [{'access_level': 'creator', 'category': 'default',
                                'description': 'Operation to keep all users',
                                'op_id': 7, 'path': 'UVXYZ'}]
            user = User.query.filter_by(emailid=self.userdata_1[0]).first()
            result = self.fm.list_operations(user)
            # we don't care here for op_id
            expected_result[0]['op_id'] = result[0]['op_id']
            assert result == expected_result

    def test_add_user(self):
        with self.app.test_client():
            assert add_user(self.userdata_2[0], self.userdata_2[1], self.userdata_2[2])
            assert add_user(self.userdata_2[0], self.userdata_2[1], self.userdata_2[2]) is False

    def test_get_user(self):
        with self.app.test_client():
            assert add_user(self.userdata_2[0], self.userdata_2[1], self.userdata_2[2])
            user = get_user(self.userdata_2[0])
            assert user.id is not None
            assert user.emailid == self.userdata_2[0]

    def test_add_user_to_operation(self):
        with self.app.test_client():
            assert add_user(self.userdata_2[0], self.userdata_2[1], self.userdata_2[2])
            assert add_operation("operation2", "description")
            assert add_user_to_operation(path="operation2", access_level='admin', emailid=self.userdata_2[0])

    def test_delete_user(self,):
        with self.app.test_client():
            assert add_user(self.userdata_2[0], self.userdata_2[1], self.userdata_2[2])
            user = User.query.filter_by(emailid=self.userdata_2[0]).first()
            assert user is not None
            assert delete_user(self.userdata_2[0])
            user = User.query.filter_by(emailid=self.userdata_2[0]).first()
            assert user is None