예제 #1
0
def session_fixture():
    session = UbusSession(TIMEOUT)
    session["test1"] = 1
    session["test2"] = "2"
    session["test3"] = True
    session.save()
    yield session.session_id
    session.destroy()
예제 #2
0
 def __init__(self, env_key, timeout, session_id):
     super(SessionForisProxy, self).__init__(env_key, timeout)
     self._session = UbusSession(self.timeout, session_id)
     self.tainted = False
     if self.is_anonymous:
         self._session.filtered_keys = list(
             SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
     self.ws_session = None
     logger.debug("session '%s' loaded" % self.session_id)
예제 #3
0
def test_obtain_session(session_fixture):
    session = UbusSession(TIMEOUT, session_fixture)
    assert session.expires_in == TIMEOUT
    assert len(session) == 3
    assert sorted([e for e in session]) == ["test1", "test2", "test3"]
    assert session["test1"] == 1
    assert session["test2"] == "2"
    assert session["test3"]
    assert session.get("test3", False)
예제 #4
0
def test_obtain_session(session_fixture):
    session = UbusSession(TIMEOUT, session_fixture)
    assert session.expires_in == TIMEOUT
    assert len(session) == 3
    assert sorted([e for e in session]) == ['test1', 'test2', 'test3']
    assert session['test1'] == 1
    assert session['test2'] == "2"
    assert session['test3']
    assert session.get('test3', False)
예제 #5
0
    def recreate(self):
        if not self.is_anonymous:
            self.unload()
            self.ws_session.unload()
            self.destroy()

        self._session = UbusSession(self.timeout)
        logger.debug("session '%s' created" % self.session_id)
        self._session.filtered_keys = []
        self.load()
예제 #6
0
def test_destroy(session_fixture):
    session = UbusSession(TIMEOUT, session_fixture)
    session.destroy()

    with pytest.raises(SessionDestroyed):
        session.save()

    with pytest.raises(SessionDestroyed):
        session.destroy()

    with pytest.raises(SessionDestroyed):
        session['test1']

    with pytest.raises(SessionDestroyed):
        session['test2'] = 5

    with pytest.raises(SessionDestroyed):
        del session['test3']

    with pytest.raises(SessionDestroyed):
        [e for e in session]

    with pytest.raises(SessionDestroyed):
        'test1' in session

    with pytest.raises(SessionDestroyed):
        len(session)

    with pytest.raises(SessionNotFound):
        UbusSession(TIMEOUT, session_fixture)
예제 #7
0
    def load_anonymous(self):
        if self.is_anonymous:
            return  # already loaded

        self.destroy()
        self.unload()
        self._session = UbusSession(self.timeout,
                                    session_id=UbusSession.ANONYMOUS)
        self._session.filtered_keys = list(
            SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
        self.ws_session = None
예제 #8
0
    def __init__(self, env_key, timeout, session_id=None):
        super(SessionWsProxy, self).__init__(env_key, timeout)
        self._session = UbusSession(self.timeout, session_id)

        if session_id is None:
            # grant listen for the new session
            self.grant_listen()
            # set the cookie
            self.set_cookie()

        logger.debug("ws session '%s' loaded" % self.session_id)
예제 #9
0
파일: sessions.py 프로젝트: CZ-NIC/foris
class SessionWsProxy(SessionProxy):
    def __init__(self, env_key, timeout, session_id=None):
        super(SessionWsProxy, self).__init__(env_key, timeout)
        self._session = UbusSession(self.timeout, session_id)

        if session_id is None:
            # grant listen for the new session
            self.grant_listen()
            # set the cookie
            self.set_cookie()

        logger.debug("ws session '%s' loaded" % self.session_id)

    def grant_listen(self):
        self._session.grant("websocket-listen", "listen-allowed")
예제 #10
0
class SessionWsProxy(SessionProxy):
    def __init__(self, env_key, timeout, session_id=None):
        super(SessionWsProxy, self).__init__(env_key, timeout)
        self._session = UbusSession(self.timeout, session_id)

        if session_id is None:
            # grant listen for the new session
            self.grant_listen()
            # set the cookie
            self.set_cookie()

        logger.debug("ws session '%s' loaded" % self.session_id)

    def grant_listen(self):
        self._session.grant("websocket-listen", "listen-allowed")
예제 #11
0
파일: sessions.py 프로젝트: CZ-NIC/foris
 def __init__(self, env_key, timeout, session_id):
     super(SessionForisProxy, self).__init__(env_key, timeout)
     self._session = UbusSession(self.timeout, session_id)
     self.tainted = False
     if self.is_anonymous:
         self._session.filtered_keys = list(SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
     self.ws_session = None
     logger.debug("session '%s' loaded" % self.session_id)
예제 #12
0
def test_destroy(session_fixture):
    session = UbusSession(TIMEOUT, session_fixture)
    session.destroy()

    with pytest.raises(SessionDestroyed):
        session.save()

    with pytest.raises(SessionDestroyed):
        session.destroy()

    with pytest.raises(SessionDestroyed):
        session["test1"]

    with pytest.raises(SessionDestroyed):
        session["test2"] = 5

    with pytest.raises(SessionDestroyed):
        del session["test3"]

    with pytest.raises(SessionDestroyed):
        [e for e in session]

    with pytest.raises(SessionDestroyed):
        "test1" in session

    with pytest.raises(SessionDestroyed):
        len(session)

    with pytest.raises(SessionNotFound):
        UbusSession(TIMEOUT, session_fixture)
예제 #13
0
파일: sessions.py 프로젝트: CZ-NIC/foris
    def load_anonymous(self):
        if self.is_anonymous:
            return  # already loaded

        self.destroy()
        self.unload()
        self._session = UbusSession(self.timeout, session_id=UbusSession.ANONYMOUS)
        self._session.filtered_keys = list(SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
        self.ws_session = None
예제 #14
0
파일: sessions.py 프로젝트: CZ-NIC/foris
    def recreate(self):
        if not self.is_anonymous:
            self.unload()
            self.ws_session.unload()
            self.destroy()

        self._session = UbusSession(self.timeout)
        logger.debug("session '%s' created" % self.session_id)
        self._session.filtered_keys = []
        self.load()
예제 #15
0
파일: sessions.py 프로젝트: CZ-NIC/foris
    def __init__(self, env_key, timeout, session_id=None):
        super(SessionWsProxy, self).__init__(env_key, timeout)
        self._session = UbusSession(self.timeout, session_id)

        if session_id is None:
            # grant listen for the new session
            self.grant_listen()
            # set the cookie
            self.set_cookie()

        logger.debug("ws session '%s' loaded" % self.session_id)
예제 #16
0
def session_fixture():
    session = UbusSession(TIMEOUT)
    session['test1'] = 1
    session['test2'] = "2"
    session['test3'] = True
    session.save()
    yield session.session_id
    session.destroy()
예제 #17
0
def test_incorrect_data(session_fixture):
    session = UbusSession(TIMEOUT, session_fixture)

    # wrong key
    with pytest.raises(TypeError):
        session[6] = {}
    with pytest.raises(TypeError):
        session[None] = {}
    with pytest.raises(TypeError):
        session[False] = {}
    with pytest.raises(UnicodeDecodeError):
        session["ř"] = {}

    # wrong data
    with pytest.raises(TypeError):
        session["key"] = NonSerializable
    with pytest.raises(TypeError):
        session["key"] = [NonSerializable]
    with pytest.raises(TypeError):
        session["key"] = {"key": NonSerializable}
    session["key"] = {u"ř": 6}

    session.save()
예제 #18
0
def test_incorrect_data(session_fixture):
    session = UbusSession(TIMEOUT, session_fixture)

    # wrong key
    with pytest.raises(TypeError):
        session[6] = {}
    with pytest.raises(TypeError):
        session[None] = {}
    with pytest.raises(TypeError):
        session[False] = {}
    with pytest.raises(UnicodeDecodeError):
        session["ř"] = {}

    # wrong data
    with pytest.raises(TypeError):
        session["key"] = NonSerializable
    with pytest.raises(TypeError):
        session["key"] = [NonSerializable]
    with pytest.raises(TypeError):
        session["key"] = {"key": NonSerializable}
    session["key"] = {u"ř": 6}

    session.save()
예제 #19
0
def test_readonly():
    session = UbusSession(TIMEOUT, UbusSession.ANONYMOUS)

    assert session.session_id == UbusSession.ANONYMOUS
    assert session.expires_in == 0
    assert session.readonly is False
    session.readonly = True

    len(session)
    [e for e in session]
    assert session["key"] is None
    assert "qwerty" not in session

    with pytest.raises(SessionReadOnly):
        session.save()

    with pytest.raises(SessionReadOnly):
        session.destroy()

    with pytest.raises(SessionReadOnly):
        session["key"] = "value"

    with pytest.raises(SessionReadOnly):
        del session["key"]
예제 #20
0
def test_readonly():
    session = UbusSession(TIMEOUT, UbusSession.ANONYMOUS)

    assert session.session_id == UbusSession.ANONYMOUS
    assert session.expires_in == 0
    assert session.readonly is False
    session.readonly = True

    len(session)
    [e for e in session]
    assert session["key"] is None
    assert "qwerty" not in session

    with pytest.raises(SessionReadOnly):
        session.save()

    with pytest.raises(SessionReadOnly):
        session.destroy()

    with pytest.raises(SessionReadOnly):
        session["key"] = "value"

    with pytest.raises(SessionReadOnly):
        del session["key"]
예제 #21
0
def test_create_session():
    session = UbusSession(TIMEOUT)
    assert session.expires_in == TIMEOUT
    assert len(session) == 0
    assert [e for e in session] == []
    session.destroy()
예제 #22
0
def test_save():
    session = UbusSession(TIMEOUT)
    session["test1"] = 1
    session["test2"] = 2
    session["test3"] = 3
    session.save()

    session = UbusSession(TIMEOUT, session_id=session.session_id)
    assert len(session) == 3
    assert sorted([e for e in session]) == ["test1", "test2", "test3"]
    assert [session[s] for s in sorted([e for e in session])] == [1, 2, 3]
    assert session["test1"] == 1
    assert session["test2"] == 2
    assert session["test3"] == 3
    assert "test1" in session
    assert "test2" in session
    assert "test3" in session

    del session["test2"]
    session["test1"] = 0
    session["test4"] = 4
    session["test5"] = 5
    session.save()

    session = UbusSession(TIMEOUT, session_id=session.session_id)
    assert len(session) == 4
    assert sorted([e for e in session]) == ["test1", "test3", "test4", "test5"]
    assert [session[s] for s in sorted([e for e in session])] == [0, 3, 4, 5]
    assert session["test1"] == 0
    assert session["test2"] is None
    assert session["test3"] == 3
    assert session["test4"] == 4
    assert session["test5"] == 5
    assert session.get("test2") is None
    assert session.get("test2", True)
    assert session.get("test5", False) == 5

    assert "test1" in session
    assert "test2" not in session
    assert "test3" in session
    assert "test4" in session
    assert "test5" in session

    session.destroy()
예제 #23
0
파일: sessions.py 프로젝트: CZ-NIC/foris
class SessionForisProxy(SessionProxy):
    DONT_STORE_IN_ANONYMOUS = ["user_authenticated"]

    def __init__(self, env_key, timeout, session_id):
        super(SessionForisProxy, self).__init__(env_key, timeout)
        self._session = UbusSession(self.timeout, session_id)
        self.tainted = False
        if self.is_anonymous:
            self._session.filtered_keys = list(SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
        self.ws_session = None
        logger.debug("session '%s' loaded" % self.session_id)

    def __len__(self):
        return self._session.__len__()

    def __setitem__(self, key, value):
        self.tainted = True
        return self._session.__setitem__(key, value)

    def __contains__(self, key):
        return self._session.__contains__(key)

    def __iter__(self):
        return self._session.__iter__()

    def __delitem__(self, key):
        self.tainted = True
        return self._session.__delitem__(key)

    def __getitem__(self, key):
        return self._session.__getitem__(key)

    def get(self, *args, **kwargs):
        return self._session.get(*args, **kwargs)

    def save(self):
        self._session.save()
        self.tainted = False
        logger.debug("session '%s' stored" % self.session_id)

    def destroy(self):
        self._session.destroy()
        self.tainted = False
        logger.debug("session '%s' destroyed" % self.session_id)
        if self.ws_session:
            self.ws_session.destroy()

    def recreate(self):
        if not self.is_anonymous:
            self.unload()
            self.ws_session.unload()
            self.destroy()

        self._session = UbusSession(self.timeout)
        logger.debug("session '%s' created" % self.session_id)
        self._session.filtered_keys = []
        self.load()

    def load_anonymous(self):
        if self.is_anonymous:
            return  # already loaded

        self.destroy()
        self.unload()
        self._session = UbusSession(self.timeout, session_id=UbusSession.ANONYMOUS)
        self._session.filtered_keys = list(SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
        self.ws_session = None
예제 #24
0
class SessionForisProxy(SessionProxy):
    DONT_STORE_IN_ANONYMOUS = [
        "user_authenticated",
    ]

    def __init__(self, env_key, timeout, session_id):
        super(SessionForisProxy, self).__init__(env_key, timeout)
        self._session = UbusSession(self.timeout, session_id)
        self.tainted = False
        if self.is_anonymous:
            self._session.filtered_keys = list(
                SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
        self.ws_session = None
        logger.debug("session '%s' loaded" % self.session_id)

    def __len__(self):
        return self._session.__len__()

    def __setitem__(self, key, value):
        self.tainted = True
        return self._session.__setitem__(key, value)

    def __contains__(self, key):
        return self._session.__contains__(key)

    def __iter__(self):
        return self._session.__iter__()

    def __delitem__(self, key):
        self.tainted = True
        return self._session.__delitem__(key)

    def __getitem__(self, key):
        return self._session.__getitem__(key)

    def get(self, *args, **kwargs):
        return self._session.get(*args, **kwargs)

    def save(self):
        self._session.save()
        self.tainted = False
        logger.debug("session '%s' stored" % self.session_id)

    def destroy(self):
        self._session.destroy()
        self.tainted = False
        logger.debug("session '%s' destroyed" % self.session_id)
        if self.ws_session:
            self.ws_session.destroy()

    def recreate(self):
        if not self.is_anonymous:
            self.unload()
            self.ws_session.unload()
            self.destroy()

        self._session = UbusSession(self.timeout)
        logger.debug("session '%s' created" % self.session_id)
        self._session.filtered_keys = []
        self.load()

    def load_anonymous(self):
        if self.is_anonymous:
            return  # already loaded

        self.destroy()
        self.unload()
        self._session = UbusSession(self.timeout,
                                    session_id=UbusSession.ANONYMOUS)
        self._session.filtered_keys = list(
            SessionForisProxy.DONT_STORE_IN_ANONYMOUS)
        self.ws_session = None
예제 #25
0
def test_save():
    session = UbusSession(TIMEOUT)
    session['test1'] = 1
    session['test2'] = 2
    session['test3'] = 3
    session.save()

    session = UbusSession(TIMEOUT, session_id=session.session_id)
    assert len(session) == 3
    assert sorted([e for e in session]) == ['test1', 'test2', 'test3']
    assert [session[s] for s in sorted([e for e in session])] == [1, 2, 3]
    assert session['test1'] == 1
    assert session['test2'] == 2
    assert session['test3'] == 3
    assert 'test1' in session
    assert 'test2' in session
    assert 'test3' in session

    del session['test2']
    session['test1'] = 0
    session['test4'] = 4
    session['test5'] = 5
    session.save()

    session = UbusSession(TIMEOUT, session_id=session.session_id)
    assert len(session) == 4
    assert sorted([e for e in session]) == ['test1', 'test3', 'test4', 'test5']
    assert [session[s] for s in sorted([e for e in session])] == [0, 3, 4, 5]
    assert session['test1'] == 0
    assert session['test2'] is None
    assert session['test3'] == 3
    assert session['test4'] == 4
    assert session['test5'] == 5
    assert session.get('test2') is None
    assert session.get('test2', True)
    assert session.get('test5', False) == 5

    assert 'test1' in session
    assert 'test2' not in session
    assert 'test3' in session
    assert 'test4' in session
    assert 'test5' in session

    session.destroy()
예제 #26
0
def test_create_session():
    session = UbusSession(TIMEOUT)
    assert session.expires_in == TIMEOUT
    assert len(session) == 0
    assert [e for e in session] == []
    session.destroy()