Skip to content

Commit

Permalink
feat: Added methods for SetCookiesEnabled and GetCookiesEnabled (#172)
Browse files Browse the repository at this point in the history
* Added methods for SetCookiesEnabled and GetCookiesEnabled

---------

Co-authored-by: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com>
  • Loading branch information
BradFallows and IvanJosipovic committed Feb 17, 2023
1 parent 2310582 commit b3e56eb
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build:
name: Create Release
runs-on: ubuntu-latest
timeout-minutes: 10
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
9 changes: 8 additions & 1 deletion src/BlazorApplicationInsights.Sample/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@
User ID: <span id="UserId">@UserId</span>
<br />
<button class="btn btn-primary" @onclick="GetSessionId" id="GetSessionId">Get Session Id</button>
Session ID: <span id="SessionId">@SessionId</span>
Session ID: <span id="SessionId">@SessionId</span>
<br />
<button class="btn btn-primary" @onclick="EnableCookies" id="EnableCookies">Enable Cookies</button>
<br />
<button class="btn btn-primary" @onclick="DisableCookies" id="DisableCookies">Disable Cookies</button>
<br />
<button class="btn btn-primary" @onclick="GetCookiesEnabled" id="GetCookiesEnabled">Get Cookies Enabled</button>
Cookies Enabled: <span id="CookiesEnabled">@CookiesEnabled</span>
17 changes: 17 additions & 0 deletions src/BlazorApplicationInsights.Sample/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public partial class Index
private string UserId = string.Empty;
private string SessionId = string.Empty;

private bool? CookiesEnabled;

private async Task TrackEvent()
{
await AppInsights.TrackEvent("My Event", new Dictionary<string, object>() {{"customProperty", "customValue"}});
Expand Down Expand Up @@ -140,5 +142,20 @@ private async Task GetSessionId()
{
this.SessionId = await AppInsights.GetSessionId();
}

private async Task EnableCookies()
{
await AppInsights.SetCookiesEnabled(true);
}

private async Task DisableCookies()
{
await AppInsights.SetCookiesEnabled(false);
}

private async Task GetCookiesEnabled()
{
CookiesEnabled = await AppInsights.GetCookiesEnabled();
}
}
}
56 changes: 51 additions & 5 deletions src/BlazorApplicationInsights.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public async Task Test(string id, int timeout, List<AIRequestObject> expectedCal
&& x.tags.ContainsKey("ai.cloud.roleInstance")
&& x.tags["ai.cloud.roleInstance"] == "Blazor Wasm")
.ToArray()[i];

var compare = CompareObjects(expectedCall, call);

if (compare)
Expand Down Expand Up @@ -544,6 +544,11 @@ internal bool CompareObjects(object sourceObj, object desinationObj)
[InlineData("StartStopTrackEvent")]
[InlineData("TrackHttpRequest")]
[InlineData("SetInstrumentationKey")]
[InlineData("GetUserId")]
[InlineData("GetSessionId")]
[InlineData("EnableCookies")]
[InlineData("DisableCookies")]
[InlineData("GetCookiesEnabled")]
public async Task TestBlocked(string id)
{
bool hasError = false;
Expand Down Expand Up @@ -580,7 +585,7 @@ public async Task TestBlocked(string id)

hasError.Should().Be(false);
}

[Fact]
public async Task GetUserId_ShouldReturnAGeneratedValue_WhenNotSet()
{
Expand Down Expand Up @@ -609,10 +614,10 @@ public async Task GetUserId_ShouldReturnSetUserId_WhenSet()

await page.GotoAsync(BaseAddress);
await page.ClickAsync("#SetAuthenticatedUserContext");

await page.WaitForTimeoutAsync(1000);
await page.ClickAsync("#GetUserId");

await page.WaitForTimeoutAsync(1000);

var userId = (await page.Locator("#UserId").AllInnerTextsAsync()).FirstOrDefault();
Expand All @@ -637,6 +642,47 @@ public async Task GetSessionId_ShouldReturnAGeneratedValue()

sessionId.Should().NotBeNullOrEmpty();
}


[Fact]
public async Task SetCookiesEnabled_False_ShouldDisableCookies()
{
using var playwright = await Playwright.CreateAsync();

await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() { Headless = Headless });
var page = await browser.NewPageAsync(new BrowserNewPageOptions() { IgnoreHTTPSErrors = true });

await page.GotoAsync(BaseAddress);
await page.ClickAsync("#DisableCookies");

await page.WaitForTimeoutAsync(1000);

await page.ClickAsync("#GetCookiesEnabled");
var enabled = (await page.Locator("#CookiesEnabled").AllInnerTextsAsync()).FirstOrDefault();

enabled.Should().Be("False");
}


[Fact]
public async Task SetCookiesEnabled_True_ShouldEnableCookies()
{
using var playwright = await Playwright.CreateAsync();

await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() { Headless = Headless });
var page = await browser.NewPageAsync(new BrowserNewPageOptions() { IgnoreHTTPSErrors = true });

await page.GotoAsync(BaseAddress);
await page.ClickAsync("#DisableCookies");
await page.WaitForTimeoutAsync(1000);

await page.ClickAsync("#EnableCookies");
await page.WaitForTimeoutAsync(1000);

await page.ClickAsync("#GetCookiesEnabled");
var enabled = (await page.Locator("#CookiesEnabled").AllInnerTextsAsync()).FirstOrDefault();

enabled.Should().Be("True");
}

}
}
8 changes: 8 additions & 0 deletions src/BlazorApplicationInsights/ApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public async Task<string> GetUserId()
public async Task<string> GetSessionId()
=> await _jsRuntime.InvokeAsync<string>("blazorApplicationInsights.getSessionId");

/// <inheritdoc />
public async Task SetCookiesEnabled(bool enabled)
=> await _jsRuntime.InvokeVoidAsync("blazorApplicationInsights.setCookiesEnabled", enabled);

/// <inheritdoc />
public async Task<bool> GetCookiesEnabled()
=> await _jsRuntime.InvokeAsync<bool>("blazorApplicationInsights.getCookiesEnabled");

private class NoOpJSRuntime : IJSRuntime
{
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object?[]? args) => Invoked<TValue>();
Expand Down
13 changes: 13 additions & 0 deletions src/BlazorApplicationInsights/IApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,18 @@ public interface IApplicationInsights
/// </summary>
/// <returns></returns>
Task<string> GetUserId();

/// <summary>
/// Sets whether cookies are enabled
/// </summary>
/// <param name="enabled"></param>
/// <returns></returns>
Task SetCookiesEnabled(bool enabled);

/// <summary>
/// Gets whether cookies are enabled
/// </summary>
/// <returns></returns>
Task<bool> GetCookiesEnabled();
}
}
20 changes: 18 additions & 2 deletions src/BlazorApplicationInsights/wwwroot/JsInterop.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,25 @@
}
},
getUserId: function () {
return appInsights.context.user.authenticatedId || appInsights.context.user.id;
if (appInsights.context !== undefined) {
return appInsights.context.user.authenticatedId || appInsights.context.user.id;
}
},
getSessionId: function () {
return appInsights.context.sessionManager.automaticSession.id;
if (appInsights.context !== undefined) {
return appInsights.context.sessionManager.automaticSession.id;
}
},
setCookiesEnabled: function (enabled) {
if (appInsights.core !== undefined) {
appInsights.core.getCookieMgr().setEnabled(enabled);
}
},
getCookiesEnabled: function () {
if (appInsights.core !== undefined) {
return appInsights.core.getCookieMgr().isEnabled();
} else {
return false;
}
},
};

0 comments on commit b3e56eb

Please sign in to comment.