@@ -63,6 +63,15 @@ def set_data(package):
6363 'Done :o'
6464 )
6565
66+ @self .app .route ('/priv_test' )
67+ def test_privileges ():
68+ # type: () -> typing.Any
69+ return flask .render_template_string (
70+ "{{ session.has_privilege('m_edit') }},"
71+ "{{ session.has_privilege('m_delete') }},"
72+ "{{ session.is_authenticated }}"
73+ )
74+
6675 self .ctx = self .app .app_context ()
6776 self .ctx .push ()
6877 # Inject connection
@@ -102,6 +111,38 @@ def test_unknown_user(self):
102111 self .assertEqual (unknown_user , None )
103112
104113
114+ class TestFetch (TestWithDatabase ):
115+ def test_paging (self ):
116+ # type: () -> None
117+ _create_privilege (self .cursor , 'm_edit' )
118+ _create_privilege (self .cursor , 'm_delete' )
119+ _create_privilege (self .cursor , 'm_some_random' )
120+
121+ expected_privileges = [(0 , [{
122+ 'auth_option' : 'm_edit' ,
123+ 'auth_option_id' : 1 ,
124+ 'founder_only' : 0 ,
125+ 'is_global' : 1 ,
126+ 'is_local' : 0 ,
127+ }]), (1 , [{
128+ 'auth_option' : 'm_delete' ,
129+ 'auth_option_id' : 2 ,
130+ 'founder_only' : 0 ,
131+ 'is_global' : 1 ,
132+ 'is_local' : 0 ,
133+ }]), (2 , [{
134+ 'auth_option' : 'm_some_random' ,
135+ 'auth_option_id' : 3 ,
136+ 'founder_only' : 0 ,
137+ 'is_global' : 1 ,
138+ 'is_local' : 0 ,
139+ }]), (3 , [])]
140+
141+ for skip in range (0 , 4 ):
142+ privilege = self .app .phpbb3 .fetch_acl_options (skip = skip , limit = 1 )
143+ self .assertEqual ((skip , privilege ), expected_privileges [skip ])
144+
145+
105146class TestSession (TestWithDatabase ):
106147 def setUp (self ):
107148 # type: () -> None
@@ -152,6 +193,22 @@ def test_storage(self):
152193 data = self .client .get ('/data' ).data
153194 self .assertEqual (data , 'something' )
154195
196+ def test_privilege (self ):
197+ # type: () -> None
198+ _create_user (self .cursor )
199+ _create_session (self .cursor , self .session_id , 2 )
200+ _create_privilege (self .cursor , 'm_edit' )
201+ _grant_privilege (self .cursor , 2 )
202+
203+ data = self .client .get ('/priv_test' ).data
204+ self .assertEqual (data , 'False,False,False' )
205+
206+ # We do a login via phpbb3 :P
207+ self .client .set_cookie ('127.0.0.1' , 'phpbb3_sid' , self .session_id )
208+
209+ data = self .client .get ('/priv_test' ).data
210+ self .assertEqual (data , 'True,False,True' )
211+
155212
156213def _create_user (cursor ):
157214 # type: (psycopg2.extensions.cursor) -> None
@@ -174,6 +231,32 @@ def _create_session(cursor, session_id, user_id):
174231 )
175232
176233
234+ def _create_privilege (cursor , privilege ):
235+ # type: (psycopg2.extensions.cursor, str) -> None
236+ cursor .execute (
237+ "insert into"
238+ " phpbb_acl_options (auth_option, is_global)"
239+ " values (%(privilege)s, 1)" , {
240+ 'privilege' : privilege ,
241+ }
242+ )
243+
244+
245+ def _grant_privilege (cursor , user_id ):
246+ # type: (psycopg2.extensions.cursor, int) -> None
247+ # Cryptic value to allow only m_edit permission
248+ permission_set = 'HRA0HS'
249+ cursor .execute (
250+ "update phpbb_users"
251+ " set"
252+ " user_permissions=%(permission_set)s"
253+ " where user_id=%(user_id)s" , {
254+ 'user_id' : user_id ,
255+ 'permission_set' : permission_set ,
256+ }
257+ )
258+
259+
177260def _create_db ():
178261 # type: () -> psycopg2.extensions.connection
179262 connection = _get_connection (DB_HOST , DB_ROOT_USER , DB_ROOT_USER )
0 commit comments