<s> """<STR_LIT>""" <EOL> import os <EOL> import unittest <EOL> import tempfile <EOL> import psiturk <EOL> import json <EOL> from faker import Faker <EOL> fake = Faker ( ) <EOL> class FlaskTestClientProxy ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> def __call__ ( self , environ , start_response ) : <EOL> environ [ '<STR_LIT>' ] = environ . get ( '<STR_LIT>' , fake . ipv4 ( ) ) <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . app ( environ , start_response ) <EOL> class BadFlaskTestClientProxy ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> def __call__ ( self , environ , start_response ) : <EOL> environ [ '<STR_LIT>' ] = environ . get ( '<STR_LIT>' , fake . ipv4 ( ) ) <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . app ( environ , start_response ) <EOL> class PsiturkUnitTest ( unittest . TestCase ) : <EOL> def setUp ( self , case = None ) : <EOL> '''<STR_LIT>''' <EOL> os . chdir ( '<STR_LIT>' ) <EOL> import psiturk . db <EOL> import psiturk . experiment <EOL> reload ( psiturk . experiment ) <EOL> fd , db_path = tempfile . mkstemp ( ) <EOL> self . db_fd = fd <EOL> psiturk . experiment . app . config [ '<STR_LIT>' ] = db_path <EOL> psiturk . experiment . app . wsgi_app = FlaskTestClientProxy ( <EOL> psiturk . experiment . app . wsgi_app ) <EOL> self . app = psiturk . experiment . app . test_client ( ) <EOL> psiturk . db . init_db ( ) <EOL> self . worker_id = fake . md5 ( raw_output = False ) <EOL> self . hit_id = fake . md5 ( raw_output = False ) <EOL> self . assignment_id = fake . md5 ( raw_output = False ) <EOL> def tearDown ( self ) : <EOL> '''<STR_LIT>''' <EOL> os . close ( self . db_fd ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> os . unlink ( psiturk . experiment . app . config [ '<STR_LIT>' ] ) <EOL> self . app = None <EOL> class PsiTurkStandardTests ( PsiturkUnitTest ) : <EOL> def test_default_page ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . app . get ( '<STR_LIT:/>' ) <EOL> assert '<STR_LIT>' in rv . data <EOL> def test_exp_debug_no_url_vars ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . app . get ( '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in rv . data <EOL> def test_ad_no_url_vars ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . app . get ( '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in rv . data <EOL> def test_ad_with_all_urls ( self ) : <EOL> '''<STR_LIT>''' <EOL> args = '<STR_LIT:&>' . join ( [ <EOL> '<STR_LIT>' % self . assignment_id , <EOL> '<STR_LIT>' % self . worker_id , <EOL> '<STR_LIT>' % self . hit_id , <EOL> '<STR_LIT>' ] ) <EOL> rv = self . app . get ( '<STR_LIT>' % args ) <EOL> assert '<STR_LIT>' in rv . data <EOL> def test_exp_with_all_url_vars_not_registered_on_ad_server ( self ) : <EOL> '''<STR_LIT>''' <EOL> args = '<STR_LIT:&>' . join ( [ <EOL> '<STR_LIT>' % self . assignment_id , <EOL> '<STR_LIT>' % self . worker_id , <EOL> '<STR_LIT>' % self . hit_id , <EOL> '<STR_LIT>' ] ) <EOL> rv = self . app . get ( '<STR_LIT>' % args ) <EOL> assert '<STR_LIT>' in rv . data <EOL> def test_sync_put ( self ) : <EOL> request = "<STR_LIT:&>" . join ( [ <EOL> "<STR_LIT>" % self . assignment_id , <EOL> "<STR_LIT>" % self . worker_id , <EOL> "<STR_LIT>" % self . hit_id , <EOL> "<STR_LIT>" ] ) <EOL> rv = self . app . get ( "<STR_LIT>" % request ) <EOL> uniqueid = "<STR_LIT>" % ( self . worker_id , self . assignment_id ) <EOL> rv = self . app . put ( '<STR_LIT>' % uniqueid ) <EOL> status = json . loads ( rv . data ) . get ( "<STR_LIT:status>" , "<STR_LIT>" ) <EOL> assert status == "<STR_LIT>" <EOL> def test_sync_get ( self ) : <EOL> request = "<STR_LIT:&>" . join ( [ <EOL> "<STR_LIT>" % self . assignment_id , <EOL> "<STR_LIT>" % self . worker_id , <EOL> "<STR_LIT>" % self . hit_id , <EOL> "<STR_LIT>" ] ) <EOL> rv = self . app . get ( "<STR_LIT>" % request ) <EOL> uniqueid = "<STR_LIT>" % ( self . worker_id , self . assignment_id ) <EOL> rv = self . app . put ( '<STR_LIT>' % uniqueid ) <EOL> uniqueid = "<STR_LIT>" % ( self . worker_id , self . assignment_id ) <EOL> rv = self . app . get ( '<STR_LIT>' % uniqueid ) <EOL> response = json . loads ( rv . data ) <EOL> assert response . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" % self . assignment_id <EOL> assert response . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" % self . worker_id <EOL> assert response . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" % self . hit_id <EOL> assert response . get ( "<STR_LIT>" , None ) == <NUM_LIT:0> <EOL> assert response . get ( "<STR_LIT>" , None ) == <NUM_LIT:0> <EOL> assert response . get ( "<STR_LIT>" , None ) == <NUM_LIT:0.0> <EOL> def test_favicon ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . app . get ( '<STR_LIT>' ) <EOL> assert rv . status_code == <NUM_LIT:200> <EOL> class BadUserAgent ( PsiturkUnitTest ) : <EOL> '''<STR_LIT>''' <EOL> def setUp ( self ) : <EOL> '''<STR_LIT>''' <EOL> os . chdir ( '<STR_LIT>' ) <EOL> import psiturk . db <EOL> import psiturk . experiment <EOL> reload ( psiturk . experiment ) <EOL> fd , db_path = tempfile . mkstemp ( ) <EOL> self . db_fd = fd <EOL> psiturk . experiment . app . config [ '<STR_LIT>' ] = db_path <EOL> psiturk . experiment . app . wsgi_app = BadFlaskTestClientProxy ( <EOL> psiturk . experiment . app . wsgi_app ) <EOL> self . app = psiturk . experiment . app . test_client ( ) <EOL> psiturk . db . init_db ( ) <EOL> self . worker_id = fake . md5 ( raw_output = False ) <EOL> self . hit_id = fake . md5 ( raw_output = False ) <EOL> self . assignment_id = fake . md5 ( raw_output = False ) <EOL> def test_ad_with_bad_user_agent ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . app . get ( <EOL> '<STR_LIT>' + '<STR_LIT>' + self . assignment_id + '<STR_LIT>' + <EOL> self . worker_id + '<STR_LIT>' + self . hit_id + '<STR_LIT>' <EOL> ) <EOL> assert '<STR_LIT>' in rv . data <EOL> class PsiTurkTestPsiturkJS ( PsiturkUnitTest ) : <EOL> '''<STR_LIT>''' <EOL> def setUp ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . PSITURK_JS_FILE = '<STR_LIT>' <EOL> os . chdir ( '<STR_LIT>' ) <EOL> os . rename ( self . PSITURK_JS_FILE , self . PSITURK_JS_FILE + '<STR_LIT>' ) <EOL> import psiturk . db <EOL> import psiturk . experiment <EOL> reload ( psiturk . experiment ) <EOL> fd , db_path = tempfile . mkstemp ( ) <EOL> self . db_fd = fd <EOL> psiturk . experiment . app . config [ '<STR_LIT>' ] = db_path <EOL> psiturk . experiment . app . wsgi_app = FlaskTestClientProxy ( <EOL> psiturk . experiment . app . wsgi_app ) <EOL> self . app = psiturk . experiment . app . test_client ( ) <EOL> psiturk . db . init_db ( ) <EOL> def test_psiturk_js_is_missing ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . app . get ( '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in rv . data <EOL> def tearDown ( self ) : <EOL> '''<STR_LIT>''' <EOL> super ( PsiTurkTestPsiturkJS , self ) . tearDown ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> os . rename ( self . PSITURK_JS_FILE + '<STR_LIT>' , self . PSITURK_JS_FILE ) <EOL> class ExperimentErrorsTest ( PsiturkUnitTest ) : <EOL> def test_experiment_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> error_cls = psiturk . experiment_errors . ExperimentError <EOL> for error_name in error_cls . experiment_errors : <EOL> assert error_name in error_cls . error_descriptions <EOL> for error_name in error_cls . error_descriptions : <EOL> assert error_name in error_cls . experiment_errors <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
