Exemplo n.º 1
0
        public void CourseGroup_EditCoursesGroup()
        {
            var loggedUsers = LoggedUsers.GetInstance();
            var user1       = new UserFactory().CreateAdmin(new User
            {
                Id       = 20,
                Login    = "******",
                Password = new Encryption.HasherFactory().GetHasher().Hash("Pass"),
                Rank     = Rank.Administrator
            });

            var(_, tokenAdmin) = loggedUsers.LogInUser(user1);
            var user2 = new UserFactory().CreateStudent(new User
            {
                Id       = 21,
                Login    = "******",
                Password = new Encryption.HasherFactory().GetHasher().Hash("Pass1"),
                Rank     = Rank.Student
            }, new StudentData());

            var(_, tokenStudent) = loggedUsers.LogInUser(user2);
            var field = new Field {
                Id = 1, Name = "Inf"
            };
            var fields = new List <Field> {
                field
            };
            var courseGroup = new CourseGroup {
                Id = 1, Field = field, FieldId = field.Id
            };
            var courseGroups = new List <CourseGroup> {
                courseGroup
            };

            var mockContext = new Mock <DatabaseContext>();

            var mockSetFields = CreateNewMockSetWithData(fields);

            mockContext.Setup(m => m.Set <Field>()).Returns(mockSetFields.Object);
            var mockSetCG = CreateNewMockSetWithData(courseGroups);

            mockContext.Setup(m => m.Set <CourseGroup>()).Returns(mockSetCG.Object);

            var oldDb = RepositoryFactory.SetDbContext(mockContext.Object);


            var rightData = new Dictionary <string, string>
            {
                ["id"]       = "1",
                ["ects"]     = "29",
                ["semester"] = "6",
                ["fieldId"]  = "1",
                ["code"]     = "INEK000420",
                ["name"]     = "Analiza matematyczna 4",
            };
            var cgController = new CourseGroupController();

            // nieprawidłowy użytkownik
            var(result, _) = cgController.EditCoursesGroup(rightData, tokenStudent);
            Assert.IsFalse(result);
            // niezalogowany użytkownik
            (result, _) = cgController.EditCoursesGroup(rightData, "");
            Assert.IsFalse(result);
            // Nieprawidłowe Id grupy kursów
            var badData = new Dictionary <string, string>(rightData);

            badData["id"] = "2";
            (result, _)   = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            // Nieprawidłowe ects
            badData         = new Dictionary <string, string>(rightData);
            badData["ects"] = "asd";
            (result, _)     = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            badData         = new Dictionary <string, string>(rightData);
            badData["ects"] = "-1";
            (result, _)     = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            // Nieprawidłowy semestr
            badData             = new Dictionary <string, string>(rightData);
            badData["semester"] = "asd";
            (result, _)         = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            badData             = new Dictionary <string, string>(rightData);
            badData["semester"] = "-1";
            (result, _)         = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            // nieprawidłowy kierunek
            badData            = new Dictionary <string, string>(rightData);
            badData["fieldId"] = "asd";
            (result, _)        = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            badData            = new Dictionary <string, string>(rightData);
            badData["fieldId"] = "2";
            (result, _)        = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            // pusty kod grupy kursów
            badData         = new Dictionary <string, string>(rightData);
            badData["code"] = "";
            (result, _)     = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            // pusta nazwa grupy kursów
            badData         = new Dictionary <string, string>(rightData);
            badData["name"] = "";
            (result, _)     = cgController.EditCoursesGroup(badData, tokenAdmin);
            Assert.IsFalse(result);
            // prawidłowe
            (result, _) = cgController.EditCoursesGroup(rightData, tokenAdmin);
            Assert.IsTrue(result && courseGroup.Id == Int32.Parse(rightData["id"]) &&
                          courseGroup.NumberOfEcts == Int32.Parse(rightData["ects"]) &&
                          courseGroup.Semester == Int32.Parse(rightData["semester"]) &&
                          Object.ReferenceEquals(courseGroup.Field, field) &&
                          courseGroup.Code == rightData["code"] &&
                          courseGroup.Name == rightData["name"]);

            loggedUsers.LogOutUser(tokenStudent);
            loggedUsers.LogOutUser(tokenAdmin);
            RepositoryFactory.SetDbContext(oldDb);
        }
Exemplo n.º 2
0
        public void CourseGroupController_CheckPermissions()
        {
            // tworzenie studentów
            var password = new Encryption.HasherFactory().GetHasher().Hash("Pass");
            var admin    = new User {
                Id = 21, Login = "******", Password = password, Rank = Rank.Administrator
            };
            var student1 = new User {
                Id = 22, Login = "******", Password = password, Rank = Rank.Student
            };
            var student2 = new User {
                Id = 23, Login = "******", Password = password, Rank = Rank.Student
            };
            var student3 = new User {
                Id = 24, Login = "******", Password = password, Rank = Rank.Student
            };
            var student4 = new User {
                Id = 25, Login = "******", Password = password, Rank = Rank.Student
            };
            var student5 = new User {
                Id = 26, Login = "******", Password = password, Rank = Rank.Student
            };
            var users = new List <User> {
                admin, student1, student2, student3, student4, student5
            };
            // tworzenie danych dla semestrów
            var field1 = new Field {
                Id = 1, Name = "Inf"
            };
            var field2 = new Field {
                Id = 2, Name = "Eka"
            };
            var flieds = new List <Field> {
                field1, field2
            };
            // tworzenie danych studentów
            var studentData1 = new StudentData
            {
                Field = field1, Id = 1, FieldId = field1.Id, RegistrationDate = DateTime.Now.AddDays(-1), Semester = 1,
                User  = student1, UserId = student1.Id
            }; // poprawny
            var studentData2 = new StudentData
            {
                Field = field2, Id = 1, FieldId = field2.Id, RegistrationDate = DateTime.Now.AddDays(-1), Semester = 1,
                User  = student2, UserId = student2.Id
            }; // zły kierunek
            var studentData3 = new StudentData
            {
                Field = field1, Id = 1, FieldId = field1.Id, RegistrationDate = DateTime.Now.AddDays(1), Semester = 1,
                User  = student3, UserId = student3.Id
            }; // zła data
            var studentData4 = new StudentData
            {
                Field = field1, Id = 1, FieldId = field1.Id, RegistrationDate = DateTime.Now.AddDays(-1), Semester = 2,
                User  = student4, UserId = student4.Id
            }; // zły semestr
            var studentData5 = new StudentData
            {
                Field  = field1, Id = 1, FieldId = field1.Id, RegistrationDate = null, Semester = 1, User = student5,
                UserId = student5.Id
            }; // nieprzydzielona data
            var studentDatas = new List <StudentData>
            {
                studentData1, studentData2, studentData3, studentData4, studentData5
            };

            // tworzenie danych grup kursów
            var courseGroups = new List <CourseGroup> {
                new CourseGroup {
                    Field = field1, Id = 1, FieldId = field1.Id, Semester = 1
                }
            };


            var mockContext = new Mock <DatabaseContext>();

            var mockSetUsers = CreateNewMockSetWithData(users);

            mockContext.Setup(m => m.Set <User>()).Returns(mockSetUsers.Object);
            var mockSetFields = CreateNewMockSetWithData(flieds);

            mockContext.Setup(m => m.Set <Field>()).Returns(mockSetFields.Object);
            var mockSetSD = CreateNewMockSetWithData(studentDatas);

            mockContext.Setup(m => m.Set <StudentData>()).Returns(mockSetSD.Object);
            var mockSetCG = CreateNewMockSetWithData(courseGroups);

            mockContext.Setup(m => m.Set <CourseGroup>()).Returns(mockSetCG.Object);

            var oldDb = RepositoryFactory.SetDbContext(mockContext.Object);

            var cgController = new CourseGroupController();

            // admin
            var(result, _) = cgController.CheckPermissions(new UserFactory().CreateAdmin(admin), 1);
            Assert.IsTrue(result);
            // poprawny student
            (result, _) = cgController.CheckPermissions(new UserFactory().CreateStudent(student1, studentData1), 1);
            Assert.IsTrue(result);
            // zły kierunek
            (result, _) = cgController.CheckPermissions(new UserFactory().CreateStudent(student2, studentData2), 1);
            Assert.IsFalse(result);
            // zła data
            (result, _) = cgController.CheckPermissions(new UserFactory().CreateStudent(student3, studentData3), 1);
            Assert.IsFalse(result);
            // zły semestr
            (result, _) = cgController.CheckPermissions(new UserFactory().CreateStudent(student4, studentData4), 1);
            Assert.IsFalse(result);
            // nieprzydzielona data
            (result, _) = cgController.CheckPermissions(new UserFactory().CreateStudent(student5, studentData5), 1);
            Assert.IsFalse(result);
            // przydzielenie aktualnej daty
            studentData5.RegistrationDate = DateTime.Now;
            (result, _) = cgController.CheckPermissions(new UserFactory().CreateStudent(student5, studentData5), 1);
            Assert.IsTrue(result);

            RepositoryFactory.SetDbContext(oldDb);
        }