Exemplo n.º 1
0
        public async Task AddCourseShouldThrowIfUserNotTeacher()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "AddCourseShouldThrowIfUserNotTeacher")
                                 .Options;

            var courseName = "SQL";
            var startDate  = DateTime.Now;
            var endDate    = DateTime.Now.AddDays(30);

            using (var context = new AcademySiteContext(contextOptions))
            {
                var teacher = new User()
                {
                    Id       = 1,
                    UserName = "******",
                    RoleId   = 3
                };

                context.Users.Add(teacher);

                context.SaveChanges();

                var courseService = new CourseService(context);

                await Assert.ThrowsExceptionAsync <IncorrectPermissionsException>(async() => await courseService.AddCourseAsync(1, startDate, endDate, courseName));
            }
        }
Exemplo n.º 2
0
        public async Task AddAssignmentShouldThrowIfInvalidName()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "AddAssignmentShouldThrowIfInvalidName")
                                 .Options;

            string name      = null;
            var    courseId  = 1;
            var    teacherId = 1;
            var    dateDue   = DateTime.Now;

            using (var context = new AcademySiteContext(contextOptions))
            {
                var course = new Course
                {
                    CourseId  = 1,
                    Name      = "MockCourse",
                    TeacherId = 1
                };
                var teacher = new User
                {
                    Id       = 1,
                    UserName = "******"
                };
                context.Courses.Add(course);
                context.Users.Add(teacher);
                context.SaveChanges();

                var courseService = new CourseService(context);

                await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await courseService.AddAssignment(courseId, teacherId, 100, name, dateDue));
            }
        }
Exemplo n.º 3
0
        public async Task GetUserByIdShouldThrowExceptionWhenIdNotFound()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "GetUserByIdShouldThrowExceptionWhenIdNotFound")
                                 .Options;


            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev",
                    RoleId   = 2
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                var user        = await userService.GetUserByIdAsync(10);
            }
        }
Exemplo n.º 4
0
        public async Task GetUserByIdShouldReturnUser()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "GetUserByIdShouldReturnUser")
                                 .Options;


            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev",
                    RoleId   = 2
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                var user        = await userService.GetUserByIdAsync(1);

                Assert.IsTrue(user.UserName == "pesho12");
            }
        }
Exemplo n.º 5
0
        public async Task UpdateRoleThrowExceptionWhenPermissionNotValid()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "UpdateRoleThrowExceptionWhenPermissionNotValid")
                                 .Options;


            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev",
                    RoleId   = 2
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                await userService.UpdateRoleAsync(1, 1);
            }
        }
Exemplo n.º 6
0
        public async Task EvaluateStudentShouldThrowExceptionIfStudentAlreadyEvaluatedForThisCourse()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "EvaluateStudentShouldThrowExceptionIfStudentAlreadyEvaluatedForThisCourse")
                                 .Options;

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev - Uchitelq",
                    RoleId   = 2
                });

                context.Users.Add(new User()
                {
                    Id       = 2,
                    UserName = "******",
                    FullName = "Gosho Goshev - Studentcheto",
                    RoleId   = 3
                });
                context.Assignments.Add(new Assignment()
                {
                    Id        = 1,
                    Name      = "Grebane s lajica",
                    MaxPoints = 100,
                    CourseId  = 1
                });

                context.Courses.Add(new Course()
                {
                    CourseId  = 1,
                    Name      = "asdv",
                    TeacherId = 1
                });

                context.EnrolledStudents.Add(new EnrolledStudent()
                {
                    StudentId = 2,
                    CourseId  = 1
                });

                context.Grades.Add(new Grade()
                {
                    StudentId     = 2,
                    ReceivedGrade = 43,
                    AssignmentId  = 1
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                await userService.EvaluateStudentAsync(2, 1, 45, 1);
            }
        }
Exemplo n.º 7
0
        public async Task EnrollStudentShouldThrowIfCourseNotFound()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "EnrollStudentShouldThrowIfCourseNotFound")
                                 .Options;

            var student = new User()
            {
                Id       = 1,
                UserName = "******"
            };

            var course = new Course()
            {
                CourseId  = 1,
                Name      = "SQL",
                TeacherId = 2
            };

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(course);
                context.Users.Add(student);

                context.SaveChanges();

                var courseService = new CourseService(context);

                await Assert.ThrowsExceptionAsync <CourseDoesntExistsException>(async() => await courseService.EnrollStudentToCourseAsync(1, 2));
            }
        }
Exemplo n.º 8
0
        public void RetrieveStudentsInCourseShouldThrowIfUserNotFound()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "RetrieveStudentsInCourseShouldThrowIfUserNotFound")
                                 .Options;


            var course = new Course()
            {
                CourseId = 1,
                Name     = "SQL"
            };

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(course);

                context.SaveChanges();

                var courseService = new CourseService(context);

                var error = Assert.ThrowsExceptionAsync <IncorrectPermissionsException>
                                (async() => await courseService.RetrieveStudentsInCourseAsync(1, 2, 1)).GetAwaiter().GetResult();
                Assert.AreEqual("Invalid Permission", error.Message);
            }
        }
Exemplo n.º 9
0
        public async Task AddCourseShouldCorrectlyAddCourse()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "AddCourseShouldCorrectlyAddCourse")
                                 .Options;

            var courseName = "SQL";
            var startDate  = DateTime.Now;
            var endDate    = DateTime.Now.AddDays(30);

            using (var context = new AcademySiteContext(contextOptions))
            {
                var teacher = new User()
                {
                    Id       = 1,
                    UserName = "******",
                    RoleId   = 2
                };

                context.Users.Add(teacher);

                context.SaveChanges();

                var courseService = new CourseService(context);

                await courseService.AddCourseAsync(1, startDate, endDate, courseName);

                Assert.AreEqual(1, context.Courses.Count());
                Assert.IsTrue(context.Courses.Any(co => co.Name == "SQL" && co.TeacherId == 1));
            }
        }
Exemplo n.º 10
0
        public async Task EvaluateStudentShouldAddGradeWhenCorrectParametersArePassed()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "EvaluateStudentShouldAddGradeWhenCorrectParametersArePassed")
                                 .Options;

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev - Uchitelq",
                    RoleId   = 2
                });

                context.Users.Add(new User()
                {
                    Id       = 2,
                    UserName = "******",
                    FullName = "Gosho Goshev - Studentcheto",
                    RoleId   = 3
                });
                context.Assignments.Add(new Assignment()
                {
                    Id        = 1,
                    Name      = "Grebane s lajica",
                    MaxPoints = 100,
                    CourseId  = 1
                });

                context.Courses.Add(new Course()
                {
                    CourseId  = 1,
                    Name      = "asdv",
                    TeacherId = 1
                });

                context.EnrolledStudents.Add(new EnrolledStudent()
                {
                    StudentId = 2,
                    CourseId  = 1
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                await userService.EvaluateStudentAsync(2, 1, 45, 1);

                var users = userService.RetrieveUsersAsync(3).Result.ToList();
                Assert.IsTrue(users.Count == 1);
                Assert.IsTrue(users[0].Grades.Any(gr => gr.ReceivedGrade == 45));
            }
        }
Exemplo n.º 11
0
        public async Task RetrieveCoursesByStudentShouldReturnCorrectCourses()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "RetrieveCoursesByStudentShouldReturnCorrectCourses")
                                 .Options;

            var courseByStudent = new Course()
            {
                CourseId         = 1,
                Name             = "Grebane s lajica",
                EnrolledStudents = new List <EnrolledStudent>()
            };

            courseByStudent.EnrolledStudents.Add(new EnrolledStudent()
            {
                StudentId = 2, CourseId = 1
            });


            var courseNotByStudent = new Course()
            {
                CourseId = 2,
                Name     = "Pletene na potnici",
                Teacher  = new User
                {
                    UserName = "******",
                    RoleId   = 2
                }
            };

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(courseByStudent);

                context.Courses.Add(courseNotByStudent);

                context.Users.Add(new User()
                {
                    Id       = 2,
                    UserName = "******",
                    FullName = "Gosho Goshev - Studentcheto",
                    RoleId   = 3
                });

                context.SaveChanges();

                var courseService = new CourseService(context);

                var result = await courseService.RetrieveCoursesByStudentAsync(2);

                Assert.IsTrue(result.Count() == 1);
                Assert.IsTrue(result.Any(co => co.Name == "Grebane s lajica"));
            }
        }
Exemplo n.º 12
0
        public async Task UpdateRoleShouldUpdateRoleWhenAllParametersAreValid()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "UpdateRoleShouldUpdateRoleWhenAllParametersAreValid")
                                 .Options;

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev",
                    RoleId   = 2
                });
                context.Roles.Add(new Microsoft.AspNetCore.Identity.IdentityRole <int>()
                {
                    Name           = "SomeRole",
                    NormalizedName = "SOMEROLE"
                });
                context.SaveChanges();
                context.UserRoles.Add(new Microsoft.AspNetCore.Identity.IdentityUserRole <int>()
                {
                    UserId = 1, RoleId = 1
                });
                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                await userService.UpdateRoleAsync(1, 3);

                var user = await context.Users.FirstOrDefaultAsync(us => us.Id == 1);

                Assert.IsTrue(user.RoleId == 3);
                Assert.IsTrue(context.UserRoles.Where(ur => ur.UserId == user.Id).ToList().Count == 1);
                Assert.IsTrue(context.UserRoles.FirstOrDefault(ur => ur.UserId == user.Id).RoleId == 3);
            }
        }
Exemplo n.º 13
0
        public async Task EvaluateStudentShouldThrowExceptionIfTeacherIdIsInvalid()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "EvaluateStudentShouldThrowExceptionIfTeacherIdIsInvalid")
                                 .Options;

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev",
                    RoleId   = 1
                });

                context.Users.Add(new User()
                {
                    Id       = 2,
                    UserName = "******",
                    FullName = "Gosho Goshev",
                    RoleId   = 3
                });
                context.Assignments.Add(new Assignment()
                {
                    Id        = 1,
                    Name      = "Grebane s lajica",
                    MaxPoints = 100,
                    CourseId  = 1
                });

                context.Courses.Add(new Course()
                {
                    CourseId = 1,
                    Name     = "asdv"
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                await userService.EvaluateStudentAsync(1, 1, 45, 12);
            }
        }
Exemplo n.º 14
0
        public async Task RetrieveCoursesByTeacherShouldReturnCorrectCourses()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "RetrieveCoursesByTeacherShouldReturnCorrectCourses")
                                 .Options;

            var courseByTeacher = new Course()
            {
                CourseId = 1,
                Name     = "SQL",
                Teacher  = new User
                {
                    UserName = "******",
                    RoleId   = 2
                }
            };

            var courseNotByTeacher = new Course()
            {
                CourseId = 2,
                Name     = "SQL2",
                Teacher  = new User
                {
                    UserName = "******",
                    RoleId   = 2
                }
            };

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(courseByTeacher);

                context.Courses.Add(courseNotByTeacher);

                context.SaveChanges();

                var courseService = new CourseService(context);

                var result = await courseService.RetrieveCoursesByTeacherAsync(1);

                Assert.IsTrue(result.Count() == 1);
                Assert.IsTrue(result.Any(co => co.Teacher.UserName == "Pesho"));
            }
        }
Exemplo n.º 15
0
        public async Task GetAllCoursesShouldReturnCorses()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "GetAllCoursesShouldReturnCorses")
                                 .Options;


            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(new Course()
                {
                    Name    = "DummyCourse",
                    Start   = DateTime.Now,
                    Teacher = new User {
                        Id = 1, UserName = "******"
                    }
                });
                context.Courses.Add(new Course()
                {
                    Name    = "DummyCourseTwo",
                    Start   = DateTime.Now,
                    Teacher = new User {
                        Id = 2, UserName = "******"
                    }
                });
                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var courseService = new CourseService(context);
                var result        = await courseService.GetAllCoursesAsync();

                Assert.IsTrue(result.Count() == 2);
                Assert.IsTrue(result.Any(co => co.Name == "DummyCourse") && result.Any(co => co.Name == "DummyCourseTwo"));
            }
        }
Exemplo n.º 16
0
        public async Task AddAssignmentShouldAddAssignmentIfCorrectParameters()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "AddAssignmentShouldAddAssignmentIfCorrectParameters")
                                 .Options;

            string name      = "Test Assignment";
            var    courseId  = 1;
            var    teacherId = 1;
            var    dateDue   = DateTime.Now;

            using (var context = new AcademySiteContext(contextOptions))
            {
                var course = new Course
                {
                    CourseId  = 1,
                    Name      = "MockCourse",
                    TeacherId = 1
                };
                var teacher = new User
                {
                    Id       = 1,
                    UserName = "******"
                };
                context.Courses.Add(course);
                context.Users.Add(teacher);
                context.SaveChanges();

                var courseService = new CourseService(context);

                var assignemnt = await courseService.AddAssignment(courseId, teacherId, 100, name, dateDue);

                Assert.IsTrue(assignemnt.Name == name);
                Assert.IsTrue(context.Assignments.Any(a => a.Name == name));
                Assert.IsTrue(context.Assignments.Count() == 1);
            }
        }
Exemplo n.º 17
0
        public async Task EnrollStudentToCourseShouldEnrollIfAllParamsValid()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "EnrollStudentToCourseShouldEnrollIfAllParamsValid")
                                 .Options;

            var student = new User()
            {
                Id       = 1,
                UserName = "******"
            };

            var course = new Course()
            {
                CourseId  = 1,
                Name      = "SQL",
                TeacherId = 2
            };

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(course);
                context.Users.Add(student);

                context.SaveChanges();

                var courseService = new CourseService(context);

                await courseService.EnrollStudentToCourseAsync(1, 1);

                var user = context.Users.Include(us => us.EnrolledStudents).FirstOrDefault(us => us.Id == 1);

                Assert.IsTrue(user.EnrolledStudents.Count == 1);
                Assert.IsTrue(user.EnrolledStudents.Any(es => es.CourseId == 1));
            }
        }
Exemplo n.º 18
0
        public async Task EnrollStudentShouldThrowIfUserAlreadyEnrolled()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "EnrollStudentShouldThrowIfUserAlreadyEnrolled")
                                 .Options;

            var course = new Course()
            {
                CourseId  = 1,
                Name      = "SQL",
                TeacherId = 2
            };

            var student = new User()
            {
                Id               = 1,
                UserName         = "******",
                EnrolledStudents = new List <EnrolledStudent> {
                    new EnrolledStudent()
                    {
                        Course = course
                    }
                }
            };

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(student);

                context.SaveChanges();

                var courseService = new CourseService(context);

                await Assert.ThrowsExceptionAsync <EntityAlreadyExistsException>(async() => await courseService.EnrollStudentToCourseAsync(1, 1));
            }
        }
Exemplo n.º 19
0
        public async Task RetriveUsersShouldReturnUsers()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "RetriveUsersShouldThrowExceptionWhenNoUsersFound")
                                 .Options;

            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Users.Add(new User()
                {
                    Id       = 1,
                    UserName = "******",
                    FullName = "Pesho Peshev",
                    RoleId   = 3
                });

                context.Users.Add(new User()
                {
                    Id       = 2,
                    UserName = "******",
                    FullName = "Gosho Goshev",
                    RoleId   = 3
                });

                context.SaveChanges();
            }

            using (var context = new AcademySiteContext(contextOptions))
            {
                var userService = new UserService(context);
                var users       = await userService.RetrieveUsersAsync(3);

                var usersList = users.ToList();
                Assert.IsTrue(usersList.Count == 2);
            }
        }
Exemplo n.º 20
0
        public async Task RetrieveStudentsInCourseShouldRetrieveCorrectStudents()
        {
            var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>()
                                 .UseInMemoryDatabase(databaseName: "RetrieveStudentsInCourseShouldRetrieveCorrectStudents")
                                 .Options;

            var studentOne = new User()
            {
                Id       = 2,
                RoleId   = 3,
                UserName = "******"
            };

            var studentTwo = new User()
            {
                Id       = 3,
                RoleId   = 3,
                UserName = "******",
            };

            var studentThree = new User()
            {
                Id       = 4,
                RoleId   = 3,
                UserName = "******"
            };

            var actualTeacher = new User()
            {
                Id       = 1,
                RoleId   = 2,
                UserName = "******"
            };

            var course = new Course()
            {
                CourseId         = 1,
                Teacher          = actualTeacher,
                Name             = "SQL",
                EnrolledStudents = new List <EnrolledStudent>()
                {
                    new EnrolledStudent()
                    {
                        Student = studentOne, CourseId = 1
                    },
                    new EnrolledStudent()
                    {
                        Student = studentTwo, CourseId = 1
                    }
                }
            };


            using (var context = new AcademySiteContext(contextOptions))
            {
                context.Courses.Add(course);

                context.Users.Add(studentThree);

                context.SaveChanges();

                var courseService = new CourseService(context);

                var result = await courseService.RetrieveStudentsInCourseAsync(1, 2, 1);

                Assert.IsTrue(result.Count() == 2);
                Assert.IsTrue(result.Any(re => re.EnrolledStudents.Any(en => en.StudentId == 2)));
                Assert.IsTrue(result.Any(re => re.EnrolledStudents.Any(en => en.StudentId == 3)));
            }
        }