package tests
{
    import mx.rpc.remoting.RemoteObject;
     import mx.controls.Alert;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.messaging.config.ServerConfig;
    import mx.messaging.channels.AMFChannel;
        
    public class SecureInvocationTest
    {    
        private var summaryObj:Array;
        private var secureTestService:RemoteObject;
        private var unsecureTestService:RemoteObject;
        private var application:Object;
        
        public function SecureInvocationTest( application:Object )
        {
            this.application = application;
        }
        
        public function runSecureTests():void
        {
            summaryObj = new Array();
            // this invocation should succeed
            testUnsecureInvocation();
        }
        
        private function testUnsecureInvocation():void
        {
            // this remote object will NOT have credentials
            unsecureTestService = new RemoteObject();
               unsecureTestService.destination = "SecureTest";
              unsecureTestService.CheckBalance.addEventListener("fault", helloWorldNoCred_Fault);
               unsecureTestService.CheckBalance.addEventListener("result", helloWorldNoCred_Result);
            // this invocation should fail
            unsecureTestService.CheckBalance();
        }
        
        private function testSecureInvocation():void
        {
            // this remote object will have credentials
            secureTestService = new RemoteObject(); 
               secureTestService.destination = "SecureTest";
               secureTestService.setCredentials( "testuser", "password" );
              secureTestService.CheckBalance.addEventListener("fault", helloWorld_Fault);
               secureTestService.CheckBalance.addEventListener("result", helloWorld_Result);
               secureTestService.CheckBalance();
        }
        
        public function helloWorld_Fault( event:FaultEvent ):void
        {
            summaryObj.push( "failure: secure destination threw an exception - " + event.fault.faultString );        
              application.setSummary( summaryObj );            
              secureTestService.logout();              
        }
        
        public function helloWorld_Result( result:ResultEvent ):void
        {
            summaryObj.push( "success: secure destination accepted credentials" );
              application.setSummary( summaryObj );            
              secureTestService.logout();
        }
        
        public function helloWorldNoCred_Fault( event:FaultEvent ):void
        {
            summaryObj.push( "success: secure destination rejected invocation without credentials. Server error: " + event.fault.faultString );        
              application.setSummary( summaryObj );    
              testSecureInvocation();        
        }
        
        public function helloWorldNoCred_Result( result:ResultEvent ):void
        {
            summaryObj.push( "failure: secure destination accepted invocation without credentials" );
              application.setSummary( summaryObj );            
              testSecureInvocation();
        }        
    }
}