Skip to content

Commit

Permalink
Hide symmetric keys on connection page + Create Copy to Clipboard but…
Browse files Browse the repository at this point in the history
…ton (#382)

* not display Symmetric key

* Use Clipboard service to write to clipboard

* Add unit test to Clipboard Service

* Not_display_Symmetric_key_suite

* not_display_symmetric_key_suite2

Co-authored-by: crib <[email protected]>
Co-authored-by: Beaugrand, Kevin <[email protected]>
  • Loading branch information
3 people authored Mar 8, 2022
1 parent 38489f1 commit 0c6efa6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using AzureIoTHub.Portal.Client.Services;
using Microsoft.JSInterop;
using Moq;
using NUnit.Framework;
using System;
using System.Linq;
using Microsoft.JSInterop.Infrastructure;

namespace AzureIoTHub.Portal.Server.Tests.Unit.Services
{
[TestFixture]
public class ClipboardServiceTests
{
private MockRepository mockRepository;

private Mock<IJSRuntime> mockJSRuntime;

[SetUp]
public void SetUp()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockJSRuntime = this.mockRepository.Create<IJSRuntime>();
}

private ClipboardService CreateService()
{
return new ClipboardService(
this.mockJSRuntime.Object);
}

[Test]
public void WriteTextAsync_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
string text = Guid.NewGuid().ToString();

// Act
var result = service.WriteTextAsync(text);

// Assert
this.mockJSRuntime.Verify(c => c.InvokeAsync<IJSVoidResult>(
It.Is<string>(x => x == "navigator.clipboard.writeText"),
It.Is<object?[]>(x => x.Single().ToString() == text)), Times.Once);

this.mockRepository.VerifyAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@using Microsoft.AspNetCore.Authorization
@using AzureIoTHub.Portal.Client.Services
@using Microsoft.AspNetCore.Authorization
@using AzureIoTHub.Portal.Shared.Models.V10

@inject HttpClient Http
@inject ISnackbar Snackbar
@inject ClipboardService ClipboardService

<div class="ConnectionString-dialog">
<MudDialog>
Expand All @@ -20,15 +22,15 @@
<MudGrid>
<MudItem xs="12">
<MudText Style="text-decoration:underline"><b>Service Endpoint</b></MudText>
<MudTextField @bind-Value="@Credentials.ProvisioningEndpoint" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" />
<MudTextField @bind-Value="@Credentials.ProvisioningEndpoint" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true"Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.ContentCopy" OnAdornmentClick="@(() => ClipboardService.WriteTextAsync(Credentials.ProvisioningEndpoint))"/>
</MudItem>
<MudItem Class="mt-0" xs="12">
<MudText Style="text-decoration:underline"><b>Device Id</b></MudText>
<MudTextField @bind-Value="@Credentials.RegistrationID" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" />
<MudTextField @bind-Value="@Credentials.RegistrationID" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.ContentCopy" OnAdornmentClick="@(() => ClipboardService.WriteTextAsync(Credentials.RegistrationID))" />
</MudItem>
<MudItem Class="mt-0" xs="12">
<MudText Style="text-decoration:underline"><b>Symmetric Key</b></MudText>
<MudTextField @bind-Value="@Credentials.SymmetricKey" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" />
<MudTextField @bind-Value="@Credentials.SymmetricKey" Variant="Variant.Text" InputType="InputType.Password" Margin="Margin.Dense" ReadOnly="true" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.ContentCopy" OnAdornmentClick="@(() => ClipboardService.WriteTextAsync(Credentials.SymmetricKey))" />
</MudItem>
</MudGrid>
</MudCardContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@using Microsoft.AspNetCore.Authorization
@using AzureIoTHub.Portal.Client.Services
@using Microsoft.AspNetCore.Authorization
@using AzureIoTHub.Portal.Shared.Models.V10

@inject HttpClient Http
@inject ISnackbar Snackbar
@inject ClipboardService ClipboardService

<div class="ConnectionString-dialog">
<MudDialog>
Expand All @@ -20,15 +22,15 @@
<MudGrid>
<MudItem xs="12">
<MudText Style="text-decoration:underline"><b>Service Endpoint</b></MudText>
<MudTextField @bind-Value="@Credentials.ProvisioningEndpoint" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" />
<MudTextField @bind-Value="@Credentials.ProvisioningEndpoint" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.ContentCopy" OnAdornmentClick="@(() => ClipboardService.WriteTextAsync(Credentials.ProvisioningEndpoint))" />
</MudItem>
<MudItem Class="mt-0" xs="12">
<MudText Style="text-decoration:underline"><b>Device Id</b></MudText>
<MudTextField @bind-Value="@Credentials.RegistrationID" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" />
<MudTextField @bind-Value="@Credentials.RegistrationID" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.ContentCopy" OnAdornmentClick="@(() => ClipboardService.WriteTextAsync(Credentials.RegistrationID))" />
</MudItem>
<MudItem Class="mt-0" xs="12">
<MudText Style="text-decoration:underline"><b>Symmetric Key</b></MudText>
<MudTextField @bind-Value="@Credentials.SymmetricKey" Class="mt-0" Variant="Variant.Text" Margin="Margin.Dense" ReadOnly="true" />
<MudTextField @bind-Value="@Credentials.SymmetricKey" Variant="Variant.Text" InputType="InputType.Password" Margin="Margin.Dense" ReadOnly="true" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.ContentCopy" OnAdornmentClick="@(() => ClipboardService.WriteTextAsync(Credentials.SymmetricKey))" />
</MudItem>
</MudGrid>
</MudCardContent>
Expand Down Expand Up @@ -72,5 +74,6 @@
}
}


void Cancel() => MudDialog.Cancel();
}
3 changes: 2 additions & 1 deletion src/AzureIoTHub.Portal/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AzureIoTHub.Portal.Client
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

using AzureIoTHub.Portal.Client.Services;
using AzureIoTHub.Portal.Shared.Settings;
using Blazored.Modal;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
Expand Down Expand Up @@ -36,6 +36,7 @@ public static async Task Main(string[] args)
builder.Services.AddBlazoredModal();

builder.Services.AddMudServices();
builder.Services.AddScoped<ClipboardService>();

await ConfigureOidc(builder);

Expand Down
23 changes: 23 additions & 0 deletions src/AzureIoTHub.Portal/Client/Services/ClipboardService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Client.Services
{
using Microsoft.JSInterop;
using System.Threading.Tasks;

public class ClipboardService
{
private readonly IJSRuntime _jsRuntime;

public ClipboardService(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}

public ValueTask WriteTextAsync(string text)
{
return _jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", text);
}
}
}

0 comments on commit 0c6efa6

Please sign in to comment.