コード例 #1
0
        public async Task GetAllRoles_Returns_Roles()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "InMemoryDatabase")
                          .Options;

            var mockUserInfoProvider = new MockUserInfoProvider("Test User");

            // Setup some data
            using (var context = new ApplicationDbContext(options, mockUserInfoProvider))
            {
                context.Roles.Add(new Role("TestUser")
                {
                    Name        = "SomeRole",
                    Description = "SomeRole"
                });
                await context.SaveChangesAsync();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options, mockUserInfoProvider))
            {
                // Arrange
                var service = new RolesService(context, mockUserInfoProvider);

                // Act
                var roles = (await service.GetRoles()).ToList();

                // Assert
                Assert.Equal(1, roles.Count);
                Assert.Equal("SomeRole", roles.First().Name);
                Assert.Equal("TestUser", roles.First().CreatedBy);
            }
        }
コード例 #2
0
        public async Task RegisterUser_Creates_And_Returns_User()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "InMemoryDatabase")
                          .Options;

            var tester           = "Tester";
            var encrypter        = new Encrypter();
            var userInfoProvider = new MockUserInfoProvider(tester);

            var mockRegisterUserPolicy       = new Mock <IPolicy <RegisterUserModel> >();
            var mockRegisterDomainUserPolicy = new Mock <IPolicy <RegisterDomainUserModel> >();
            var updateUserPolicy             = new Mock <IPolicy <UpdateUserModel> >();

            var mockResetPasswordPolicy        = new Mock <IPolicy <ResetPasswordModel> >();
            var mockPasswordResetRequestPolicy = new Mock <IPolicy <PasswordResetRequestModel> >();
            var mockPasswordPolicy             = new Mock <IPolicy <PasswordModel> >();
            var mockCommsService      = new Mock <ICommsService>();
            var mockConfigurationRoot = new Mock <IConfigurationRoot>();

            var newUser = new RegisterUserModel
            {
                Email    = "*****@*****.**",
                Password = "******",
                Username = "******"
            };

            mockRegisterUserPolicy.Setup(service => service.IsValid(It.IsAny <RegisterUserModel>())).Returns(Task.FromResult(true));

            // Make sure there is no user with username
            using (var context = new ApplicationDbContext(options, userInfoProvider))
            {
                // PreAssert
                var user = await context.Users.FirstOrDefaultAsync(u => u.Username == newUser.Username);

                Assert.Null(user);
            }

            // Register user through domain service
            using (var context = new ApplicationDbContext(options, userInfoProvider))
            {
                // Arrange
                var service = new UsersService(context,
                                               encrypter,
                                               userInfoProvider,
                                               mockRegisterUserPolicy.Object,
                                               mockRegisterDomainUserPolicy.Object,
                                               updateUserPolicy.Object,
                                               mockPasswordResetRequestPolicy.Object,
                                               mockResetPasswordPolicy.Object,
                                               mockPasswordPolicy.Object,
                                               mockCommsService.Object,
                                               mockConfigurationRoot.Object
                                               );

                // Act
                var registeredUser = (await service.RegisterUser(newUser));

                Assert.NotNull(registeredUser);
            }

            // Check that domain user was registered
            using (var context = new ApplicationDbContext(options, userInfoProvider))
            {
                var dbUser = await context
                             .Users
                             .FirstOrDefaultAsync(u => u.Username == newUser.Username);

                // Assert
                Assert.NotNull(dbUser);
                Assert.Equal(newUser.Username, dbUser.Username);
                Assert.Equal(newUser.Email, dbUser.Email);
                Assert.NotEqual(newUser.Password, dbUser.PasswordHash);
                Assert.Equal(tester, dbUser.CreatedBy);
            }
        }