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(DefaultSessionKey('sessionkey123'), 'identifiers')

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

    test case:
    equality based on session_id
    """
    dsk1 = DefaultSessionKey(session_id=first)
    dsk2 = DefaultSessionKey(session_id=second)
    assert (dsk1 == dsk2) == boolcheck
def test_session_manager_stop(session_manager, cache_handler, session_context,
                              session_handler, capsys):
    """
    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.register(event_listener, 'SESSION.STOP')

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

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

    with pytest.raises(UnknownSessionException):
        sh.do_get_session(DefaultSessionKey(sessionid))

        out, err = capsys.readouterr()
        assert ('Coult not find session' in out
                and isinstance(event_detected.results, namedtuple))
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=DefaultSessionKey('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)
Exemple #5
0
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(
        'DefaultSubjectContext.IDENTIFIERS_SESSION_KEY', 'user12345678')
    sessionid = sh.create_session(session)
    cachedsession = sh.do_get_session(DefaultSessionKey(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(DefaultSessionKey(sessionid))

        assert event_detected.items.identifiers
def test_sh_expired_session(session_handler, cache_handler, session,
                            myminutes):
    """
    test objective:  validate idle and absolute timeout expiration handling

    session_handler aspects tested:
        - create_session
        - do_get_session
        - on_change
        - validate
        - on_expiration
        - before_invalid_notification
    """
    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.register(event_listener, 'SESSION.EXPIRE')

    session.set_internal_attribute(
        'DefaultSubjectContext.IDENTIFIERS_SESSION_KEY', 'user12345678')
    sessionid = sh.create_session(session)
    cachedsession = sh.do_get_session(DefaultSessionKey(sessionid))

    now = datetime.datetime.now(pytz.utc)
    minutes_ago = datetime.timedelta(minutes=myminutes)
    cachedsession.last_access_time = now - minutes_ago

    sh.on_change(cachedsession)

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

        assert event_detected.items.identifiers
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 = DefaultSessionKey(None)

    result = sh._retrieve_session(session_key)
    assert result is None
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 = DefaultSessionKey('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, DefaultSessionKey('sessionkey123'))
                mock_ns.assert_called_once_with(mysession, 'SESSION.STOP')
                mock_as.assert_called_once_with(mock_session)
Exemple #9
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 == DefaultSessionKey('sessionid123')
Exemple #10
0
def test_csd_cache_identifiers_to_key_map_w_idents(
        caching_session_store, mock_cache_handler, mock_session, monkeypatch,
        simple_identifier_collection):
    sic = simple_identifier_collection
    csd = caching_session_store
    monkeypatch.setattr(csd, 'cache_handler', mock_cache_handler)
    monkeypatch.setattr(mock_session, 'get_internal_attribute', lambda x: sic)

    with mock.patch.object(mock_cache_handler, 'set') as mock_set:
        mock_set.return_value = None

        csd._cache_identifiers_to_key_map(mock_session, 'sessionid123')

        mock_set.assert_called_once_with(
            domain='session',
            identifier=sic.primary_identifier,
            value=DefaultSessionKey('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(
        'DefaultSubjectContext.IDENTIFIERS_SESSION_KEY', 'user12345678')

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

    with pytest.raises(UnknownSessionException):
        sh.do_get_session(DefaultSessionKey(sessionid))

        out, err = capsys.readouterr()
        assert 'Coult not find session' in out
def test_sh_stopped_session(session_handler, cache_handler, session,
                            monkeypatch):
    """
    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
    monkeypatch.setattr(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.register(event_listener, 'SESSION.STOP')

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

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

    now = datetime.datetime.now(pytz.utc)
    cachedsession.stop_timestamp = now
    ch.set(domain='session', identifier=sessionid, value=cachedsession)

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

        assert event_detected.identifiers
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(
        'DefaultSubjectContext.IDENTIFIERS_SESSION_KEY', 'user12345678')
    sessionid = sh.create_session(session)
    cachedsession = sh.do_get_session(DefaultSessionKey(sessionid))

    assert cachedsession == session
def test_create_cache_session(session_store, session, cache_handler):
    """
    test objective:  cache new session entry and read session from cache

    aspects tested:
        - session.set_internal_attribute
        - session_store.create
        - session_store.read
        - session_store.delete
        - cache_handler.get
        - session.__eq__
    """
    css = session_store
    sic = SimpleIdentifierCollection(source_name='AccountStoreRealm',
                                     identifier='user12345678')
    session.set_internal_attribute('identifiers_session_key', sic)
    sessionid = css.create(session)

    cached_session = css.read(sessionid)
    cached_session_token = css.cache_handler.get('session', 'user12345678')
    assert (cached_session == session
            and cached_session_token == DefaultSessionKey(sessionid))
Exemple #15
0
 def get_session_key(self, subject_context):
     session_id = subject_context.session_id
     if (session_id is not None):
         return DefaultSessionKey(session_id)
     return None
Exemple #16
0
def session_key():
    return DefaultSessionKey('sessionid123')
Exemple #17
0
def session_key(session):
    return DefaultSessionKey(session.session_id)