示例#1
0
        public void List_Empty_Secrets_File()
        {
            var projectPath   = UserSecretHelper.GetTempSecretProject();
            var logger        = new TestLogger(_runtimeEnv);
            var secretManager = new Program(_runtimeEnv)
            {
                Logger = logger
            };

            secretManager.Run(new string[] { "list", "-p", projectPath });
            Assert.Equal(1, logger.Messages.Count);
            Assert.Contains(Resources.Error_No_Secrets_Found, logger.Messages);
        }
        public void Throws_If_Project_Json_Does_Not_Contain_UserSecretId()
        {
            var projectPath = UserSecretHelper.GetTempSecretProject();

            File.WriteAllText(Path.Combine(projectPath, "project.json"), "{}");

            Assert.Throws <InvalidOperationException>(() =>
            {
                PathHelper.GetSecretsPath(projectPath);
            });

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }
示例#3
0
        public void Remove_Non_Existing_Secret()
        {
            var projectPath   = UserSecretHelper.GetTempSecretProject();
            var logger        = new TestLogger(_runtimeEnv);
            var secretManager = new Program(_runtimeEnv)
            {
                Logger = logger
            };

            secretManager.Run(new string[] { "remove", "secret1", "-p", projectPath });
            Assert.Equal(1, logger.Messages.Count);
            Assert.Contains("Cannot find 'secret1' in the secret store.", logger.Messages);
        }
        public void Throws_If_Project_Json_Not_Found()
        {
            var projectPath = UserSecretHelper.GetTempSecretProject();

            File.Delete(Path.Combine(projectPath, "project.json"));

            Assert.Throws <InvalidOperationException>(() =>
            {
                PathHelper.GetSecretsPath(projectPath);
            });

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }
        public void Throws_If_UserSecretId_Contains_Invalid_Characters()
        {
            var projectPath = UserSecretHelper.GetTempSecretProject();

            foreach (var character in Path.GetInvalidPathChars())
            {
                UserSecretHelper.SetTempSecretInProject(projectPath, "Test" + character);
                Assert.Throws <InvalidOperationException>(() =>
                {
                    PathHelper.GetSecretsPath(projectPath);
                });
            }

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }
        public void Gives_Correct_Secret_Path()
        {
            string userSecretsId;
            var    projectPath      = UserSecretHelper.GetTempSecretProject(out userSecretsId);
            var    actualSecretPath = PathHelper.GetSecretsPath(projectPath);

            var root = Environment.GetEnvironmentVariable("APPDATA") ??         // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\
                       Environment.GetEnvironmentVariable("HOME");              // On Mac/Linux it goes to ~/.microsoft/usersecrets/

            var expectedSecretPath = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPDATA"))?
                                     Path.Combine(root, "Microsoft", "UserSecrets", userSecretsId, "secrets.json") :
                                     Path.Combine(root, ".microsoft", "usersecrets", userSecretsId, "secrets.json");

            Assert.Equal(expectedSecretPath, actualSecretPath);

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }
示例#7
0
        public void SetSecret_With_Verbose_Flag()
        {
            var projectPath   = UserSecretHelper.GetTempSecretProject();
            var logger        = new TestLogger(_runtimeEnv, debug: true);
            var secretManager = new Program(_runtimeEnv)
            {
                Logger = logger
            };

            secretManager.Run(new string[] { "-v", "set", "secret1", "value1", "-p", projectPath });
            Assert.Equal(3, logger.Messages.Count);
            Assert.Contains(string.Format("Project file path {0}.", projectPath), logger.Messages);
            Assert.Contains(string.Format("Secrets file path {0}.", PathHelper.GetSecretsPath(projectPath)), logger.Messages);
            Assert.Contains("Successfully saved secret1 = value1 to the secret store.", logger.Messages);
            logger.Messages.Clear();

            secretManager.Run(new string[] { "-v", "list", "-p", projectPath });
            Assert.Equal(3, logger.Messages.Count);
            Assert.Contains(string.Format("Project file path {0}.", projectPath), logger.Messages);
            Assert.Contains(string.Format("Secrets file path {0}.", PathHelper.GetSecretsPath(projectPath)), logger.Messages);
            Assert.Contains("secret1 = value1", logger.Messages);

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }
示例#8
0
        public void SetSecret_Update_Existing_Secret()
        {
            var projectPath   = UserSecretHelper.GetTempSecretProject();
            var logger        = new TestLogger(_runtimeEnv);
            var secretManager = new Program(_runtimeEnv)
            {
                Logger = logger
            };

            secretManager.Run(new string[] { "set", "secret1", "value1", "-p", projectPath });
            Assert.Equal(1, logger.Messages.Count);
            Assert.Contains("Successfully saved secret1 = value1 to the secret store.", logger.Messages);
            secretManager.Run(new string[] { "set", "secret1", "value2", "-p", projectPath });
            Assert.Equal(2, logger.Messages.Count);
            Assert.Contains("Successfully saved secret1 = value2 to the secret store.", logger.Messages);

            logger.Messages.Clear();

            secretManager.Run(new string[] { "list", "-p", projectPath });
            Assert.Equal(1, logger.Messages.Count);
            Assert.Contains("secret1 = value2", logger.Messages);

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }
示例#9
0
        private void SetSecrets(bool fromCurrentDirectory)
        {
            var secrets = new KeyValuePair <string, string>[]
            {
                new KeyValuePair <string, string>("key1", Guid.NewGuid().ToString()),
                new KeyValuePair <string, string>("Facebook:AppId", Guid.NewGuid().ToString()),
                new KeyValuePair <string, string>(@"key-@\/.~123!#$%^&*())-+==", @"key-@\/.~123!#$%^&*())-+=="),
                new KeyValuePair <string, string>("key2", string.Empty)
            };

            var projectPath = UserSecretHelper.GetTempSecretProject();

            if (fromCurrentDirectory)
            {
                Directory.SetCurrentDirectory(projectPath);         // Point current directory to the project.json directory.
            }

            var logger        = new TestLogger(_runtimeEnv);
            var secretManager = new Program(_runtimeEnv)
            {
                Logger = logger
            };

            foreach (var secret in secrets)
            {
                var parameters = fromCurrentDirectory ?
                                 new string[] { "set", secret.Key, secret.Value } :
                new string[] { "set", secret.Key, secret.Value, "-p", projectPath };
                secretManager.Run(parameters);
            }

            Assert.Equal(4, logger.Messages.Count);

            foreach (var keyValue in secrets)
            {
                Assert.Contains(
                    string.Format("Successfully saved {0} = {1} to the secret store.", keyValue.Key, keyValue.Value),
                    logger.Messages);
            }

            logger.Messages.Clear();
            var args = fromCurrentDirectory ?
                       new string[] { "list" } : new string[] { "list", "-p", projectPath };

            secretManager.Run(args);
            Assert.Equal(4, logger.Messages.Count);
            foreach (var keyValue in secrets)
            {
                Assert.Contains(
                    string.Format("{0} = {1}", keyValue.Key, keyValue.Value),
                    logger.Messages);
            }

            // Remove secrets.
            logger.Messages.Clear();
            foreach (var secret in secrets)
            {
                var parameters = fromCurrentDirectory ?
                                 new string[] { "remove", secret.Key } :
                new string[] { "remove", secret.Key, "-p", projectPath };
                secretManager.Run(parameters);
            }

            // Verify secrets are removed.
            logger.Messages.Clear();
            args = fromCurrentDirectory ?
                   new string[] { "list" } : new string[] { "list", "-p", projectPath };
            secretManager.Run(args);
            Assert.Equal(1, logger.Messages.Count);
            Assert.Contains(Resources.Error_No_Secrets_Found, logger.Messages);

            UserSecretHelper.DeleteTempSecretProject(projectPath);
        }