From 231cad31cf76bab57fbf6ed318a6f78a7a373680 Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Wed, 27 Jun 2018 13:33:12 -0700 Subject: [PATCH 1/9] Updated documentation for issue #40 --- src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md b/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md index c888e991..e0139d88 100644 --- a/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md +++ b/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md @@ -47,6 +47,7 @@ Get-PowerBIDataset [-Scope ] [-First ] [-Skip ] ## DESCRIPTION Retrieves a list of Power BI datasets that match the specified search criteria and scope. Before you run this command, make sure you log in using Connect-PowerBIServiceAccount. +For -Scope Individual, datasets are returned from "My Workspace" unless -Workspace or -WorkspaceId is specified. ## EXAMPLES @@ -127,7 +128,7 @@ Accept wildcard characters: False ``` ### -Scope -Indicates scope of the call. Individual returns only datasets assigned to the caller; Organization returns all datasets within a tenant (must be an administrator to initiate). Individual is the default. +Indicates scope of the call. Individual returns datasets from "My Workspace" unless -Workspace or -WorkspaceId is specified which shows datasets under the workspace assigned to the caller; Organization returns all datasets within a tenant (must be an administrator to initiate). Individual is the default. ```yaml Type: PowerBIUserScope From d6cc9052b3f42ef0769fa164812419c45b25ef7f Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Wed, 27 Jun 2018 14:16:47 -0700 Subject: [PATCH 2/9] Implemented issue #39, added -Tenant to Connect-PowerBIServiceAccount --- .../ConnectPowerBIServiceAccount.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs b/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs index 63c08892..91b78856 100644 --- a/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs +++ b/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs @@ -32,7 +32,6 @@ public class ConnectPowerBIServiceAccount : PowerBICmdlet [Parameter(Mandatory = false)] public PowerBIEnvironmentType Environment { get; set; } = PowerBIEnvironmentType.Public; - [Parameter(ParameterSetName = UserParameterSet, Mandatory = false)] [Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = true)] public PSCredential Credential { get; set; } @@ -45,6 +44,12 @@ public class ConnectPowerBIServiceAccount : PowerBICmdlet [Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = true)] [Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = true)] public SwitchParameter ServicePrincipal { get; set; } + + [Alias("TenantId")] + [Parameter(ParameterSetName = UserParameterSet, Mandatory = false)] + [Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = false)] + [Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = false)] + public string Tenant { get; set; } #endregion #region Constructors @@ -56,6 +61,13 @@ public ConnectPowerBIServiceAccount(IPowerBICmdletInitFactory init) : base(init) public override void ExecuteCmdlet() { var environment = this.Settings.Environments[this.Environment]; + if(!string.IsNullOrEmpty(this.Tenant)) + { + var tempEnvironment = (PowerBIEnvironment) environment; + tempEnvironment.AzureADAuthority = tempEnvironment.AzureADAuthority.ToLowerInvariant().Replace("/common/", $"/{this.Tenant}/"); + this.Logger.WriteVerbose($"Updated Azure AD authority with -Tenant specified, new value: {tempEnvironment.AzureADAuthority}"); + environment = tempEnvironment; + } this.Authenticator.Challenge(); // revoke any previous login IAccessToken token = null; From 081e2c2f84873acfa5ba8f22354ab8b3773fffd3 Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Wed, 27 Jun 2018 15:26:42 -0700 Subject: [PATCH 3/9] Fixing piping dataset into Get-PowerBIDatasouce. The parameter sets were not defined enough so there was no ambiguity between them. --- src/Modules/Data/Commands.Data/GetPowerBIDatasource.cs | 2 ++ .../Data/Commands.Data/help/Get-PowerBIDatasource.md | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Modules/Data/Commands.Data/GetPowerBIDatasource.cs b/src/Modules/Data/Commands.Data/GetPowerBIDatasource.cs index 3b1f905b..e0a557ef 100644 --- a/src/Modules/Data/Commands.Data/GetPowerBIDatasource.cs +++ b/src/Modules/Data/Commands.Data/GetPowerBIDatasource.cs @@ -48,10 +48,12 @@ public class GetPowerBIDatasource : PowerBIClientCmdlet, IUserScope [Alias("DatasourceId")] [Parameter(Mandatory = true, ParameterSetName = IdParameterSetName)] + [Parameter(Mandatory = true, ParameterSetName = ObjectIdParameterSetName)] public Guid Id { get; set; } [Alias("DatasoureName")] [Parameter(Mandatory = true, ParameterSetName = NameParameterSetName)] + [Parameter(Mandatory = true, ParameterSetName = ObjectNameParameterSetName)] public string Name { get; set; } [Parameter(Mandatory = false)] diff --git a/src/Modules/Data/Commands.Data/help/Get-PowerBIDatasource.md b/src/Modules/Data/Commands.Data/help/Get-PowerBIDatasource.md index 753fcef2..6177b091 100644 --- a/src/Modules/Data/Commands.Data/help/Get-PowerBIDatasource.md +++ b/src/Modules/Data/Commands.Data/help/Get-PowerBIDatasource.md @@ -31,12 +31,14 @@ Get-PowerBIDatasource -DatasetId [-WorkspaceId ] -Name [-S ### ObjectAndId ``` -Get-PowerBIDatasource -Dataset [-WorkspaceId ] [-Scope ] [] +Get-PowerBIDatasource -Dataset [-WorkspaceId ] -Id [-Scope ] + [] ``` ### ObjectAndName ``` -Get-PowerBIDatasource -Dataset [-WorkspaceId ] [-Scope ] [] +Get-PowerBIDatasource -Dataset [-WorkspaceId ] -Name [-Scope ] + [] ``` ### ObjectAndList @@ -101,7 +103,7 @@ Id of the data source to return. ```yaml Type: Guid -Parameter Sets: Id +Parameter Sets: Id, ObjectAndId Aliases: DatasourceId Required: True @@ -116,7 +118,7 @@ Name of the data source to return if one exists with that name. Case insensitive ```yaml Type: String -Parameter Sets: Name +Parameter Sets: Name, ObjectAndName Aliases: DatasoureName Required: True From 7e8e2ee113c896adc08c4ca4e50cd0319c3f5233 Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Wed, 27 Jun 2018 15:31:51 -0700 Subject: [PATCH 4/9] Documented -Tenant for issue #39 --- .../help/Connect-PowerBIServiceAccount.md | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md b/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md index d7869b6f..0c40ebfc 100644 --- a/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md +++ b/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md @@ -14,20 +14,19 @@ Log in to the Power BI service. ### User (Default) ``` -Connect-PowerBIServiceAccount [-Environment ] [-Credential ] - [] +Connect-PowerBIServiceAccount [-Environment ] [-Tenant ] [] ``` ### ServicePrincipal ``` Connect-PowerBIServiceAccount [-Environment ] -Credential - [-ServicePrincipal] [] + [-ServicePrincipal] [-Tenant ] [] ``` ### ServicePrincipalCertificate ``` Connect-PowerBIServiceAccount [-Environment ] -CertificateThumbprint - -ApplicationId [-ServicePrincipal] [] + -ApplicationId [-ServicePrincipal] [-Tenant ] [] ``` ## DESCRIPTION @@ -102,18 +101,6 @@ Accept wildcard characters: False ### -Credential PSCredential representing the Azure Active Directory (AAD) application client ID (username) and application secret key (password) to authenticate with a service principal account (-ServicePrincipal). -```yaml -Type: PSCredential -Parameter Sets: User -Aliases: - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - ```yaml Type: PSCredential Parameter Sets: ServicePrincipal @@ -157,6 +144,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Tenant +Tenant name or tenant ID containing the service principal account. If not specified, the 'COMMON' tenant is used. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: TenantId + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). From 844263752d059422eeb47bfc08c5151c191a53fc Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Thu, 28 Jun 2018 13:27:59 -0700 Subject: [PATCH 5/9] Added tests and fixes from tests. * Added dataset non-interactive test * Added interactive piping dataset to get datasource * Adeed non-interactive of getting datasource from dataset object * Added non-interactive test for Connect-PowerBIServiceAccount with -Tenant --- .../TestPowerBICmdletNoClientInitFactory.cs | 74 +++++++++++++++++++ .../GetPowerBIDatasetTests.cs | 27 +++++++ .../GetPowerBIDatasourceTests.cs | 55 ++++++++++++++ .../ConnectPowerBIServiceAccountTests.cs | 29 +++++++- .../ConnectPowerBIServiceAccount.cs | 3 +- .../help/Connect-PowerBIServiceAccount.md | 4 +- 6 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs diff --git a/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs b/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs new file mode 100644 index 00000000..844c0ab1 --- /dev/null +++ b/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs @@ -0,0 +1,74 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.PowerBI.Common.Abstractions; +using Microsoft.PowerBI.Common.Abstractions.Interfaces; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.PowerBI.Commands.Common.Test +{ + public class TestPowerBICmdletNoClientInitFactory : PowerBICmdletInitFactory + { + public IPowerBIProfile Profile { get; set; } + + public TestLogger Logger + { + get + { + if (this.LoggerFactory is TestLoggerFactory logger) + { + return logger.Logger; + } + + return null; + } + } + + public TestPowerBICmdletNoClientInitFactory(bool setProfile, IPowerBIProfile profile = null) : + base(new TestLoggerFactory(), new ModuleDataStorage(), new TestAuthenticator(), new PowerBISettings()) + { + if(setProfile) + { + this.SetProfile(profile); + } + } + + public void SetProfile(IPowerBIProfile profile = null) + { + if (profile == null) + { + this.Profile = new TestProfile(); + } + else if (this.Profile != profile) + { + this.Profile = profile; + } + + this.Storage.SetItem("profile", this.Profile); + } + + public IPowerBIProfile GetProfileFromStorage() + { + if (this.Storage.TryGetItem("profile", out IPowerBIProfile profile)) { + return profile; + } + + return null; + } + + public void AssertExpectedUnitTestResults(IEnumerable expected) + { + Assert.IsFalse(this.Logger.ErrorRecords.Any()); + var results = this.Logger.Output.ToList(); + Assert.AreEqual(expected.Count(), results.Count()); + var castedResults = results.Cast().ToList(); + CollectionAssert.AreEqual(expected.ToList(), castedResults); + } + } +} diff --git a/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasetTests.cs b/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasetTests.cs index e5137993..faeca6c3 100644 --- a/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasetTests.cs +++ b/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasetTests.cs @@ -3,10 +3,16 @@ * Licensed under the MIT License. */ +using System; +using System.Collections.Generic; using System.Management.Automation; using Microsoft.PowerBI.Commands.Common.Test; using Microsoft.PowerBI.Commands.Profile.Test; +using Microsoft.PowerBI.Common.Abstractions; +using Microsoft.PowerBI.Common.Api; +using Microsoft.PowerBI.Common.Api.Datasets; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; namespace Microsoft.PowerBI.Commands.Data { @@ -35,5 +41,26 @@ public void EndToEndGetPowerBIDatasetOrganizationScope() Assert.IsTrue(result.Count > 0); } } + + [TestMethod] + public void GetPowerBIDatasetIndividualScope_ListParameterSet() + { + // Arrange + var expectedDatasets = new List { new Dataset { Id = Guid.NewGuid(), Name = "TestDataset" } }; + var client = new Mock(); + client.Setup(x => x.Datasets.GetDatasets()).Returns(expectedDatasets); + var initFactory = new TestPowerBICmdletInitFactory(client.Object); + var cmdlet = new GetPowerBIDataset(initFactory) + { + Scope = PowerBIUserScope.Individual, + ParameterSet = "List", + }; + + // Act + cmdlet.InvokePowerBICmdlet(); + + // Assert + initFactory.AssertExpectedUnitTestResults(expectedDatasets); + } } } diff --git a/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs b/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs index a7e3aaf7..15dc5622 100644 --- a/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs +++ b/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs @@ -28,6 +28,12 @@ public class GetPowerBIDatasourceTests [TestCategory("SkipWhenLiveUnitTesting")] // Ignore for Live Unit Testing public void EndToEndGetPowerBIDatasourceIndividualScope() { + /* + * Requirement to run test: + * Need at least one dataset containing a datasource assigned to the user logging into the test. + * Update the test with the dataset ID before running. + */ + using (var ps = System.Management.Automation.PowerShell.Create()) { // Arrange @@ -91,6 +97,32 @@ public void EndToEndGetPowerBIDatasourceOrganizationScopeAndWorkspaceId() } } + [TestMethod] + [TestCategory("Interactive")] + [TestCategory("SkipWhenLiveUnitTesting")] // Ignore for Live Unit Testing + public void EndToEndPipingDatasetIntoGetPowerBIDatasourceIndividualScope() + { + /* + * Requirement to run test: + * Need at least one dataset containing a datasource assigned to the user logging into the test. + */ + + using (var ps = System.Management.Automation.PowerShell.Create()) + { + // Arrange + ProfileTestUtilities.ConnectToPowerBI(ps, nameof(PowerBIEnvironmentType.Public)); + ps.AddCommand(GetPowerBIDatasetTests.GetPowerBIDatasetCmdletInfo).AddCommand(GetPowerBIDatasourceCmdletInfo); + + // Act + var results = ps.Invoke(); + + // Assert + TestUtilities.AssertNoCmdletErrors(ps); + Assert.IsNotNull(results); + Assert.IsTrue(results.Count > 0); + } + } + [TestMethod] public void GetPowerBIDatasourceIndividualScope_ListParameterSet() { @@ -161,5 +193,28 @@ public void GetPowerBIDatasourceOrganizationScope_ListParameterSet() // Assert initFactory.AssertExpectedUnitTestResults(expectedDatasources); } + + [TestMethod] + public void GetPowerBIDatasourceIndividualScope_ObjectAndListParameterSet() + { + // Arrange + var testDataset = new Dataset { Id = Guid.NewGuid(), Name = "TestDataset" }; + var expectedDatasources = new List { new Datasource { DatasourceId = Guid.NewGuid().ToString(), Name = "TestDatasource", GatewayId = Guid.NewGuid().ToString() } }; + var client = new Mock(); + client.Setup(x => x.Datasets.GetDatasources(testDataset.Id, null)).Returns(expectedDatasources); + var initFactory = new TestPowerBICmdletInitFactory(client.Object); + var cmdlet = new GetPowerBIDatasource(initFactory) + { + Scope = PowerBIUserScope.Individual, + Dataset = testDataset, + ParameterSet = "ObjectAndList", + }; + + // Act + cmdlet.InvokePowerBICmdlet(); + + // Assert + initFactory.AssertExpectedUnitTestResults(expectedDatasources); + } } } diff --git a/src/Modules/Profile/Commands.Profile.Test/ConnectPowerBIServiceAccountTests.cs b/src/Modules/Profile/Commands.Profile.Test/ConnectPowerBIServiceAccountTests.cs index 467cc45d..3dc78199 100644 --- a/src/Modules/Profile/Commands.Profile.Test/ConnectPowerBIServiceAccountTests.cs +++ b/src/Modules/Profile/Commands.Profile.Test/ConnectPowerBIServiceAccountTests.cs @@ -3,10 +3,11 @@ * Licensed under the MIT License. */ +using System.Management.Automation; +using System.Security; +using Microsoft.PowerBI.Commands.Common.Test; using Microsoft.PowerBI.Common.Abstractions; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Diagnostics.CodeAnalysis; -using Microsoft.PowerBI.Commands.Common.Test; namespace Microsoft.PowerBI.Commands.Profile.Test { @@ -48,5 +49,29 @@ public void EndToEndInteractiveLogin() Assert.AreEqual(0, results.Count); } } + + [TestMethod] + public void ConnectPowerBIServiceAccountServiceWithTenantId_PrincipalParameterSet() + { + // Arrange + var initFactory = new TestPowerBICmdletNoClientInitFactory(false); + var testTenantName = "test.microsoftonline.com"; + var cmdlet = new ConnectPowerBIServiceAccount(initFactory) + { + Tenant = testTenantName, + ServicePrincipal = true, + Credential = new PSCredential("appId", new SecureString()), + ParameterSet = "ServicePrincipal" + }; + + // Act + cmdlet.InvokePowerBICmdlet(); + + // Assert + var profile = initFactory.GetProfileFromStorage(); + Assert.IsNotNull(profile); + Assert.IsTrue(profile.Environment.AzureADAuthority.Contains(testTenantName)); + initFactory.AssertExpectedUnitTestResults(new[] { profile }); + } } } diff --git a/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs b/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs index 91b78856..d80f1c6f 100644 --- a/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs +++ b/src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs @@ -46,7 +46,6 @@ public class ConnectPowerBIServiceAccount : PowerBICmdlet public SwitchParameter ServicePrincipal { get; set; } [Alias("TenantId")] - [Parameter(ParameterSetName = UserParameterSet, Mandatory = false)] [Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = false)] [Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = false)] public string Tenant { get; set; } @@ -64,7 +63,7 @@ public override void ExecuteCmdlet() if(!string.IsNullOrEmpty(this.Tenant)) { var tempEnvironment = (PowerBIEnvironment) environment; - tempEnvironment.AzureADAuthority = tempEnvironment.AzureADAuthority.ToLowerInvariant().Replace("/common/", $"/{this.Tenant}/"); + tempEnvironment.AzureADAuthority = tempEnvironment.AzureADAuthority.ToLowerInvariant().Replace("/common", $"/{this.Tenant}"); this.Logger.WriteVerbose($"Updated Azure AD authority with -Tenant specified, new value: {tempEnvironment.AzureADAuthority}"); environment = tempEnvironment; } diff --git a/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md b/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md index 0c40ebfc..6ac671f5 100644 --- a/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md +++ b/src/Modules/Profile/Commands.Profile/help/Connect-PowerBIServiceAccount.md @@ -14,7 +14,7 @@ Log in to the Power BI service. ### User (Default) ``` -Connect-PowerBIServiceAccount [-Environment ] [-Tenant ] [] +Connect-PowerBIServiceAccount [-Environment ] [] ``` ### ServicePrincipal @@ -149,7 +149,7 @@ Tenant name or tenant ID containing the service principal account. If not specif ```yaml Type: String -Parameter Sets: (All) +Parameter Sets: ServicePrincipal, ServicePrincipalCertificate Aliases: TenantId Required: False From 7a5370ab7749e737c751f8c4568f2445376350ad Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Thu, 28 Jun 2018 13:49:36 -0700 Subject: [PATCH 6/9] Doc fix for Get-PowerBIDataset --- src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md b/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md index e0139d88..2fd5c404 100644 --- a/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md +++ b/src/Modules/Data/Commands.Data/help/Get-PowerBIDataset.md @@ -128,7 +128,7 @@ Accept wildcard characters: False ``` ### -Scope -Indicates scope of the call. Individual returns datasets from "My Workspace" unless -Workspace or -WorkspaceId is specified which shows datasets under the workspace assigned to the caller; Organization returns all datasets within a tenant (must be an administrator to initiate). Individual is the default. +Indicates scope of the call. Individual returns datasets from "My Workspace" by default. With -Workspace or -WorkspaceId, datasets under the workspace assigned to the caller are returned; Organization returns all datasets within a tenant (must be an administrator to initiate). Individual is the default. ```yaml Type: PowerBIUserScope From e3c69fbd55fffa2fdf931fb8072a88bc9a4218b5 Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Thu, 28 Jun 2018 15:01:32 -0700 Subject: [PATCH 7/9] Addressing PR feedback --- .../TestPowerBICmdletNoClientInitFactory.cs | 6 ++++++ .../GetPowerBIDatasourceTests.cs | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs b/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs index 844c0ab1..ea35183e 100644 --- a/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs +++ b/src/Common/Commands.Common.Test/TestPowerBICmdletNoClientInitFactory.cs @@ -30,6 +30,12 @@ public TestLogger Logger } } + /// + /// Constructor for TestPowerBICmdletNoClientInitFactory. + /// Used by tests\cmdlets that don't need a client. + /// + /// True if profile should be assigned; false otherwise. If testing cmdlets for profile assignment, pass false. + /// Profile to assign, only assigned if setProfile is true. If setProfile is true and profile is null, TestProfile will be constructed for assignment. public TestPowerBICmdletNoClientInitFactory(bool setProfile, IPowerBIProfile profile = null) : base(new TestLoggerFactory(), new ModuleDataStorage(), new TestAuthenticator(), new PowerBISettings()) { diff --git a/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs b/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs index 15dc5622..3ba1a021 100644 --- a/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs +++ b/src/Modules/Data/Commands.Data.Test/GetPowerBIDatasourceTests.cs @@ -31,14 +31,24 @@ public void EndToEndGetPowerBIDatasourceIndividualScope() /* * Requirement to run test: * Need at least one dataset containing a datasource assigned to the user logging into the test. - * Update the test with the dataset ID before running. */ using (var ps = System.Management.Automation.PowerShell.Create()) { // Arrange ProfileTestUtilities.ConnectToPowerBI(ps); - ps.AddCommand(GetPowerBIDatasourceCmdletInfo).AddParameter(nameof(GetPowerBIDatasource.DatasetId), "b077389f-0238-4312-b014-0c6212fc904e"); + ps.AddCommand(GetPowerBIDatasetTests.GetPowerBIDatasetCmdletInfo); + var existingDatasets = ps.Invoke(); + TestUtilities.AssertNoCmdletErrors(ps); + ps.Commands.Clear(); + + if(!existingDatasets.Any()) + { + Assert.Inconclusive("No datasets returned. Verify you have datasets under your logged in user."); + } + + var testDataset = existingDatasets.Select(d => (Dataset)d.BaseObject).FirstOrDefault(); + ps.AddCommand(GetPowerBIDatasourceCmdletInfo).AddParameter(nameof(GetPowerBIDatasource.DatasetId), testDataset.Id.ToString()); // Act var result = ps.Invoke(); @@ -119,6 +129,11 @@ public void EndToEndPipingDatasetIntoGetPowerBIDatasourceIndividualScope() // Assert TestUtilities.AssertNoCmdletErrors(ps); Assert.IsNotNull(results); + if (!results.Any()) + { + Assert.Inconclusive("No datasources returned. Verify you have datasources under your logged in user."); + } + Assert.IsTrue(results.Count > 0); } } From ef314a5c636f1d88b951449c24ff995d8343cce4 Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Thu, 28 Jun 2018 15:32:30 -0700 Subject: [PATCH 8/9] Added a cache layer for getting Global Service environments. More important for tests than end-users but it should reduce test errors due to Global Service issues. --- src/Common/Commands.Common/PowerBISettings.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Common/Commands.Common/PowerBISettings.cs b/src/Common/Commands.Common/PowerBISettings.cs index d8c1ded2..c76f7dcb 100644 --- a/src/Common/Commands.Common/PowerBISettings.cs +++ b/src/Common/Commands.Common/PowerBISettings.cs @@ -21,6 +21,8 @@ public class PowerBISettings : IPowerBISettings { private IConfigurationRoot Configuration { get; } + private static GSEnvironments GlobalServiceEnvironments { get; set; } + public PowerBISettings(IConfigurationBuilder builder = null) { var executingDirectory = this.GetExecutingDirectory(); @@ -83,14 +85,19 @@ public PowerBISettings(IConfigurationBuilder builder = null) public async Task GetGlobalServiceConfig(string clientName = "powerbi-msolap") { - using (var client = new HttpClient()) + if(GlobalServiceEnvironments != null) { - client.DefaultRequestHeaders.Accept.Clear(); - var response = await client.PostAsync("https://api.powerbi.com/powerbi/globalservice/v201606/environments/discover?client=" + clientName, null); - var serializer = new DataContractJsonSerializer(typeof(GSEnvironments)); + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Accept.Clear(); + var response = await client.PostAsync("https://api.powerbi.com/powerbi/globalservice/v201606/environments/discover?client=" + clientName, null); + var serializer = new DataContractJsonSerializer(typeof(GSEnvironments)); - return serializer.ReadObject(await response.Content.ReadAsStreamAsync()) as GSEnvironments; + GlobalServiceEnvironments = serializer.ReadObject(await response.Content.ReadAsStreamAsync()) as GSEnvironments; + } } + + return GlobalServiceEnvironments; } public IDictionary Environments { get; } From 62ac2d072dad3c4f2a9203cd12dce7a47941e1f8 Mon Sep 17 00:00:00 2001 From: Chaz Beck Date: Thu, 28 Jun 2018 15:41:36 -0700 Subject: [PATCH 9/9] Check for GlobalServiceEnvironments is null --- src/Common/Commands.Common/PowerBISettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Commands.Common/PowerBISettings.cs b/src/Common/Commands.Common/PowerBISettings.cs index c76f7dcb..4cb207db 100644 --- a/src/Common/Commands.Common/PowerBISettings.cs +++ b/src/Common/Commands.Common/PowerBISettings.cs @@ -85,7 +85,7 @@ public PowerBISettings(IConfigurationBuilder builder = null) public async Task GetGlobalServiceConfig(string clientName = "powerbi-msolap") { - if(GlobalServiceEnvironments != null) + if(GlobalServiceEnvironments == null) { using (var client = new HttpClient()) {