File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,13 @@ def invalidate(self):
68
68
self ._changed = True
69
69
self ._mapping = {}
70
70
71
+ def set_new_identity (self , identity ):
72
+ if not self ._new :
73
+ raise RuntimeError (
74
+ "Can't change identity for a session which is not new" )
75
+
76
+ self ._identity = identity
77
+
71
78
def __len__ (self ):
72
79
return len (self ._mapping )
73
80
Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ Session
90
90
.. attribute :: identity
91
91
92
92
Client's identity. It may be cookie name or database
93
- key. Read-only property.
93
+ key. Read-only property. For change use :func: ` Session.set_new_identity `.
94
94
95
95
.. attribute :: new
96
96
@@ -130,6 +130,13 @@ Session
130
130
Call this when you want to invalidate the session (dump all
131
131
data, and -- perhaps -- set a clearing cookie).
132
132
133
+ .. method :: set_new_identity(identity)
134
+
135
+ Call this when you want to change the :py:attr: `identity `.
136
+
137
+ .. warning ::
138
+
139
+ Never change :py:attr: `identity ` of a session which is not new.
133
140
134
141
.. _aiohttp-session-storage :
135
142
Original file line number Diff line number Diff line change
1
+ import pytest
1
2
import time
2
3
3
4
from aiohttp_session import Session
@@ -31,6 +32,22 @@ def test_create3():
31
32
assert s .created is not None
32
33
33
34
35
+ def test_set_new_identity_ok ():
36
+ s = Session (identity = 1 , data = None , new = True )
37
+ assert s .new
38
+ assert s .identity == 1
39
+
40
+ s .set_new_identity (2 )
41
+ assert s .new
42
+ assert s .identity == 2
43
+
44
+
45
+ def test_set_new_identity_for_not_new_session ():
46
+ s = Session (identity = 1 , data = None , new = False )
47
+ with pytest .raises (RuntimeError ):
48
+ s .set_new_identity (2 )
49
+
50
+
34
51
def test__repr__ ():
35
52
s = Session ('test_identity' , data = None , new = True )
36
53
assert str (s ) == \
You can’t perform that action at this time.
0 commit comments