handleauthenticateasync unit test

Solutions on MaxInterview for handleauthenticateasync unit test by the best coders in the world

showing results for - "handleauthenticateasync unit test"
Noan
13 Jan 2016
1        [TestMethod]
2        public async Task HandleAuthenticateAsync_CredentialsTryParseFails_ReturnsAuthenticateResultFail()
3        {
4            var context = new DefaultHttpContext();
5            var authorizationHeader = new StringValues(String.Empty);
6            context.Request.Headers.Add(HeaderNames.Authorization, authorizationHeader);
7
8            await _handler.InitializeAsync(new AuthenticationScheme(BasicAuthenticationHandler.SchemeName, null, typeof(BasicAuthenticationHandler)), context);
9            var result = await _handler.AuthenticateAsync();
10
11            Assert.IsFalse(result.Succeeded);
12            Assert.AreEqual("Basic authentication failed.  Unable to parse username and password.", result.Failure.Message);
13        }
14
15        [TestMethod]
16        public async Task HandleAuthenticateAsync_PrincipalIsNull_ReturnsAuthenticateResultFail()
17        {
18            _principalProvider.Setup(m => m.GetClaimsPrincipalAsync(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<String>())).ReturnsAsync((ClaimsPrincipal)null);
19
20            var context = new DefaultHttpContext();
21            var authorizationHeader = new StringValues("Basic VGVzdFVzZXJOYW1lOlRlc3RQYXNzd29yZA==");
22            context.Request.Headers.Add(HeaderNames.Authorization, authorizationHeader);
23
24            await _handler.InitializeAsync(new AuthenticationScheme(BasicAuthenticationHandler.SchemeName, null, typeof(BasicAuthenticationHandler)), context);
25            var result = await _handler.AuthenticateAsync();
26
27            Assert.IsFalse(result.Succeeded);
28            Assert.AreEqual("Basic authentication failed.  Invalid username and password.", result.Failure.Message);
29        }
30
31        [TestMethod]
32        public async Task HandleAuthenticateAsync_PrincipalIsNull_ReturnsAuthenticateResultSuccessWithPrincipalInTicket()
33        {
34            var username = "TestUserName";
35            var claims = new[] { new Claim(ClaimTypes.Name, username) };
36            var identity = new ClaimsIdentity(claims, BasicAuthenticationHandler.SchemeName);
37            var claimsPrincipal = new ClaimsPrincipal(identity);
38            _principalProvider.Setup(m => m.GetClaimsPrincipalAsync(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<String>())).ReturnsAsync(claimsPrincipal);
39
40            var context = new DefaultHttpContext();
41            var authorizationHeader = new StringValues("Basic VGVzdFVzZXJOYW1lOlRlc3RQYXNzd29yZA==");
42            context.Request.Headers.Add(HeaderNames.Authorization, authorizationHeader);
43
44            await _handler.InitializeAsync(new AuthenticationScheme(BasicAuthenticationHandler.SchemeName, null, typeof(BasicAuthenticationHandler)), context);
45            var result = await _handler.AuthenticateAsync();
46
47            Assert.IsTrue(result.Succeeded);
48            Assert.AreEqual(BasicAuthenticationHandler.SchemeName, result.Ticket.AuthenticationScheme);
49            Assert.AreEqual(username, result.Ticket.Principal.Identity.Name);
50        }