|
| 1 | +using CloudinaryDotNet.Actions; |
| 2 | +using NUnit.Framework; |
| 3 | + |
| 4 | +namespace CloudinaryDotNet.Tests.AdminApi |
| 5 | +{ |
| 6 | + [TestFixture] |
| 7 | + public class GetResourceTest |
| 8 | + { |
| 9 | + private const string CommonJsonPrefix = @" |
| 10 | + { |
| 11 | + ""asset_id"": ""8abd06560fc75b3bbe80299b988035b0"", |
| 12 | + ""public_id"": ""REPLACE_PUBLIC_ID"", |
| 13 | + ""format"": ""jpg"", |
| 14 | + ""version"": 1739308959, |
| 15 | + ""resource_type"": ""image"", |
| 16 | + ""type"": ""upload"", |
| 17 | + ""created_at"": ""2025-02-11T21:22:39Z"", |
| 18 | + ""bytes"": 582249, |
| 19 | + ""width"": 1000, |
| 20 | + ""height"": 688, |
| 21 | + ""url"": ""http://res.cloudinary.com/demo/image/upload/v1739308959/REPLACE_PUBLIC_ID.jpg"", |
| 22 | + ""secure_url"": ""https://res.cloudinary.com/demo/image/upload/v1739308959/REPLACE_PUBLIC_ID.jpg"", |
| 23 | + ""moderation"": [ |
| 24 | + "; |
| 25 | + |
| 26 | + private const string CommonJsonSuffix = @" |
| 27 | + ] |
| 28 | + } |
| 29 | + "; |
| 30 | + |
| 31 | + // "response" is an object containing "moderation_labels" |
| 32 | + private const string LabelsModerationBlock = @" |
| 33 | + { |
| 34 | + ""response"": { |
| 35 | + ""moderation_labels"": [ |
| 36 | + { |
| 37 | + ""confidence"": 94.9907455444336, |
| 38 | + ""name"": ""Suggestive"", |
| 39 | + ""parent_name"": """" |
| 40 | + }, |
| 41 | + { |
| 42 | + ""confidence"": 94.9907455444336, |
| 43 | + ""name"": ""Female Swimwear Or Underwear"", |
| 44 | + ""parent_name"": ""Suggestive"" |
| 45 | + } |
| 46 | + ] |
| 47 | + }, |
| 48 | + ""status"": ""rejected"", |
| 49 | + ""kind"": ""aws_rek"", |
| 50 | + ""updated_at"": ""2017-08-03T08:26:58Z"" |
| 51 | + } |
| 52 | + "; |
| 53 | + |
| 54 | + // "response" is an array of objects (duplicates) |
| 55 | + private const string DuplicatesModerationBlock = @" |
| 56 | + { |
| 57 | + ""kind"": ""duplicate"", |
| 58 | + ""status"": ""rejected"", |
| 59 | + ""response"": [ |
| 60 | + { |
| 61 | + ""public_id"": ""duplicate_id"", |
| 62 | + ""confidence"": 1.0 |
| 63 | + } |
| 64 | + ], |
| 65 | + ""updated_at"": ""2025-02-07T08:30:29Z"" |
| 66 | + } |
| 67 | + "; |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void TestGetResourceModerationResponse_WithLabels() |
| 71 | + { |
| 72 | + var responseData = CommonJsonPrefix |
| 73 | + .Replace("REPLACE_PUBLIC_ID", "mhor383ejnw0j6iokmlh") |
| 74 | + + LabelsModerationBlock |
| 75 | + + CommonJsonSuffix; |
| 76 | + |
| 77 | + var localCloudinaryMock = new MockedCloudinary(responseData); |
| 78 | + var result = localCloudinaryMock.GetResource("mhor383ejnw0j6iokmlh"); |
| 79 | + |
| 80 | + Assert.NotNull(result); |
| 81 | + Assert.AreEqual("mhor383ejnw0j6iokmlh", result.PublicId); |
| 82 | + Assert.NotNull(result.Moderation); |
| 83 | + Assert.IsNotEmpty(result.Moderation); |
| 84 | + |
| 85 | + var firstModeration = result.Moderation[0]; |
| 86 | + Assert.AreEqual("aws_rek", firstModeration.Kind); |
| 87 | + Assert.AreEqual(ModerationStatus.Rejected, firstModeration.Status); |
| 88 | + Assert.NotNull(firstModeration.Response.ModerationLabels); |
| 89 | + Assert.IsNotEmpty(firstModeration.Response.ModerationLabels); |
| 90 | + Assert.AreEqual("Suggestive", firstModeration.Response.ModerationLabels[0].Name); |
| 91 | + Assert.AreEqual(94.9907455444336f, firstModeration.Response.ModerationLabels[0].Confidence); |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public void TestGetResourceModerationResponse_WithDuplicates() |
| 96 | + { |
| 97 | + var responseData = CommonJsonPrefix |
| 98 | + .Replace("REPLACE_PUBLIC_ID", "duplicate_id") |
| 99 | + + DuplicatesModerationBlock |
| 100 | + + CommonJsonSuffix; |
| 101 | + |
| 102 | + var localCloudinaryMock = new MockedCloudinary(responseData); |
| 103 | + var result = localCloudinaryMock.GetResource("duplicate_id"); |
| 104 | + |
| 105 | + Assert.NotNull(result); |
| 106 | + Assert.AreEqual("duplicate_id", result.PublicId); |
| 107 | + Assert.NotNull(result.Moderation); |
| 108 | + Assert.IsNotEmpty(result.Moderation); |
| 109 | + |
| 110 | + var firstModeration = result.Moderation[0]; |
| 111 | + Assert.AreEqual("duplicate", firstModeration.Kind); |
| 112 | + Assert.AreEqual(ModerationStatus.Rejected, firstModeration.Status); |
| 113 | + Assert.NotNull(firstModeration.Response.ModerationLabels); |
| 114 | + Assert.IsNotEmpty(firstModeration.Response.ModerationLabels); |
| 115 | + Assert.AreEqual("duplicate_id", firstModeration.Response.ModerationLabels[0].PublicId); |
| 116 | + Assert.AreEqual(1.0f, firstModeration.Response.ModerationLabels[0].Confidence); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments