|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Net.Http.Headers; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using CloudinaryDotNet.Actions; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace CloudinaryDotNet.Tests |
| 10 | +{ |
| 11 | + public class ApiAuthorizationTest |
| 12 | + { |
| 13 | + private const string m_oauthToken = "NTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZj17"; |
| 14 | + private const string m_cloudName = "test123"; |
| 15 | + private const string m_apiKey = "key"; |
| 16 | + private const string m_apiSecret = "secret"; |
| 17 | + private MockedCloudinary m_mockedCloudinary; |
| 18 | + |
| 19 | + [Test] |
| 20 | + public async Task TestOAuthTokenAdminApi() |
| 21 | + { |
| 22 | + InitCloudinaryApi(); |
| 23 | + |
| 24 | + await m_mockedCloudinary.PingAsync(); |
| 25 | + |
| 26 | + AssertHasBearerAuthorization(m_mockedCloudinary, m_oauthToken); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task TestKeyAndSecretAdminApi() |
| 31 | + { |
| 32 | + InitCloudinaryApi(m_apiKey, m_apiSecret); |
| 33 | + |
| 34 | + await m_mockedCloudinary.PingAsync(); |
| 35 | + |
| 36 | + AssertHasBasicAuthorization(m_mockedCloudinary, "a2V5OnNlY3JldA=="); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public async Task TestOAuthTokenUploadApi() |
| 41 | + { |
| 42 | + InitCloudinaryApi(); |
| 43 | + |
| 44 | + var uploadParams = new ImageUploadParams() |
| 45 | + { |
| 46 | + File = GetFileDescription() |
| 47 | + }; |
| 48 | + |
| 49 | + await m_mockedCloudinary.UploadAsync(uploadParams); |
| 50 | + |
| 51 | + AssertHasBearerAuthorization(m_mockedCloudinary, m_oauthToken); |
| 52 | + Assert.IsFalse(m_mockedCloudinary.HttpRequestContent.Contains("signature")); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public async Task TestKeyAndSecretUploadApi() |
| 57 | + { |
| 58 | + InitCloudinaryApi(m_apiKey, m_apiSecret); |
| 59 | + |
| 60 | + var uploadParams = new ImageUploadParams() |
| 61 | + { |
| 62 | + File = GetFileDescription() |
| 63 | + }; |
| 64 | + await m_mockedCloudinary.UploadAsync(uploadParams); |
| 65 | + |
| 66 | + AssertUploadSignature(); |
| 67 | + } |
| 68 | + |
| 69 | + [TestCaseSource(typeof(UploadApiProvider), nameof(UploadApiProvider.UploadApis))] |
| 70 | + public async Task TestUploadAuthorization(Func<MockedCloudinary, Task> func) |
| 71 | + { |
| 72 | + InitCloudinaryApi(m_apiKey, m_apiSecret); |
| 73 | + |
| 74 | + await func(m_mockedCloudinary); |
| 75 | + |
| 76 | + AssertUploadSignature(); |
| 77 | + } |
| 78 | + |
| 79 | + private static FileDescription GetFileDescription() |
| 80 | + => new FileDescription("foo", new MemoryStream(new byte[5])); |
| 81 | + |
| 82 | + private void AssertUploadSignature() |
| 83 | + { |
| 84 | + var httpRequestContent = m_mockedCloudinary.HttpRequestContent; |
| 85 | + Assert.IsTrue(httpRequestContent.Contains("signature")); |
| 86 | + Assert.IsTrue(httpRequestContent.Contains("api_key")); |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + public async Task TestMissingCredentialsUploadApi() |
| 91 | + { |
| 92 | + InitCloudinaryApi(null, null); |
| 93 | + |
| 94 | + var uploadParams = new ImageUploadParams() |
| 95 | + { |
| 96 | + File = new FileDescription(Path.GetTempFileName()), |
| 97 | + Unsigned = true, |
| 98 | + UploadPreset = "api_test_upload_preset" |
| 99 | + }; |
| 100 | + |
| 101 | + await m_mockedCloudinary.UploadAsync(uploadParams); |
| 102 | + |
| 103 | + Assert.IsTrue(m_mockedCloudinary.HttpRequestContent.Contains("upload_preset")); |
| 104 | + } |
| 105 | + |
| 106 | + private void InitCloudinaryApi() |
| 107 | + { |
| 108 | + m_mockedCloudinary = new MockedCloudinary(account: new Account(m_cloudName, m_oauthToken)); |
| 109 | + } |
| 110 | + |
| 111 | + private void InitCloudinaryApi(string apiKey, string apiSecret) |
| 112 | + { |
| 113 | + m_mockedCloudinary = new MockedCloudinary(account: new Account(m_cloudName, apiKey, apiSecret)); |
| 114 | + } |
| 115 | + |
| 116 | + private void AssertHasAuthorization(MockedCloudinary cloudinary, string scheme, string value) => |
| 117 | + Assert.AreEqual(cloudinary.HttpRequestHeaders.Authorization, new AuthenticationHeaderValue(scheme, value)); |
| 118 | + |
| 119 | + private void AssertHasBearerAuthorization(MockedCloudinary cloudinary, string value) => |
| 120 | + AssertHasAuthorization(cloudinary, "Bearer", value); |
| 121 | + |
| 122 | + private void AssertHasBasicAuthorization(MockedCloudinary cloudinary, string value) => |
| 123 | + AssertHasAuthorization(cloudinary, "Basic", value); |
| 124 | + |
| 125 | + private static class UploadApiProvider |
| 126 | + { |
| 127 | + public static IEnumerable<object> UploadApis() |
| 128 | + { |
| 129 | + yield return new Func<MockedCloudinary, Task>[] |
| 130 | + { m => m.UploadAsync(new VideoUploadParams { File = GetFileDescription() }) }; |
| 131 | + |
| 132 | + yield return new Func<MockedCloudinary, Task>[] |
| 133 | + { m => m.UploadAsync(new ImageUploadParams { File = GetFileDescription() }) }; |
| 134 | + |
| 135 | + yield return new Func<MockedCloudinary, Task>[] |
| 136 | + { m => m.UploadAsync(new RawUploadParams { File = GetFileDescription() }) }; |
| 137 | + |
| 138 | + yield return new Func<MockedCloudinary, Task>[] |
| 139 | + { m => m.UploadLargeAsync(new RawUploadParams { File = GetFileDescription() }) }; |
| 140 | + |
| 141 | + yield return new Func<MockedCloudinary, Task>[] |
| 142 | + { m => m.UploadLargeRawAsync(new RawUploadParams { File = GetFileDescription() }) }; |
| 143 | + |
| 144 | + yield return new Func<MockedCloudinary, Task>[] |
| 145 | + { m => m.TagAsync(new TagParams()) }; |
| 146 | + |
| 147 | + yield return new Func<MockedCloudinary, Task>[] |
| 148 | + { m => m.ContextAsync(new ContextParams()) }; |
| 149 | + |
| 150 | + yield return new Func<MockedCloudinary, Task>[] |
| 151 | + { m => m.ExplicitAsync(new ExplicitParams("id")) }; |
| 152 | + |
| 153 | + yield return new Func<MockedCloudinary, Task>[] |
| 154 | + { m => m.ExplodeAsync(new ExplodeParams("id", new Transformation())) }; |
| 155 | + |
| 156 | + yield return new Func<MockedCloudinary, Task>[] |
| 157 | + { m => m.CreateZipAsync(new ArchiveParams().PublicIds(new List<string> { "id" })) }; |
| 158 | + |
| 159 | + yield return new Func<MockedCloudinary, Task>[] |
| 160 | + { m => m.CreateArchiveAsync(new ArchiveParams().PublicIds(new List<string> { "id" })) }; |
| 161 | + |
| 162 | + yield return new Func<MockedCloudinary, Task>[] |
| 163 | + { m => m.MakeSpriteAsync(new SpriteParams("tag")) }; |
| 164 | + |
| 165 | + yield return new Func<MockedCloudinary, Task>[] |
| 166 | + { m => m.MultiAsync(new MultiParams("tag")) }; |
| 167 | + |
| 168 | + yield return new Func<MockedCloudinary, Task>[] |
| 169 | + { m => m.TextAsync(new TextParams("text")) }; |
| 170 | + |
| 171 | + yield return new Func<MockedCloudinary, Task>[] |
| 172 | + { m => m.CreateSlideshowAsync( |
| 173 | + new CreateSlideshowParams { ManifestTransformation = new Transformation() }) }; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments