public async Task CannotCheckoutWithEmptyCart(SliceFixture fixture)
        {
            // Arrange
            // Create a user
            var registerUserCommand = new Register.Command
            {
                FirstName = "John",
                UserName  = "******",
                Password  = "******"
            };

            await fixture.SendAsync(registerUserCommand);

            // Act
            var user = await fixture
                       .ExecuteDbContextAsync(db => db
                                              .Users
                                              .FirstOrDefaultAsync(u => u.UserName == registerUserCommand.UserName));

            bool canCheckout = true;

            // Get the custom validator and check whether it allows for checkout
            await fixture.ExecuteScopeAsync(async sp =>
            {
                canCheckout = await sp.GetService <IOrderValidator>()
                              .UserHasItemsInCartAsync(user.UserName, CancellationToken.None);
            });

            // Assert
            canCheckout.ShouldBeFalse();
        }
Esempio n. 2
0
        public async Task FindsIfNameIsTaken(SliceFixture fixture)
        {
            // Arrange
            var registerUserCommand = new Register.Command
            {
                FirstName = "John",
                UserName  = "******",
                Password  = "******"
            };

            await fixture.SendAsync(registerUserCommand);

            // Act
            var anotherRegisterCommand = new Register.Command
            {
                FirstName = "Mark",
                UserName  = "******",
                Password  = "******"
            };

            var nameNotTaken = true;

            await fixture.ExecuteScopeAsync(async sp =>
            {
                nameNotTaken = await sp.GetService <IUserValidator>()
                               .NameNotTakenAsync(anotherRegisterCommand.UserName, CancellationToken.None);
            });

            nameNotTaken.ShouldBeFalse();
        }
Esempio n. 3
0
        public async Task CanRegister(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var userManager  = (UserManager <User>)sp.GetService(typeof(UserManager <User>));
                    var usersService = new UsersService(userManager, GetFakeSignInManager(userManager), GetFakeRoleManager(sp), db);

                    var userToRegister = new User
                    {
                        FirstName = "Peter",
                        LastName  = "McDonald",
                        UserName  = "******"
                    };

                    // Act
                    var registeredSuccessfully = await usersService.RegisterAsync(userToRegister, "somePassword123");

                    // Assert
                    registeredSuccessfully.ShouldBeTrue();

                    var registeredUser = await usersService.FindByUserNameAsync(userToRegister.UserName);

                    registeredUser.ShouldNotBeNull();
                    registeredUser.FirstName.ShouldBe(userToRegister.FirstName);
                    registeredUser.LastName.ShouldBe(userToRegister.LastName);
                });
            });
        }
Esempio n. 4
0
        public async Task CorrectlyDetectsIfDepartmentExists(SliceFixture fixture)
        {
            //Arrange
            var department = new Department
            {
                Name      = "Physics",
                StartDate = new DateTime(2013, 01, 01),
                Budget    = 12903.00m
            };

            await fixture.InsertAsync(department);

            //Act
            bool departmentExists = false;

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
            await fixture.ExecuteScopeAsync(async sp =>
            {
                departmentExists = sp.GetService <ICoursesValidator>()
                                   .DepartmentExistsInDb(department.Name);
            });

#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously

            //Assert
            departmentExists.ShouldBeTrue();
        }
Esempio n. 5
0
        public async Task CanFindByUserName(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var registerUserCommand = new Register.Command
                    {
                        UserName  = "******",
                        Password  = "******",
                        FirstName = "John",
                        LastName  = "Smith"
                    };

                    await fixture.SendAsync(registerUserCommand);

                    var expectedUser = await db
                                       .Users
                                       .FirstOrDefaultAsync(u => u.UserName == registerUserCommand.UserName);

                    var userManager  = (UserManager <User>)sp.GetService(typeof(UserManager <User>));
                    var usersService = new UsersService(userManager, GetFakeSignInManager(userManager), GetFakeRoleManager(sp), db);

                    // Act
                    var actualUser = await usersService.FindByUserNameAsync(registerUserCommand.UserName);

                    // Assert
                    actualUser.UserName.ShouldBe(expectedUser.UserName);
                    actualUser.FirstName.ShouldBe(expectedUser.FirstName);
                    actualUser.LastName.ShouldBe(expectedUser.LastName);
                });
            });
        }
Esempio n. 6
0
        public async Task CanUpdate(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var registerUserCommand = new Register.Command
                    {
                        UserName = "******",
                        Password = "******"
                    };

                    await fixture.SendAsync(registerUserCommand);

                    var user = await db
                               .Users
                               .AsNoTracking()
                               .FirstOrDefaultAsync(u => u.UserName == registerUserCommand.UserName);

                    var userManager  = (UserManager <User>)sp.GetService(typeof(UserManager <User>));
                    var usersService = new UsersService(userManager, GetFakeSignInManager(userManager), GetFakeRoleManager(sp), db);

                    // Act
                    user.UserName    = "******";
                    var updateResult = await usersService.UpdateAsync(user);

                    // Assert
                    updateResult.ShouldBeTrue();

                    var userAfterUpdate = await usersService.FindByUserNameAsync(user.UserName);
                    userAfterUpdate.UserName.ShouldBe(user.UserName);
                });
            });
        }
Esempio n. 7
0
        public async Task PasswordMatchesConfirmation(SliceFixture fixture)
        {
            // Act
            var passwordsMatch = false;
            var password       = "******";

            await fixture.ExecuteScopeAsync(async sp =>
            { // That Task.Run() is actually unnessecary, but it is there to avoid the warning message
                await Task.Run(() =>
                {
                    passwordsMatch = sp.GetService <IUserValidator>()
                                     .PasswordMatchesConfirmation(password, password);
                });
            });

            passwordsMatch.ShouldBeTrue();
        }
Esempio n. 8
0
        public async Task CantLoginWithInvalidAccount(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var userManager  = GetFakeUserManager(sp);
                    var usersService = new UsersService(userManager, GetFakeSignInManager(userManager), GetFakeRoleManager(sp), db);

                    // Act
                    var result = await usersService.LoginAsync("unexisting", "user");

                    // Assert
                    result.ShouldBeFalse();
                });
            });
        }
Esempio n. 9
0
        public async Task CanLogout(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var userManager   = GetFakeUserManager(sp);
                    var signInManager = GetFakeSignInManager(userManager);

                    var usersService = new UsersService(userManager, signInManager, GetFakeRoleManager(sp), db);

                    // Act
                    await usersService.LogoutAsync();

                    // Assert
                    A.CallTo(() => signInManager.SignOutAsync()).MustHaveHappened();
                });
            });
        }
        public async Task CannotCreateCategoryWithTheSameName(SliceFixture fixture)
        {
            // Arrange
            var category = new Category
            {
                Name = "Category"
            };

            await fixture.InsertAsync(category);

            // Act
            bool categoryDoesntExist = true;

            await fixture.ExecuteScopeAsync(async sp =>
            {
                categoryDoesntExist = await sp.GetService <ICategoryValidator>()
                                      .CategoryDoesntExistAsync(category.Name, CancellationToken.None);
            });

            // Assert
            categoryDoesntExist.ShouldBeFalse();
        }
Esempio n. 11
0
        public async Task CanLogin(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var registerUserCommand = new Register.Command
                    {
                        UserName = "******",
                        Password = "******"
                    };

                    await fixture.SendAsync(registerUserCommand);

                    var user = await db
                               .Users
                               .FirstOrDefaultAsync(u => u.UserName == registerUserCommand.UserName);

                    var userManager = GetFakeUserManager(sp);
                    A.CallTo(() => userManager.FindByNameAsync(registerUserCommand.UserName)).Returns(user);

                    var signInManager = GetFakeSignInManager(userManager);

                    A.CallTo(() => signInManager.PasswordSignInAsync(user, registerUserCommand.Password, false, false))
                    .Returns(SignInResult.Success);

                    var usersService = new UsersService(userManager, signInManager, GetFakeRoleManager(sp), db);

                    // Act
                    var result = await usersService.LoginAsync(registerUserCommand.UserName, registerUserCommand.Password);

                    // Assert
                    result.ShouldBeTrue();
                });
            });
        }
Esempio n. 12
0
        public async Task CanGetRoles(SliceFixture fixture)
        {
            await fixture.ExecuteScopeAsync(async sp =>
            {
                await fixture.ExecuteDbContextAsync(async db =>
                {
                    // Arrange
                    var role = await AddSampleRoleToDatabaseAsync(fixture, db);

                    var registerUserCommand = new Register.Command
                    {
                        UserName = "******",
                        Password = "******"
                    };

                    await fixture.SendAsync(registerUserCommand);

                    var user = await db
                               .Users
                               .FirstOrDefaultAsync(u => u.UserName == registerUserCommand.UserName);

                    await AssignRoleToUserAsync(fixture, role, user);

                    var userManager  = (UserManager <User>)sp.GetService(typeof(UserManager <User>));
                    var usersService = new UsersService(userManager, GetFakeSignInManager(userManager), GetFakeRoleManager(sp), db);

                    // Act
                    var roles = await usersService.GetRolesAsync(user.Id);

                    // Assert
                    roles.Count().ShouldBe(1);
                    roles.First().Id.ShouldBe(role.Id);
                    roles.First().Name.ShouldBe(role.Name);
                });
            });
        }