@@ -123,125 +123,124 @@ def test_oauth_access_token(self):
123123 cur .execute ("SELECT authentication_method FROM sessions WHERE session_id=(SELECT current_session())" )
124124 res = cur .fetchone ()
125125 self .assertEqual (res [0 ], 'OAuth' )
126- # -------------------------------
127- # TOTP Authentication Test for Vertica-Python Driver
128- # -------------------------------
129- import os
130- import pyotp
131- import pytest
132- import vertica_python
133- from io import StringIO
134- import sys
135-
136-
137- @pytest .fixture
138- # Positive TOTP Test (Like SHA512 format)
139- def test_TOTP (self ):
140- with self ._connect () as conn :
141- cur = conn .cursor ()
142-
143- cur .execute ("DROP USER IF EXISTS totp_user" )
144- cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
145-
146- try :
147- # Create user with MFA
148- cur .execute ("CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
149-
150- # Grant authentication
151- # Note: METHOD is 'trusted' or 'password' depending on how MFA is enforced in Vertica
152- cur .execute ("CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
153- cur .execute ("GRANT AUTHENTICATION totp_auth TO totp_user" )
154-
155- # Generate TOTP
156- import pyotp
157- TOTP_SECRET = "O5D7DQICJTM34AZROWHSAO4O53ELRJN3"
158- totp_code = pyotp .TOTP (TOTP_SECRET ).now ()
159-
160- # Set connection info
161- self ._conn_info ['user' ] = 'totp_user'
162- self ._conn_info ['password' ] = 'password'
163- self ._conn_info ['totp' ] = totp_code
164-
165- # Try connection
166- with self ._connect () as totp_conn :
167- c = totp_conn .cursor ()
168- c .execute ("SELECT 1" )
169- res = c .fetchone ()
170- self .assertEqual (res [0 ], 1 )
171-
172- finally :
126+ # -------------------------------
127+ # TOTP Authentication Test for Vertica-Python Driver
128+ # -------------------------------
129+ import os
130+ import pyotp
131+ import pytest
132+ import vertica_python
133+ from io import StringIO
134+ import sys
135+
136+
137+ # Positive TOTP Test (Like SHA512 format)
138+ def totp_positive_scenario (self ):
139+ with self ._connect () as conn :
140+ cur = conn .cursor ()
141+
173142 cur .execute ("DROP USER IF EXISTS totp_user" )
174143 cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
175144
176- # Negative Test: Missing TOTP
177- def test_TOTP_missing_code (self ):
178- with self ._connect () as conn :
179- cur = conn .cursor ()
145+ try :
146+ # Create user with MFA
147+ cur .execute ("CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
180148
181- cur .execute ("DROP USER IF EXISTS totp_user" )
182- cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
149+ # Grant authentication
150+ # Note: METHOD is 'trusted' or 'password' depending on how MFA is enforced in Vertica
151+ cur .execute ("CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
152+ cur .execute ("GRANT AUTHENTICATION totp_auth TO totp_user" )
153+
154+ # Generate TOTP
155+ import pyotp
156+ TOTP_SECRET = "O5D7DQICJTM34AZROWHSAO4O53ELRJN3"
157+ totp_code = pyotp .TOTP (TOTP_SECRET ).now ()
158+
159+ # Set connection info
160+ self ._conn_info ['user' ] = 'totp_user'
161+ self ._conn_info ['password' ] = 'password'
162+ self ._conn_info ['totp' ] = totp_code
183163
184- try :
185- cur .execute ("CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
186- cur .execute ("CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
187- cur .execute ("GRANT AUTHENTICATION totp_auth TO totp_user" )
164+ # Try connection
165+ with self ._connect () as totp_conn :
166+ c = totp_conn .cursor ()
167+ c .execute ("SELECT 1" )
168+ res = c .fetchone ()
169+ self .assertEqual (res [0 ], 1 )
188170
189- self . _conn_info [ 'user' ] = 'totp_user'
190- self . _conn_info [ 'password' ] = 'password'
191- self . _conn_info . pop ( 'totp' , None ) # No TOTP
171+ finally :
172+ cur . execute ( "DROP USER IF EXISTS totp_user" )
173+ cur . execute ( "DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
192174
193- err_msg = "TOTP was requested but not provided"
194- self .assertConnectionFail (err_msg = err_msg )
175+ # Negative Test: Missing TOTP
176+ def totp_missing_code_scenario (self ):
177+ with self ._connect () as conn :
178+ cur = conn .cursor ()
195179
196- finally :
197180 cur .execute ("DROP USER IF EXISTS totp_user" )
198181 cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
199182
200- # Negative Test: Invalid TOTP Format
201- def test_TOTP_invalid_format ( self ):
202- with self . _connect () as conn :
203- cur = conn . cursor ( )
183+ try :
184+ cur . execute ( "CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
185+ cur . execute ( "CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
186+ cur . execute ( "GRANT AUTHENTICATION totp_auth TO totp_user" )
204187
205- cur .execute ("DROP USER IF EXISTS totp_user" )
206- cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
188+ self ._conn_info ['user' ] = 'totp_user'
189+ self ._conn_info ['password' ] = 'password'
190+ self ._conn_info .pop ('totp' , None ) # No TOTP
207191
208- try :
209- cur .execute ("CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
210- cur .execute ("CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
211- cur .execute ("GRANT AUTHENTICATION totp_auth TO totp_user" )
192+ err_msg = "TOTP was requested but not provided"
193+ self .assertConnectionFail (err_msg = err_msg )
212194
213- self . _conn_info [ 'user' ] = 'totp_user'
214- self . _conn_info [ 'password' ] = 'password'
215- self . _conn_info [ 'totp' ] = "123" # Invalid
195+ finally :
196+ cur . execute ( "DROP USER IF EXISTS totp_user" )
197+ cur . execute ( "DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
216198
217- err_msg = "Invalid TOTP format"
218- self .assertConnectionFail (err_msg = err_msg )
199+ # Negative Test: Invalid TOTP Format
200+ def totp_invalid_format_scenario (self ):
201+ with self ._connect () as conn :
202+ cur = conn .cursor ()
219203
220- finally :
221204 cur .execute ("DROP USER IF EXISTS totp_user" )
222205 cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
223206
224- # Negative Test: Wrong TOTP (Valid format, wrong value)
225- def test_TOTP_wrong_code ( self ):
226- with self . _connect () as conn :
227- cur = conn . cursor ( )
207+ try :
208+ cur . execute ( "CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
209+ cur . execute ( "CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
210+ cur . execute ( "GRANT AUTHENTICATION totp_auth TO totp_user" )
228211
229- cur .execute ("DROP USER IF EXISTS totp_user" )
230- cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
212+ self ._conn_info ['user' ] = 'totp_user'
213+ self ._conn_info ['password' ] = 'password'
214+ self ._conn_info ['totp' ] = "123" # Invalid
231215
232- try :
233- cur .execute ("CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
234- cur .execute ("CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
235- cur .execute ("GRANT AUTHENTICATION totp_auth TO totp_user" )
216+ err_msg = "Invalid TOTP format"
217+ self .assertConnectionFail (err_msg = err_msg )
236218
237- self . _conn_info [ 'user' ] = 'totp_user'
238- self . _conn_info [ 'password' ] = 'password'
239- self . _conn_info [ 'totp' ] = "999999" # Wrong OTP
219+ finally :
220+ cur . execute ( "DROP USER IF EXISTS totp_user" )
221+ cur . execute ( "DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
240222
241- err_msg = "Invalid TOTP"
242- self .assertConnectionFail (err_msg = err_msg )
223+ # Negative Test: Wrong TOTP (Valid format, wrong value)
224+ def totp_wrong_code_scenario (self ):
225+ with self ._connect () as conn :
226+ cur = conn .cursor ()
243227
244- finally :
245228 cur .execute ("DROP USER IF EXISTS totp_user" )
246229 cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
247230
231+ try :
232+ cur .execute ("CREATE USER totp_user IDENTIFIED BY 'password' ENFORCEMFA" )
233+ cur .execute ("CREATE AUTHENTICATION totp_auth METHOD 'password' HOST '0.0.0.0/0'" )
234+ cur .execute ("GRANT AUTHENTICATION totp_auth TO totp_user" )
235+
236+ self ._conn_info ['user' ] = 'totp_user'
237+ self ._conn_info ['password' ] = 'password'
238+ self ._conn_info ['totp' ] = "999999" # Wrong OTP
239+
240+ err_msg = "Invalid TOTP"
241+ self .assertConnectionFail (err_msg = err_msg )
242+
243+ finally :
244+ cur .execute ("DROP USER IF EXISTS totp_user" )
245+ cur .execute ("DROP AUTHENTICATION IF EXISTS totp_auth CASCADE" )
246+
0 commit comments