|
| 1 | +namespace ByteBard.AsyncAPI.Tests.Bindings |
| 2 | +{ |
| 3 | + using FluentAssertions; |
| 4 | + using ByteBard.AsyncAPI.Models; |
| 5 | + using ByteBard.AsyncAPI.Readers; |
| 6 | + using NUnit.Framework; |
| 7 | + using BindingsCollection = ByteBard.AsyncAPI.Bindings.BindingsCollection; |
| 8 | + |
| 9 | + internal class AwsPolicyParsing_Should : TestBase |
| 10 | + { |
| 11 | + [TestCase("action:\n foo: bar", "StringOrStringList")] |
| 12 | + [TestCase("principal: someone", "Principal")] |
| 13 | + [TestCase("principal:\n Unknown: arn", "Principal")] |
| 14 | + [TestCase("principal: {}", "Principal")] |
| 15 | + [TestCase("principal:\n - '*'", "Principal")] |
| 16 | + [TestCase("condition: invalid", "Condition")] |
| 17 | + [TestCase("condition:\n StringEquals: invalid", "Condition")] |
| 18 | + public void SqsChannelBinding_WithInvalidPolicyStatement_ReportsDiagnosticInsteadOfThrowing(string statementField, string expectedType) |
| 19 | + { |
| 20 | + var yaml = |
| 21 | + "bindings:\n" + |
| 22 | + " sqs:\n" + |
| 23 | + " queue:\n" + |
| 24 | + " name: myQueue\n" + |
| 25 | + " policy:\n" + |
| 26 | + " statements:\n" + |
| 27 | + " - effect: Allow\n" + |
| 28 | + Indent(statementField, 12); |
| 29 | + |
| 30 | + var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection.Sqs }; |
| 31 | + |
| 32 | + AsyncApiDiagnostic diagnostic = null; |
| 33 | + var act = () => new AsyncApiStringReader(settings).ReadFragment<AsyncApiChannel>(yaml, AsyncApiVersion.AsyncApi2_0, out diagnostic); |
| 34 | + |
| 35 | + act.Should().NotThrow(); |
| 36 | + diagnostic.Errors.Should().ContainSingle().Which.Message.Should().Contain(expectedType); |
| 37 | + } |
| 38 | + |
| 39 | + [TestCase("action:\n foo: bar", "StringOrStringList")] |
| 40 | + [TestCase("principal: someone", "Principal")] |
| 41 | + [TestCase("condition: invalid", "Condition")] |
| 42 | + public void SnsChannelBinding_WithInvalidPolicyStatement_ReportsDiagnosticInsteadOfThrowing(string statementField, string expectedType) |
| 43 | + { |
| 44 | + var yaml = |
| 45 | + "bindings:\n" + |
| 46 | + " sns:\n" + |
| 47 | + " name: myTopic\n" + |
| 48 | + " policy:\n" + |
| 49 | + " statements:\n" + |
| 50 | + " - effect: Allow\n" + |
| 51 | + Indent(statementField, 10); |
| 52 | + |
| 53 | + var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection.Sns }; |
| 54 | + |
| 55 | + AsyncApiDiagnostic diagnostic = null; |
| 56 | + var act = () => new AsyncApiStringReader(settings).ReadFragment<AsyncApiChannel>(yaml, AsyncApiVersion.AsyncApi2_0, out diagnostic); |
| 57 | + |
| 58 | + act.Should().NotThrow(); |
| 59 | + diagnostic.Errors.Should().ContainSingle().Which.Message.Should().Contain(expectedType); |
| 60 | + } |
| 61 | + |
| 62 | + private static string Indent(string value, int spaces) |
| 63 | + { |
| 64 | + var prefix = new string(' ', spaces); |
| 65 | + return prefix + value.Replace("\n", "\n" + prefix) + "\n"; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments