public void Post_request_hook_should_not_return_a_challenge_on_an_ajax_request_when_set_to_nonajax()
        {
            // Given
            var config = new BasicAuthenticationConfiguration(A.Fake <IUserValidator>(), "realm", UserPromptBehaviour.NonAjax);
            var hooks  = new Pipelines();

            BasicAuthentication.Enable(hooks, config);
            var headers = new Dictionary <string, IEnumerable <string> >();

            headers.Add(ajaxRequestHeaderKey, new [] { ajaxRequestHeaderValue });

            var context = new NancyContext()
            {
                Request = new FakeRequest("GET", "/", headers)
            };

            context.Response = new Response {
                StatusCode = HttpStatusCode.Unauthorized
            };

            // When
            hooks.AfterRequest.Invoke(context, new CancellationToken());

            // Then
            context.Response.Headers.ContainsKey("WWW-Authenticate").ShouldBeFalse();
        }
        public void Should_throw_with_null_module_passed_to_enable_with_config()
        {
            // Given, When
            var result = Record.Exception(() => BasicAuthentication.Enable((INancyModule)null, this.config));

            // Then
            result.ShouldBeOfType(typeof(ArgumentNullException));
        }
        public void Should_throw_with_null_config_passed_to_enable_with_module()
        {
            // Given, When
            var result = Record.Exception(() => BasicAuthentication.Enable(new FakeModule(), null));

            // Then
            result.ShouldBeOfType(typeof(ArgumentNullException));
        }
Beispiel #4
0
        private void EnableBasicAuth(IUnityContainer container, IPipelines pipelines)
        {
            var config =
                new BasicAuthenticationConfiguration(
                    container.Resolve <IUserValidator>()
                    , "you need login"
                    , UserPromptBehaviour.NonAjax);

            BasicAuthentication.Enable(pipelines, config);
        }
        public void Should_add_both_basic_and_requires_auth_pre_and_post_hooks_in_module_when_enabled()
        {
            // Given
            var module = new FakeModule();

            // When
            BasicAuthentication.Enable(module, this.config);

            // Then
            module.Before.PipelineDelegates.ShouldHaveCount(2);
        }
        public void Should_add_a_pre_and_post_hook_in_application_when_enabled()
        {
            // Given
            var pipelines = A.Fake <IApplicationPipelines>();

            // When
            BasicAuthentication.Enable(pipelines, this.config);

            // Then
            A.CallTo(() => pipelines.BeforeRequest.AddItemToStartOfPipeline(A <Func <NancyContext, Response> > .Ignored))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
        public void Should_set_user_in_context_with_valid_username_in_auth_header()
        {
            // Given
            var fakePipelines = new Pipelines();

            var validator = A.Fake <IUserValidator>();
            var fakeUser  = A.Fake <ClaimsPrincipal>();

            A.CallTo(() => validator.Validate("foo", "bar")).Returns(fakeUser);

            var cfg = new BasicAuthenticationConfiguration(validator, "realm");

            var context = CreateContextWithHeader(
                "Authorization", new [] { "Basic" + " " + EncodeCredentials("foo", "bar") });

            BasicAuthentication.Enable(fakePipelines, cfg);

            // When
            fakePipelines.BeforeRequest.Invoke(context, new CancellationToken());

            // Then
            context.CurrentUser.ShouldBeSameAs(fakeUser);
        }
        public void Post_request_hook_should_not_return_a_challenge_when_set_to_never()
        {
            // Given
            var config = new BasicAuthenticationConfiguration(A.Fake <IUserValidator>(), "realm", UserPromptBehaviour.Never);
            var hooks  = new Pipelines();

            BasicAuthentication.Enable(hooks, config);

            var context = new NancyContext()
            {
                Request = new FakeRequest("GET", "/")
            };

            context.Response = new Response {
                StatusCode = HttpStatusCode.Unauthorized
            };

            // When
            hooks.AfterRequest.Invoke(context, new CancellationToken());

            // Then
            context.Response.Headers.ContainsKey("WWW-Authenticate").ShouldBeFalse();
        }
Beispiel #9
0
 public void Initialize(IPipelines pipelines)
 {
     BasicAuthentication.Enable(pipelines, new BasicAuthenticationConfiguration(new DummyValidator(), "argolis"));
 }
 public BasicAuthenticationFixture()
 {
     this.config = new BasicAuthenticationConfiguration(A.Fake <IUserValidator>(), "realm", UserPromptBehaviour.Always);
     this.hooks  = new Pipelines();
     BasicAuthentication.Enable(this.hooks, this.config);
 }
 public BasicAuthenticationFixture()
 {
     this.config = new BasicAuthenticationConfiguration(A.Fake <IUserValidator>(), "realm");
     this.hooks  = new FakeApplicationPipelines();
     BasicAuthentication.Enable(this.hooks, this.config);
 }
Beispiel #12
0
 public void Enable(IPipelines pipelines)
 {
     BasicAuthentication.Enable(pipelines, _configuration);
 }