Example #1
0
        public TwoFactorAuthService_Tests()
        {
            // Arrange
            var mockOptionsAccessor    = Substitute.For <IOptions <IdentityOptions> >();
            var mockPasswordHasher     = Substitute.For <IPasswordHasher <UserModel> >();
            var mockUserValidators     = Substitute.For <IEnumerable <IUserValidator <UserModel> > >();
            var mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >();
            var mockKeyNormalizer      = Substitute.For <ILookupNormalizer>();
            var mockErrors             = Substitute.For <IdentityErrorDescriber>();
            var mockServices           = Substitute.For <IServiceProvider>();
            var mockUserLogger         = Substitute.For <ILogger <UserManager <UserModel> > >();
            var mockConfiguration      = Substitute.For <IConfiguration>();
            var a3SContextFake         = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>());
            var customUserStoreFake    = new CustomUserStoreFake(a3SContextFake, mockConfiguration);

            customUserManagerFake = new CustomUserManagerFake(customUserStoreFake, mockOptionsAccessor, mockPasswordHasher, mockUserValidators, mockPasswordValidators, mockKeyNormalizer,
                                                              mockErrors, mockServices, mockUserLogger);

            mockedUserModel = new UserModel()
            {
                Id         = Guid.NewGuid().ToString(),
                UserTokens = new List <UserTokenModel>()
                {
                    new UserTokenModel(),
                    new UserTokenModel(),
                    new UserTokenModel(),
                }
            };

            twoFactorAuthOTP = new TwoFactorAuthOTP()
            {
                UserId = Guid.NewGuid(),
                OTP    = "232020"
            };
        }
        public CustomUserManager_Tests()
        {
            var fakeA3SContext = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>());

            mockConfiguration    = Substitute.For <IConfiguration>();
            fakesCustomUserStore = new CustomUserStoreFake(fakeA3SContext, mockConfiguration);

            mockOptionsAccessor    = Substitute.For <IOptions <IdentityOptions> >();
            mockPasswordHasher     = Substitute.For <IPasswordHasher <UserModel> >();
            mockUserValidators     = Substitute.For <IEnumerable <IUserValidator <UserModel> > >();
            mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >();
            mockKeyNormalizer      = Substitute.For <ILookupNormalizer>();
            mockErrors             = Substitute.For <IdentityErrorDescriber>();
            mockServices           = Substitute.For <IServiceProvider>();
            mockUserLogger         = Substitute.For <ILogger <UserManager <UserModel> > >();
        }
        public TermsOfServiceController_Tests()
        {
            var mockOptionsAccessor    = Substitute.For <IOptions <IdentityOptions> >();
            var mockPasswordHasher     = Substitute.For <IPasswordHasher <UserModel> >();
            var mockUserValidators     = Substitute.For <IEnumerable <IUserValidator <UserModel> > >();
            var mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >();
            var mockKeyNormalizer      = Substitute.For <ILookupNormalizer>();
            var mockErrors             = Substitute.For <IdentityErrorDescriber>();
            var mockServices           = Substitute.For <IServiceProvider>();
            var mockUserLogger         = Substitute.For <ILogger <UserManager <UserModel> > >();
            var fakeA3SContext         = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>());

            mockConfiguration = Substitute.For <IConfiguration>();
            var fakesCustomUserStore = new CustomUserStoreFake(fakeA3SContext, mockConfiguration);

            fakeUserManager = new CustomUserManagerFake(fakesCustomUserStore, mockOptionsAccessor, mockPasswordHasher, mockUserValidators, mockPasswordValidators, mockKeyNormalizer,
                                                        mockErrors, mockServices, mockUserLogger);

            var mockContextAccessor = Substitute.For <IHttpContextAccessor>();
            var mocClaimsFactory    = Substitute.For <IUserClaimsPrincipalFactory <UserModel> >();
            var mockSignInLogger    = Substitute.For <ILogger <SignInManager <UserModel> > >();
            var mockLdapAuthenticationModeRepository = Substitute.For <LdapAuthenticationModeRepository>(fakeA3SContext, mockConfiguration);
            var mockLdapConnectionService            = Substitute.For <ILdapConnectionService>();

            mockAuthenticationSchemeProvider = Substitute.For <IAuthenticationSchemeProvider>();
            var mockUserConfirmation = Substitute.For <IUserConfirmation <UserModel> >();

            fakeSignInManager = new CustomSignInManagerFake <UserModel>(fakeUserManager, mockContextAccessor, mocClaimsFactory, mockOptionsAccessor, mockSignInLogger, fakeA3SContext,
                                                                        mockAuthenticationSchemeProvider, mockLdapAuthenticationModeRepository, mockLdapConnectionService, mockUserConfirmation);

            termsOfServiceRepository             = Substitute.For <ITermsOfServiceRepository>();
            mockClientStore                      = Substitute.For <IClientStore>();
            mockEventService                     = Substitute.For <IEventService>();
            mockIdentityServerInteractionService = Substitute.For <IIdentityServerInteractionService>();

            userModel = new UserModel()
            {
                UserName = "******",
                Id       = Guid.NewGuid().ToString()
            };

            termsOfServiceModel = new TermsOfServiceModel()
            {
                AgreementName = "Test agreement",
                Version       = "2019.1",
                HtmlContents  = "<html />",
                CssContents   = ".my-class { font-size: 2; }"
            };

            // Prepare controller contexts
            authorizationRequest = new AuthorizationRequest()
            {
                IdP       = "testIdp",
                ClientId  = "clientId1",
                LoginHint = "LoginHint"
            };

            client = new Client()
            {
                EnableLocalLogin = true
            };
        }
Example #4
0
        public IdentityWithAdditionalClaimsProfileService_Tests()
        {
            var mockOptionsAccessor    = Substitute.For <IOptions <IdentityOptions> >();
            var mockPasswordHasher     = Substitute.For <IPasswordHasher <UserModel> >();
            var mockUserValidators     = Substitute.For <IEnumerable <IUserValidator <UserModel> > >();
            var mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >();
            var mockKeyNormalizer      = Substitute.For <ILookupNormalizer>();
            var mockErrors             = Substitute.For <IdentityErrorDescriber>();
            var mockServices           = Substitute.For <IServiceProvider>();
            var mockUserLogger         = Substitute.For <ILogger <UserManager <UserModel> > >();
            var fakeA3SContext         = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>());

            mockConfiguration = Substitute.For <IConfiguration>();
            var fakesCustomUserStore = new CustomUserStoreFake(fakeA3SContext, mockConfiguration);

            fakeUserManager = new CustomUserManagerFake(fakesCustomUserStore, mockOptionsAccessor, mockPasswordHasher, mockUserValidators, mockPasswordValidators, mockKeyNormalizer,
                                                        mockErrors, mockServices, mockUserLogger);

            mockUserClaimsPrincipalFactory = Substitute.For <IUserClaimsPrincipalFactory <UserModel> >();
            mockLogger            = Substitute.For <ILogger <IdentityWithAdditionalClaimsProfileService> >();
            mockProfileRepository = Substitute.For <IProfileRepository>();
            mockApplicationDataPolicyRepository = Substitute.For <IApplicationDataPolicyRepository>();
            mockPermissionRepository            = Substitute.For <IPermissionRepository>();
            mockTeamRepository = Substitute.For <ITeamRepository>();

            var id = Guid.NewGuid().ToString();

            profileDataRequestContext = new ProfileDataRequestContext()
            {
                Subject = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, "example name"),
                    new Claim(ClaimTypes.NameIdentifier, id),
                    new Claim("sub", id),
                    new Claim("custom-claim", "example claim value"),
                }, "mock")),
                Client = new Client()
                {
                    ClientName = "mockClient"
                },
                RequestedClaimTypes = new List <string>()
                {
                    ClaimTypes.Name,
                    ClaimTypes.NameIdentifier,
                    "sub",
                    "custom-claim"
                },
                Caller = "mockCaller"
            };

            isActiveContext = new IsActiveContext(
                new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, "example name"),
                new Claim(ClaimTypes.NameIdentifier, id),
                new Claim("sub", id),
                new Claim("custom-claim", "example claim value"),
            }, "mock")),
                new Client()
            {
                ClientName = "mockClient"
            },
                "mockCaller");

            userModel = new UserModel()
            {
                UserName  = "******",
                Id        = id,
                Email     = "temp@local",
                FirstName = "Temp",
                Surname   = "User"
            };
        }
        public AccountController_Tests()
        {
            var mockOptionsAccessor    = Substitute.For <IOptions <IdentityOptions> >();
            var mockPasswordHasher     = Substitute.For <IPasswordHasher <UserModel> >();
            var mockUserValidators     = Substitute.For <IEnumerable <IUserValidator <UserModel> > >();
            var mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >();
            var mockKeyNormalizer      = Substitute.For <ILookupNormalizer>();
            var mockErrors             = Substitute.For <IdentityErrorDescriber>();
            var mockServices           = Substitute.For <IServiceProvider>();
            var mockUserLogger         = Substitute.For <ILogger <UserManager <UserModel> > >();
            var fakeA3SContext         = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>());

            mockConfiguration = Substitute.For <IConfiguration>();
            var fakesCustomUserStore = new CustomUserStoreFake(fakeA3SContext, mockConfiguration);

            fakeUserManager = new CustomUserManagerFake(fakesCustomUserStore, mockOptionsAccessor, mockPasswordHasher, mockUserValidators, mockPasswordValidators, mockKeyNormalizer,
                                                        mockErrors, mockServices, mockUserLogger);

            var mockContextAccessor = Substitute.For <IHttpContextAccessor>();
            var mocClaimsFactory    = Substitute.For <IUserClaimsPrincipalFactory <UserModel> >();
            var mockSignInLogger    = Substitute.For <ILogger <SignInManager <UserModel> > >();
            var mockLdapAuthenticationModeRepository = Substitute.For <LdapAuthenticationModeRepository>(fakeA3SContext, mockConfiguration);
            var mockLdapConnectionService            = Substitute.For <ILdapConnectionService>();

            mockAuthenticationSchemeProvider = Substitute.For <IAuthenticationSchemeProvider>();
            var mockUserConfirmation = Substitute.For <IUserConfirmation <UserModel> >();

            fakeSignInManager = new CustomSignInManagerFake <UserModel>(fakeUserManager, mockContextAccessor, mocClaimsFactory, mockOptionsAccessor, mockSignInLogger, fakeA3SContext,
                                                                        mockAuthenticationSchemeProvider, mockLdapAuthenticationModeRepository, mockLdapConnectionService, mockUserConfirmation);

            mockIdentityServerInteractionService = Substitute.For <IIdentityServerInteractionService>();
            mockClientStore  = Substitute.For <IClientStore>();
            mockEventService = Substitute.For <IEventService>();
            urlTestEncoder   = new UrlTestEncoder();

            // Prepare controller contexts
            authorizationRequest = new AuthorizationRequest()
            {
                IdP       = "testIdp",
                ClientId  = "clientId1",
                LoginHint = "LoginHint"
            };

            var mockAuthenticationHandler = Substitute.For <IAuthenticationHandler>();

            mockAuthenticationSchemeProvider.GetAllSchemesAsync()
            .Returns(new List <AuthenticationScheme>()
            {
                new AuthenticationScheme("testName", AccountOptions.WindowsAuthenticationSchemeName, mockAuthenticationHandler.GetType())
            });

            client = new Client()
            {
                EnableLocalLogin = true
            };

            client.IdentityProviderRestrictions.Add(AccountOptions.WindowsAuthenticationSchemeName);
            mockClientStore.FindEnabledClientByIdAsync(Arg.Any <string>()).Returns(client);

            userModel = new UserModel()
            {
                UserName = "******",
                Id       = Guid.NewGuid().ToString()
            };
        }