예제 #1
0
        public async Task DeleteCourseTest()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var courseRepository = new EfDeletableEntityRepository <Course>(new ApplicationDbContext(options.Options));

            var mockUserCourseRepository = new Mock <IRepository <UserCourse> >();
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockCategoryService      = new Mock <ICategoriesService>();
            var store           = new Mock <IUserStore <ApplicationUser> >();
            var mockUserManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null);

            var service      = new CoursesService(courseRepository, mockUserCourseRepository.Object, mockConfiguration.Object, mockCategoryService.Object, mockUserManager.Object);
            var coursesToAdd = this.GetCourseData();

            foreach (var item in coursesToAdd)
            {
                await courseRepository.AddAsync(item);

                await courseRepository.SaveChangesAsync();
            }

            await service.DeleteCourse("1", "User1");

            await service.DeleteCourse("2", "User2");

            var courses = courseRepository.All();

            Assert.Equal(3, courses.Count());
        }
예제 #2
0
        public async Task DeleteCourse_ShouldNotDeleteCourse_WhenCourseNotExist()
        {
            Course deletedCourse = null;

            var courseRepoBuilder = new CoursesRepositoryBuilder();

            courseRepoBuilder.CoursesRepoMock.Setup(r => r.Delete(It.IsAny <Course>()))
            .Callback <Course>(c => deletedCourse = c);

            var courseRepo = courseRepoBuilder
                             .WithAll()
                             .Build();

            var sut = new CoursesService(null, courseRepo, null, Mapper);

            var user = new GoUser {
                Id = "9"
            };

            await sut.DeleteCourse("7", user);

            Assert.Null(deletedCourse);
            courseRepoBuilder.CoursesRepoMock.Verify();

            courseRepoBuilder.CoursesRepoMock.Verify(d => d.Delete(It.IsAny <Course>()), Times.Exactly(0));
            courseRepoBuilder.CoursesRepoMock.Verify(d => d.SaveChangesAsync(), Times.Exactly(0));
        }
예제 #3
0
        public async Task DeleteCourse_ShouldDeleteCourseCorrectly_IfCourseexist_AndUserIsCreator()
        {
            Course deletedCourse = null;

            var courseRepoBuilder = new CoursesRepositoryBuilder();

            courseRepoBuilder.CoursesRepoMock.Setup(r => r.Delete(It.IsAny <Course>()))
            .Callback <Course>(c => deletedCourse = c);

            var courseRepo = courseRepoBuilder
                             .WithAll()
                             .Build();

            var sut = new CoursesService(null, courseRepo, null, Mapper);

            var user = new GoUser {
                Id = "9"
            };

            await sut.DeleteCourse("2", user);

            Assert.Equal("2", deletedCourse.Id);
            courseRepoBuilder.CoursesRepoMock.Verify();

            courseRepoBuilder.CoursesRepoMock.Verify(d => d.Delete(It.IsAny <Course>()), Times.Once);
            courseRepoBuilder.CoursesRepoMock.Verify(d => d.SaveChangesAsync(), Times.Once);
        }
예제 #4
0
        public void DeleteCourse(int id)
        {
            string         login   = HttpContext.Session.GetString("User_Login");
            CoursesService service = new CoursesService(services);

            service.DeleteCourse(id, login);
        }
예제 #5
0
        public async Task DeleteCourse_ShouldNotDeleteCourse_WhenUserIsNotCreator()
        {
            var user = new GoUser {
                Id = "6"
            };

            var coursesRepoBuilder = new CoursesRepositoryBuilder();
            var courseRepo         = coursesRepoBuilder
                                     .WithAll()
                                     .Build();

            var sut = new CoursesService(null, courseRepo, null, Mapper);

            await sut.DeleteCourse("2", user);

            coursesRepoBuilder.CoursesRepoMock.Verify(c => c.Delete(It.IsAny <Course>()), Times.Exactly(0));
            coursesRepoBuilder.CoursesRepoMock.Verify(d => d.SaveChangesAsync(), Times.Exactly(0));
        }
예제 #6
0
        public async Task <IActionResult> Delete(int id, string returnUrl = "/")
        {
            var course = coursesService.GetCourse(id);

            if (course == null)
            {
                return(NotFound());
            }
            var authorizationResult = await authorizationService.AuthorizeAsync(User, course, Operations.Delete);

            if (authorizationResult.Succeeded)
            {
                coursesService.DeleteCourse(id);
                TempData["Toast"] = Toasts.CourseDeleted;
                return(Redirect(returnUrl));
            }
            else
            {
                return(Forbid());
            }
        }
예제 #7
0
 public async Task <ActionResult <Course> > DeleteCourse(long id)
 {
     return(await _coursesService.DeleteCourse(id));
 }