|
| 1 | +using System; |
| 2 | +using System.Reflection; |
1 | 3 | using System.Threading.Tasks; |
2 | 4 |
|
3 | 5 | using Amazon.Lambda.Core; |
4 | 6 | using Amazon.S3; |
| 7 | +using Amazon.SecurityToken; |
| 8 | +using Amazon.SecurityToken.Model; |
5 | 9 |
|
6 | 10 | using FluentAssertions; |
7 | 11 |
|
|
12 | 16 | using Microsoft.Extensions.DependencyInjection; |
13 | 17 | using Microsoft.Extensions.Logging; |
14 | 18 |
|
15 | | -using NUnit.Framework; |
| 19 | +using NSubstitute; |
16 | 20 |
|
| 21 | +using NUnit.Framework; |
17 | 22 | namespace Lambdajection.Tests |
18 | 23 | { |
19 | 24 | [Lambda(Startup = typeof(Startup))] |
@@ -86,5 +91,33 @@ public async Task TestExampleLambdaRun() |
86 | 91 |
|
87 | 92 | result.Should().BeEquivalentTo("foo bar"); |
88 | 93 | } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public async Task TestFactoriesDoNotPerformAssumeRoleIfNoRoleArnGiven() |
| 97 | + { |
| 98 | + var client = Substitute.For<IAmazonSecurityTokenService>(); |
| 99 | + var configuratorType = typeof(ExampleLambda).GetNestedType("LambdajectionConfigurator", BindingFlags.NonPublic)!; |
| 100 | + var s3FactoryType = configuratorType.GetNestedType("S3Factory", BindingFlags.NonPublic)!; |
| 101 | + var factory = Activator.CreateInstance(s3FactoryType, new object[] { client }); |
| 102 | + var createMethod = s3FactoryType.GetMethod("Create")!; |
| 103 | + createMethod.Invoke(factory, new object[] { null! }); |
| 104 | + |
| 105 | + await client.DidNotReceive().AssumeRoleAsync(Arg.Any<AssumeRoleRequest>()); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public async Task TestFactoriesPerformAssumeRoleIfRoleArnGiven() |
| 110 | + { |
| 111 | + var client = Substitute.For<IAmazonSecurityTokenService>(); |
| 112 | + var configuratorType = typeof(ExampleLambda).GetNestedType("LambdajectionConfigurator", BindingFlags.NonPublic)!; |
| 113 | + var s3FactoryType = configuratorType.GetNestedType("S3Factory", BindingFlags.NonPublic)!; |
| 114 | + var factory = Activator.CreateInstance(s3FactoryType, new object[] { client }); |
| 115 | + var createMethod = s3FactoryType.GetMethod("Create")!; |
| 116 | + var roleArn = "rolearn"; |
| 117 | + |
| 118 | + createMethod.Invoke(factory, new object[] { roleArn }); |
| 119 | + |
| 120 | + await client.Received().AssumeRoleAsync(Arg.Is<AssumeRoleRequest>(req => req.RoleArn == roleArn)); |
| 121 | + } |
89 | 122 | } |
90 | 123 | } |
0 commit comments