Exemplo n.º 1
0
        public async Task Create_WithNonExistentUser_ShouldNotIncludeIntoDatabase()
        {
            await roleManager.CreateAsync(new IdentityRole("Student"));

            await roleManager.CreateAsync(new IdentityRole("Teacher"));

            await roleManager.CreateAsync(new IdentityRole("Principal"));


            EdumanUser teacher = new EdumanUser
            {
                UserName    = "******",
                IsConfirmed = true,
                Id          = "TestTeacherId"
            };

            this.userManager.CreateAsync(teacher).GetAwaiter();

            userManager.AddToRoleAsync(teacher, "Teacher").GetAwaiter();

            this.Context.SaveChanges();

            CreateAbsenceBindingModel model = new CreateAbsenceBindingModel()
            {
                StudentFirstName = "Student",
                DateAbsent       = DateTime.Now,
                StudentLastName  = "TestName",
                Subject          = "Maths"
            };

            // Assert
            var ex = Assert.ThrowsAsync <Exception>(() => absenceService.CreateAsync(model, "teacherName"));

            Assert.AreEqual(ex.Message, "The User is either non-existent or is not a student");
        }
Exemplo n.º 2
0
        public async Task All_DifferentUser_ShouldReturnNoData()
        {
            await roleManager.CreateAsync(new IdentityRole("Student"));

            await roleManager.CreateAsync(new IdentityRole("Teacher"));

            await roleManager.CreateAsync(new IdentityRole("Principal"));

            EdumanUser student = new EdumanUser
            {
                UserName    = "******",
                FirstName   = "Student",
                LastName    = "TestName",
                IsConfirmed = true,
                Id          = "TestStudentId"
            };
            EdumanUser anotherStudent = new EdumanUser
            {
                UserName    = "******",
                FirstName   = "Student1",
                LastName    = "TestName1",
                IsConfirmed = true,
                Id          = "TestStudentId1"
            };

            EdumanUser teacher = new EdumanUser
            {
                UserName    = "******",
                IsConfirmed = true,
                Id          = "TestTeacherId"
            };

            this.userManager.CreateAsync(student).GetAwaiter();
            this.userManager.CreateAsync(anotherStudent).GetAwaiter();
            this.userManager.CreateAsync(teacher).GetAwaiter();

            userManager.AddToRoleAsync(student, "Student").GetAwaiter();
            userManager.AddToRoleAsync(anotherStudent, "Student").GetAwaiter();
            userManager.AddToRoleAsync(teacher, "Teacher").GetAwaiter();

            this.Context.SaveChanges();

            CreateAbsenceBindingModel model = new CreateAbsenceBindingModel()
            {
                StudentFirstName = "Student1",
                DateAbsent       = DateTime.Now,
                StudentLastName  = "TestName1",
                Subject          = "Maths"
            };

            await absenceService.CreateAsync(model, "teacherName");

            var count   = (absenceService.GetAllAsync("TestStudentId").GetAwaiter().GetResult()).Count;
            var absence = absenceService.GetAllAsync("TestStudentId").GetAwaiter().GetResult().FirstOrDefault();

            // Assert
            Assert.AreEqual(0, count);
        }
Exemplo n.º 3
0
 public async Task <IActionResult> Create(CreateAbsenceBindingModel createAbsenceBindingModel)
 {
     try
     {
         await absenceService.CreateAsync(createAbsenceBindingModel, this.User.Identity.Name);
     }
     catch (Exception e)
     {
         if (e.Message == "The User is either non-existent or is not a student")
         {
             return(this.View("~/Views/Shared/NonExistentStudentPage.cshtml"));
         }
     }
     return(this.RedirectToAction("Index", "Home"));
 }
Exemplo n.º 4
0
        public async Task Create_WithValidData_ShouldIncludeIntoDatabase()
        {
            await roleManager.CreateAsync(new IdentityRole("Student"));

            await roleManager.CreateAsync(new IdentityRole("Teacher"));

            await roleManager.CreateAsync(new IdentityRole("Principal"));

            EdumanUser student = new EdumanUser
            {
                UserName    = "******",
                FirstName   = "Student",
                LastName    = "TestName",
                IsConfirmed = true,
                Id          = "TestStudentId"
            };

            EdumanUser teacher = new EdumanUser
            {
                UserName    = "******",
                IsConfirmed = true,
                Id          = "TestTeacherId"
            };

            this.userManager.CreateAsync(student).GetAwaiter();
            this.userManager.CreateAsync(teacher).GetAwaiter();

            userManager.AddToRoleAsync(student, "Student").GetAwaiter();
            userManager.AddToRoleAsync(teacher, "Teacher").GetAwaiter();

            this.Context.SaveChanges();

            CreateAbsenceBindingModel model = new CreateAbsenceBindingModel()
            {
                StudentFirstName = "Student",
                DateAbsent       = DateTime.Now,
                StudentLastName  = "TestName",
                Subject          = "Maths"
            };

            await absenceService.CreateAsync(model, "teacherName");

            var count = (absenceService.GetAllAsync("TestStudentId").GetAwaiter().GetResult()).Count;

            // Assert
            Assert.AreEqual(1, count);
        }
Exemplo n.º 5
0
        public async Task CreateAsync(CreateAbsenceBindingModel absenceModel, string teacherName)
        {
            EdumanUser Student = this.context.Users.FirstOrDefault(u =>
                                                                   u.FirstName == absenceModel.StudentFirstName && u.LastName == absenceModel.StudentLastName && u.IsConfirmed);

            EdumanUser Teacher =
                this.context.Users.FirstOrDefault(u => u.UserName == teacherName);

            if (Student == null || !(await userManager.IsInRoleAsync(Student, "Student")))
            {
                throw new Exception("The User is either non-existent or is not a student");
            }

            Absence absence = new Absence
            {
                DateAbsent = absenceModel.DateAbsent,
                StudentId  = Student.Id,
                Subject    = absenceModel.Subject,
                TeacherId  = Teacher.Id
            };

            this.context.Absences.Add(absence);
            this.context.SaveChanges();
        }