<s> from django import forms <EOL> from django import http <EOL> import six <EOL> from horizon import exceptions <EOL> from horizon . test import helpers as test <EOL> from horizon import workflows <EOL> PROJECT_ID = "<STR_LIT>" <EOL> INSTANCE_ID = "<STR_LIT>" <EOL> def local_callback_func ( request , context ) : <EOL> return "<STR_LIT>" <EOL> def other_callback_func ( request , context ) : <EOL> return "<STR_LIT>" <EOL> def extra_callback_func ( request , context ) : <EOL> return "<STR_LIT>" <EOL> class TestActionOne ( workflows . Action ) : <EOL> project_id = forms . ChoiceField ( label = "<STR_LIT>" ) <EOL> user_id = forms . ChoiceField ( label = "<STR_LIT>" ) <EOL> class Meta ( object ) : <EOL> name = "<STR_LIT>" <EOL> slug = "<STR_LIT>" <EOL> def populate_project_id_choices ( self , request , context ) : <EOL> return [ ( PROJECT_ID , "<STR_LIT>" ) ] <EOL> def populate_user_id_choices ( self , request , context ) : <EOL> return [ ( request . user . id , request . user . username ) ] <EOL> def handle ( self , request , context ) : <EOL> return { "<STR_LIT:foo>" : "<STR_LIT:bar>" } <EOL> class TestActionTwo ( workflows . Action ) : <EOL> instance_id = forms . CharField ( label = "<STR_LIT>" ) <EOL> class Meta ( object ) : <EOL> name = "<STR_LIT>" <EOL> slug = "<STR_LIT>" <EOL> class TestActionThree ( workflows . Action ) : <EOL> extra = forms . CharField ( widget = forms . widgets . Textarea ) <EOL> class Meta ( object ) : <EOL> name = "<STR_LIT>" <EOL> slug = "<STR_LIT>" <EOL> class AdminAction ( workflows . Action ) : <EOL> admin_id = forms . CharField ( label = "<STR_LIT>" ) <EOL> class Meta ( object ) : <EOL> name = "<STR_LIT>" <EOL> slug = "<STR_LIT>" <EOL> permissions = ( "<STR_LIT>" , ) <EOL> class TestStepOne ( workflows . Step ) : <EOL> action_class = TestActionOne <EOL> contributes = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class TestStepTwo ( workflows . Step ) : <EOL> action_class = TestActionTwo <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = ( "<STR_LIT>" , ) <EOL> connections = { "<STR_LIT>" : <EOL> ( local_callback_func , <EOL> "<STR_LIT>" ) } <EOL> class TestExtraStep ( workflows . Step ) : <EOL> action_class = TestActionThree <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = ( "<STR_LIT>" , ) <EOL> connections = { "<STR_LIT>" : ( extra_callback_func , ) } <EOL> after = TestStepOne <EOL> before = TestStepTwo <EOL> class AdminStep ( workflows . Step ) : <EOL> action_class = AdminAction <EOL> contributes = ( "<STR_LIT>" , ) <EOL> after = TestStepOne <EOL> before = TestStepTwo <EOL> class TestWorkflow ( workflows . Workflow ) : <EOL> slug = "<STR_LIT>" <EOL> default_steps = ( TestStepOne , TestStepTwo ) <EOL> class TestWorkflowView ( workflows . WorkflowView ) : <EOL> workflow_class = TestWorkflow <EOL> template_name = "<STR_LIT>" <EOL> class TestFullscreenWorkflow ( workflows . Workflow ) : <EOL> slug = '<STR_LIT>' <EOL> default_steps = ( TestStepOne , TestStepTwo ) <EOL> fullscreen = True <EOL> class TestFullscreenWorkflowView ( workflows . WorkflowView ) : <EOL> workflow_class = TestFullscreenWorkflow <EOL> template_name = "<STR_LIT>" <EOL> class WorkflowsTests ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( WorkflowsTests , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( WorkflowsTests , self ) . tearDown ( ) <EOL> self . _reset_workflow ( ) <EOL> def _reset_workflow ( self ) : <EOL> TestWorkflow . _cls_registry = set ( [ ] ) <EOL> def test_workflow_construction ( self ) : <EOL> TestWorkflow . register ( TestExtraStep ) <EOL> flow = TestWorkflow ( self . request ) <EOL> self . assertQuerysetEqual ( flow . steps , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> self . assertEqual ( set ( [ '<STR_LIT>' ] ) , flow . depends_on ) <EOL> def test_step_construction ( self ) : <EOL> step_one = TestStepOne ( TestWorkflow ( self . request ) ) <EOL> self . assertEqual ( TestActionOne . name , step_one . name ) <EOL> self . assertEqual ( TestActionOne . slug , step_one . slug ) <EOL> self . assertEqual ( step_one . _handlers , { } ) <EOL> step_two = TestStepTwo ( TestWorkflow ( self . request ) ) <EOL> self . assertEqual ( [ local_callback_func , other_callback_func ] , <EOL> step_two . _handlers [ "<STR_LIT>" ] ) <EOL> def test_step_invalid_connections_handlers_not_list_or_tuple ( self ) : <EOL> class InvalidStepA ( TestStepTwo ) : <EOL> connections = { '<STR_LIT>' : { } } <EOL> class InvalidStepB ( TestStepTwo ) : <EOL> connections = { '<STR_LIT>' : '<STR_LIT>' } <EOL> with self . assertRaises ( TypeError ) : <EOL> InvalidStepA ( TestWorkflow ( self . request ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> InvalidStepB ( TestWorkflow ( self . request ) ) <EOL> def test_step_invalid_connection_handler_not_string_or_callable ( self ) : <EOL> class InvalidStepA ( TestStepTwo ) : <EOL> connections = { '<STR_LIT>' : ( None , ) } <EOL> class InvalidStepB ( TestStepTwo ) : <EOL> connections = { '<STR_LIT>' : ( <NUM_LIT:0> , ) } <EOL> with self . assertRaises ( TypeError ) : <EOL> InvalidStepA ( TestWorkflow ( self . request ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> InvalidStepB ( TestWorkflow ( self . request ) ) <EOL> def test_step_invalid_callback ( self ) : <EOL> class InvalidStep ( TestStepTwo ) : <EOL> connections = { "<STR_LIT>" : ( '<STR_LIT>' , ) } <EOL> with self . assertRaises ( ValueError ) : <EOL> InvalidStep ( TestWorkflow ( self . request ) ) <EOL> def test_connection_handlers_called ( self ) : <EOL> TestWorkflow . register ( TestExtraStep ) <EOL> flow = TestWorkflow ( self . request ) <EOL> flow . context [ '<STR_LIT>' ] = False <EOL> self . assertFalse ( flow . context [ '<STR_LIT>' ] ) <EOL> val = flow . context . set ( '<STR_LIT>' , PROJECT_ID ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] , val ) <EOL> def test_workflow_validation ( self ) : <EOL> flow = TestWorkflow ( self . request ) <EOL> with self . assertRaises ( exceptions . WorkflowValidationError ) : <EOL> flow . is_valid ( ) <EOL> seed = { "<STR_LIT>" : PROJECT_ID , <EOL> "<STR_LIT>" : self . user . id , <EOL> "<STR_LIT>" : INSTANCE_ID } <EOL> req = self . factory . post ( "<STR_LIT:/>" , seed ) <EOL> req . user = self . user <EOL> flow = TestWorkflow ( req , context_seed = { "<STR_LIT>" : PROJECT_ID } ) <EOL> for step in flow . steps : <EOL> if not step . action . is_valid ( ) : <EOL> self . fail ( "<STR_LIT>" <EOL> % ( step . slug , step . action . errors ) ) <EOL> self . assertTrue ( flow . is_valid ( ) ) <EOL> flow . context . set ( "<STR_LIT>" , "<STR_LIT:foo>" ) <EOL> self . assertTrue ( flow . is_valid ( ) ) <EOL> def test_workflow_finalization ( self ) : <EOL> flow = TestWorkflow ( self . request ) <EOL> self . assertTrue ( flow . finalize ( ) ) <EOL> def test_workflow_view ( self ) : <EOL> view = TestWorkflowView . as_view ( ) <EOL> req = self . factory . get ( "<STR_LIT:/>" ) <EOL> res = view ( req ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_code ) <EOL> def test_workflow_registration ( self ) : <EOL> req = self . factory . get ( "<STR_LIT>" ) <EOL> flow = TestWorkflow ( req ) <EOL> self . assertQuerysetEqual ( flow . steps , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> TestWorkflow . register ( TestExtraStep ) <EOL> flow = TestWorkflow ( req ) <EOL> self . assertQuerysetEqual ( flow . steps , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_workflow_render ( self ) : <EOL> TestWorkflow . register ( TestExtraStep ) <EOL> req = self . factory . get ( "<STR_LIT>" ) <EOL> flow = TestWorkflow ( req ) <EOL> output = http . HttpResponse ( flow . render ( ) ) <EOL> self . assertContains ( output , six . text_type ( flow . name ) ) <EOL> self . assertContains ( output , six . text_type ( TestActionOne . name ) ) <EOL> self . assertContains ( output , six . text_type ( TestActionTwo . name ) ) <EOL> self . assertContains ( output , six . text_type ( TestActionThree . name ) ) <EOL> def test_has_permissions ( self ) : <EOL> self . assertQuerysetEqual ( TestWorkflow . _cls_registry , [ ] ) <EOL> TestWorkflow . register ( AdminStep ) <EOL> flow = TestWorkflow ( self . request ) <EOL> step = AdminStep ( flow ) <EOL> self . assertItemsEqual ( step . permissions , <EOL> ( "<STR_LIT>" , ) ) <EOL> self . assertQuerysetEqual ( flow . steps , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> self . set_permissions ( [ '<STR_LIT:test>' ] ) <EOL> self . request . user = self . user <EOL> flow = TestWorkflow ( self . request ) <EOL> self . assertQuerysetEqual ( flow . steps , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_entry_point ( self ) : <EOL> req = self . factory . get ( "<STR_LIT>" ) <EOL> flow = TestWorkflow ( req ) <EOL> self . assertEqual ( "<STR_LIT>" , flow . get_entry_point ( ) ) <EOL> flow = TestWorkflow ( req , entry_point = "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT>" , flow . get_entry_point ( ) ) <EOL> def test_fullscreenworkflow_view ( self ) : <EOL> view = TestFullscreenWorkflowView . as_view ( ) <EOL> req = self . factory . get ( "<STR_LIT:/>" ) <EOL> req . is_ajax = lambda : True <EOL> res = view ( req ) <EOL> output = res . render ( ) <EOL> self . assertRegexpMatches ( bytes ( output ) , <EOL> b'<STR_LIT>' ) <EOL> def test_notfullscreenworkflow_view ( self ) : <EOL> view = TestWorkflowView . as_view ( ) <EOL> req = self . factory . get ( "<STR_LIT:/>" ) <EOL> req . is_ajax = lambda : True <EOL> res = view ( req ) <EOL> output = res . render ( ) <EOL> self . assertNotRegexpMatches ( bytes ( output ) , <EOL> b'<STR_LIT>' ) </s>
