public void ThrowsYubikeyInvalidResponseExceptionWhenExceptionWhileClientCall()
            {
                var response = new Mock <IYubicoResponse>();

                response.SetupGet(x => x.Status).Returns(YubicoResponseStatus.Ok);
                response.SetupGet(x => x.PublicId).Returns("some other id");

                var client = new Mock <IYubicoClient>();

                client.Setup(x => x.Verify(It.IsAny <string>())).Throws <WebException>();
                var configuration = new YubikeyConfiguration {
                    Users = new List <UserMapping> {
                        new UserMapping("some name", "some id")
                    }
                };
                var formCollection = new NameValueCollection {
                    { "yubiKey", "some id" }
                };
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/", UriKind.Absolute), null, null, formCollection);

                var target = new YubikeyCheckAttribute(configuration, client.Object)
                {
                    SkipIdentityNameCheck = true,
                };

                target.OnActionExecuting(context);
            }
            public void NullConfigurationThrowsWhenInstance()
            {
                var target = new YubikeyCheckAttribute(null, new YubicoClientAdapter())
                {
                    ImageOnly = true
                };

                target.OnActionExecuting(MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/?42FE943EC8A64735A978D1F81D5FFD00", UriKind.Absolute)));
            }
            public void DoesNotInterceptNonImageRequestsWhenImageOnly()
            {
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/", UriKind.Absolute));

                var target = new YubikeyCheckAttribute(new YubikeyConfiguration(), new YubicoClientAdapter())
                {
                    ImageOnly = true
                };

                target.OnActionExecuting(context);

                Assert.IsNull(context.Result);
            }
            public void DeliversImageForCorrectRequest()
            {
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/?42FE943EC8A64735A978D1F81D5FFD00", UriKind.Absolute));

                var target = new YubikeyCheckAttribute(new YubikeyConfiguration(), new YubicoClientAdapter())
                {
                    ImageOnly = true
                };

                target.OnActionExecuting(context);

                var fileResult = context.Result as FileResult;

                Assert.IsNotNull(fileResult);
                Assert.AreEqual("image/png", fileResult.ContentType);
            }
            public void ThrowsInvalidOperationExceptionWhenNoUsers()
            {
                var client        = SetupClient(YubicoResponseStatus.Ok, "some id");
                var configuration = new YubikeyConfiguration {
                    Users = null
                };
                var formCollection = new NameValueCollection {
                    { "yubiKey", "some id" }
                };
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/", UriKind.Absolute), null, null, formCollection);

                var target = new YubikeyCheckAttribute(configuration, client.Object)
                {
                    SkipIdentityNameCheck = true,
                };

                target.OnActionExecuting(context);
            }
            public void ThrowsNullResponseExceptionForNullResponse()
            {
                var client        = SetupClient();
                var configuration = new YubikeyConfiguration {
                    Users = new List <UserMapping> {
                        new UserMapping("some name", "some id")
                    }
                };
                var formCollection = new NameValueCollection {
                    { "yubiKey", "some id" }
                };
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/", UriKind.Absolute), null, null, formCollection);

                var target = new YubikeyCheckAttribute(configuration, client.Object)
                {
                    SkipIdentityNameCheck = true,
                };

                target.OnActionExecuting(context);
            }
            public void ThrowsWhenUnKnownIdAndMatchingUser()
            {
                var client        = SetupClient(YubicoResponseStatus.Ok, "some id");
                var configuration = new YubikeyConfiguration {
                    Users = new List <UserMapping> {
                        new UserMapping("some name", "some id")
                    }
                };
                var formCollection = new NameValueCollection {
                    { "yubiKey", "some id" }
                };
                var context = MvcTestBase.CreateRequestContext(new Uri("http://localhost/test/", UriKind.Absolute), null, null, formCollection, "some unknown name");

                var target = new YubikeyCheckAttribute(configuration, client.Object)
                {
                    SkipIdentityNameCheck = false,
                };

                target.OnActionExecuting(context);
            }
            public void ConfiguresClient()
            {
                var client        = new YubicoClientAdapter();
                var configuration = new YubikeyConfiguration
                {
                    Server = new ServerConfiguration
                    {
                        ApiKey    = "apikey",
                        ClientId  = "clientid",
                        SyncLevel = "synclevel",
                    }
                };

                var target = new YubikeyCheckAttribute(configuration, client)
                {
                    ImageOnly = true
                };

                Assert.IsNotNull(target);
                Assert.AreEqual("apikey", client.ApiKey);
                Assert.AreEqual("clientid", client.ClientId);
                Assert.AreEqual("synclevel", client.SyncLevel);
            }