예제 #1
0
        public async Task <ActionResult <AccessTokenResultDto> > CreateAttachmentAccessToken(
            [FromRoute] long noteId,
            [FromRoute] long attachmentId,
            [FromBody] MutateAttachmentAccessTokenModel model)
        {
            var command = Mapper.Map <CreateAttachmentAccessTokenCommand>(model);

            command.AttachmentId = attachmentId;
            var result = await Mediator.Send(command);

            return(CreatedAtAction(
                       nameof(GetAttachmentByAccessToken),
                       new { noteId, attachmentId, token = result.Token, filename = result.Filename },
                       result));
        }
        public async Task CreateAttachmentAccessToken_HappyFlow()
        {
            // Arrange
            var attachment = await WolkDbContext.CreateAndSaveAttachment();

            var model = new MutateAttachmentAccessTokenModel
            {
                ExpirationDateTime = new DateTimeOffset(2019, 12, 31, 23, 0, 0, TimeSpan.FromHours(2))
            };

            var url = $"/api/note/{attachment.NoteId}/attachments/{attachment.Id}/accessTokens";

            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, MimeTypes.Json)
            };
            var token = await GetJwt();

            request.AddJwtBearer(token);

            // Act
            using var response = await HttpClient.SendAsync(request);

            // Assert
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            var content = await response.Content.ReadAsStringAsync();

            var returnedToken = JsonConvert.DeserializeObject <AccessTokenResultDto>(content);

            Assert.AreEqual(model.ExpirationDateTime, returnedToken.ExpirationDateTime);
            Assert.IsTrue(Guid.TryParse(returnedToken.Token, out var _));

            var addedToken = await WolkDbContext.AccessTokens.SingleAsync();

            Assert.AreEqual(returnedToken.Token, addedToken.Token);
            Assert.AreEqual(returnedToken.ExpirationDateTime, addedToken.ExpirationDateTime);
        }