-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_smsshell_model_session.py
More file actions
93 lines (76 loc) · 2.52 KB
/
Copy pathtest_smsshell_model_session.py
File metadata and controls
93 lines (76 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf8 -*-
import datetime
import pytest
import time
import SMSShell
import SMSShell.models.session
from SMSShell.models.session import SessionStates, BadStateTransitionException
def test_session():
"""Test Session attributes
"""
s = SMSShell.models.session.Session('sender', 20)
assert s.subject == 'sender'
assert s.state == SessionStates.STATE_GUEST
assert s.ttl == 20
assert 'sender' in str(s)
assert 'sender' in repr(s)
assert s.access_at <= datetime.datetime.today()
def test_session_good_state_transition():
"""Perform a good state transition
"""
s = SMSShell.models.session.Session('sender')
s.state = SessionStates.STATE_LOGININPROGRESS
def test_session_bad_state_transition():
"""Performe a bad state transition
"""
s = SMSShell.models.session.Session('sender')
s.state = SessionStates.STATE_USER
with pytest.raises(BadStateTransitionException):
s.state = SessionStates.STATE_LOGININPROGRESS
with pytest.raises(BadStateTransitionException):
s.state = 1
with pytest.raises(BadStateTransitionException):
s.forceState(1)
def test_session_ttl():
"""Test ttl validation
"""
s = SMSShell.models.session.Session('sender', 1)
assert s.isValid()
time.sleep(1.1)
assert not s.isValid()
def test_session_storage_access():
"""Test session storage get/set
"""
s = SMSShell.models.session.Session('sender')
s.set('key', 'sample')
assert s.get('key') == 'sample'
def test_session_storage_isolation():
"""Test session storage for isolation
"""
s = SMSShell.models.session.Session('sender')
s.setStoragePrefix('user1')
s.set('key', 'sample1')
s.setStoragePrefix('user2')
s.set('key', 'sample2')
s.setStoragePrefix('')
assert s.get('key') is None
s.setStoragePrefix('user1')
assert s.get('key') == 'sample1'
s.setStoragePrefix('user2')
assert s.get('key') == 'sample2'
def test_session_secure_wrapper():
"""Test to use secure session wrapper
"""
s = SMSShell.models.session.Session('sender')
sw = s.getSecureSession()
sw.set('key', 'sample')
assert sw.get('key') == 'sample'
def test_session_secure_wrapper_isolation():
"""Test secure session for critical attributes
"""
s = SMSShell.models.session.Session('sender')
sw = s.getSecureSession()
with pytest.raises(AttributeError):
sw.setStoragePrefix('user1')
with pytest.raises(AttributeError):
sw.forceState(SessionStates.STATE_LOGININPROGRESS)