<s> import json <EOL> import os <EOL> import re <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from corehq . apps . app_manager . tests . util import add_build <EOL> from corehq . apps . app_manager . util import new_careplan_module <EOL> from corehq . apps . app_manager . views import AppSummaryView <EOL> from corehq . apps . builds . models import BuildSpec <EOL> from corehq import toggles <EOL> from corehq . apps . users . models import WebUser <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . app_manager . models import ( <EOL> AdvancedModule , <EOL> Application , <EOL> APP_V1 , <EOL> APP_V2 , <EOL> Module , <EOL> ReportModule , <EOL> ShadowModule , <EOL> ) <EOL> from . test_form_versioning import BLANK_TEMPLATE <EOL> class TestViews ( TestCase ) : <EOL> app = None <EOL> build = None <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . domain = Domain ( name = '<STR_LIT>' , is_active = True ) <EOL> cls . domain . save ( ) <EOL> cls . username = '<STR_LIT>' <EOL> cls . password = '<STR_LIT>' <EOL> cls . user = WebUser . create ( cls . domain . name , cls . username , cls . password , is_active = True ) <EOL> cls . user . is_superuser = True <EOL> cls . user . save ( ) <EOL> cls . build = add_build ( version = '<STR_LIT>' , build_number = <NUM_LIT> ) <EOL> cls . app = Application . new_app ( cls . domain . name , "<STR_LIT>" , application_version = APP_V2 ) <EOL> cls . app . build_spec = BuildSpec . from_string ( '<STR_LIT>' ) <EOL> toggles . CUSTOM_PROPERTIES . set ( "<STR_LIT>" . format ( domain = cls . domain . name ) , True ) <EOL> def setUp ( self ) : <EOL> self . client . login ( username = self . username , password = self . password ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . user . delete ( ) <EOL> cls . build . delete ( ) <EOL> if cls . app : <EOL> cls . app . delete ( ) <EOL> cls . domain . delete ( ) <EOL> def test_download_file_bad_xform_404 ( self ) : <EOL> '''<STR_LIT>''' <EOL> module = self . app . add_module ( Module . new_module ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> build1 = { '<STR_LIT:version>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } <EOL> build2 = { '<STR_LIT:version>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } <EOL> add_build ( ** build1 ) <EOL> add_build ( ** build2 ) <EOL> with open ( os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:data>" , "<STR_LIT>" ) ) as f : <EOL> xform_str = f . read ( ) <EOL> self . app . new_form ( module . id , name = "<STR_LIT>" , attachment = xform_str , lang = "<STR_LIT>" ) <EOL> self . app . save ( ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = dict ( domain = self . domain . name , <EOL> app_id = self . app . get_id , <EOL> path = '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_edit_commcare_profile ( self ) : <EOL> app = Application . new_app ( self . domain . name , "<STR_LIT>" , application_version = APP_V2 ) <EOL> app . save ( ) <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:value>" , <EOL> "<STR_LIT>" : "<STR_LIT:value>" <EOL> } <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , args = [ self . domain . name , app . _id ] ) , <EOL> json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> content = json . loads ( response . content ) <EOL> custom_properties = content [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . assertEqual ( custom_properties [ "<STR_LIT>" ] , "<STR_LIT:value>" ) <EOL> self . assertEqual ( custom_properties [ "<STR_LIT>" ] , "<STR_LIT:value>" ) <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , args = [ self . domain . name , app . _id ] ) , <EOL> json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> content = json . loads ( response . content ) <EOL> custom_properties = content [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . assertEqual ( custom_properties [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def _test_status_codes ( self , names , kwargs ) : <EOL> for name in names : <EOL> response = self . client . get ( reverse ( name , kwargs = kwargs ) , follow = False ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def _json_content_from_get ( self , name , kwargs , data = { } ) : <EOL> response = self . client . get ( reverse ( name , kwargs = kwargs ) , data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> return json . loads ( response . content ) <EOL> def test_basic_app ( self ) : <EOL> module = self . app . add_module ( Module . new_module ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> form = self . app . new_form ( module . id , "<STR_LIT>" , "<STR_LIT>" , attachment = BLANK_TEMPLATE . format ( xmlns = '<STR_LIT>' ) ) <EOL> self . app . save ( ) <EOL> kwargs = { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> } <EOL> self . _test_status_codes ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> AppSummaryView . urlname , <EOL> ] , kwargs ) <EOL> self . build = self . app . make_build ( ) <EOL> self . build . save ( ) <EOL> content = self . _json_content_from_get ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> } ) <EOL> self . assertEqual ( content [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> content = self . _json_content_from_get ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> } , { '<STR_LIT>' : <NUM_LIT:5> } ) <EOL> self . assertEqual ( len ( content ) , <NUM_LIT:1> ) <EOL> content = content [ <NUM_LIT:0> ] <EOL> self . assertEqual ( content [ '<STR_LIT>' ] , self . app . id ) <EOL> kwargs [ '<STR_LIT>' ] = module . id <EOL> self . _test_status_codes ( [ '<STR_LIT>' ] , kwargs ) <EOL> kwargs [ '<STR_LIT>' ] = form . id <EOL> self . _test_status_codes ( [ '<STR_LIT>' , '<STR_LIT>' ] , kwargs ) <EOL> def test_advanced_module ( self ) : <EOL> module = self . app . add_module ( AdvancedModule . new_module ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . app . save ( ) <EOL> self . _test_status_codes ( [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> '<STR_LIT>' : module . id , <EOL> } ) <EOL> def test_report_module ( self ) : <EOL> module = self . app . add_module ( ReportModule . new_module ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . app . save ( ) <EOL> self . _test_status_codes ( [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> '<STR_LIT>' : module . id , <EOL> } ) <EOL> def test_shadow_module ( self ) : <EOL> module = self . app . add_module ( ShadowModule . new_module ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . app . save ( ) <EOL> self . _test_status_codes ( [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> '<STR_LIT>' : module . id , <EOL> } ) <EOL> def test_careplan_module ( self ) : <EOL> target_module = self . app . add_module ( Module . new_module ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> target_module . case_type = '<STR_LIT>' <EOL> module = new_careplan_module ( self . app , '<STR_LIT>' , '<STR_LIT>' , target_module ) <EOL> self . app . save ( ) <EOL> self . _test_status_codes ( [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . app . id , <EOL> '<STR_LIT>' : module . id , <EOL> } ) <EOL> class TestTemplateAppViews ( TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . domain = Domain ( name = '<STR_LIT>' , is_active = True ) <EOL> cls . domain . save ( ) <EOL> cls . username = '<STR_LIT>' <EOL> cls . password = '<STR_LIT>' <EOL> cls . user = WebUser . create ( cls . domain . name , cls . username , cls . password , is_active = True ) <EOL> cls . user . is_superuser = True <EOL> cls . user . save ( ) <EOL> def setUp ( self ) : <EOL> self . client . login ( username = self . username , password = self . password ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . user . delete ( ) <EOL> cls . domain . delete ( ) <EOL> def _check_response ( self , response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> redirect_location = response [ '<STR_LIT>' ] <EOL> [ app_id ] = re . compile ( r'<STR_LIT>' ) . findall ( redirect_location ) <EOL> expected = '<STR_LIT>' . format ( app_id ) <EOL> self . assertTrue ( redirect_location . endswith ( expected ) ) <EOL> self . addCleanup ( lambda : Application . get_db ( ) . delete_doc ( app_id ) ) <EOL> def test_case_management_app_from_template ( self ) : <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) , follow = False ) <EOL> self . _check_response ( response ) <EOL> def test_survey_app_from_template ( self ) : <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) , follow = False ) <EOL> self . _check_response ( response ) <EOL> def test_default_new_app ( self ) : <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . domain . name , <EOL> } ) , follow = False ) <EOL> self . _check_response ( response ) </s>
