Ejemplo n.º 1
0
        public async Task <SendFileUploadDataResponseModel> PostFile([FromBody] SendRequestModel model)
        {
            if (model.Type != SendType.File)
            {
                throw new BadRequestException("Invalid content.");
            }

            if (!model.FileLength.HasValue)
            {
                throw new BadRequestException("Invalid content. File size hint is required.");
            }

            if (model.FileLength.Value > SendService.MAX_FILE_SIZE)
            {
                throw new BadRequestException($"Max file size is {SendService.MAX_FILE_SIZE_READABLE}.");
            }

            var userId = _userService.GetProperUserId(User).Value;

            var(send, data) = model.ToSend(userId, model.File.FileName, _sendService);
            var uploadUrl = await _sendService.SaveFileSendAsync(send, data, model.FileLength.Value);

            return(new SendFileUploadDataResponseModel
            {
                Url = uploadUrl,
                FileUploadType = _sendFileStorageService.FileUploadType,
                SendResponse = new SendResponseModel(send, _globalSettings)
            });
        }
Ejemplo n.º 2
0
        public async Task <SendResponseModel> Post([FromBody] SendRequestModel model)
        {
            var userId = _userService.GetProperUserId(User).Value;
            var send   = model.ToSend(userId, _sendService);
            await _sendService.SaveSendAsync(send);

            return(new SendResponseModel(send, _globalSettings));
        }
Ejemplo n.º 3
0
        public async Task <SendResponseModel> Put(string id, [FromBody] SendRequestModel model)
        {
            var userId = _userService.GetProperUserId(User).Value;
            var send   = await _sendRepository.GetByIdAsync(new Guid(id));

            if (send == null || send.UserId != userId)
            {
                throw new NotFoundException();
            }

            await _sendService.SaveSendAsync(model.ToSend(send, _sendService));

            return(new SendResponseModel(send, _globalSettings));
        }
Ejemplo n.º 4
0
        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       = "******",
                Text           = new SendTextModel()
                {
                    Hidden = false,
                    Text   = "encrypted_text"
                },
                Type = SendType.Text,
            };

            var sendService = Substitute.For <ISendService>();

            sendService.HashPassword(Arg.Any <string>())
            .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);
        }