From b1cd42e394e0e0c520ca275d7380f5b5294f3d9b Mon Sep 17 00:00:00 2001 From: Justin Baur Date: Mon, 7 Feb 2022 10:28:11 -0500 Subject: [PATCH] Fix json only serializing base properties (#1840) * Fix json only serializing base properties * Run formatting * Switch to returning concrete type * Update method name --- src/Api/Models/Request/SendRequestModel.cs | 4 +- .../Models/Request/SendRequestModelTests.cs | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/Api.Test/Models/Request/SendRequestModelTests.cs diff --git a/src/Api/Models/Request/SendRequestModel.cs b/src/Api/Models/Request/SendRequestModel.cs index 106f4b5c6..b6a802adf 100644 --- a/src/Api/Models/Request/SendRequestModel.cs +++ b/src/Api/Models/Request/SendRequestModel.cs @@ -71,7 +71,7 @@ namespace Bit.Api.Models.Request existingSend.Data = JsonSerializer.Serialize(fileData, JsonHelpers.IgnoreWritingNull); break; case SendType.Text: - existingSend.Data = JsonSerializer.Serialize(ToSendData(), JsonHelpers.IgnoreWritingNull); + existingSend.Data = JsonSerializer.Serialize(ToSendTextData(), JsonHelpers.IgnoreWritingNull); break; default: throw new ArgumentException("Unsupported type: " + nameof(Type) + "."); @@ -128,7 +128,7 @@ namespace Bit.Api.Models.Request return existingSend; } - private SendData ToSendData() + private SendTextData ToSendTextData() { return new SendTextData(Name, Notes, Text.Text, Text.Hidden); } diff --git a/test/Api.Test/Models/Request/SendRequestModelTests.cs b/test/Api.Test/Models/Request/SendRequestModelTests.cs new file mode 100644 index 000000000..01b2587f7 --- /dev/null +++ b/test/Api.Test/Models/Request/SendRequestModelTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Text.Json; +using Bit.Api.Models; +using Bit.Api.Models.Request; +using Bit.Core.Enums; +using Bit.Core.Services; +using Bit.Test.Common.Helpers; +using NSubstitute; +using Xunit; + +namespace Bit.Api.Test.Models.Request +{ + public class SendRequestModelTests + { + [Fact] + public void ToSend_Text_Success() + { + var deletionDate = DateTime.UtcNow.AddDays(5); + var sendRequest = new SendRequestModel + { + DeletionDate = deletionDate, + Disabled = false, + ExpirationDate = null, + HideEmail = false, + Key = "encrypted_key", + MaxAccessCount = null, + Name = "encrypted_name", + Notes = null, + Password = "Password", + Text = new SendTextModel() + { + Hidden = false, + Text = "encrypted_text" + }, + Type = SendType.Text, + }; + + var sendService = Substitute.For(); + sendService.HashPassword(Arg.Any()) + .Returns((info) => $"hashed_{(string)info[0]}"); + + var send = sendRequest.ToSend(Guid.NewGuid(), sendService); + + Assert.Equal(deletionDate, send.DeletionDate); + Assert.False(send.Disabled); + Assert.Null(send.ExpirationDate); + Assert.False(send.HideEmail); + Assert.Equal("encrypted_key", send.Key); + Assert.Equal("hashed_Password", send.Password); + + using var jsonDocument = JsonDocument.Parse(send.Data); + var root = jsonDocument.RootElement; + var text = AssertHelper.AssertJsonProperty(root, "Text", JsonValueKind.String).GetString(); + Assert.Equal("encrypted_text", text); + AssertHelper.AssertJsonProperty(root, "Hidden", JsonValueKind.False); + Assert.False(root.TryGetProperty("Notes", out var _)); + var name = AssertHelper.AssertJsonProperty(root, "Name", JsonValueKind.String).GetString(); + Assert.Equal("encrypted_name", name); + } + } +}