def test_new_session_not_changed(): """Assert that a session should not be saved on creation without modifications. """ session = Session({'foo': 'bar'}) assert not session.should_save()
def test_session_pop_flash_queues(): session = Session({}) session.flash('msg1', queue='queue1') session.flash('msg2', queue='queue2') assert session.pop_flash('queue1') == ['msg1'] assert session.pop_flash('queue2') == ['msg2']
def test_session_get_csrf_token_after_new_csrf_token(monkeypatch): monkeypatch.setattr(crypto, 'random_token', pretend.call_recorder(lambda: '12345678')) session = Session({}) csrf_token = session.new_csrf_token() assert csrf_token == session.get_csrf_token() assert csrf_token == '12345678'
def test_session_changed(func, args, changed, expected): """Assert that if a session is changed that it should be saved and that the modifications occur correctly. """ session = Session({'foo': 'bar'}) getattr(session, func)(*args) assert session.should_save() == changed assert session == expected
def test_session_invalidated_with_no_sid(monkeypatch): monkeypatch.setattr(crypto, 'random_token', pretend.call_recorder(func=lambda: '1')) session = Session() session.invalidate() assert session.invalidated == set([]) assert session.sid == '1' assert crypto.random_token.calls == [pretend.call()]
def test_session_invalidated(monkeypatch): sids = iter(['1', '2']) monkeypatch.setattr(crypto, 'random_token', pretend.call_recorder(func=lambda: next(sids))) session = Session({'foo': 'bar'}) assert session.sid == '1' session.invalidate() assert session.invalidated == {'1'} assert not session.changed() assert session.new assert session == {} assert session.sid == '2' assert crypto.random_token.calls == [pretend.call(), pretend.call()]
def test_session_peek_flash(): session = Session({}) session.flash('msg1') session.flash('msg2', queue='queue') assert session.peek_flash() == ['msg1'] assert session.peek_flash() == ['msg1'] assert session.peek_flash('queue') == ['msg2'] assert session.peek_flash('queue') == ['msg2']
def test_session_id_generated_once(monkeypatch): monkeypatch.setattr(crypto, 'random_token', pretend.call_recorder(func=lambda: '12345678')) session = Session({}) assert session.sid == '12345678' assert session.sid == '12345678' assert crypto.random_token.calls == [pretend.call()]
def test_create_new_timestamp(monkeypatch): """Assert that session.created is the UTC timestamp that it was created on. """ monkeypatch.setattr(time, 'time', lambda: 100) session = Session({}) assert session.created == 100
def test_create_new_session_id(monkeypatch, data, expected): """Assert that when a new session is created that it's properties are correctly set. """ monkeypatch.setattr(crypto, 'random_token', lambda: '12345678') session = Session(data) assert session == expected assert session.sid == '12345678' assert session.new assert not session.invalidated
def test_session_flash_empty_queue(): session = Session({}) assert session.pop_flash() == [] assert session.peek_flash() == [] assert session.pop_flash('notfound') == [] assert session.peek_flash('notfound') == []
def test_session_renew_csrf_token(monkeypatch): csrf_tokens = iter(['1', '2']) monkeypatch.setattr(crypto, 'random_token', pretend.call_recorder(lambda: next(csrf_tokens))) session = Session({}) csrf1 = session.new_csrf_token() assert csrf1 == session.get_csrf_token() csrf2 = session.new_csrf_token() assert csrf2 == session.get_csrf_token() assert csrf1 != csrf2
def test_session_flash_no_allow_duplicates(queue): session = Session({}) session.flash('message', queue=queue, allow_duplicate=False) session.flash('message', queue=queue, allow_duplicate=False) assert session.pop_flash(queue=queue) == ['message']
def test_session_pop_flash_no_queue(): session = Session({}) session.flash('message1') session.flash('message2') assert session.pop_flash() == ['message1', 'message2']
def test_session_flash_queue(): session = Session({}) session.flash('message', queue='queue') assert session == {session._get_flash_queue_key('queue'): ['message']} assert session.should_save()