Пример #1
0
        private async Task AddSampleSubscriptions(TestingContext testingContext, CranUser user)
        {
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            IPrincipal           principal = testingContext.GetSimple <IPrincipal>();

            if (user == null)
            {
                user = await dbContext.CranUsers.FirstOrDefaultAsync(x => x.UserId == principal.Identity.Name);
            }


            dbContext.Notifications.Add(new NotificationSubscription()
            {
                Active   = true,
                Endpoint = "testendpoint",
                User     = user,
            });


            dbContext.Notifications.Add(new NotificationSubscription()
            {
                Active   = false,
                Endpoint = "testendpoint",
                User     = user,
            });

            await dbContext.SaveChangesAsync();
        }
Пример #2
0
        public async Task TestNotEnoughQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            context.AddPrincipalMock();
            context.AddBinaryServiceMock();
            context.AddInMemoryDb();
            context.AddMockLogService();
            context.AddGermanCultureServiceMock();
            context.AddQuestionService();
            CourseService courseService = context.GetService <CourseService>();

            context.DependencyMap[typeof(ICourseService)] = courseService;

            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Course course = dbContext.Courses.First();

            course.NumQuestionsToAsk = 100;

            ICourseInstanceService courseInstanceService = context.GetService <CourseInstanceService>();

            //Act
            var courseInstance = await courseInstanceService.StartCourseAsync(course.Id);

            //Assert
            Assert.Equal(10, courseInstance.NumQuestionsTotal);
        }
Пример #3
0
        public async Task UpdateQuestionOk()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            InitContext(testingContext);
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            testingContext.AddPrincipalMock(question.User.UserId, Roles.User);
            testingContext.AddBusinessSecurityService();
            IQuestionService questionService = testingContext.GetService <QuestionService>();

            QuestionDto dto = await questionService.GetQuestionAsync(question.Id);

            dto.Title = "Another Title";

            //Act
            await questionService.UpdateQuestionAsync(dto);

            //Assert
            QuestionDto dtoResult = await questionService.GetQuestionAsync(question.Id);

            Assert.Equal("Another Title", dtoResult.Title);
        }
Пример #4
0
        public async Task TestGetComments()
        {
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);

            ApplicationDbContext appDbContext    = testingContext.GetSimple <ApplicationDbContext>();
            ICommentsService     commentsService = testingContext.GetService <CommentsService>();

            Question question = appDbContext.Questions.First();

            for (int i = 0; i < 20; i++)
            {
                CommentDto commentDto = new CommentDto()
                {
                    IdQuestion  = question.Id,
                    CommentText = "Hello Comment",
                };
                await commentsService.AddCommentAsync(commentDto);
            }
            GetCommentsDto getCommentsDto = new GetCommentsDto
            {
                IdQuestion = question.Id,
                Page       = 0,
            };

            //Act
            PagedResultDto <CommentDto> comments = await commentsService.GetCommentssAsync(getCommentsDto);

            //Assert
            Assert.Equal(5, comments.Data.Count);
            Assert.Equal(4, comments.Numpages);
            Assert.Equal(20, comments.Count);
        }
Пример #5
0
        public async Task TestCopyQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            InitContext(context);
            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            context.AddPrincipalMock(question.User.UserId, Roles.User);

            IQuestionService questionService = context.GetService <QuestionService>();

            context.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = context.GetService <VersionService>();

            //Act
            int newId = await versionService.CopyQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);
            QuestionDto newQuestion = await questionService.GetQuestionAsync(newId);

            Assert.Equal(question.Options.Count, newQuestion.Options.Count);
            Assert.Equal(question.QuestionType, newQuestion.QuestionType);
            for (int i = 0; i < question.Options.Count; i++)
            {
                QuestionOption    optionSource      = question.Options[i];
                QuestionOptionDto optionDestination = newQuestion.Options[i];

                Assert.NotEqual(optionSource.Id, optionDestination.Id);
                Assert.True(optionDestination.Id > 0);
            }
        }
Пример #6
0
        public async Task TestAddPushNotificationSubscriptionAsync()
        {
            //Setup
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);

            INotificationService        notificationService = testingContext.GetService <NotificationService>();
            NotificationSubscriptionDto dto = new NotificationSubscriptionDto()
            {
                Endpoint = "Test",
                Keys     = new KeyDto()
                {
                    Auth   = "TestAut",
                    P256dh = "testp256",
                },
                ExpirationTime = DateTime.Today.AddYears(3),
            };

            //Act
            await notificationService.AddPushNotificationSubscriptionAsync(dto);

            //Assert
            ApplicationDbContext     dbContext = testingContext.GetSimple <ApplicationDbContext>();
            NotificationSubscription sub       = await dbContext.Notifications.LastAsync();

            Assert.Equal(dto.Endpoint, sub.Endpoint);
            Assert.True(sub.Id > 0);
        }
Пример #7
0
        public async Task TestTag()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddLogServiceMock();
            testingContext.AddRealDb();


            ApplicationDbContext context = testingContext.GetSimple <ApplicationDbContext>();

            Tag tag = new Tag
            {
                Name        = "Test",
                TagType     = TagType.Standard,
                Description = "Desc",
                ShortDescDe = "ShortDescDe",
                ShortDescEn = "ShortDescEn",
            };

            context.Tags.Add(tag);

            //Act
            await context.SaveChangesAsync();

            //Assert
            Assert.True(tag.Id > 0);

            //Cleanup
            context.Remove(tag);
            context.SaveChanges();
        }
Пример #8
0
        public async Task TestGetVersions()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            testingContext.AddPrincipalMock(question.User.UserId, Roles.User);
            testingContext.AddBusinessSecurityService();

            IQuestionService questionService = testingContext.GetService <QuestionService>();

            testingContext.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = testingContext.GetService <VersionService>();


            int newId = await versionService.VersionQuestionAsync(question.Id);

            await versionService.AcceptQuestionAsync(newId);

            VersionInfoParametersDto versionInfoParametersDto = new VersionInfoParametersDto()
            {
                Page       = 0,
                IdQuestion = newId,
            };

            //Act
            PagedResultDto <VersionInfoDto> result = await versionService.GetVersionsAsync(versionInfoParametersDto);

            //Assert
            Assert.Equal(2, result.Count);
        }
Пример #9
0
        public async Task GetTextEnTest()
        {
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddInMemoryDb();
            testingContext.AddLogServiceMock();
            testingContext.AddEnglishCultureServiceMock();

            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();

            dbContext.Texts.Add(new Text()
            {
                Key       = "TestKey",
                ContentDe = "ContentDe  {0} {1} {2}",
                ContentEn = "ContentEn {0} {1} {2}",
            });
            await dbContext.SaveChangesAsync();

            ITextService textService = testingContext.GetService <TextService>();

            //Act
            string text = await textService.GetTextAsync("TestKey", "pl1", "pl2", "pl3");

            //Assert
            Assert.Equal("ContentEn pl1 pl2 pl3", text);
        }
Пример #10
0
        public async Task TestImage()
        {
            //Perpare
            TestingContext context = new TestingContext();

            InitContext(context);
            context.DependencyMap[typeof(IBinaryService)] = context.GetService <BinaryService>();
            IQuestionService questionService = InitQuestionService(context);


            //Add Q
            QuestionDto qdto = new QuestionDto()
            {
                Title       = "Bla",
                Explanation = "bla",
                Language    = "De",
            };
            int newId = await questionService.InsertQuestionAsync(qdto);

            //Add Binary
            IBinaryService binaryService = context.GetSimple <IBinaryService>();
            int            id            = await binaryService.AddBinaryAsync(new BinaryDto
            {
                ContentDisposition = "ContentDisposition",
                ContentType        = "ContentType",
                FileName           = "FileName",
                Name   = "Name",
                Length = 2334,
            });


            ImageDto imageDto = new ImageDto()
            {
                IdBinary = id,
                Full     = false,
                Height   = 124,
                Width    = 64,
            };

            imageDto = await questionService.AddImageAsync(imageDto);

            QuestionDto questionDto = await questionService.GetQuestionAsync(newId);

            questionDto.Images.Add(imageDto);

            await questionService.UpdateQuestionAsync(questionDto);

            questionDto = await questionService.GetQuestionAsync(newId);

            Assert.True(questionDto.Images.Count == 1);
        }
Пример #11
0
        public async void TestPaging()
        {
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddInMemoryDb();
            testingContext.AddLogServiceMock();
            testingContext.AddEnglishCultureServiceMock();

            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();

            for (int i = 0; i < 13; i++)
            {
                dbContext.Texts.Add(new Text()
                {
                    Key       = $"TestKey{i}",
                    ContentDe = $"ContentDe",
                    ContentEn = $"ContentEn",
                });
            }

            await dbContext.SaveChangesAsync();

            ITextService textService = testingContext.GetService <TextService>();

            //Act
            SearchTextDto dto = new SearchTextDto();

            dto.Page = 0;
            PagedResultDto <TextDto> res = await textService.GetTextsAsync(dto);

            //Assert
            Assert.Equal(13, res.Count);
            Assert.Equal(5, res.Pagesize);
            Assert.Equal(5, res.Data.Count);
            Assert.Equal(3, res.Numpages);

            //Act
            dto      = new SearchTextDto();
            dto.Page = 2;
            res      = await textService.GetTextsAsync(dto);

            //Assert
            Assert.Equal(13, res.Count);
            Assert.Equal(5, res.Pagesize);
            Assert.Equal(3, res.Data.Count);
            Assert.Equal(3, res.Numpages);
        }
Пример #12
0
        public async Task GetQuestion()
        {
            //Perpare
            TestingContext       context         = new TestingContext();
            IQuestionService     questionService = InitQuestionService(context);
            ApplicationDbContext dbContext       = context.GetSimple <ApplicationDbContext>();
            int firstQuestionId = dbContext.Questions.First().Id;

            //Act
            QuestionDto questionDto = await questionService.GetQuestionAsync(firstQuestionId);

            //Assert
            Assert.Equal(firstQuestionId, questionDto.Id);
            Assert.Equal(4, questionDto.Tags.Count);
            Assert.True(questionDto.Tags.All(x => x.IdTagType == (int)TagType.Standard));
        }
Пример #13
0
        public async Task DeleteTagNoRights()
        {
            TestingContext testingContext = new TestingContext();

            SetupContext(testingContext);

            ITagService          tagService = testingContext.GetService <TagService>();
            ApplicationDbContext dbContext  = testingContext.GetSimple <ApplicationDbContext>();

            int firstTagId = dbContext.Tags.First().Id;

            //Act and Assert
            Exception ex = await Assert.ThrowsAsync <SecurityException>(async() =>
            {
                await tagService.DeleteTagAsync(firstTagId);
            });
        }
Пример #14
0
        public async Task TestVersionQuestion()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            testingContext.AddPrincipalMock(question.User.UserId, Roles.User);
            testingContext.AddBusinessSecurityService();

            IQuestionService questionService = testingContext.GetService <QuestionService>();

            testingContext.DependencyMap[typeof(IQuestionService)] = questionService;

            Mock <INotificationService> notificationMock = new Mock <INotificationService>(MockBehavior.Loose);

            notificationMock.Setup(x => x.SendNotificationAboutQuestionAsync(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);
            testingContext.DependencyMap[typeof(INotificationService)] = notificationMock.Object;

            IVersionService versionService = testingContext.GetService <VersionService>();

            //Act
            int newId = await versionService.VersionQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);

            await versionService.AcceptQuestionAsync(newId);

            //Assert
            QuestionDto oldDto = await questionService.GetQuestionAsync(question.Id);

            QuestionDto newDto = await questionService.GetQuestionAsync(newId);

            Assert.Contains(oldDto.Tags, x => x.Name == "Deprecated");
            Assert.Equal(QuestionStatus.Released, (QuestionStatus)newDto.Status);
            Assert.Equal(QuestionStatus.Obsolete, (QuestionStatus)oldDto.Status);
            question = await dbContext.FindAsync <Question>(newId);

            Assert.NotNull(question.ApprovalDate);
            notificationMock.Verify(x => x.SendNotificationAboutQuestionAsync(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once());
        }
Пример #15
0
        public async Task GetTags()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            SetupContext(testingContext);

            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();

            ITagService tagService = testingContext.GetService <TagService>();
            IList <int> tagIds     = dbContext.Tags.Select(x => x.Id).Take(3).ToList();

            //Act
            IList <TagDto> tags = await tagService.GetTagsAsync(tagIds);

            //Assert
            Assert.Equal(3, tags.Count);
        }
Пример #16
0
        public async Task TestSendNotificationAsync()
        {
            //Setup
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);
            await AddSampleSubscriptions(testingContext, null);

            Mock <IWebPushClient> webPushClientMock = new Mock <IWebPushClient>(MockBehavior.Loose);


            string           messageSent = string.Empty;
            PushSubscription psSent      = null;
            VapidDetails     vapiDetSent = null;

            webPushClientMock.Setup(x => x.SendNotificationAsync(It.IsAny <PushSubscription>(),
                                                                 It.IsAny <string>(), It.IsAny <VapidDetails>()))
            .Callback <PushSubscription, string, VapidDetails>((ps, message, vapiDet) => {
                messageSent = message;
                psSent      = ps;
                vapiDetSent = vapiDet;
            })
            .Returns(Task.CompletedTask);

            testingContext.DependencyMap[typeof(IWebPushClient)] = webPushClientMock.Object;
            INotificationService notificationService = testingContext.GetService <NotificationService>();

            NotificationDto dto = new NotificationDto();

            dto.SubscriptionId = testingContext.GetSimple <ApplicationDbContext>().Notifications.First().Id;
            dto.Text           = "TestText";
            dto.Title          = "TestTitle";



            //Act
            await notificationService.SendNotificationToUserAsync(dto);


            //Assert
            webPushClientMock.Verify(x => x.SendNotificationAsync(It.IsAny <PushSubscription>(),
                                                                  It.IsAny <string>(), It.IsAny <VapidDetails>()), Times.Once());
            Assert.Matches("TestText", messageSent);
        }
Пример #17
0
        public async Task TestQuestion()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddLogServiceMock();
            testingContext.AddRealDb();


            ApplicationDbContext context = testingContext.GetSimple <ApplicationDbContext>();


            Container container = new Container();

            context.Containers.Add(container);

            Question question1 = new Question();

            question1.Container = container;
            question1.Title     = "hello";
            question1.Text      = "Hello Text";
            question1.Status    = QuestionStatus.Created;
            question1.User      = await GetTestUserAsync(context);

            question1.Language     = Language.De;
            question1.QuestionType = QuestionType.SingleChoice;
            context.Questions.Add(question1);


            //Act
            context.SaveChanges();

            //Assert
            Question found = await context.FindAsync <Question>(question1.Id);

            Assert.NotNull(found);
            Assert.Equal(QuestionType.SingleChoice, found.QuestionType);

            //Cleanup
            context.Remove(question1);
            context.Remove(container);
            context.SaveChanges();
        }
Пример #18
0
        public async Task UpdateQuestionNoAccess()
        {
            //Perpare
            TestingContext   context         = new TestingContext();
            IQuestionService questionService = InitQuestionService(context);

            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            int firstQuestionId            = dbContext.Questions.First().Id;

            QuestionDto dto = await questionService.GetQuestionAsync(firstQuestionId);

            dto.Title = "Another Title";

            //Act and Assert
            Exception ex = await Assert.ThrowsAsync <SecurityException>(async() =>
            {
                await questionService.UpdateQuestionAsync(dto);
            });
        }
Пример #19
0
        public async Task TestAddComment()
        {
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);

            ApplicationDbContext appDbContext    = testingContext.GetSimple <ApplicationDbContext>();
            ICommentsService     commentsService = testingContext.GetService <CommentsService>();

            Question   question   = appDbContext.Questions.First();
            CommentDto commentDto = new CommentDto()
            {
                IdQuestion  = question.Id,
                CommentText = "Hello Comment",
            };

            //Act
            int newId = await commentsService.AddCommentAsync(commentDto);

            Assert.True(newId > 0);
        }
Пример #20
0
        public async Task DeleteTagOk()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            SetupContext(testingContext);
            testingContext.AddAdminPrincipalMock();

            ITagService          tagService = testingContext.GetService <TagService>();
            ApplicationDbContext dbContext  = testingContext.GetSimple <ApplicationDbContext>();

            int firstTagId = dbContext.Tags.First().Id;

            //Act
            await tagService.DeleteTagAsync(firstTagId);

            //Assert
            Exception ex = await Assert.ThrowsAsync <EntityNotFoundException>(async() =>
            {
                TagDto dto = await tagService.GetTagAsync(firstTagId);
            });
        }
Пример #21
0
        public async Task TestSendNotificationAboutQuestionAsync()
        {
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);


            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             first     = await dbContext.Questions.LastAsync();

            await AddSampleSubscriptions(testingContext, first.User);


            string           messageSent = string.Empty;
            PushSubscription psSent      = null;
            VapidDetails     vapiDetSent = null;

            Mock <IWebPushClient> webPushClientMock = new Mock <IWebPushClient>(MockBehavior.Loose);

            webPushClientMock.Setup(x => x.SendNotificationAsync(It.IsAny <PushSubscription>(),
                                                                 It.IsAny <string>(), It.IsAny <VapidDetails>()))
            .Callback <PushSubscription, string, VapidDetails>((ps, message, vapiDet) => {
                messageSent = message;
                psSent      = ps;
                vapiDetSent = vapiDet;
            })
            .Returns(Task.CompletedTask);
            testingContext.DependencyMap[typeof(IWebPushClient)] = webPushClientMock.Object;

            INotificationService notificationService = testingContext.GetService <NotificationService>();

            // Act
            await notificationService.SendNotificationAboutQuestionAsync(first.Id, "MessageAkzeptiert", "Some");

            //Assert
            webPushClientMock.Verify(x => x.SendNotificationAsync(It.IsAny <PushSubscription>(),
                                                                  It.IsAny <string>(), It.IsAny <VapidDetails>()), Times.Once());
        }
Пример #22
0
        public async Task TestVersionQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            InitContext(context);
            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            context.AddPrincipalMock(question.User.UserId, Roles.User);

            IQuestionService questionService = context.GetService <QuestionService>();

            context.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = context.GetService <VersionService>();

            //Act
            int newId = await versionService.VersionQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);

            await versionService.AcceptQuestionAsync(newId);

            //Assert
            QuestionDto oldDto = await questionService.GetQuestionAsync(question.Id);

            QuestionDto newDto = await questionService.GetQuestionAsync(newId);

            Assert.Contains(oldDto.Tags, x => x.Name == "Deprecated");
            Assert.Equal(QuestionStatus.Released, (QuestionStatus)newDto.Status);
            Assert.Equal(QuestionStatus.Obsolete, (QuestionStatus)oldDto.Status);
            question = await dbContext.FindAsync <Question>(newId);

            Assert.NotNull(question.ApprovalDate);
        }
Пример #23
0
        public async Task TestNotEnoughQuestion()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            InitTestingContext(testingContext);

            CourseService courseService = testingContext.GetService <CourseService>();

            testingContext.DependencyMap[typeof(ICourseService)] = courseService;

            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Course course = dbContext.Courses.First();

            course.NumQuestionsToAsk = 100;

            ICourseInstanceService courseInstanceService = testingContext.GetService <CourseInstanceService>();

            //Act
            var courseInstance = await courseInstanceService.StartCourseAsync(course.Id);

            //Assert
            Assert.Equal(10, courseInstance.NumQuestionsTotal);
        }