1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-22 20:11:04 -05:00
bitwarden/test/Api.Test/Tools/Models/Request/SendRequestModelTests.cs
Graham Walker 818934487f
PM-18939 refactoring send service to 'cqrs' (#5652)
* PM-18939 refactoring send service to 'cqrs'

* PM-18939 fixing import issue with sendValidationService

* PM-18939 fixing code based on PR comments

* PM-18339 reverting to previous code in test

* PM-18939 adding XMLdocs to services

* PM-18939 reverting send validation methods

* PM-18939 updating code to match main

* PM-18939 reverting validateUserCanSaveAsync to match main

* PM-18939 fill our param and return sections of XMLdocs

* PM-18939 updating XMLdocs based on PR comments

* Update src/Core/Tools/SendFeatures/Commands/Interfaces/IAnonymousSendCommand.cs

Co-authored-by:  Audrey  <ajensen@bitwarden.com>

* Update src/Core/Tools/SendFeatures/Commands/Interfaces/INonAnonymousSendCommand.cs

Co-authored-by:  Audrey  <ajensen@bitwarden.com>

* Update src/Core/Tools/SendFeatures/Commands/Interfaces/INonAnonymousSendCommand.cs

Co-authored-by:  Audrey  <ajensen@bitwarden.com>

* Update src/Core/Tools/SendFeatures/Services/Interfaces/ISendStorageService.cs

Co-authored-by:  Audrey  <ajensen@bitwarden.com>

* PM-18939 adding commits to change tuple to enum type

* PM-18939 resetting stream position to 0 when uploading file

* PM-18939 updating XMLdocs based on PR comments

* PM-18939 updating XMLdocs

* PM-18939 removing circular dependency

* PM-18939 fixing based on comments

* PM-18939 updating method name and documentation

---------

Co-authored-by:  Audrey  <ajensen@bitwarden.com>
2025-05-19 22:59:30 -05:00

119 lines
3.6 KiB
C#

using System.Text.Json;
using Bit.Api.Tools.Models;
using Bit.Api.Tools.Models.Request;
using Bit.Core.Exceptions;
using Bit.Core.Tools.Enums;
using Bit.Core.Tools.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 sendAuthorizationService = Substitute.For<ISendAuthorizationService>();
sendAuthorizationService.HashPassword(Arg.Any<string>())
.Returns((info) => $"hashed_{(string)info[0]}");
var send = sendRequest.ToSend(Guid.NewGuid(), sendAuthorizationService);
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);
}
[Fact]
public void ValidateEdit_DeletionDateInPast_ThrowsBadRequestException()
{
var send = new SendRequestModel
{
DeletionDate = DateTime.UtcNow.AddMinutes(-5)
};
Assert.Throws<BadRequestException>(() => send.ValidateEdit());
}
[Fact]
public void ValidateEdit_DeletionDateTooFarInFuture_ThrowsBadRequestException()
{
var send = new SendRequestModel
{
DeletionDate = DateTime.UtcNow.AddDays(32)
};
Assert.Throws<BadRequestException>(() => send.ValidateEdit());
}
[Fact]
public void ValidateEdit_ExpirationDateInPast_ThrowsBadRequestException()
{
var send = new SendRequestModel
{
ExpirationDate = DateTime.UtcNow.AddMinutes(-5)
};
Assert.Throws<BadRequestException>(() => send.ValidateEdit());
}
[Fact]
public void ValidateEdit_ExpirationDateGreaterThanDeletionDate_ThrowsBadRequestException()
{
var send = new SendRequestModel
{
DeletionDate = DateTime.UtcNow.AddDays(1),
ExpirationDate = DateTime.UtcNow.AddDays(2)
};
Assert.Throws<BadRequestException>(() => send.ValidateEdit());
}
[Fact]
public void ValidateEdit_ValidDates_Success()
{
var send = new SendRequestModel
{
DeletionDate = DateTime.UtcNow.AddDays(10),
ExpirationDate = DateTime.UtcNow.AddDays(5)
};
Exception ex = Record.Exception(() => send.ValidateEdit());
Assert.Null(ex);
}
}