AzureBlobXmlRepositoryTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. using Microsoft.AspNetCore.DataProtection.Azure.Blob;
  9. using Microsoft.WindowsAzure.Storage;
  10. using Microsoft.WindowsAzure.Storage.Blob;
  11. using Moq;
  12. using Xunit;
  13. namespace Microsoft.AspNetCore.DataProtection.Azure.Test
  14. {
  15. public class AzureBlobXmlRepositoryTests
  16. {
  17. [Fact]
  18. public void StoreCreatesBlobWhenNotExist()
  19. {
  20. AccessCondition downloadCondition = null;
  21. AccessCondition uploadCondition = null;
  22. byte[] bytes = null;
  23. BlobProperties properties = new BlobProperties();
  24. var mock = new Mock<ICloudBlob>();
  25. mock.SetupGet(c => c.Properties).Returns(properties);
  26. mock.Setup(c => c.UploadFromByteArrayAsync(
  27. It.IsAny<byte[]>(),
  28. It.IsAny<int>(),
  29. It.IsAny<int>(),
  30. It.IsAny<AccessCondition>(),
  31. It.IsAny<BlobRequestOptions>(),
  32. It.IsAny<OperationContext>()))
  33. .Returns(async (byte[] buffer, int index, int count, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) =>
  34. {
  35. bytes = buffer.Skip(index).Take(count).ToArray();
  36. uploadCondition = accessCondition;
  37. await Task.Yield();
  38. });
  39. var repository = new AzureBlobXmlRepository(() => mock.Object);
  40. repository.StoreElement(new XElement("Element"), null);
  41. Assert.Null(downloadCondition);
  42. Assert.Equal("*", uploadCondition.IfNoneMatchETag);
  43. Assert.Equal("application/xml; charset=utf-8", properties.ContentType);
  44. var element = "<Element />";
  45. Assert.Equal(bytes, GetEnvelopedContent(element));
  46. }
  47. [Fact]
  48. public void StoreUpdatesWhenExistsAndNewerExists()
  49. {
  50. AccessCondition downloadCondition = null;
  51. byte[] bytes = null;
  52. BlobProperties properties = new BlobProperties();
  53. var mock = new Mock<ICloudBlob>();
  54. mock.SetupGet(c => c.Properties).Returns(properties);
  55. mock.Setup(c => c.DownloadToStreamAsync(
  56. It.IsAny<Stream>(),
  57. It.IsAny<AccessCondition>(),
  58. null,
  59. null))
  60. .Returns(async (Stream target, AccessCondition condition, BlobRequestOptions options, OperationContext context) =>
  61. {
  62. var data = GetEnvelopedContent("<Element1 />");
  63. await target.WriteAsync(data, 0, data.Length);
  64. })
  65. .Verifiable();
  66. mock.Setup(c => c.UploadFromByteArrayAsync(
  67. It.IsAny<byte[]>(),
  68. It.IsAny<int>(),
  69. It.IsAny<int>(),
  70. It.Is((AccessCondition cond) => cond.IfNoneMatchETag == "*"),
  71. It.IsAny<BlobRequestOptions>(),
  72. It.IsAny<OperationContext>()))
  73. .Throws(new StorageException(new RequestResult { HttpStatusCode = 412 }, null, null))
  74. .Verifiable();
  75. mock.Setup(c => c.UploadFromByteArrayAsync(
  76. It.IsAny<byte[]>(),
  77. It.IsAny<int>(),
  78. It.IsAny<int>(),
  79. It.Is((AccessCondition cond) => cond.IfNoneMatchETag != "*"),
  80. It.IsAny<BlobRequestOptions>(),
  81. It.IsAny<OperationContext>()))
  82. .Returns(async (byte[] buffer, int index, int count, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) =>
  83. {
  84. bytes = buffer.Skip(index).Take(count).ToArray();
  85. await Task.Yield();
  86. })
  87. .Verifiable();
  88. var repository = new AzureBlobXmlRepository(() => mock.Object);
  89. repository.StoreElement(new XElement("Element2"), null);
  90. mock.Verify();
  91. Assert.Null(downloadCondition);
  92. Assert.Equal(bytes, GetEnvelopedContent("<Element1 /><Element2 />"));
  93. }
  94. private static byte[] GetEnvelopedContent(string element)
  95. {
  96. return Encoding.UTF8.GetBytes($"<?xml version=\"1.0\" encoding=\"utf-8\"?><repository>{element}</repository>");
  97. }
  98. }
  99. }