public IView Create(string name) { this.EnsureAuthorization(Role.Lecturer); var course = new Course(name); this.Database.Courses.Add(course); return this.CreateView(course); }
public IView Create(string name) { this.EnsureAuthorization(new[] { Role.Lecturer }); var course = new Course(name); this.Data.Courses.Add(course); return this.View(course); }
public void TestAddLecture_InvalidUser_ShouldThrow() { var course = new Course("Very advanced Java"); var mockedData = new Mock<IBangaloreUniversityData>(); var mockedCourseRepo = new Mock<IRepository<Course>>(); mockedCourseRepo.Setup(repo => repo.Get(It.IsAny<int>())).Returns(course); mockedData.Setup(data => data.Courses).Returns(mockedCourseRepo.Object); var controller = new CoursesController(mockedData.Object, null); controller.AddLecture(12, ""); }
public IView Create(string name) { // There is no logged in currentUser in the system this.EnsureThereIsLoggedInUser(); // The currently logged in currentUser is not a lecturer this.EnsureUserIsLecturer(); var course = new Course(name); this.Data.Courses.Add(course); return this.View(course); }
public IView Create(string name) { if (this.User.IsInRole(Role.Lecturer)) { throw new DivideByZeroException("The current user is not authorized to perform this operation."); } var course = new Course(name); Data.courses.Add(course); return View(course); }
public void TestAddAndSearchValidCourse() { // Arrange IRepository<Course> testRepo = new Repository<Course>(); Course testCourse = new Course("test course"); // Act testRepo.Add(testCourse); Course returnedCourse = testRepo.Get(1); // Assert Assert.AreEqual(testCourse, returnedCourse); }
public void AddLecture_WithValidInput_ShouldAddNewLectureToSpecifiedCourse() { var database = new Mock<IBangaloreUniversityDatabase>(); var course = new Course("Test1234"); Assert.AreEqual(0, course.Lectures.Count); database.Setup(a => a.Courses.Get(It.IsAny<int>())).Returns(course); var courseController = new CoursesController(database.Object, new User("Ivan Ivanov", "123456", Role.Lecturer)); var result = courseController.AddLecture(5, "TestLecture"); Assert.AreEqual(1, course.Lectures.Count); }
public void TestAddLecture_InvalidUserRole_ShouldThrow() { var course = new Course("Advanced Java"); var mockedData = new Mock<IBangaloreUniversityData>(); var mockedCoursesRepo = new Mock<IRepository<Course>>(); mockedCoursesRepo.Setup(repo => repo.Get(It.IsAny<int>())).Returns(course); mockedData.Setup(data => data.Courses).Returns(mockedCoursesRepo.Object); var controller = new CoursesController( mockedData.Object, new User("123456", "123456", Role.Student)); var view = controller.AddLecture(800, "ABC"); }
public void TestNonEmptyRepositoryNegativeIndex_ShouldReturnNullObject() { // Arrange IRepository<Course> testRepo = new Repository<Course>(); Course testCourse = new Course("test course"); // Act testRepo.Add(testCourse); Course returnedCourse = testRepo.Get(-1); // Assert Assert.AreEqual(returnedCourse, null); }
public void InitializeMock() { var dataMock = new Mock<IBangaloreUniversityData>(); var courseRepoMock = new Mock<IRepository<Course>>(); this.course = new Course("C# for babies"); courseRepoMock.Setup(r => r.Get(It.IsAny<int>())) .Returns(this.course); dataMock.Setup(d => d.Courses) .Returns(courseRepoMock.Object); this.mockedData = dataMock.Object; }
public void TestAddLecture_ValidCourse_ShouldAddToCourse() { var course = new Course("Advanced Java"); var mockedData = new Mock<IBangaloreUniversityData>(); var mockedCoursesRepo = new Mock<IRepository<Course>>(); mockedCoursesRepo.Setup(repo => repo.Get(It.IsAny<int>())).Returns(course); mockedData.Setup(data => data.Courses).Returns(mockedCoursesRepo.Object); var controller = new CoursesController( mockedData.Object, new User("123456", "123456", Role.Lecturer)); var view = controller.AddLecture(800, "ABC"); Assert.AreEqual(course.Lectures.First().Name, "ABC"); Assert.IsNotNull(view); }
public IView Create(string name) { if (!this.HasCurrentUser) { throw new ArgumentException("There is no currently logged in user."); } if (!this.User.IsInRole(Role.Lecturer)) { throw new ArgumentException("The current user is not authorized to perform this operation."); } var course = new Course(name); this.Data.Courses.Items.Add(course); return this.View(course); }
public void TestAllCoursesMultipleCoursesNoStudents_ShouldReturnCourses() { // Arrange IBangaloreUniversityDatabase data = new BangaloreUniversityDatabase(); User testUser = new User("test user", "password", Role.Student); CoursesController controller = new CoursesController(data, testUser); Course testCourse1 = new Course("test course1"); Course testCourse2 = new Course("test course2"); Course testCourse3 = new Course("test course3"); Course testCourse4 = new Course("test course4"); Course testCourse5 = new Course("test course5"); Course testCourse6 = new Course("test course6"); Course testCourse7 = new Course("test course7"); Course testCourse8 = new Course("test course8"); Course testCourse9 = new Course("test course9"); Course testCourse10 = new Course("test course10"); data.Courses.Add(testCourse1); data.Courses.Add(testCourse2); data.Courses.Add(testCourse3); data.Courses.Add(testCourse4); data.Courses.Add(testCourse5); data.Courses.Add(testCourse6); data.Courses.Add(testCourse7); data.Courses.Add(testCourse8); data.Courses.Add(testCourse9); data.Courses.Add(testCourse10); // Act IView view = controller.All(); string result = view.Display(); StringBuilder expected = new StringBuilder(); expected.AppendLine("All courses:"); expected.AppendLine("test course1 (0 students)"); expected.AppendLine("test course10 (0 students)"); expected.AppendLine("test course2 (0 students)"); expected.AppendLine("test course3 (0 students)"); expected.AppendLine("test course4 (0 students)"); expected.AppendLine("test course5 (0 students)"); expected.AppendLine("test course6 (0 students)"); expected.AppendLine("test course7 (0 students)"); expected.AppendLine("test course8 (0 students)"); expected.Append("test course9 (0 students)"); // Assert Assert.AreEqual(expected.ToString(), result); }
public void AddLecture_InvalidCourseId_ShouldThrow() { var dataMock = new Mock<IBangaloreUniversityData>(); var courseRepoMock = new Mock<IRepository<Course>>(); this.course = null; courseRepoMock.Setup(r => r.Get(It.IsAny<int>())) .Returns(this.course); dataMock.Setup(d => d.Courses) .Returns(courseRepoMock.Object); this.mockedData = dataMock.Object; var controller = new CoursesController(this.mockedData, new User("nasko", "123456", Role.Lecturer)); var view = controller.AddLecture(5, DateTime.Now.ToString()); }
public void AddLecture_WithIncorrectRole_ShouldThrowCorrectException() { var database = new Mock<IBangaloreUniversityDatabase>(); var course = new Course("Test1234"); database.Setup(a => a.Courses.Get(It.IsAny<int>())).Returns(course); var courseController = new CoursesController(database.Object, new User("Ivan ivanov", "123456", Role.Student)); var exception = NUnit.Framework.Assert.Catch<AuthorizationFailedException>( () => { var result = courseController.AddLecture(5, "TestLecture"); }); Assert.AreEqual(Constants.NotAuthorized, exception.Message); }
public AddLecture(Course course) : base(course) { }
public Enroll(Course course) : base(course) { }
public IView Create(string name) { if (!this.HasCurrentUser) { throw new ArgumentException("There is no currently logged in user."); } // BUG FIX: Check if user is in role lecture and can add a course if (!this.User.IsInRole(Role.Lecturer)) { throw new AuthorizationFailedException("The current user is not authorized to perform this operation."); } var course = new Course(name); Data.Courses.Add(course); return this.View(course); }
public void TestNonEmptyRepository_ShouldReturnCorrectObject() { // Arrange IRepository<Course> testRepo = new Repository<Course>(); Course testCourse1 = new Course("test course1"); Course testCourse2 = new Course("test course2"); Course testCourse3 = new Course("test course3"); Course testCourse4 = new Course("test course4"); Course testCourse5 = new Course("test course5"); Course testCourse6 = new Course("test course6"); Course testCourse7 = new Course("test course7"); Course testCourse8 = new Course("test course8"); Course testCourse9 = new Course("test course9"); Course testCourse10 = new Course("test course10"); Course testCourse11 = new Course("test course11"); Course testCourse12 = new Course("test course12"); // Act testRepo.Add(testCourse1); testRepo.Add(testCourse2); testRepo.Add(testCourse3); testRepo.Add(testCourse4); testRepo.Add(testCourse5); testRepo.Add(testCourse6); testRepo.Add(testCourse7); testRepo.Add(testCourse8); testRepo.Add(testCourse9); testRepo.Add(testCourse10); testRepo.Add(testCourse11); testRepo.Add(testCourse12); Course returnedCourse = testRepo.Get(10); // Assert Assert.AreEqual(returnedCourse, testCourse10); }
public void TestAllCoursesOneCourse_ShouldReturnCourse() { // Arrange IBangaloreUniversityDatabase data = new BangaloreUniversityDatabase(); User testUser = new User("test user", "password", Role.Student); CoursesController controller = new CoursesController(data, testUser); Course testCourse = new Course("test course"); data.Courses.Add(testCourse); // Act IView view = controller.All(); string result = view.Display(); // Assert string expected = string.Format("All courses:{0}test course (0 students)", Environment.NewLine); Assert.AreEqual(expected, result); }
public void TestAllCoursesMultipleCoursesWithStudents_ShouldReturnCoursesWithCorrectStudentCountOrderedCorrectly() { // Arrange IBangaloreUniversityDatabase data = new BangaloreUniversityDatabase(); User testUser = new User("test user", "password", Role.Student); CoursesController controller = new CoursesController(data, testUser); Course testCourse1 = new Course("C# Basics"); Course testCourse2 = new Course("Java Basics"); Course testCourse3 = new Course("C Basics"); Course testCourse4 = new Course("JavaScript"); Course testCourse5 = new Course("Java Advanced"); Course testCourse6 = new Course("HTML & CSS"); Course testCourse7 = new Course("Java Basics"); Course testCourse8 = new Course("C# Advanced"); Course testCourse9 = new Course("C# Basics"); User student1 = new User("student1", "password", Role.Student); User student2 = new User("student2", "password", Role.Student); User student3 = new User("student3", "password", Role.Student); User student4 = new User("student4", "password", Role.Student); User student5 = new User("student5", "password", Role.Student); User student6 = new User("student6", "password", Role.Student); testCourse9.AddStudent(student1); testCourse9.AddStudent(student2); testCourse9.AddStudent(student3); testCourse9.AddStudent(student4); testCourse9.AddStudent(student5); testCourse9.AddStudent(student6); testCourse8.AddStudent(student1); testCourse8.AddStudent(student2); testCourse8.AddStudent(student3); testCourse8.AddStudent(student4); testCourse8.AddStudent(student5); testCourse1.AddStudent(student1); testCourse1.AddStudent(student2); testCourse1.AddStudent(student3); testCourse1.AddStudent(student4); testCourse1.AddStudent(student5); testCourse2.AddStudent(student2); testCourse2.AddStudent(student3); testCourse2.AddStudent(student4); testCourse2.AddStudent(student5); testCourse7.AddStudent(student4); testCourse7.AddStudent(student5); testCourse7.AddStudent(student6); testCourse5.AddStudent(student1); testCourse5.AddStudent(student3); testCourse6.AddStudent(student2); testCourse6.AddStudent(student3); testCourse3.AddStudent(student5); data.Courses.Add(testCourse1); data.Courses.Add(testCourse2); data.Courses.Add(testCourse3); data.Courses.Add(testCourse4); data.Courses.Add(testCourse5); data.Courses.Add(testCourse6); data.Courses.Add(testCourse7); data.Courses.Add(testCourse8); data.Courses.Add(testCourse9); // Act IView view = controller.All(); string result = view.Display(); StringBuilder expected = new StringBuilder(); expected.AppendLine("All courses:"); expected.AppendLine("C Basics (1 students)"); expected.AppendLine("C# Advanced (5 students)"); expected.AppendLine("C# Basics (6 students)"); expected.AppendLine("C# Basics (5 students)"); expected.AppendLine("HTML & CSS (2 students)"); expected.AppendLine("Java Advanced (2 students)"); expected.AppendLine("Java Basics (4 students)"); expected.AppendLine("Java Basics (3 students)"); expected.Append("JavaScript (0 students)"); // Assert Assert.AreEqual(expected.ToString(), result); }
public void EnrollCourse(Course course) { this.Courses.Add(course); }
public Create(Course course) : base(course) { }
public Details(Course course) : base(course) { }
public void TestAllCoursesMultipleCoursesWithStudents_ShouldReturnCoursesWithCorrectStudentCount() { // Arrange IBangaloreUniversityDatabase data = new BangaloreUniversityDatabase(); User testUser = new User("test user", "password", Role.Student); CoursesController controller = new CoursesController(data, testUser); Course testCourse1 = new Course("test course1"); Course testCourse2 = new Course("test course2"); Course testCourse3 = new Course("test course3"); Course testCourse4 = new Course("test course4"); Course testCourse5 = new Course("test course5"); Course testCourse6 = new Course("test course6"); Course testCourse7 = new Course("test course7"); Course testCourse8 = new Course("test course8"); Course testCourse9 = new Course("test course9"); User student1 = new User("student1", "password", Role.Student); User student2 = new User("student2", "password", Role.Student); User student3 = new User("student3", "password", Role.Student); User student4 = new User("student4", "password", Role.Student); User student5 = new User("student5", "password", Role.Student); User student6 = new User("student6", "password", Role.Student); testCourse1.AddStudent(student1); testCourse1.AddStudent(student2); testCourse1.AddStudent(student3); testCourse1.AddStudent(student4); testCourse1.AddStudent(student5); testCourse1.AddStudent(student6); testCourse2.AddStudent(student1); testCourse2.AddStudent(student2); testCourse2.AddStudent(student3); testCourse2.AddStudent(student4); testCourse2.AddStudent(student5); testCourse3.AddStudent(student1); testCourse3.AddStudent(student2); testCourse3.AddStudent(student3); testCourse3.AddStudent(student4); testCourse3.AddStudent(student5); testCourse4.AddStudent(student2); testCourse4.AddStudent(student3); testCourse4.AddStudent(student4); testCourse4.AddStudent(student5); testCourse5.AddStudent(student4); testCourse5.AddStudent(student5); testCourse5.AddStudent(student6); testCourse6.AddStudent(student1); testCourse6.AddStudent(student2); testCourse6.AddStudent(student3); testCourse7.AddStudent(student2); testCourse7.AddStudent(student3); testCourse8.AddStudent(student5); data.Courses.Add(testCourse1); data.Courses.Add(testCourse2); data.Courses.Add(testCourse3); data.Courses.Add(testCourse4); data.Courses.Add(testCourse5); data.Courses.Add(testCourse6); data.Courses.Add(testCourse7); data.Courses.Add(testCourse8); data.Courses.Add(testCourse9); // Act IView view = controller.All(); string result = view.Display(); StringBuilder expected = new StringBuilder(); expected.AppendLine("All courses:"); expected.AppendLine("test course1 (6 students)"); expected.AppendLine("test course2 (5 students)"); expected.AppendLine("test course3 (5 students)"); expected.AppendLine("test course4 (4 students)"); expected.AppendLine("test course5 (3 students)"); expected.AppendLine("test course6 (3 students)"); expected.AppendLine("test course7 (2 students)"); expected.AppendLine("test course8 (1 students)"); expected.Append("test course9 (0 students)"); // Assert Assert.AreEqual(expected.ToString(), result); }
public void AddLecture_WithoutLoggedUser_ShouldThrowCorrectException() { var database = new Mock<IBangaloreUniversityDatabase>(); var course = new Course("Test1234"); database.Setup(a => a.Courses.Get(It.IsAny<int>())).Returns(course); var courseController = new CoursesController(database.Object, null); var exception = NUnit.Framework.Assert.Catch<ArgumentException>( () => { var result = courseController.AddLecture(5, "TestLecture"); }); Assert.AreEqual(Constants.NoLoggedInUserMessage, exception.Message); }
public void AddCource(Course course) { this.Courses.Add(course); }
public void AddLecture_WithValidData_ReturnAnIViewObject() { var database = new Mock<IBangaloreUniversityDatabase>(); var course = new Course("Test1234"); Assert.AreEqual(0, course.Lectures.Count); database.Setup(a => a.Courses.Get(It.IsAny<int>())).Returns(course); var courseController = new CoursesController(database.Object, new User("Ivan Ivanov", "123456", Role.Lecturer)); var result = courseController.AddLecture(5, "TestLecture"); NUnit.Framework.Assert.IsInstanceOf(typeof(IView),result); }