Esempio n. 1
0
        private static ArgumentSyntax DefineCommand(string args)
        {
            var cmd = new AccountUpdateCommand(
                NoopSettings(), (u, k) => new Mock <IAcmeContext>().Object, new FileUtil());

            Assert.Equal(CommandGroup.Account.Command, cmd.Group.Command);
            return(ArgumentSyntax.Parse(args.Split(' '), syntax =>
            {
                syntax.HandleErrors = false;
                syntax.DefineCommand("noop");
                cmd.Define(syntax);
            }));
        }
        public async Task CanProcessCommand()
        {
            var serverUri = new Uri("http://acme.com/d");
            var email     = "*****@*****.**";
            var acctLoc   = new Uri("http://acme.com/a/11");
            var acct      = new Account
            {
                Status = AccountStatus.Valid
            };

            var settingsMock = new Mock <IUserSettings>(MockBehavior.Strict);

            settingsMock.Setup(m => m.GetDefaultServer()).ReturnsAsync(serverUri);
            settingsMock.Setup(m => m.GetAccountKey(serverUri)).ReturnsAsync(GetKeyV2());

            var acctCtxMock = new Mock <IAccountContext>(MockBehavior.Strict);

            acctCtxMock.SetupGet(m => m.Location).Returns(acctLoc);
            acctCtxMock.Setup(m => m.Update(new[] { $"mailto://{email}" }, true))
            .ReturnsAsync(acct);

            var ctxMock = new Mock <IAcmeContext>(MockBehavior.Strict);

            ctxMock.Setup(m => m.GetDirectory()).ReturnsAsync(MockDirectoryV2);
            ctxMock.Setup(m => m.Account()).ReturnsAsync(acctCtxMock.Object);

            var fileMock = new Mock <IFileUtil>(MockBehavior.Strict);

            var(console, stdOutput, errOutput) = MockConsole();

            var cmd = new AccountUpdateCommand(
                settingsMock.Object, (u, k) => ctxMock.Object, fileMock.Object);
            var command = cmd.Define();

            await command.InvokeAsync($"update {email} ", console.Object);

            Assert.True(errOutput.Length == 0, errOutput.ToString());
            var ret = JsonConvert.DeserializeObject(stdOutput.ToString());

            Assert.Equal(
                JsonConvert.SerializeObject(new
            {
                location = acctLoc,
                resource = acct
            }, JsonSettings),
                JsonConvert.SerializeObject(ret));

            acctCtxMock.Verify(m => m.Update(new[] { $"mailto://{email}" }, true), Times.Once);
        }