public void GetSystemClient_considers_configuration(string configurationName)
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);
            var filesystemMock  = SetupEnvironmentAndFileSystemWithClientKey(environmentMock);

            var lockPath = configurationName == "local" ? "identity" : "zero";

            environmentMock.Setup(x => x.IsOsPlatform(It.Is <OSPlatform>(p => p == OSPlatform.Windows))).Returns(true);
            environmentMock.Setup(x => x.IsWindowsAdminUser).Returns(true);

            filesystemMock.Setup(x =>
                                 x.OpenText(It.Is <string>(p => p.EndsWith($"{lockPath}{Path.DirectorySeparatorChar}.lock"))))
            .Returns(() => new StringReader($"{{\"processName\":\"TestingEryph\",\"processId\":100,\"endpoints\":{{\"identity\":\"http://localhost\"}}}}"));

            environmentMock.Setup(x => x.IsProcessRunning("TestingEryph", 100)).Returns(true);

            var lookup = new ClientCredentialsLookup(environmentMock.Object);

            if (configurationName != "zero" && configurationName != "local")
            {
                Assert.Throws <InvalidOperationException>(() => lookup.GetSystemClientCredentials(configurationName));
            }
            else
            {
                var systemClient = lookup.GetSystemClientCredentials(configurationName);
                Assert.NotNull(systemClient);
            }

            filesystemMock.Verify();
        }
        public void GetSystemClient_Throws_if_not_AdminOn_Windows()
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);

            environmentMock.Setup(x => x.IsOsPlatform(It.Is <OSPlatform>(p => p == OSPlatform.Windows))).Returns(true);
            environmentMock.Setup(x => x.IsWindowsAdminUser).Returns(false);

            var lookup = new ClientCredentialsLookup(environmentMock.Object);

            Assert.Throws <InvalidOperationException>(() => lookup.GetSystemClientCredentials());
        }
        public void GetSystemClient_returns_null_if_process_not_running(bool forEryphZero)
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);
            var filesystemMock  = SetupEnvironmentAndFileSystemWithClientKey(environmentMock);

            var moduleName = forEryphZero ? "zero" : "identity";

            filesystemMock.Setup(x =>
                                 x.OpenText(It.Is <string>(p => p.EndsWith($"{moduleName}{Path.DirectorySeparatorChar}.run_info"))))
            .Returns(new StringReader("{\"process_id\" : 100, \"url\" : \"http://eryph.io\"}"));


            environmentMock.Setup(x => x.IsProcessRunning("", 100)).Returns(false);

            var lookup = new ClientCredentialsLookup(environmentMock.Object);

            Assert.Null(lookup.GetSystemClientCredentials());
        }
        public void GetSystemClient_reads_process_info(string baseUrl, string identityEndpoint)
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);
            var filesystemMock  = SetupEnvironmentAndFileSystemWithClientKey(environmentMock);


            filesystemMock.Setup(x =>
                                 x.OpenText(It.Is <string>(p => p.EndsWith(".lock"))))
            .Returns(() => new StringReader($"{{\"processName\":\"TestingEryph\",\"processId\":100,\"endpoints\":{{\"identity\":\"{baseUrl}\"}}}}"));

            environmentMock.Setup(x => x.IsProcessRunning("TestingEryph", 100)).Returns(true);

            var lookup   = new ClientCredentialsLookup(environmentMock.Object);
            var response = lookup.GetSystemClientCredentials();

            Assert.NotNull(response);
            Assert.Equal(identityEndpoint, response.IdentityProvider.ToString());
            Assert.NotNull(response.KeyPairData);
            Assert.Equal("system-client", response.Id);

            environmentMock.Verify();
            filesystemMock.Verify();
        }