|
4 | 4 | using System.IO;
|
5 | 5 | using System.Net;
|
6 | 6 | using System.Threading.Tasks;
|
| 7 | +using Microsoft.AspNetCore.Authentication; |
7 | 8 | using Microsoft.AspNetCore.Authorization;
|
8 | 9 | using Microsoft.AspNetCore.Mvc;
|
9 | 10 | using Microsoft.Azure.WebJobs.Script.WebHost.Controllers;
|
10 | 11 | using Microsoft.Azure.WebJobs.Script.WebHost.Management;
|
| 12 | +using Microsoft.Azure.WebJobs.Script.WebHost.Security; |
11 | 13 | using Microsoft.Extensions.Logging;
|
12 | 14 | using Microsoft.Extensions.Options;
|
13 | 15 | using Microsoft.WebJobs.Script.Tests;
|
@@ -90,5 +92,45 @@ public async Task SetState_Succeeds(string desiredState, ScriptHostState current
|
90 | 92 | Assert.False(fileExists);
|
91 | 93 | }
|
92 | 94 | }
|
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void GetAdminToken_Succeeds() |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + _mockEnvironment.Setup(p => p.GetEnvironmentVariable(It.Is<string>(k => k == EnvironmentSettingNames.ContainerName))).Returns<string>(v => v = "ContainerName"); |
| 101 | + |
| 102 | + var key = TestHelpers.GenerateKeyBytes(); |
| 103 | + var stringKey = TestHelpers.GenerateKeyHexString(key); |
| 104 | + using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, stringKey)) |
| 105 | + { |
| 106 | + // Act |
| 107 | + ObjectResult result = (ObjectResult)_hostController.GetAdminToken(); |
| 108 | + HttpStatusCode resultStatus = (HttpStatusCode)result.StatusCode; |
| 109 | + string token = (string)result.Value; |
| 110 | + |
| 111 | + // Assert |
| 112 | + Assert.Equal(HttpStatusCode.OK, resultStatus); |
| 113 | + Assert.True(SimpleWebTokenHelper.ValidateToken(token, new SystemClock())); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void GetAdminToken_Fails_NotLinuxContainer() |
| 119 | + { |
| 120 | + // Arrange |
| 121 | + _mockEnvironment.Setup(p => p.GetEnvironmentVariable(It.Is<string>(k => k == EnvironmentSettingNames.ContainerName))).Returns<string>(v => v = null); |
| 122 | + |
| 123 | + var key = TestHelpers.GenerateKeyBytes(); |
| 124 | + var stringKey = TestHelpers.GenerateKeyHexString(key); |
| 125 | + using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, stringKey)) |
| 126 | + { |
| 127 | + // Act |
| 128 | + ObjectResult result = (ObjectResult)_hostController.GetAdminToken(); |
| 129 | + HttpStatusCode resultStatus = (HttpStatusCode)result.StatusCode; |
| 130 | + |
| 131 | + // Assert |
| 132 | + Assert.Equal(HttpStatusCode.BadRequest, resultStatus); |
| 133 | + } |
| 134 | + } |
93 | 135 | }
|
94 | 136 | }
|
0 commit comments