public async Task WhenUpdateAccountSettingsCalled_ItShouldUpdateTheDatabase()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new UpdateAccountSettingsDbStatement(testDatabase, this.userManager.Object, this.requestSnapshot);
                await this.CreateUserAsync(testDatabase);
                await testDatabase.TakeSnapshotAsync();

                var hashedNewPassword = this.newPassword.Value + "1";
                this.passwordHasher.Setup(v => v.HashPassword(this.newPassword.Value)).Returns(hashedNewPassword);

                var expectedUser                = await this.GetUserAsync(testDatabase);
                expectedUser.UserName           = this.newUsername.Value;
                expectedUser.Email              = this.newEmail.Value;
                expectedUser.EmailConfirmed     = false;
                expectedUser.PasswordHash       = hashedNewPassword;
                expectedUser.ProfileImageFileId = this.newFileId.Value;
                expectedUser.SecurityStamp      = this.securityStamp;

                var result = await this.target.ExecuteAsync(
                    this.userId,
                    this.newUsername,
                    this.newEmail,
                    this.newPassword,
                    this.newFileId,
                    this.securityStamp);

                Assert.AreEqual(false, result.EmailConfirmed);

                return(new ExpectedSideEffects
                {
                    Update = expectedUser
                });
            });
        }
        public async Task WhenUpdateAccountSettingsCalledTwice_ItShouldBeIdempotent()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new UpdateAccountSettingsDbStatement(testDatabase, this.userManager.Object, this.requestSnapshot);
                await this.CreateUserAsync(testDatabase);

                var hashedNewPassword = this.newPassword.Value + "1";
                this.passwordHasher.Setup(v => v.HashPassword(this.newPassword.Value)).Returns(hashedNewPassword);

                var result = await this.target.ExecuteAsync(
                    this.userId,
                    this.newUsername,
                    this.newEmail,
                    this.newPassword,
                    this.newFileId,
                    this.securityStamp);

                await testDatabase.TakeSnapshotAsync();

                var result2 = await this.target.ExecuteAsync(
                    this.userId,
                    this.newUsername,
                    this.newEmail,
                    this.newPassword,
                    this.newFileId,
                    this.securityStamp);

                Assert.AreEqual(result, result2);

                return(ExpectedSideEffects.None);
            });
        }
        public async Task WhenUpdateAccountSettingsCalledWithSameEMailAndEmailWasNotConfirmed_ItShouldStillNotBeConfirmed()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new UpdateAccountSettingsDbStatement(testDatabase, this.userManager.Object, this.requestSnapshot);
                await this.CreateUserAsync(testDatabase, false);
                await testDatabase.TakeSnapshotAsync();

                var expectedUser                = await this.GetUserAsync(testDatabase);
                expectedUser.UserName           = this.newUsername.Value;
                expectedUser.ProfileImageFileId = this.newFileId.Value;
                expectedUser.SecurityStamp      = this.securityStamp;

                var result = await this.target.ExecuteAsync(
                    this.userId,
                    this.newUsername,
                    ValidEmail.Parse(this.email.Value),
                    null,
                    this.newFileId,
                    this.securityStamp);

                Assert.AreEqual(false, result.EmailConfirmed);

                return(new ExpectedSideEffects
                {
                    Update = expectedUser
                });
            });
        }
        public async Task WhenUpdateAccountSettingsCalled_ItShouldAbortUpdateIfSnapshotFails()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new UpdateAccountSettingsDbStatement(testDatabase, this.userManager.Object, this.requestSnapshot);
                await this.CreateUserAsync(testDatabase);
                await testDatabase.TakeSnapshotAsync();

                var hashedNewPassword = this.newPassword.Value + "1";
                this.passwordHasher.Setup(v => v.HashPassword(this.newPassword.Value)).Returns(hashedNewPassword);

                var expectedUser                = await this.GetUserAsync(testDatabase);
                expectedUser.UserName           = this.newUsername.Value;
                expectedUser.Email              = this.newEmail.Value;
                expectedUser.EmailConfirmed     = false;
                expectedUser.PasswordHash       = hashedNewPassword;
                expectedUser.ProfileImageFileId = this.newFileId.Value;
                expectedUser.SecurityStamp      = this.securityStamp;

                this.requestSnapshot.ThrowException();

                await ExpectedException.AssertExceptionAsync <SnapshotException>(
                    () => this.target.ExecuteAsync(
                        this.userId,
                        this.newUsername,
                        this.newEmail,
                        this.newPassword,
                        this.newFileId,
                        this.securityStamp));

                return(ExpectedSideEffects.TransactionAborted);
            });
        }
        public void Initialize()
        {
            this.passwordHasher = new Mock <IPasswordHasher>();
            this.userManager    = new Mock <IUserManager>();
            this.userManager.Setup(v => v.PasswordHasher).Returns(this.passwordHasher.Object);
            this.requestSnapshot = new MockRequestSnapshotService();

            // Required for non-database tests.
            this.target = new UpdateAccountSettingsDbStatement(new Mock <IFifthweekDbConnectionFactory>(MockBehavior.Strict).Object, this.userManager.Object, this.requestSnapshot);
        }