Example #1
0
def test_nsm_stop(
        default_native_session_manager, monkeypatch, mock_session, session_key):
    """
    unit tested:  stop

    test case:
    basic method exercise, calling methods and completing
    """
    nsm = default_native_session_manager

    session_tuple = collections.namedtuple(
        'session_tuple', ['identifiers', 'session_id'])
    mysession = session_tuple('identifiers', 'sessionkey123')
    monkeypatch.setattr(mock_session, 'get_internal_attribute',
                        lambda x: 'identifiers')

    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)

    with mock.patch.object(nsm.session_handler, 'on_stop') as on_stop:
        on_stop.return_value = None
        with mock.patch.object(nsm, 'notify_event') as notify_stop:
            notify_stop.return_value = None
            with mock.patch.object(nsm.session_handler, 'after_stopped') as after_stopped:
                after_stopped.return_value = None

                nsm.stop(SessionKey('sessionkey123'), 'identifiers')

                mock_session.stop.assert_called_with()
                on_stop.assert_called_with(mock_session, SessionKey('sessionkey123'))
                notify_stop.assert_called_with(mysession, 'SESSION.STOP')
                after_stopped.assert_called_with(mock_session)
Example #2
0
def test_dsk_eq(first, second, boolcheck):
    """
    unit tested:  __eq__

    test case:
    equality based on session_id
    """
    dsk1 = SessionKey(session_id=first)
    dsk2 = SessionKey(session_id=second)
    assert (dsk1 == dsk2) == boolcheck
Example #3
0
def test_sh_on_expiration_allset(session_handler, monkeypatch, mock_session):
    """
    unit tested:  on_expiration

    test case:
        all parameters are passed, calling on_change, notify_expiration, and
        after_expired
    """
    sh = session_handler

    session_tuple = collections.namedtuple(
        'session_tuple', ['identifiers', 'session_id'])
    mysession = session_tuple('identifiers', 'sessionkey123')
    mock_session.get_internal_attribute.return_value = 'identifiers'
    mock_session.session_id = 'session123'

    with mock.patch.object(sh, 'notify_event') as sh_ne:
        sh_ne.return_value = None
        with mock.patch.object(sh, 'after_expired') as sh_ae:
            sh_ae.return_value = None
            with mock.patch.object(sh, 'on_change') as sh_oc:
                sh_oc.return_value = None

                sh.on_expiration(session=mock_session,
                                 expired_session_exception='ExpiredSessionException',
                                 session_key=SessionKey('sessionkey123'))

                sh_ne.assert_called_once_with(mysession, 'SESSION.EXPIRE')
                sh_ae.assert_called_once_with(mock_session)
                sh_oc.assert_called_once_with(mock_session)
def test_session_manager_stop(session_manager, cache_handler, session_context,
                              session_handler, capsys, event_bus):
    """
    test objective:

    aspects tested:
        - stop
        - _lookup_required_session
    """
    sh = session_handler
    sm = session_manager
    sm.cache_handler = cache_handler
    sm.event_bus = event_bus

    event_detected = None

    def event_listener(items=None):
        nonlocal event_detected
        event_detected = items

    event_bus.subscribe(event_listener, 'SESSION.STOP')

    session = sm.start(session_context)  # a DelegatingSession
    sessionid = session.session_id

    sm.stop(session.session_key, 'random')

    with pytest.raises(ValueError):
        sh.do_get_session(SessionKey(sessionid))

        out, err = capsys.readouterr()
        assert ('Coult not find session' in out
                and isinstance(event_detected.results, namedtuple))
def test_sh_idle_expired_session(session_handler, cache_handler, session,
                                 event_bus, monkeypatch):
    """
    test objective:  validate idle timeout expiration handling / communication

    idle test:  idle set to 5 minutes, last accessed set to 6 minutes ago
    """
    sh = session_handler
    sh.auto_touch = False
    sh.cache_handler = cache_handler

    event_detected = None

    def event_listener(items=None):
        nonlocal event_detected
        event_detected = items

    event_bus.subscribe(event_listener, 'SESSION.EXPIRE')

    session.set_internal_attribute('SubjectContext.IDENTIFIERS_SESSION_KEY',
                                   'user12345678')
    sessionid = sh.create_session(session)
    cachedsession = sh.do_get_session(SessionKey(sessionid))

    idle_timeout = (5 * 60 * 1000)
    absolute_timeout = (30 * 60 * 1000)
    start_timestamp = round(time.time() * 1000) - (10 * 60 * 1000)
    last_access_time = round(time.time() * 1000) - (6 * 60 * 1000)

    monkeypatch.setattr(cachedsession, 'last_access_time', last_access_time)
    monkeypatch.setattr(cachedsession, 'start_timestamp', start_timestamp)
    monkeypatch.setattr(cachedsession, 'idle_timeout', idle_timeout)
    monkeypatch.setattr(cachedsession, 'absolute_timeout', absolute_timeout)

    sh.on_change(cachedsession)

    with pytest.raises(ExpiredSessionException):
        sh.do_get_session(SessionKey(sessionid))

        assert event_detected.items.identifiers
Example #6
0
def test_sh_retrieve_session_withoutsessionid(
        session_handler, monkeypatch, session_store):
    """
    unit tested:  retrieve_session

    test case:
    fails to obtain a session_id value from the sessionkey, returning None
    """
    sh = session_handler
    session_key = SessionKey(None)

    result = sh._retrieve_session(session_key)
    assert result is None
Example #7
0
def test_sh_on_invalidation_isetype(session_handler, mock_session, monkeypatch):
    """
    unit tested:  on_invalidation

    test case:
        when an exception NOT of type ExpiredSessionException is passed,
        an InvalidSessionException higher up the hierarchy is assumed
        and on_stop, notify_stop, and after_stopped are called
    """
    sh = session_handler
    ise = StoppedSessionException('testing')
    session_key = SessionKey('sessionkey123')

    session_tuple = collections.namedtuple(
        'session_tuple', ['identifiers', 'session_key'])
    mysession = session_tuple('identifiers', 'sessionkey123')

    monkeypatch.setattr(mock_session, 'get_internal_attribute',
                        lambda x: 'identifiers')

    with mock.patch.object(sh, 'on_stop') as mock_onstop:
        mock_onstop.return_value = None

        with mock.patch.object(sh, 'notify_event') as mock_ns:
            mock_ns.return_value = None

            with mock.patch.object(sh, 'after_stopped') as mock_as:
                mock_as.return_value = None

                sh.on_invalidation(session=mock_session,
                                   ise=ise,
                                   session_key=session_key)

                mock_onstop.assert_called_once_with(mock_session, SessionKey('sessionkey123'))
                mock_ns.assert_called_once_with(mysession, 'SESSION.STOP')
                mock_as.assert_called_once_with(mock_session)
Example #8
0
def test_nsm_get_session_key_w_sessionid(native_security_manager, monkeypatch,
                                         mock_subject_context):
    """
    unit tested:  get_session_key

    test case:

    """
    nsm = native_security_manager
    msc = mock_subject_context

    monkeypatch.setattr(msc, 'session_id', 'sessionid123', raising=False)

    result = nsm.get_session_key(msc)

    assert result == SessionKey('sessionid123')
def test_session_handler_delete(session_handler, cache_handler, session,
                                capsys):
    """
    test objective:  delete a session from cache storage
    """
    sh = session_handler
    sh.cache_handler = cache_handler

    session.set_internal_attribute('SubjectContext.IDENTIFIERS_SESSION_KEY',
                                   'user12345678')

    sessionid = sh.create_session(session)
    sh.delete(session)

    with pytest.raises(ValueError):
        sh.do_get_session(SessionKey(sessionid))

        out, err = capsys.readouterr()
        assert 'Coult not find session' in out
def test_session_handler_create_dgs(session_handler, cache_handler, session):
    """
    test objective:  create a new session and read it from the cachestore

    session_handler aspects tested:
        - apply_cache_handler_to_session_store
        - create_session
        - do_get_session
        - _retrieve_session
        - validate
        - on_change
    """
    sh = session_handler
    sh.cache_handler = cache_handler

    session.set_internal_attribute('SubjectContext.IDENTIFIERS_SESSION_KEY',
                                   'user12345678')
    sessionid = sh.create_session(session)
    cachedsession = sh.do_get_session(SessionKey(sessionid))

    assert cachedsession == session
def test_sh_stopped_session(session_handler, cache_handler, session,
                            monkeypatch, event_bus):
    """
    test objective:  validate stopped session handling

    session_handler aspects tested:
        - create_session
        - do_get_session
        - on_change
        - validate
        - on_invalidation
        - before_invalid_notification
        - after_stopped
    """
    ch = cache_handler
    sh = session_handler
    sh.cache_handler = cache_handler

    event_detected = None

    def event_listener(items=None):
        nonlocal event_detected
        event_detected = items

    event_bus.subscribe(event_listener, 'SESSION.STOP')

    session.set_internal_attribute('SubjectContext.IDENTIFIERS_SESSION_KEY',
                                   'user12345678')

    sessionid = sh.create_session(session)
    cachedsession = ch.get(domain='session', identifier=sessionid)

    now = round(time.time() * 1000)
    cachedsession.stop_timestamp = now
    ch.set(domain='session', identifier=sessionid, value=cachedsession)

    with pytest.raises(InvalidSessionException):
        sh.do_get_session(SessionKey(sessionid))

        assert event_detected.identifiers
Example #12
0
def session_key(session):
    return SessionKey(session.session_id)
Example #13
0
 def get_session_key(self, subject_context):
     session_id = subject_context.session_id
     if (session_id is not None):
         return SessionKey(session_id)
     return None
Example #14
0
def session_key():
    return SessionKey('sessionid123')