o
    =ý¤h'  ã                   @   s~   d Z ddlZddlm  mZ ddlZddlZddl	m
Z
 dd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )zê
test_setup_verification.py

Purpose:
  Simple tests to verify the testing infrastructure is working correctly.
  These tests validate that fixtures are properly configured and the test
  environment is isolated from production data.
é    N)ÚFlaskc                 C   s  t | tƒ}|sJddt ¡ v st t ¡rt t ¡nddt ¡ v s$t | ¡r)t | ¡nddt ¡ v s5t t¡r:t t¡ndt |¡dœ }tt |¡ƒ‚d}| j	d }d}||u }|s|t 
d	|fd
||f¡t |¡t |¡dœ }dd|i }tt |¡ƒ‚d } }}dS )z3Test that the app fixture creates a Flask instance.z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}Ú
isinstanceÚappr   )Úpy0Úpy1Úpy2Úpy4NÚTESTINGT©Úis©z%(py1)s is %(py4)s©r   r   úassert %(py6)sÚpy6)r   r   Ú@py_builtinsÚlocalsÚ
@pytest_arÚ_should_repr_global_nameÚ	_safereprÚAssertionErrorÚ_format_explanationÚconfigÚ_call_reprcompare)r   Ú@py_assert3Ú@py_format5Ú@py_assert0Ú@py_assert2Ú@py_format7© r   ú:/var/www/html/tests/integration/test_setup_verification.pyÚ'test_app_fixture_creates_flask_instance   s   ˜pr    c                 C   s†   |   d¡}d}||u}|s=t d|fd||f¡dt ¡ v s"t |¡r't |¡ndt |¡dœ }dd|i }tt |¡ƒ‚d }}dS )	z4Test that the client fixture provides a test client.ú/N©úis not©z%(py0)s is not %(py3)sÚresponse©r   Úpy3úassert %(py5)sÚpy5)	Úgetr   r   r   r   r   r   r   r   )Úclientr%   r   Ú@py_assert1Ú@py_format4Ú@py_format6r   r   r   Ú(test_client_fixture_provides_test_client   s   
|r/   c           	      C   sÎ   |  d¡}dd„ | ¡ D ƒ}g d¢}|D ]P}||v }|sbt d|fd||f¡dt ¡ v s1t |¡r6t |¡nddt ¡ v sBt |¡rGt |¡ndd	œ }t d
|› d¡d d|i }t	t 
|¡ƒ‚d}qdS )zTTest that the database fixture provides a working database with all required tables.z1SELECT name FROM sqlite_master WHERE type='table'c                 S   s   g | ]}|d  ‘qS )r   r   )Ú.0Úrowr   r   r   Ú
<listcomp>    s    z=test_database_fixture_has_required_tables.<locals>.<listcomp>)ÚusersÚcamerasÚmarkersÚlogin_attemptsÚlogin_ip_attemptsÚauth_logÚadmin_audit©Úin)z%(py0)s in %(py2)sÚtableÚtables)r   r   zRequired table 'z' not found in databasez
>assert %(py4)sr   N)ÚexecuteÚfetchallr   r   r   r   r   r   Ú_format_assertmsgr   r   )	r   ÚdbÚcursorr=   Úrequired_tablesr<   r,   Ú@py_format3r   r   r   r   Ú)test_database_fixture_has_required_tables   s   
žÿrE   c                 C   s¬  d}| |u}|s8t  d|fd| |f¡dt ¡ v st  | ¡r"t  | ¡ndt  |¡dœ }dd|i }tt  |¡ƒ‚d }}tj	}|j
}|| ƒ}|s‚dd	t ¡ v sTt  t¡rYt  t¡nd	t  |¡t  |¡dt ¡ v smt  | ¡rrt  | ¡ndt  |¡d
œ }tt  |¡ƒ‚d } }}tj	}|j}|| ƒ}|sÎdd	t ¡ v s t  t¡r¥t  t¡nd	t  |¡t  |¡dt ¡ v s¹t  | ¡r¾t  | ¡ndt  |¡d
œ }tt  |¡ƒ‚d } }}dS )zATest that the temp_static fixture provides a temporary directory.Nr"   r$   Útemp_staticr&   r(   r)   zbassert %(py7)s
{%(py7)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.path
}.exists
}(%(py5)s)
}Úos)r   r   r   r)   Úpy7zaassert %(py7)s
{%(py7)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.path
}.isdir
}(%(py5)s)
})r   r   r   r   r   r   r   r   rG   ÚpathÚexistsÚisdir)rF   r   r,   r-   r.   r   Ú@py_assert6Ú@py_format8r   r   r   Ú+test_temp_static_fixture_provides_directory)   s   x˜œrN   c           	      C   s
  d}||v }|s8t  d|fd||f¡t  |¡dt ¡ v s!t  |¡r&t  |¡nddœ }dd|i }tt  |¡ƒ‚d }}d	}||v }|stt  d|fd||f¡t  |¡dt ¡ v s]t  |¡rbt  |¡nddœ }dd|i }tt  |¡ƒ‚d }}d
}||v }|s°t  d|fd||f¡t  |¡dt ¡ v s™t  |¡ržt  |¡nddœ }dd|i }tt  |¡ƒ‚d }}|d d }d}||k}|såt  d|fd||f¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d } }}|d	 d }d}||k}|st  d|fd||f¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d } }}|d
 d }d}||k}|sUt  d|fd||f¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d } }}|d d }d}||u }|st  d|fd||f¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d } }}|d	 d }d}||u }|sÅt  d|fd||f¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d } }}|d
 d }d}||u }|sýt  d|fd||f¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d } }}dS )z?Test that the test_users fixture creates users in the database.Úregularr:   ©z%(py1)s in %(py3)sÚ
test_users©r   r'   r(   r)   NÚadminÚlimitedÚusernameÚtestuser©ú==©z%(py1)s == %(py4)sr   r   r   Ú	testadminÚlimiteduserÚis_adminFr
   r   T)r   r   r   r   r   r   r   r   )	r   rQ   r   r   r-   r.   r   r   r   r   r   r   Ú%test_test_users_fixture_creates_users0   s   xxxnpppptr]   c                 C   s¨  d}|| v }|s8t  d|fd|| f¡t  |¡dt ¡ v s!t  | ¡r&t  | ¡nddœ }dd|i }tt  |¡ƒ‚d }}d	}| d }||v }|skt  d|fd
||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}d}| d }||v }|s t  d|fd
||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}| d d	 }
t|
ƒ}d}||k}|sðt  d|fd||f¡dt ¡ v sÍt  t¡rÒt  t¡ndt  |
¡t  |¡t  |¡dœ }dd|i }tt  |¡ƒ‚d }
 } }}d}||v }|s3t  d|fd||f¡t  |¡dt ¡ v st  |¡r!t  |¡nddœ }dd|i }tt  |¡ƒ‚d }}d}|d }||v }|sgt  d|fd
||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}d}|d }||v }|st  d|fd
||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}|d d }d}||k}|sÕt  d|fd||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}d}||v }|st  d|fd||f¡t  |¡dt ¡ v sÿt  |¡rt  |¡nddœ }dd|i }tt  |¡ƒ‚d }}d}|d }||v }|sJt  d|fd
||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}d}|d }||v }|s€t  d|fd
||f¡t  |¡t  |¡dœ }dd|i }	tt  |	¡ƒ‚d } }}|d d }
t	|
t
ƒ}|sÎd d!t ¡ v s¢t  t	¡r§t  t	¡nd!t  |
¡d"t ¡ v s¹t  t
¡r¾t  t
¡nd"t  |¡d#œ }tt  |¡ƒ‚d }
}dS )$z@Test that sample data fixtures provide expected data structures.Úvalidr:   rP   Úsample_camera_datarR   r(   r)   NÚ	camera_id)z%(py1)s in %(py4)sr   r   r   Úcamera_nameé   rW   )z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sÚlen)r   r   r   rH   zassert %(py9)sÚpy9ÚjpgÚsample_image_dataÚfilenameÚcontent_typez
image/jpegrY   Úcamera_markerÚsample_marker_dataÚlatitudeÚ	longitudez5assert %(py5)s
{%(py5)s = %(py0)s(%(py2)s, %(py3)s)
}r   Úfloat)r   r   r'   r)   )r   r   r   r   r   r   r   r   rc   r   rm   )r_   rf   rj   r   r   r-   r.   r   r   r   r,   rL   Ú@py_assert5rM   Ú@py_format10Ú@py_assert4r   r   r   Ú+test_sample_data_fixtures_provide_test_data@   s   xjj¤~llp~llœrq   c                 C   sf  |   ¡ ¥ ddl}tj d¡}| |¡}|j|_| dd¡ | 	¡  | dd¡}| 
¡ }d}||u}|sdt d|fd	||f¡d
t ¡ v sIt |¡rNt |¡nd
t |¡dœ }dd|i }	tt |	¡ƒ‚d }}|d }
d}|
|k}|s—t d|fd|
|f¡t |
¡t |¡dœ }dd|i }tt |¡ƒ‚d }
 }}| ¡  W d  ƒ dS 1 s¬w   Y  dS )z*Test that each test gets a fresh database.r   NÚDATABASE_PATHzFINSERT INTO users (username, password_hash, is_admin) VALUES (?, ?, ?))Úisolation_testÚhashFz-SELECT username FROM users WHERE username = ?)rs   r"   r$   Úresultr&   r(   r)   rs   rW   rY   r   r   r   )Úapp_contextÚsqlite3rG   Úenvironr*   ÚconnectÚRowÚrow_factoryr>   ÚcommitÚfetchoner   r   r   r   r   r   r   r   Úclose)r   rw   Údb_pathrA   rB   ru   r   r,   r-   r.   r   r   r   r   r   r   r   Ú%test_database_isolation_between_testsU   s   

ÿxj
"îr€   c                 C   sô   d}| |u}|s8t  d|fd| |f¡dt ¡ v st  | ¡r"t  | ¡ndt  |¡dœ }dd|i }tt  |¡ƒ‚d }}d}||u}|stt  d|fd||f¡dt ¡ v sYt  |¡r^t  |¡ndt  |¡dœ }dd|i }tt  |¡ƒ‚d }}dS )	z<Test that authenticated client fixtures work without errors.Nr"   r$   Úauthenticated_client_regularr&   r(   r)   Úauthenticated_client_admin)r   r   r   r   r   r   r   r   )r   r‚   r   r,   r-   r.   r   r   r   Útest_authenticated_clients_workl   s   x|rƒ   )Ú__doc__Úbuiltinsr   Ú_pytest.assertion.rewriteÚ	assertionÚrewriter   rG   ÚpytestÚflaskr   r    r/   rE   rN   r]   rq   r€   rƒ   r   r   r   r   Ú<module>   s    "	