Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

handleauthenticateasync unit test

        [TestMethod]
        public async Task HandleAuthenticateAsync_CredentialsTryParseFails_ReturnsAuthenticateResultFail()
        {
            var context = new DefaultHttpContext();
            var authorizationHeader = new StringValues(String.Empty);
            context.Request.Headers.Add(HeaderNames.Authorization, authorizationHeader);

            await _handler.InitializeAsync(new AuthenticationScheme(BasicAuthenticationHandler.SchemeName, null, typeof(BasicAuthenticationHandler)), context);
            var result = await _handler.AuthenticateAsync();

            Assert.IsFalse(result.Succeeded);
            Assert.AreEqual("Basic authentication failed.  Unable to parse username and password.", result.Failure.Message);
        }

        [TestMethod]
        public async Task HandleAuthenticateAsync_PrincipalIsNull_ReturnsAuthenticateResultFail()
        {
            _principalProvider.Setup(m => m.GetClaimsPrincipalAsync(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<String>())).ReturnsAsync((ClaimsPrincipal)null);

            var context = new DefaultHttpContext();
            var authorizationHeader = new StringValues("Basic VGVzdFVzZXJOYW1lOlRlc3RQYXNzd29yZA==");
            context.Request.Headers.Add(HeaderNames.Authorization, authorizationHeader);

            await _handler.InitializeAsync(new AuthenticationScheme(BasicAuthenticationHandler.SchemeName, null, typeof(BasicAuthenticationHandler)), context);
            var result = await _handler.AuthenticateAsync();

            Assert.IsFalse(result.Succeeded);
            Assert.AreEqual("Basic authentication failed.  Invalid username and password.", result.Failure.Message);
        }

        [TestMethod]
        public async Task HandleAuthenticateAsync_PrincipalIsNull_ReturnsAuthenticateResultSuccessWithPrincipalInTicket()
        {
            var username = "TestUserName";
            var claims = new[] { new Claim(ClaimTypes.Name, username) };
            var identity = new ClaimsIdentity(claims, BasicAuthenticationHandler.SchemeName);
            var claimsPrincipal = new ClaimsPrincipal(identity);
            _principalProvider.Setup(m => m.GetClaimsPrincipalAsync(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<String>())).ReturnsAsync(claimsPrincipal);

            var context = new DefaultHttpContext();
            var authorizationHeader = new StringValues("Basic VGVzdFVzZXJOYW1lOlRlc3RQYXNzd29yZA==");
            context.Request.Headers.Add(HeaderNames.Authorization, authorizationHeader);

            await _handler.InitializeAsync(new AuthenticationScheme(BasicAuthenticationHandler.SchemeName, null, typeof(BasicAuthenticationHandler)), context);
            var result = await _handler.AuthenticateAsync();

            Assert.IsTrue(result.Succeeded);
            Assert.AreEqual(BasicAuthenticationHandler.SchemeName, result.Ticket.AuthenticationScheme);
            Assert.AreEqual(username, result.Ticket.Principal.Identity.Name);
        }
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript path folder above 
Javascript :: capire che giorno della settimana è javascript 
Javascript :: react native map repeate 
Javascript :: java script num toSting syntax eror 
Javascript :: implement cai webchat in react native 
Javascript :: how to write a program that determines the minutes since midnight, Jan 1, 1970 in javascript 
Javascript :: sp_oamethod post json 
Javascript :: javascript canvas clip rectangle 
Javascript :: express pourquoi mettre bodyparser avant router 
Javascript :: expect vue test utils compare objects 
Javascript :: dragula filter 
Javascript :: js library for checking if two shapes overlap 
Javascript :: what does results.push({}) nodejs mean 
Javascript :: change items per page pagination angularjs 
Javascript :: passing the href in ajax call 
Javascript :: defer accessing a variable until available in js 
Javascript :: cgi-node example 
Javascript :: which node primary pacemaker of heart 
Javascript :: angular router navigate base href 
Javascript :: add edit delete from table using jquery 
Javascript :: how to check if a kendo drop down is enabled js 
Javascript :: js remove first element from string 
Javascript :: prefix increment 
Javascript :: js var 
Javascript :: hoe lang is 50000 uur 
Javascript :: icon api node js to browser 
Javascript :: check a divide condition and print msg javascript 
Javascript :: cchartpie react 
Javascript :: react native navigation paramlist never used 
Javascript :: matriz bucle js 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =