예제 #1
0
        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");
        }
예제 #2
0
        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);
        }
예제 #3
0
        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));
        }
예제 #4
0
        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();
        }
예제 #5
0
        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);
        }
예제 #6
0
        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");
        }
예제 #7
0
#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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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();
        }
예제 #11
0
        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));
        }
예제 #12
0
        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");
        }
예제 #13
0
        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"));
        }
예제 #14
0
        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();
        }
예제 #15
0
        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"));
        }
예제 #16
0
        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));
        }
예제 #17
0
        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);
        }