public void ShouldLogon() { Models.AuthData expectedResult = new Models.AuthData { IsAuthenticated = true, Token = null, User = null }; this.mockService .Setup(e => e.GetAuthenticationData()).Returns(expectedResult); // Call actual Authenticate Models.AuthData actualResult = (Models.AuthData) this.controller.GetAuthenticationData(); Assert.True(expectedResult.IsDeepEqual(actualResult)); }
public void TestAuthenticated() { Models.AuthData expectedResult = new Models.AuthData() { IsAuthenticated = true, Token = null, User = new Models.User() { Id = "Username", Name = "Client Name", Email = "*****@*****.**", } }; var mockUser = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Email, "*****@*****.**"), new Claim("preferred_username", "Username"), new Claim("name", "Client Name"), }, "mockAuthType")); // Mock Authentication Service AuthenticateAsync call Mock <IAuthenticationService> mockAuthSvc = new Mock <IAuthenticationService>(); mockAuthSvc .Setup(e => e.AuthenticateAsync(It.IsAny <HttpContext>(), It.IsAny <string>())) .Returns(Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(mockUser, CookieAuthenticationDefaults.AuthenticationScheme)))); // Mock the serviceProvider for the http context var serviceProviderMock = new Mock <IServiceProvider>(); serviceProviderMock .Setup(_ => _.GetService(typeof(IAuthenticationService))) .Returns(mockAuthSvc.Object); // Mock the http context user and RequestServices mockHttpCtx .Setup(e => e.HttpContext.User).Returns(mockUser); mockHttpCtx .Setup(e => e.HttpContext.RequestServices).Returns(serviceProviderMock.Object); // Call actual Authenticate Models.AuthData actualResult = service.GetAuthenticationData(); Assert.True(expectedResult.User.IsDeepEqual(actualResult.User)); Assert.Equal(expectedResult.Token, actualResult.Token); Assert.Equal(expectedResult.IsAuthenticated, actualResult.IsAuthenticated); }
/// <summary> /// Authenticates the request based on the current context. /// </summary> /// <returns>The AuthData containing the token and user information.</returns> public Models.AuthData GetAuthenticationData() { Models.AuthData authData = new Models.AuthData(); ClaimsPrincipal user = this.httpContextAccessor.HttpContext.User; authData.IsAuthenticated = user.Identity.IsAuthenticated; if (authData.IsAuthenticated) { this.logger.LogDebug("Getting Authentication data"); authData.User = new Models.User { Id = user.FindFirstValue("preferred_username"), Name = user.FindFirstValue("name"), Email = user.FindFirstValue(ClaimTypes.Email), }; authData.Token = user.FindFirstValue("access_token"); } return(authData); }
public void TestUnAuthenticated() { Models.AuthData expectedResult = new Models.AuthData() { IsAuthenticated = false, Token = null, User = null, }; var mockUser = new Mock <ClaimsPrincipal>(); mockUser.Setup(u => u.Identity.IsAuthenticated).Returns(false); //var mockUser = new ClaimsPrincipal(new ClaimsIdentity(,Array.Empty<Claim>(), "mockAuthType")); // Mock Authentication Service AuthenticateAsync call Mock <IAuthenticationService> mockAuthSvc = new Mock <IAuthenticationService>(); mockAuthSvc .Setup(e => e.AuthenticateAsync(It.IsAny <HttpContext>(), It.IsAny <string>())) .Returns(Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(mockUser.Object, CookieAuthenticationDefaults.AuthenticationScheme)))); // Mock the serviceProvider for the http context var serviceProviderMock = new Mock <IServiceProvider>(); serviceProviderMock .Setup(_ => _.GetService(typeof(IAuthenticationService))) .Returns(mockAuthSvc.Object); // Mock the http context user and RequestServices mockHttpCtx .Setup(e => e.HttpContext.User).Returns(mockUser.Object); mockHttpCtx .Setup(e => e.HttpContext.RequestServices).Returns(serviceProviderMock.Object); // Call actual Authenticate Models.AuthData actualResult = service.GetAuthenticationData(); Assert.True(expectedResult.User.IsDeepEqual(actualResult.User)); Assert.Equal(expectedResult.Token, actualResult.Token); Assert.Equal(expectedResult.IsAuthenticated, actualResult.IsAuthenticated); }
public Models.AuthData GetAuthenticationData() { Models.AuthData authData = this.authSvc.GetAuthenticationData(); return(authData); }