示例#1
0
        public async Task Admin_ClientPasswordOperationsTest()
        {
            var userId = 85;

            var accountRepository     = container.Resolve <IAccountRepository>();
            var accountRepositoryMock = Mock.Get(accountRepository);

            var tokenRegistryRepository     = container.Resolve <ITokenRegistrationRepository>();
            var tokenRegistryRepositoryMock = Mock.Get(tokenRegistryRepository);

            var passwordGenerator = container.Resolve <IPasswordGenerator>();
            var passwordMock      = Mock.Get(passwordGenerator);

            passwordMock.Setup(p => p.CheckPassword(
                                   It.IsAny <string>(),
                                   It.IsAny <string>()))
            .Returns(true);

            var messageTemplateRepository = container.Resolve <IMessageTemplateRepository>();
            var messageTemplateMock       = Mock.Get(messageTemplateRepository);

            messageTemplateMock
            .Setup(r => r.GetTemplate(
                       It.IsAny <MessageTemplateName>(),
                       It.IsAny <CultureInfo>(),
                       It.IsAny <MessageTemplateType>(),
                       It.IsAny <int>()))
            .Returns <MessageTemplateName, CultureInfo, MessageTemplateType, int>((n, l, t, e) => Task.FromResult("{0} {1}"));


            accountRepositoryMock
            .Setup(x => x.FindByUserId(It.IsAny <int>()))
            .ReturnsAsync(new Account {
                Id = userId
            });

            accountRepositoryMock
            .Setup(x => x.FindByPhone(It.IsAny <decimal>()))
            .Returns <decimal>(x =>
                               Task.FromResult(new Account {
                Phone = x
            }));

            accountRepositoryMock
            .Setup(x => x.FindByLogin(It.IsAny <string>()))
            .Returns <string>(x =>
                              Task.FromResult(new Account {
                Login = x
            }));


            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer",
                                                  TestContainerBuilder.BuildToken(container, userId, TokenClaims.CLAIM_PERMISSIONS_BASIC));

                var client = new Domain0Client(TEST_URL, http);

                await client.ChangePasswordAsync(new ChangePasswordRequest("new", "old"));

                accountRepositoryMock
                .Verify(x =>
                        x.FindByUserId(It.Is <int>(id => id == userId)),
                        Times.Once);
                tokenRegistryRepositoryMock.Verify(x => x.RevokeByUserId(userId), Times.Exactly(1));

                await client.ChangeMyPasswordAsync(new ChangePasswordRequest("new", "old"));

                accountRepositoryMock
                .Verify(x =>
                        x.FindByUserId(It.Is <int>(id => id == userId)),
                        Times.Exactly(2));
                tokenRegistryRepositoryMock.Verify(x => x.RevokeByUserId(userId), Times.Exactly(2));

                var resetPhone = 123;
                await client.RequestResetPasswordAsync(resetPhone);

                accountRepositoryMock
                .Verify(x => x.FindByPhone(It.Is <decimal>(phone => phone == resetPhone)), Times.Once);

                var resetEmail = "email";
                await client.RequestResetPasswordByEmailAsync(new RegisterRequest("email"));

                accountRepositoryMock
                .Verify(x => x.FindByLogin(It.Is <string>(email => email == resetEmail)), Times.Once);
            }
        }