Ejemplo n.º 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");
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public async Task RunShouldCheckToken()
        {
            var context = BuildTestCase();
            var message = MockFunction.GetRequest(EmptyMsg, context);

            var result = await HangoutChatEvent.RunAsync(message, context);

            Assert.AreEqual(HttpStatusCode.Unauthorized, result.StatusCode);
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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");
        }
Ejemplo n.º 7
0
        public async Task RunShouldSaveInDb()
        {
            var message        = new Message();
            var context        = BuildTestCase();
            var requestMessage = MockFunction.GetRequest(FullMsg, context);

            context.Get <IHangoutsChatService>().BasicAsync(null).ReturnsForAnyArgs(message);

            await HangoutChatEvent.RunAsync(requestMessage, context);

            context.Get <IStorageService>().Received().SaveMessageAsync(message);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 9
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();
        }
Ejemplo n.º 10
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");
        }
Ejemplo n.º 11
0
        public async Task RunShouldCallService()
        {
            var message = new Message {
                Output = new ChatEventResult("OK")
            };
            var context        = BuildTestCase();
            var requestMessage = MockFunction.GetRequest(FullMsg, context);

            context.Get <IHangoutsChatService>()
            .BasicAsync(Arg.Is <ChatEvent>(it =>
                                           it.Message.Text == "What is this?" &&
                                           it.Message.Name == "spaces/q/messages/y"))
            .Returns(message);

            var result = await HangoutChatEvent.RunAsync(requestMessage, context);

            var resultOutput = GetStringResult(result);

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.AreEqual("{\"text\":\"OK\"}", resultOutput);
        }
Ejemplo n.º 12
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"));
        }
Ejemplo n.º 13
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));
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 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"));
        }
Ejemplo n.º 16
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);
        }