private void TestTemplate(InvokeCallback invoke, TestCallback proceed, TestCallback verify, bool verifyInCallback)
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;
            object state = new object();

            bool          testCompleted = false;
            AsyncCallback asyncCallback = ar =>
            {
                Assert.IsNotNull(ar,
                                 "IAsyncResult should not be null.");
                Assert.AreEqual(state, ar.AsyncState,
                                "States should be equal.");
                ExceptionHelper.ExpectException <NotSupportedException>(
                    () => Assert.IsNull(ar.AsyncWaitHandle, "This property is not supported."));
                Assert.IsFalse(ar.CompletedSynchronously,
                               "IAsyncResult should not have completed synchronously.");
                Assert.IsTrue(ar.IsCompleted || mock.DomainClient.CancellationRequested,
                              "IAsyncResult should be complete or cancelled.");

                try
                {
                    verify(mock, service, ar);
                }
                catch (Exception ex)
                {
                    this.EnqueueCallback(() => { throw ex; });
                }

                testCompleted = true;
            };

            IAsyncResult asyncResult = invoke(mock, service, verifyInCallback ? asyncCallback : null, state);

            Assert.IsNotNull(asyncResult,
                             "IAsyncResult should not be null.");
            Assert.AreEqual(state, asyncResult.AsyncState,
                            "States should be equal.");
            ExceptionHelper.ExpectException <NotSupportedException>(
                () => Assert.IsNull(asyncResult.AsyncWaitHandle, "This property is not supported."));
            Assert.IsFalse(asyncResult.CompletedSynchronously,
                           "IAsyncResult should not have completed synchronously.");

            // We don't have a dispatcher SynchronizationContext when running tests on the
            // full framework, so the operation will complete on another thread which can happen before we reach this assert
            // TODO: Try to get tests to run on a dispatcher by using mstest v2 extensobility
#if SILVERLIGHT
            Assert.IsFalse(asyncResult.IsCompleted, "IAsyncResult should not be complete.");
#endif

            proceed(mock, service, asyncResult);

            if (!verifyInCallback)
            {
                this.EnqueueCallback(() => asyncCallback(asyncResult));
            }
            this.EnqueueConditional(() => testCompleted);
        }
Beispiel #2
0
        public void InvalidCancelAndEndIARs()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            IAsyncResult invalidResult = service.BeginLoadUserMock(null, null);

            // Canceling the result once makes it invalid for subsequent use. Since we really just need
            // to confirm that the input is handled by AsyncResultBase, this is sufficient for the test.
            service.CancelLoadUserMock(invalidResult);

            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.CancelLoadUserMock(invalidResult));
            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.CancelLoginMock(invalidResult));
            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.CancelLogoutMock(invalidResult));
            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.CancelSaveUserMock(invalidResult));

            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.EndLoadUserMock(invalidResult));
            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.EndLoginMock(invalidResult));
            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.EndLogoutMock(invalidResult));
            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.EndSaveUserMock(invalidResult));
        }
        public void LogoutThrowsNotSupported()
        {
            AuthenticationDomainContext mock    = new AuthenticationDomainContext();
            WindowsAuthentication       service = new WindowsAuthentication();

            service.DomainContext = mock;

            ExceptionHelper.ExpectException <NotSupportedException>(
                () => service.Logout(false));
        }
Beispiel #4
0
        public void LoginWithNullParametersThrows()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            ExceptionHelper.ExpectArgumentNullExceptionStandard(
                () => service.LoginAsync(null, CancellationToken.None), "parameters");
        }
        public void LogoutThrowsNotSupported()
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            WindowsAuthentication service = new WindowsAuthentication();

            service.DomainContext = mock;

            ExceptionHelper.ExpectException<NotSupportedException>(
                () => service.Logout(false));
        }
Beispiel #6
0
        public async Task SaveAnonymousUserThrows()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            await ExceptionHelper.ExpectExceptionAsync <InvalidOperationException>(
                () => service.SaveUserAsync(service.User, CancellationToken.None));
        }
Beispiel #7
0
        public void SaveAnonymousUserThrows()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.BeginSaveUserMock(service.User, null, null));
        }
Beispiel #8
0
        public void LoginWithNullParametersThrows()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            ExceptionHelper.ExpectArgumentNullExceptionStandard(
                () => service.BeginLoginMock(null, null, null), "parameters");
        }
Beispiel #9
0
        public void DefaultUser()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            IPrincipal defaultUser = service.CreateDefaultUserMock();

            Assert.IsInstanceOfType(defaultUser, typeof(MockUser),
                                    "DefaultUser should be of type MockUser after service has started.");
        }
Beispiel #10
0
        private void TestTemplate(InvokeCallback invoke, TestCallback proceed, TestCallback verify, bool verifyInCallback)
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;
            object state = new object();

            bool          testCompleted = false;
            AsyncCallback asyncCallback = ar =>
            {
                Assert.IsNotNull(ar,
                                 "IAsyncResult should not be null.");
                Assert.AreEqual(state, ar.AsyncState,
                                "States should be equal.");
                ExceptionHelper.ExpectException <NotSupportedException>(
                    () => Assert.IsNull(ar.AsyncWaitHandle, "This property is not supported."));
                Assert.IsFalse(ar.CompletedSynchronously,
                               "IAsyncResult should not have completed synchronously.");
                Assert.IsTrue(ar.IsCompleted || mock.DomainClient.CancellationRequested,
                              "IAsyncResult should be complete or cancelled.");

                verify(mock, service, ar);

                testCompleted = true;
            };

            IAsyncResult asyncResult = invoke(mock, service, verifyInCallback ? asyncCallback : null, state);

            Assert.IsNotNull(asyncResult,
                             "IAsyncResult should not be null.");
            Assert.AreEqual(state, asyncResult.AsyncState,
                            "States should be equal.");
            ExceptionHelper.ExpectException <NotSupportedException>(
                () => Assert.IsNull(asyncResult.AsyncWaitHandle, "This property is not supported."));
            Assert.IsFalse(asyncResult.CompletedSynchronously,
                           "IAsyncResult should not have completed synchronously.");
            Assert.IsFalse(asyncResult.IsCompleted,
                           "IAsyncResult should not be complete.");

            proceed(mock, service, asyncResult);

            if (!verifyInCallback)
            {
                this.EnqueueCallback(() => asyncCallback(asyncResult));
            }

            this.EnqueueConditional(() => testCompleted);
        }
Beispiel #11
0
        public void DomainContext()
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            Assert.AreEqual(mock, service.DomainContext,
                            "DomainContexts should be equal.");

            WebAuthenticationServiceTest.InitializeService(service);

            Assert.AreEqual(mock, service.DomainContext,
                            "DomainContexts should be equal.");

            ExceptionHelper.ExpectException <InvalidOperationException>(
                () => service.DomainContext = null);
        }
Beispiel #12
0
        public async Task LoginError()
        {
            LoginParameters parameters = new LoginParameters(AuthenticationDomainClient.ValidUserName, string.Empty);

            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService {
                DomainContext = mock
            };

            mock.DomainClient.Error = new Exception(WebAuthenticationServiceTest.ErrorMessage);

            var task = service.LoginAsync(parameters, CancellationToken.None);

            mock.DomainClient.RequestCallback();

            await ExceptionHelper.ExpectExceptionAsync <DomainOperationException>(
                () => task,
                string.Format(Resource.DomainContext_LoadOperationFailed, "Login", WebAuthenticationServiceTest.ErrorMessage));
        }
Beispiel #13
0
        private void TestTemplate <T>(Func <AuthenticationDomainContext, MockWebAuthenticationService, Task <T> > invoke,
                                      Action <AuthenticationDomainContext, MockWebAuthenticationService, Task <T> > proceed,
                                      Action <AuthenticationDomainContext, MockWebAuthenticationService, Task <T> > verify)
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService {
                DomainContext = mock
            };

            Task <T> task = invoke(mock, service);

            Assert.IsNotNull(task);

            proceed(mock, service, task);
            this.EnqueueConditional(() => task.IsCompleted);
            this.EnqueueCallback(() =>
            {
                verify(mock, service, task);
            });
        }
Beispiel #14
0
        private void CancelTemplate <T>(Func <AuthenticationDomainContext, MockWebAuthenticationService, CancellationToken, Task <T> > invoke,
                                        Action <AuthenticationDomainContext, MockWebAuthenticationService> verify = null)
        {
            AuthenticationDomainContext  mock    = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService {
                DomainContext = mock
            };

            using var cts = new CancellationTokenSource();

            Task <T> task = invoke(mock, service, cts.Token);

            Assert.IsNotNull(task);
            Assert.IsFalse(task.IsCompleted, "task should not be complete.");
            cts.Cancel();

            this.EnqueueConditional(() => task.IsCompleted);
            this.EnqueueCallback(() =>
            {
                Assert.IsTrue(task.IsCanceled, "task should be cancelled");
                verify?.Invoke(mock, service);
            });
            this.EnqueueTestComplete();
        }
        private void TestTemplate(InvokeCallback invoke, TestCallback proceed, TestCallback verify, bool verifyInCallback)
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService() ;
            service.DomainContext = mock;
            object state = new object();
            
            bool testCompleted = false;
            AsyncCallback asyncCallback = ar =>
            {
                Assert.IsNotNull(ar,
                    "IAsyncResult should not be null.");
                Assert.AreEqual(state, ar.AsyncState,
                    "States should be equal.");
                ExceptionHelper.ExpectException<NotSupportedException>(
                    () => Assert.IsNull(ar.AsyncWaitHandle, "This property is not supported."));
                Assert.IsFalse(ar.CompletedSynchronously,
                    "IAsyncResult should not have completed synchronously.");
                Assert.IsTrue(ar.IsCompleted || mock.DomainClient.CancellationRequested,
                    "IAsyncResult should be complete or cancelled.");

                verify(mock, service, ar);

                testCompleted = true;
            };

            IAsyncResult asyncResult = invoke(mock, service, verifyInCallback ? asyncCallback : null, state);

            Assert.IsNotNull(asyncResult,
                "IAsyncResult should not be null.");
            Assert.AreEqual(state, asyncResult.AsyncState,
                "States should be equal.");
            ExceptionHelper.ExpectException<NotSupportedException>(
                () => Assert.IsNull(asyncResult.AsyncWaitHandle, "This property is not supported."));
            Assert.IsFalse(asyncResult.CompletedSynchronously,
                "IAsyncResult should not have completed synchronously.");
            Assert.IsFalse(asyncResult.IsCompleted,
                "IAsyncResult should not be complete.");

            proceed(mock, service, asyncResult);

            if (!verifyInCallback)
            {
                this.EnqueueCallback(() => asyncCallback(asyncResult));
            }

            this.EnqueueConditional(() => testCompleted);
        }
        public void SaveAnonymousUserThrows()
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.BeginSaveUserMock(service.User, null, null));
        }
        public void InvalidCancelAndEndIARs()
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            IAsyncResult invalidResult = service.BeginLoadUserMock(null, null);
            // Canceling the result once makes it invalid for subsequent use. Since we really just need
            // to confirm that the input is handled by AsyncResultBase, this is sufficient for the test.
            service.CancelLoadUserMock(invalidResult);

            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.CancelLoadUserMock(invalidResult));
            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.CancelLoginMock(invalidResult));
            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.CancelLogoutMock(invalidResult));
            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.CancelSaveUserMock(invalidResult));

            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.EndLoadUserMock(invalidResult));
            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.EndLoginMock(invalidResult));
            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.EndLogoutMock(invalidResult));
            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.EndSaveUserMock(invalidResult));
        }
        public void DomainContext()
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            Assert.AreEqual(mock, service.DomainContext,
                "DomainContexts should be equal.");

            WebAuthenticationServiceTest.InitializeService(service);

            Assert.AreEqual(mock, service.DomainContext,
                "DomainContexts should be equal.");

            ExceptionHelper.ExpectException<InvalidOperationException>(
                () => service.DomainContext = null);
        }
        public void DefaultUser()
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            IPrincipal defaultUser = service.CreateDefaultUserMock();

            Assert.IsInstanceOfType(defaultUser, typeof(MockUser),
                "DefaultUser should be of type MockUser after service has started.");
        }
        public void LoginWithNullParametersThrows()
        {
            AuthenticationDomainContext mock = new AuthenticationDomainContext();
            MockWebAuthenticationService service = new MockWebAuthenticationService();

            service.DomainContext = mock;

            ExceptionHelper.ExpectArgumentNullExceptionStandard(
                () => service.BeginLoginMock(null, null, null), "parameters");
        }