Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a RegexMask on command frame to prevent non-hexadecimal characters #949

Merged
merged 7 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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.Server.Tests.Unit.Pages.DevicesModels.LoRaWAN
{
using System.Collections.Generic;
using AzureIoTHub.Portal.Client.Pages.DeviceModels.LoRaWAN;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using Bunit;
using FluentAssertions;
using MudBlazor;
using NUnit.Framework;

[TestFixture]
public class CreateLoRaDeviceModelPageTests : BlazorUnitTest
{
public override void Setup()
{
base.Setup();
}

[Test]
public void CreateLoRaDeviceModelPageShouldBeRenderedProperly()
{
// Arrange
var model = new LoRaDeviceModel();
var commands = new List<DeviceModelCommand>();
var commandValidation = new List<MudForm>()
{
new MudForm(),
new MudForm(),
new MudForm()
};

var cut = RenderComponent<CreateLoraDeviceModel>(
ComponentParameter.CreateParameter("LoRaDeviceModel", model),
ComponentParameter.CreateParameter("Commands", commands),
ComponentParameter.CreateParameter("CommandValidation", commandValidation)
);

cut.WaitForAssertion(() => cut.FindAll(".mud-expand-panel").Count.Should().Be(3));
}

[Test]
public void ClickOnAddShouldAddRow()
{
// Arrange
var model = new LoRaDeviceModel();
var commands = new List<DeviceModelCommand>();
var commandValidation = new List<MudForm>()
{
new MudForm(),
new MudForm(),
new MudForm()
};

var cut = RenderComponent<CreateLoraDeviceModel>(
ComponentParameter.CreateParameter("LoRaDeviceModel", model),
ComponentParameter.CreateParameter("Commands", commands),
ComponentParameter.CreateParameter("CommandValidation", commandValidation)
);

cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(0));
var addButton = cut.WaitForElement("#addButton");
addButton.Click();
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(1));
}

[Test]
public void ClickOnRemoveShouldDeleteRow()
{
// Arrange
var model = new LoRaDeviceModel();
var commands = new List<DeviceModelCommand>();
var commandValidation = new List<MudForm>()
{
new MudForm(),
new MudForm(),
new MudForm()
};

var cut = RenderComponent<CreateLoraDeviceModel>(
ComponentParameter.CreateParameter("LoRaDeviceModel", model),
ComponentParameter.CreateParameter("Commands", commands),
ComponentParameter.CreateParameter("CommandValidation", commandValidation)
);

cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(0));
var addButton = cut.WaitForElement("#addButton");
addButton.Click();
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(1));
var removeButton = cut.WaitForElement("#removeButton");
removeButton.Click();
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(0));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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.Server.Tests.Unit.Pages.DevicesModels.LoRaWAN
{
using System.Collections.Generic;
using AzureIoTHub.Portal.Client.Pages.DeviceModels.LoRaWAN;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using Bunit;
using FluentAssertions;
using MudBlazor;
using NUnit.Framework;

public class EditLoraDeviceModelPageTests : BlazorUnitTest
{
public override void Setup()
{
base.Setup();
}

[Test]
public void CreateLoRaDeviceModelPageShouldBeRenderedProperly()
{
// Arrange
var model = new LoRaDeviceModel();
var commands = new List<DeviceModelCommand>();
var commandValidation = new List<MudForm>()
Fixed Show fixed Hide fixed
{
new MudForm(),
new MudForm(),
new MudForm()
};
Fixed Show fixed Hide fixed

var cut = RenderComponent<EditLoraDeviceModel>(
ComponentParameter.CreateParameter("LoRaDeviceModel", model),
ComponentParameter.CreateParameter("Commands", commands)
);

cut.WaitForAssertion(() => cut.FindAll(".mud-expand-panel").Count.Should().Be(3));
}

[Test]
public void ClickOnAddShouldAddRow()
{
// Arrange
var model = new LoRaDeviceModel();
var commands = new List<DeviceModelCommand>();
var commandValidation = new List<MudForm>()
Fixed Show fixed Hide fixed
{
new MudForm(),
new MudForm(),
new MudForm()
};
Fixed Show fixed Hide fixed

var cut = RenderComponent<EditLoraDeviceModel>(
ComponentParameter.CreateParameter("LoRaDeviceModel", model),
ComponentParameter.CreateParameter("Commands", commands)
);

cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(0));
var addButton = cut.WaitForElement("#addButton");
addButton.Click();
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(1));
}

[Test]
public void ClickOnRemoveShouldDeleteRow()
{
// Arrange
var model = new LoRaDeviceModel();
var commands = new List<DeviceModelCommand>();
var commandValidation = new List<MudForm>()
Fixed Show fixed Hide fixed
{
new MudForm(),
new MudForm(),
new MudForm()
};
Fixed Show fixed Hide fixed

var cut = RenderComponent<EditLoraDeviceModel>(
ComponentParameter.CreateParameter("LoRaDeviceModel", model),
ComponentParameter.CreateParameter("Commands", commands)
);

cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(0));
var addButton = cut.WaitForElement("#addButton");
addButton.Click();
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(1));
var removeButton = cut.WaitForElement("#removeButton");
removeButton.Click();
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@
</HeaderContent>
<RowTemplate Context="CommandContext">
<MudTd DataLabel="Name" Style="word-break: break-all;">
<MudTextField @bind-Value="@CommandContext.Name" Label="Name" For="@(() => CommandContext.Name)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin"></MudTextField>
<MudTextField id="@nameof(DeviceModelCommand.Name)" @bind-Value="@CommandContext.Name" Label="Name" For="@(() => CommandContext.Name)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin"></MudTextField>
</MudTd>
<MudTd DataLabel="Frame" Style="word-break: break-all; ">
<MudTextField @bind-Value="@CommandContext.Frame" Label="Frame" For="@(() => CommandContext.Frame)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin"></MudTextField>
<MudTextField id="@nameof(DeviceModelCommand.Frame)" @bind-Value="@CommandContext.Frame" Label="Frame" For="@(() => CommandContext.Frame)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin" Mask="@maskCommandFrame"></MudTextField>
</MudTd>
<MudTd DataLabel="Confirmed" Style="text-align: center; ">
<MudCheckBox @bind-Checked="@CommandContext.Confirmed" For="@(() => CommandContext.Confirmed)" Color="Color.Primary" Disabled="CommandContext.IsBuiltin"></MudCheckBox>
Expand All @@ -166,11 +166,11 @@
<MudNumericField @bind-Value="@CommandContext.Port" Label="Port" For="@(() => CommandContext.Port)" Variant="Variant.Outlined" Disabled="CommandContext.IsBuiltin"></MudNumericField>
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudIconButton Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Disabled="@CommandContext.IsBuiltin" OnClick="@(() => DeleteCommand(CommandContext))"></MudIconButton>
<MudIconButton id="removeButton" Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Disabled="@CommandContext.IsBuiltin" OnClick="@(() => DeleteCommand(CommandContext))"></MudIconButton>
</MudTd>
</RowTemplate>
<FooterContent>
<MudButton StartIcon="@Icons.Material.Filled.Add" Size="Size.Medium" Color="Color.Success" OnClick="AddCommand">Add Command</MudButton>
<MudButton id="addButton" StartIcon="@Icons.Material.Filled.Add" Size="Size.Medium" Color="Color.Success" OnClick="AddCommand">Add Command</MudButton>
</FooterContent>
</MudTable>
</MudItem>
Expand All @@ -195,6 +195,8 @@

private DeviceModelCommand DeviceModelCommand { get; set; } = new DeviceModelCommand();

public IMask maskCommandFrame = new RegexMask(@"^[0-9a-fA-F]{0,255}$");

private void AddCommand()
{
var last = Commands.LastOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<MudTextField @bind-Value="@CommandContext.Name" Label="Name" For="@(() => CommandContext.Name)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin"></MudTextField>
</MudTd>
<MudTd DataLabel="Frame" Style="word-break: break-all; ">
<MudTextField @bind-Value="@CommandContext.Frame" Label="Frame" For="@(() => CommandContext.Frame)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin"></MudTextField>
<MudTextField @bind-Value="@CommandContext.Frame" Label="Frame" For="@(() => CommandContext.Frame)" Variant="Variant.Outlined" Required="true" Disabled="CommandContext.IsBuiltin" Mask="@maskCommandFrame"></MudTextField>
</MudTd>
<MudTd DataLabel="Confirmed" Style="text-align: center; ">
<MudCheckBox @bind-Checked="@CommandContext.Confirmed" For="@(() => CommandContext.Confirmed)" Color="Color.Primary" Disabled="CommandContext.IsBuiltin"></MudCheckBox>
Expand All @@ -167,11 +167,11 @@
<MudNumericField @bind-Value="@CommandContext.Port" Label="Port" For="@(() => CommandContext.Port)" Variant="Variant.Outlined" Disabled="CommandContext.IsBuiltin"></MudNumericField>
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudIconButton Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Disabled="@CommandContext.IsBuiltin" OnClick="@(() => DeleteCommand(CommandContext))"></MudIconButton>
<MudIconButton id="removeButton" Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Disabled="@CommandContext.IsBuiltin" OnClick="@(() => DeleteCommand(CommandContext))"></MudIconButton>
</MudTd>
</RowTemplate>
<FooterContent>
<MudButton StartIcon="@Icons.Material.Filled.Add" Size="Size.Medium" Color="Color.Success" OnClick="AddCommand">Add Command</MudButton>
<MudButton id="addButton" StartIcon="@Icons.Material.Filled.Add" Size="Size.Medium" Color="Color.Success" OnClick="AddCommand">Add Command</MudButton>
</FooterContent>
</MudTable>
</MudItem>
Expand All @@ -192,6 +192,8 @@

private DeviceModelCommand DeviceModelCommand { get; set; } = new DeviceModelCommand();

public IMask maskCommandFrame = new RegexMask(@"^[0-9a-fA-F]{0,255}$");

protected override Task OnInitializedAsync()
{
return base.OnInitializedAsync();
Expand Down
Loading