Skip to content

Commit a766175

Browse files
author
agile.zhou
committed
Fix testcases
1 parent 23e80cb commit a766175

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

test/ApiSiteTests/TestApiConfigController.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
using System.Collections.Generic;
2-
using System.Threading.Tasks;
31
using AgileConfig.Server.Apisite.Controllers;
42
using AgileConfig.Server.Apisite.Controllers.api.Models;
53
using AgileConfig.Server.Apisite.Metrics;
64
using AgileConfig.Server.Apisite.Models;
75
using AgileConfig.Server.Common.EventBus;
86
using AgileConfig.Server.Data.Entity;
97
using AgileConfig.Server.IService;
8+
using Microsoft.AspNetCore.Http;
109
using Microsoft.AspNetCore.Mvc;
1110
using Microsoft.Extensions.Caching.Memory;
1211
using Microsoft.VisualStudio.TestTools.UnitTesting;
1312
using Moq;
13+
using System.Collections.Generic;
14+
using System.Threading.Tasks;
1415

1516
namespace ApiSiteTests;
1617

1718
[TestClass]
1819
public class TestApiConfigController
1920
{
2021
[TestMethod]
21-
public async Task TestGet()
22+
public async Task GetAppConfig_WithValidApp_ReturnsConfigs()
2223
{
2324
App newApp()
2425
{
@@ -55,8 +56,6 @@ List<Config> newConfigs()
5556
}
5657

5758
var configService = new Mock<IConfigService>();
58-
//configService.Setup(s => s.GetPublishedConfigsAsync("001"))
59-
// .ReturnsAsync(newConfigs);
6059
configService.Setup(s => s.GetPublishedConfigsByAppIdWithInheritance(It.IsAny<string>(), It.IsAny<string>()))
6160
.ReturnsAsync(newConfigs);
6261

@@ -65,20 +64,32 @@ List<Config> newConfigs()
6564
var eventBus = new Mock<ITinyEventBus>();
6665
var meterService = new Mock<IMeterService>();
6766

67+
var httpContext = new DefaultHttpContext();
68+
httpContext.Request.Headers["Authorization"] = "Basic MDAxOjE=";
69+
6870
var ctrl = new AgileConfig.Server.Apisite.Controllers.api.ConfigController(
6971
configService.Object,
7072
appService.Object,
7173
memoryCache,
7274
meterService.Object,
7375
new ConfigController(configService.Object, appService.Object, userSErvice.Object, eventBus.Object)
7476
);
77+
ctrl.ControllerContext = new ControllerContext
78+
{
79+
HttpContext = httpContext
80+
};
81+
7582
var act = await ctrl.GetAppConfig("001", new EnvString { Value = "DEV" });
7683

7784
Assert.IsNotNull(act);
7885
Assert.IsNotNull(act.Value);
7986
Assert.IsInstanceOfType(act.Value, typeof(List<ApiConfigVM>));
8087
Assert.AreEqual(2, act.Value.Count);
88+
}
8189

90+
[TestMethod]
91+
public async Task GetAppConfig_WithDisabledApp_ReturnsNotFound()
92+
{
8293
App newApp1()
8394
{
8495
return new App
@@ -87,17 +98,29 @@ App newApp1()
8798
};
8899
}
89100

90-
appService = new Mock<IAppService>();
101+
var appService = new Mock<IAppService>();
91102
appService.Setup(s => s.GetAsync(It.IsAny<string>())).ReturnsAsync(newApp1);
103+
var configService = new Mock<IConfigService>();
104+
IMemoryCache memoryCache = null;
105+
var userSErvice = new Mock<IUserService>();
106+
var eventBus = new Mock<ITinyEventBus>();
107+
var meterService = new Mock<IMeterService>();
108+
var httpContext = new DefaultHttpContext();
109+
httpContext.Request.Headers["Authorization"] = "Basic MDAxOjE=";
92110

93-
ctrl = new AgileConfig.Server.Apisite.Controllers.api.ConfigController(
111+
var ctrl = new AgileConfig.Server.Apisite.Controllers.api.ConfigController(
94112
configService.Object,
95113
appService.Object,
96114
memoryCache,
97115
meterService.Object,
98116
new ConfigController(configService.Object, appService.Object, userSErvice.Object, eventBus.Object)
99117
);
100-
act = await ctrl.GetAppConfig("001", new EnvString { Value = "DEV" });
118+
ctrl.ControllerContext = new ControllerContext
119+
{
120+
HttpContext = httpContext
121+
};
122+
123+
var act = await ctrl.GetAppConfig("001", new EnvString { Value = "DEV" });
101124

102125
Assert.IsNotNull(act);
103126
Assert.IsNull(act.Value);

0 commit comments

Comments
 (0)