示例#1
0
        private static ArgumentSyntax DefineCommand(string args)
        {
            var cmd = new OrderFinalizeCommand(
                NoopSettings(), (u, k) => new Mock <IAcmeContext>().Object, new FileUtil(), null);

            Assert.Equal(CommandGroup.Order.Command, cmd.Group.Command);
            return(ArgumentSyntax.Parse(args.Split(' '), syntax =>
            {
                syntax.HandleErrors = false;
                syntax.DefineCommand("noop");
                cmd.Define(syntax);
            }));
        }
示例#2
0
        public async Task CanProcessCommand()
        {
            var orderLoc = new Uri("http://acme.com/o/1");
            var order    = new Order
            {
                Identifiers = new[] {
                    new Identifier {
                        Value = "*.a.com"
                    },
                    new Identifier {
                        Value = "*.b.com"
                    },
                }
            };

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

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

            var orderMock = new Mock <IOrderContext>(MockBehavior.Strict);

            orderMock.SetupGet(m => m.Location).Returns(orderLoc);
            orderMock.Setup(m => m.Resource()).ReturnsAsync(order);
            orderMock.Setup(m => m.Finalize(It.IsAny <byte[]>())).ReturnsAsync(order);

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

            ctxMock.Setup(m => m.GetDirectory()).ReturnsAsync(MockDirectoryV2);
            ctxMock.Setup(m => m.Order(orderLoc)).Returns(orderMock.Object);

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

            var envMock = new Mock <IEnvironmentVariables>(MockBehavior.Strict);

            envMock.Setup(m => m.GetVar(It.IsAny <string>())).Returns((string)null);

            var cmd = new OrderFinalizeCommand(
                settingsMock.Object, (u, k) => ctxMock.Object, fileMock.Object, envMock.Object);

            var     syntax = DefineCommand($"finalize {orderLoc}");
            dynamic ret    = await cmd.Execute(syntax);

            var privateKey = KeyFactory.FromDer(ret.privateKey);

            Assert.NotNull(privateKey);
            Assert.Equal(
                JsonConvert.SerializeObject(new
            {
                location = orderLoc,
                resource = order,
            }),
                JsonConvert.SerializeObject(new
            {
                ret.location,
                ret.resource,
            }));

            orderMock.Verify(m => m.Finalize(It.IsAny <byte[]>()), Times.Once);

            var outPath = "./private-key.pem";

            orderMock.ResetCalls();
            fileMock.Setup(m => m.WriteAllText(outPath, It.IsAny <string>())).Returns(Task.CompletedTask);

            syntax = DefineCommand($"finalize {orderLoc} --dn CN=*.a.com --out {outPath}");
            ret    = await cmd.Execute(syntax);

            Assert.Equal(
                JsonConvert.SerializeObject(new
            {
                location = orderLoc,
                resource = order,
            }),
                JsonConvert.SerializeObject(ret));

            fileMock.Verify(m => m.WriteAllText(outPath, It.IsAny <string>()), Times.Once);
            orderMock.Verify(m => m.Finalize(It.IsAny <byte[]>()), Times.Once);

            var keyPath = "./private-key.pem";

            orderMock.ResetCalls();
            fileMock.Setup(m => m.ReadAllText(keyPath)).ReturnsAsync(GetKeyV2().ToPem());
            syntax = DefineCommand($"finalize {orderLoc} --dn CN=*.a.com --private-key {keyPath}");
            ret    = await cmd.Execute(syntax);

            Assert.Equal(
                JsonConvert.SerializeObject(new
            {
                location = orderLoc,
                resource = order,
            }),
                JsonConvert.SerializeObject(ret));

            orderMock.Verify(m => m.Finalize(It.IsAny <byte[]>()), Times.Once);
        }