public async Task GetUsersShouldQueryActiveUsers() { var storageService = Substitute.For <IStorageService>(); var data = new[] { new User { Id = "1", Name = "U1", Role = 1, } }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), GetAccessTokenServiceDescriptor()); var request = MockFunction.GetRequest(null, context); SetUserRole(request, UserRoles.Administrator); storageService.GetAllActiveUsersAsync().Returns(data); var result = await Queries.GetUsersAsync(request, context); Assert.AreEqual(result.First().Name, "U1"); }
public async Task GetMessagesStatisticsAsyncShouldQueryDocument() { var storageService = Substitute.For <IStorageService>(); var message1 = new Message { ProbabilityPercentage = 96 }; var message2 = new Message { ProbabilityPercentage = 82 }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), GetAccessTokenServiceDescriptor()); var req = MockFunction.GetRequest(null, context); SetUserRole(req, UserRoles.User); storageService.GetMessagesAsync().Returns(new[] { message1, message2 }); var result = await Queries.GetMessagesStatisticsAsync(req, context); var array = result.ToArray(); Assert.AreEqual(2, array.Length); Assert.AreEqual(90, array[0].ProbabilityPercentage); Assert.AreEqual(80, array[1].ProbabilityPercentage); }
public async Task GetPluginsAsyncShouldErrorWhenRequestedFromUserRole() { var context = MockFunction.GetContext(GetAccessTokenServiceDescriptor()); var request = MockFunction.GetRequest(null, context); SetUserRole(request, UserRoles.User); await Assert.ThrowsExceptionAsync <AccessViolationException>( () => Queries.GetPluginsAsync(request, context)); }
public async Task SyncUserAsyncShouldExecuteSync() { var connector = Substitute.For <IOpenAirConnector>(); var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IOpenAirConnector), connector)); await Commands.SyncUsersAsync(new TimerInfo(), context); connector.Received().SyncUsersAsync(); }
private static HttpRequestData GetRequestWithAuthHeader(string value) { var ctx = MockFunction.GetContext(); var req = MockFunction.GetRequest(null, ctx); var headers = new HttpHeadersCollection(); headers.Add("Authorization", value); req.Headers.Returns(headers); return(req); }
public async Task GetUserInfoShouldValidateToken() { var context = MockFunction.GetContext(GetAccessTokenServiceDescriptor()); var request = MockFunction.GetRequest(null, context); SetUserRole(request, UserRoles.User); var result = await Queries.GetUserInfoAsync(request, context); Assert.AreEqual(result.Role, "user"); }
#pragma warning restore CS4014 private FunctionContext BuildTestCase() { var storageService = Substitute.For <IStorageService>(); var hangoutsChatService = Substitute.For <IHangoutsChatService>(); var options = new GoogleCloudOptions("AAA", "B", "C", "D"); var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), new ServiceDescriptor(typeof(IHangoutsChatService), hangoutsChatService), new ServiceDescriptor(typeof(GoogleCloudOptions), options)); return(context); }
public async Task SaveUserPropertiesAsyncShouldAllowOnlyAdministrators() { var accessTokenService = Substitute.For <IAccessTokenService>(); var user = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.User }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService)); var request = MockFunction.GetRequest(null, context); accessTokenService.ValidateTokenAsync(request).Returns(user); await Commands.SavePluginsAsync(request, context); }
public async Task ExecuteTimesheetsReminderAsyncShouldSendNotifications() { var timesheetService = Substitute.For <ITimesheetService>(); var now = DateTime.Now; var dateTimeMinutes = (now.Minute / 10) * 10; var dateTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, dateTimeMinutes, 0, 0, now.Kind); var context = MockFunction.GetContext( new ServiceDescriptor(typeof(ITimesheetService), timesheetService), new ServiceDescriptor(typeof(Func <DateTime>), () => dateTime), new ServiceDescriptor(typeof(Func <TimeZoneInfo>), () => TimeZoneInfo.Local)); await Commands.ExecuteTimesheetsReminderAsync(new TimerInfo(), context); timesheetService.Received().SendScheduledTimesheetNotificationsAsync(dateTime); }
public async Task GetPluginsAsyncShouldCreatePluginsWhenNonInStorage() { var storageService = Substitute.For <IStorageService>(); var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), GetAccessTokenServiceDescriptor()); var request = MockFunction.GetRequest(null, context); SetUserRole(request, UserRoles.Administrator); var result = await Queries.GetPluginsAsync(request, context); Assert.AreEqual(10, result.Count()); storageService.Received().AddOrUpdatePluginsAsync(Arg.Is <IReadOnlyList <Plugin> >(list => list.Count == 10)).Wait(); }
public async Task ExecuteTimesheetsReminderAsyncShouldWorkOnDivisionByFive() { var timesheetService = Substitute.For <ITimesheetService>(); var dateTime = new DateTime(2021, 12, 1, 12, 16, 5, 123, DateTimeKind.Local); var context = MockFunction.GetContext( new ServiceDescriptor(typeof(ITimesheetService), timesheetService), new ServiceDescriptor(typeof(Func <DateTime>), () => dateTime), new ServiceDescriptor(typeof(Func <TimeZoneInfo>), () => TimeZoneInfo.Local)); await Commands.ExecuteTimesheetsReminderAsync(new TimerInfo(), context); timesheetService .Received() .SendScheduledTimesheetNotificationsAsync( new DateTime(2021, 12, 1, 12, 15, 0, 0, DateTimeKind.Local)); }
public async Task SavePluginsAsyncShouldClearCache() { var accessTokenService = Substitute.For <IAccessTokenService>(); var storageService = Substitute.For <IStorageService>(); var memoryCache = Substitute.For <IMemoryCache>(); var user = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.Administrator }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService), new ServiceDescriptor(typeof(IStorageService), storageService), new ServiceDescriptor(typeof(IMemoryCache), memoryCache)); var request = MockFunction.GetRequest("[]", context); accessTokenService.ValidateTokenAsync(request).Returns(user); await Commands.SavePluginsAsync(request, context); memoryCache.Received().Remove("plugins"); }
public async Task SavePluginsAsyncShouldSaveInStorage() { var accessTokenService = Substitute.For <IAccessTokenService>(); var storageService = Substitute.For <IStorageService>(); var memoryCache = Substitute.For <IMemoryCache>(); var user = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.Administrator }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService), new ServiceDescriptor(typeof(IStorageService), storageService), new ServiceDescriptor(typeof(IMemoryCache), memoryCache)); var request = MockFunction.GetRequest( "[{\"id\": \"1\", \"key\":\"a\", \"name\":\"A\",\"type\":\"a1\", \"enabled\": true, \"groups\": null }]", context); accessTokenService.ValidateTokenAsync(request).Returns(user); await Commands.SavePluginsAsync(request, context); storageService.Received().AddOrUpdatePluginsAsync(Arg.Is <IReadOnlyList <Plugin> >(list => list.Count == 1 && list[0].Id == "1")); }
public async Task GetSettingsShouldUpdateGroups() { var storageService = Substitute.For <IStorageService>(); var plugins = new[] { new Plugin { Id = "7239ed4d-5b95-4bdd-be2c-007c281e87e6", Name = "Jenkins Build Info", Enabled = true, Groups = new[] { new PluginPropertyGroup { Name = "Jenkins Hosts", UniqueName = "Jenkins.Hosts", } } } }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), GetAccessTokenServiceDescriptor()); var req = MockFunction.GetRequest(null, context); SetUserRole(req, UserRoles.Administrator); storageService.GetAllPluginsAsync().Returns(plugins); var result = await Queries.GetPluginsAsync(req, context); var p = result.FirstOrDefault(it => it.Name == "Jenkins Build Info"); Assert.AreEqual(2, p.Groups.Length); storageService.Received().AddOrUpdatePluginsAsync((Plugin[])result).Wait(); }
public async Task GetSettingsShouldQueryTheStorage() { var storageService = Substitute.For <IStorageService>(); var plugins = new[] { new Plugin { Id = "31235913-2cd7-4bb1-9af8-35efa521bb1d", Name = "Issues/Ticketing" } }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), GetAccessTokenServiceDescriptor()); var req = MockFunction.GetRequest(null, context); SetUserRole(req, UserRoles.Administrator); storageService.GetAllPluginsAsync().Returns(plugins); var result = await Queries.GetPluginsAsync(req, context); Assert.IsNotNull(result.FirstOrDefault(it => it.Name == "Issues/Ticketing")); }
public async Task SaveUserPropertiesAsyncShouldOnlyUpdateGroups() { var accessTokenService = Substitute.For <IAccessTokenService>(); var storageService = Substitute.For <IStorageService>(); var userInfo = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.User }; var user = new User { Id = "2", Name = "Original", Properties = null }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService), new ServiceDescriptor(typeof(IStorageService), storageService)); var request = MockFunction.GetRequest( "[{\"id\":\"2\",\"email\":\"[email protected]\",\"name\":\"Test\",\"properties\":{\"p1\":[[\"key\":\"k1\",\"value\":\"v1\"]]}}]", context); storageService.GetUserByEmailAsync("[email protected]").Returns(user); accessTokenService.ValidateTokenAsync(request).Returns(userInfo); await Commands.SavePluginsAsync(request, context); storageService.Received().AddOrUpdateUserAsync(Arg.Is <User>(u => u.Name == "Original" && u.Properties.Count == 1)); }
public async Task GetTimesheettatisticsAsyncShouldQueryDocument() { var storageService = Substitute.For <IStorageService>(); var stat1 = new Statistics <TimesheetStatistics[]> { Date = "2020-08-14", Time = "20:00", Data = new TimesheetStatistics[] { new TimesheetStatistics { DepartmentName = ".NET", State = TimesheetStates.Unsubmitted, }, new TimesheetStatistics { DepartmentName = ".NET", State = TimesheetStates.Unsubmitted, }, new TimesheetStatistics { DepartmentName = "QA", State = TimesheetStates.Unsubmitted, }, } }; var stat2 = new Statistics <TimesheetStatistics[]> { Date = "2020-08-07", Time = "20:00", Data = new TimesheetStatistics[] { new TimesheetStatistics { DepartmentName = ".NET", State = TimesheetStates.Unsubmitted, }, new TimesheetStatistics { DepartmentName = "QA", State = TimesheetStates.Unsubmitted, }, } }; var stats3 = new Statistics <TimesheetStatistics[]> [0]; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IStorageService), storageService), GetAccessTokenServiceDescriptor()); var request = MockFunction.GetRequest(null, context); SetUserRole(request, UserRoles.User); storageService.GetStatisticsAsync <TimesheetStatistics[]>(null, null) .ReturnsForAnyArgs(new[] { stat1 }, new[] { stat2 }, stats3); var result = await Queries.GetTimesheetStatisticsAsync(request, context); var array = result.ToArray(); Assert.AreEqual(4, array.Length); Assert.AreEqual("QA", array[0].Department); Assert.AreEqual(1, array[0].Count); Assert.AreEqual(".NET", array[1].Department); Assert.AreEqual(1, array[1].Count); Assert.AreEqual("QA", array[2].Department); Assert.AreEqual(1, array[2].Count); Assert.AreEqual(".NET", array[3].Department); Assert.AreEqual(2, array[3].Count); }