Пример #1
0
        public void ClearAzureProfileClearsCustomProfile()
        {
            string subscriptionDataFile = "C:\\foo.json";

            ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
            // Setup
            ProfileClient client = new ProfileClient(subscriptionDataFile);

            client.AddOrSetAccount(azureAccount);
            client.AddOrSetEnvironment(azureEnvironment);
            client.AddOrSetSubscription(azureSubscription1);
            client.Profile.Save();

            cmdlt.CommandRuntime       = commandRuntimeMock;
            cmdlt.Force                = new SwitchParameter(true);
            cmdlt.SubscriptionDataFile = subscriptionDataFile;

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            // Verify
            client = new ProfileClient(subscriptionDataFile);
            Assert.Equal(0, client.Profile.Subscriptions.Count);
            Assert.Equal(0, client.Profile.Accounts.Count);
            Assert.Equal(2, client.Profile.Environments.Count); //only default environments
        }
Пример #2
0
        public void ClearAzureProfileClearsTokenCache()
        {
            string cacheFileName = Path.Combine(AzurePowerShell.ProfileDirectory, "TokenCache.dat");
            ProtectedFileTokenCache tokenCache = ProtectedFileTokenCache.Instance;

            tokenCache.HasStateChanged = true;

            // HACK: Do not look at this code
            TokenCacheNotificationArgs args = new TokenCacheNotificationArgs();

            typeof(TokenCacheNotificationArgs).GetProperty("ClientId").SetValue(args, "123");
            typeof(TokenCacheNotificationArgs).GetProperty("Resource").SetValue(args, "123");
            typeof(TokenCacheNotificationArgs).GetProperty("TokenCache").SetValue(args, tokenCache);
            typeof(TokenCacheNotificationArgs).GetProperty("UniqueId").SetValue(args, "*****@*****.**");
            typeof(TokenCacheNotificationArgs).GetProperty("DisplayableId").SetValue(args, "*****@*****.**");
            AuthenticationResult authenticationResult =
                typeof(AuthenticationResult).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
                                                            null, new Type[] { typeof(string), typeof(string), typeof(string), typeof(DateTimeOffset) }, null).Invoke(new object[]
            {
                "foo", "123", "123",
                new DateTimeOffset(DateTime.Now.AddDays(1))
            }) as AuthenticationResult;
            var storeToCache = typeof(TokenCache).GetMethod("StoreToCache", BindingFlags.Instance | BindingFlags.NonPublic);

            storeToCache.Invoke(tokenCache,
                                new object[] { authenticationResult, "Common", "123", "123", 0 });

            tokenCache.AfterAccess.Invoke(args);

            Assert.Equal(1, tokenCache.ReadItems().Count());

            ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();

            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Force          = new SwitchParameter(true);

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            // Verify
            Assert.Equal(0, tokenCache.ReadItems().Count());
        }