-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
279 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace CloudinaryDotNet.IntegrationTests.AdminApi | ||
{ | ||
public class OAuthTest: IntegrationTestBase | ||
{ | ||
private const string FAKE_OAUTH_TOKEN = "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4"; | ||
private static string m_uniqueImagePublicId; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
m_uniqueImagePublicId = $"asset_image_{m_uniqueTestId}"; | ||
} | ||
|
||
[Test] | ||
public void TestOAuthToken() | ||
{ | ||
var result = m_cloudinary.GetResource(m_uniqueImagePublicId); | ||
Assert.IsNotNull(result.Error); | ||
Assert.IsTrue(result.Error.Message.Contains("Invalid token")); | ||
} | ||
|
||
protected override Account GetAccountInstance() | ||
{ | ||
Account account = new Account(m_cloudName, FAKE_OAUTH_TOKEN); | ||
|
||
Assert.IsNotEmpty(account.Cloud, $"Cloud name must be specified in {CONFIG_PLACE}"); | ||
Assert.IsNotEmpty(account.OAuthToken); | ||
|
||
return account; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using CloudinaryDotNet.Actions; | ||
using NUnit.Framework; | ||
|
||
namespace CloudinaryDotNet.Tests | ||
{ | ||
public class ApiAuthorizationTest | ||
{ | ||
private const string m_oauthToken = "NTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZj17"; | ||
private const string m_cloudName = "test123"; | ||
private const string m_apiKey = "key"; | ||
private const string m_apiSecret = "secret"; | ||
private MockedCloudinary m_mockedCloudinary; | ||
|
||
[Test] | ||
public async Task TestOAuthTokenAdminApi() | ||
{ | ||
InitCloudinaryApi(); | ||
|
||
await m_mockedCloudinary.PingAsync(); | ||
|
||
AssertHasBearerAuthorization(m_mockedCloudinary, m_oauthToken); | ||
} | ||
|
||
[Test] | ||
public async Task TestKeyAndSecretAdminApi() | ||
{ | ||
InitCloudinaryApi(m_apiKey, m_apiSecret); | ||
|
||
await m_mockedCloudinary.PingAsync(); | ||
|
||
AssertHasBasicAuthorization(m_mockedCloudinary, "a2V5OnNlY3JldA=="); | ||
} | ||
|
||
[Test] | ||
public async Task TestOAuthTokenUploadApi() | ||
{ | ||
InitCloudinaryApi(); | ||
|
||
var uploadParams = new ImageUploadParams() | ||
{ | ||
File = GetFileDescription() | ||
}; | ||
|
||
await m_mockedCloudinary.UploadAsync(uploadParams); | ||
|
||
AssertHasBearerAuthorization(m_mockedCloudinary, m_oauthToken); | ||
Assert.IsFalse(m_mockedCloudinary.HttpRequestContent.Contains("signature")); | ||
} | ||
|
||
[Test] | ||
public async Task TestKeyAndSecretUploadApi() | ||
{ | ||
InitCloudinaryApi(m_apiKey, m_apiSecret); | ||
|
||
var uploadParams = new ImageUploadParams() | ||
{ | ||
File = GetFileDescription() | ||
}; | ||
await m_mockedCloudinary.UploadAsync(uploadParams); | ||
|
||
AssertUploadSignature(); | ||
} | ||
|
||
[TestCaseSource(typeof(UploadApiProvider), nameof(UploadApiProvider.UploadApis))] | ||
public async Task TestUploadAuthorization(Func<MockedCloudinary, Task> func) | ||
{ | ||
InitCloudinaryApi(m_apiKey, m_apiSecret); | ||
|
||
await func(m_mockedCloudinary); | ||
|
||
AssertUploadSignature(); | ||
} | ||
|
||
private static FileDescription GetFileDescription() | ||
=> new FileDescription("foo", new MemoryStream(new byte[5])); | ||
|
||
private void AssertUploadSignature() | ||
{ | ||
var httpRequestContent = m_mockedCloudinary.HttpRequestContent; | ||
Assert.IsTrue(httpRequestContent.Contains("signature")); | ||
Assert.IsTrue(httpRequestContent.Contains("api_key")); | ||
} | ||
|
||
[Test] | ||
public async Task TestMissingCredentialsUploadApi() | ||
{ | ||
InitCloudinaryApi(null, null); | ||
|
||
var uploadParams = new ImageUploadParams() | ||
{ | ||
File = new FileDescription(Path.GetTempFileName()), | ||
Unsigned = true, | ||
UploadPreset = "api_test_upload_preset" | ||
}; | ||
|
||
await m_mockedCloudinary.UploadAsync(uploadParams); | ||
|
||
Assert.IsTrue(m_mockedCloudinary.HttpRequestContent.Contains("upload_preset")); | ||
} | ||
|
||
private void InitCloudinaryApi() | ||
{ | ||
m_mockedCloudinary = new MockedCloudinary(account: new Account(m_cloudName, m_oauthToken)); | ||
} | ||
|
||
private void InitCloudinaryApi(string apiKey, string apiSecret) | ||
{ | ||
m_mockedCloudinary = new MockedCloudinary(account: new Account(m_cloudName, apiKey, apiSecret)); | ||
} | ||
|
||
private void AssertHasAuthorization(MockedCloudinary cloudinary, string scheme, string value) => | ||
Assert.AreEqual(cloudinary.HttpRequestHeaders.Authorization, new AuthenticationHeaderValue(scheme, value)); | ||
|
||
private void AssertHasBearerAuthorization(MockedCloudinary cloudinary, string value) => | ||
AssertHasAuthorization(cloudinary, "Bearer", value); | ||
|
||
private void AssertHasBasicAuthorization(MockedCloudinary cloudinary, string value) => | ||
AssertHasAuthorization(cloudinary, "Basic", value); | ||
|
||
private static class UploadApiProvider | ||
{ | ||
public static IEnumerable<object> UploadApis() | ||
{ | ||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.UploadAsync(new VideoUploadParams { File = GetFileDescription() }) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.UploadAsync(new ImageUploadParams { File = GetFileDescription() }) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.UploadAsync(new RawUploadParams { File = GetFileDescription() }) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.UploadLargeAsync(new RawUploadParams { File = GetFileDescription() }) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.UploadLargeRawAsync(new RawUploadParams { File = GetFileDescription() }) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.TagAsync(new TagParams()) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.ContextAsync(new ContextParams()) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.ExplicitAsync(new ExplicitParams("id")) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.ExplodeAsync(new ExplodeParams("id", new Transformation())) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.CreateZipAsync(new ArchiveParams().PublicIds(new List<string> { "id" })) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.CreateArchiveAsync(new ArchiveParams().PublicIds(new List<string> { "id" })) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.MakeSpriteAsync(new SpriteParams("tag")) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.MultiAsync(new MultiParams("tag")) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.TextAsync(new TextParams("text")) }; | ||
|
||
yield return new Func<MockedCloudinary, Task>[] | ||
{ m => m.CreateSlideshowAsync( | ||
new CreateSlideshowParams { ManifestTransformation = new Transformation() }) }; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace CloudinaryDotNet.Actions | ||
{ | ||
using System.Runtime.Serialization; | ||
|
||
/// <summary> | ||
/// Parsed details of a single ping request. | ||
/// </summary> | ||
[DataContract] | ||
public class PingResult : BaseResult | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters