|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the MIT License. See License.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
4 | 9 | using System.Reflection;
|
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.Azure.WebJobs.Host; |
| 13 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 14 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
5 | 15 | using Microsoft.Azure.WebJobs.Script.WebHost.Controllers;
|
6 | 16 | using Microsoft.Azure.WebJobs.Script.WebHost.Filters;
|
| 17 | +using Microsoft.Azure.WebJobs.Script.WebHost.Models; |
| 18 | +using Moq; |
7 | 19 | using Xunit;
|
8 | 20 |
|
9 | 21 | namespace Microsoft.Azure.WebJobs.Script.Tests
|
10 | 22 | {
|
11 | 23 | public class AdminControllerTests
|
12 | 24 | {
|
| 25 | + private Mock<ScriptHost> hostMock; |
| 26 | + private Mock<WebScriptHostManager> managerMock; |
| 27 | + private Collection<FunctionDescriptor> testFunctions; |
| 28 | + private AdminController testController; |
| 29 | + |
| 30 | + public AdminControllerTests() |
| 31 | + { |
| 32 | + testFunctions = new Collection<FunctionDescriptor>(); |
| 33 | + |
| 34 | + var config = new ScriptHostConfiguration(); |
| 35 | + hostMock = new Mock<ScriptHost>(MockBehavior.Strict, new object[] { config }); |
| 36 | + hostMock.Setup(p => p.Functions).Returns(testFunctions); |
| 37 | + |
| 38 | + SecretManager secretManager = new SecretManager(); |
| 39 | + WebHostSettings settings = new WebHostSettings(); |
| 40 | + managerMock = new Mock<WebScriptHostManager>(MockBehavior.Strict, new object[] { config, secretManager, settings }); |
| 41 | + managerMock.SetupGet(p => p.Instance).Returns(hostMock.Object); |
| 42 | + |
| 43 | + testController = new AdminController(managerMock.Object); |
| 44 | + } |
| 45 | + |
13 | 46 | [Fact]
|
14 |
| - public void AdminController_HasAuthorizationLevelAttribute() |
| 47 | + public void HasAuthorizationLevelAttribute() |
15 | 48 | {
|
16 | 49 | AuthorizationLevelAttribute attribute = typeof(AdminController).GetCustomAttribute<AuthorizationLevelAttribute>();
|
17 | 50 | Assert.Equal(AuthorizationLevel.Admin, attribute.Level);
|
18 | 51 | }
|
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task Invoke_CallsFunction() |
| 55 | + { |
| 56 | + string testFunctionName = "TestFunction"; |
| 57 | + string triggerParameterName = "testTrigger"; |
| 58 | + string testInput = Guid.NewGuid().ToString(); |
| 59 | + bool functionInvoked = false; |
| 60 | + |
| 61 | + hostMock.Setup(p => p.CallAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, object>>(), CancellationToken.None)) |
| 62 | + .Callback<string, Dictionary<string, object>, CancellationToken>((name, args, token) => |
| 63 | + { |
| 64 | + functionInvoked = true; |
| 65 | + |
| 66 | + // verify the correct arguments were passed to the invoke |
| 67 | + Assert.Equal(testFunctionName, name); |
| 68 | + Assert.Equal(1, args.Count); |
| 69 | + Assert.Equal(testInput, (string)args[triggerParameterName]); |
| 70 | + }) |
| 71 | + .Returns(Task.CompletedTask); |
| 72 | + |
| 73 | + // Add a few parameters, with the trigger parameter last |
| 74 | + // to verify parameter order handling |
| 75 | + Collection<ParameterDescriptor> parameters = new Collection<ParameterDescriptor> |
| 76 | + { |
| 77 | + new ParameterDescriptor("context", typeof(ExecutionContext)), |
| 78 | + new ParameterDescriptor("log", typeof(TraceWriter)), |
| 79 | + new ParameterDescriptor(triggerParameterName, typeof(string)) |
| 80 | + { |
| 81 | + IsTrigger = true |
| 82 | + } |
| 83 | + }; |
| 84 | + testFunctions.Add(new FunctionDescriptor(testFunctionName, null, null, parameters)); |
| 85 | + |
| 86 | + FunctionInvocation invocation = new FunctionInvocation |
| 87 | + { |
| 88 | + Input = testInput |
| 89 | + }; |
| 90 | + HttpResponseMessage response = testController.Invoke(testFunctionName, invocation); |
| 91 | + Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); |
| 92 | + |
| 93 | + // allow the invoke task to run |
| 94 | + await Task.Delay(200); |
| 95 | + |
| 96 | + Assert.True(functionInvoked); |
| 97 | + } |
19 | 98 | }
|
20 | 99 | }
|
0 commit comments