public void AllInitialization()
 {
     this.testUser = new User("SampleName", "SamplePassword", Role.Lecturer);
     this.testData = new BangaloreUniversityDate();
     this.testController = new CoursesController(this.testData,
         this.testUser);
 }
Example #2
0
        public void Run()
        {
            while (true)
            {
                string input = this.UserInterface.ReadLine();
                if (input == null)
                {
                    break;
                }

                var route = new Route(input);
                var controllerType =
                    Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(type => type.Name == route.ControllerName);
                var controller = Activator.CreateInstance(controllerType, this.Database, this.User) as Controller;
                var action = controllerType.GetMethod(route.ActionName);
                object[] @params = MapParameters(route, action);
                try
                {
                    var view = action.Invoke(controller, @params) as IView;
                    this.UserInterface.WriteLine(view.Display());
                    this.User = controller.CurrentUser;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }
        public IView Register(string username, string password, string confirmPassword, string role)
        {
            if (password != confirmPassword)
            {
                throw new ArgumentException("The provided passwords do not match.");
            }

            this.EnsureNoLoggedInUser();

            var existingUser = this.Data.Users.GetByUsername(username);
            if (existingUser != null)
            {
                var message = string.Format("A user with username {0} already exists.", username);

                throw new ArgumentException(message);
            }

            Role userRole = (Role)Enum.Parse(typeof(Role), role, true);

            var user = new User(username, password, userRole);

            this.Data.Users.Add(user);

            return this.View(user);
        }
Example #4
0
        public void TestLogoutWithWrongUserRoleShouldReturnAppropriateMessage()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Guest);
            UsersController controller = new UsersController(data, user);

            controller.Logout().Display();
        }
        public void Logout_ShouldThrowAuthorizationFailedException_WhenThe_User_IsNotAuthorizedTo_PErform_CertainOperation()
        {
            User temporalUser = new User("RandomName", "WithRandomPassowrd", Role.Guest);
            this.testUserController.User = temporalUser;
            this.testUserController.HasCurrentUser = false;

            this.testUserController.Logout();
        }
Example #6
0
        public void TestLogoutIfNoUserIsLoggedInShouldReturnAppropriateMessage()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Lecturer);
            UsersController controller = new UsersController(data, null);

            controller.Logout().Display();
        }
Example #7
0
        public void Get_WithAnExistingID_ShouldReturnElement()
        {
            var repository = new Repository<User>();
            var user = new User("Ivan Ivanov", "123456", Role.Student);
            repository.Add(user);
            var result = repository.Get(1);

            Assert.AreEqual(user, result);
        }
        public void ValidUserShouldReturnCorrectString()
        {
            var user = new User("Pesho", "123455", Role.Student);
            var view = new Logout(user);

            string actualString = string.Format("User {0} logged out successfully.", user.Username);
            string expectedString = view.Display();

            Assert.AreEqual(expectedString, actualString);
        }
Example #9
0
        public void TestWithNoCourses()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Guest);
            CoursesController controller = new CoursesController(data, user);

            string message = controller.All().Display();

            Assert.AreEqual("No courses.", message);
        }
Example #10
0
        public void TestSuccessfullLogoutShouldReturnAppropriateMessage()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Lecturer);
            UsersController controller = new UsersController(data, user);

            string message = controller.Logout().Display();

            Assert.AreEqual("User firstLecturer logged out successfully.", message);
        }
        public void LogoutShouldSetUserToBeNull()
        {
            var user = new User("Pesho", "435321", Role.Lecturer);
            this.controller.User = user;

            Assert.AreEqual(this.controller.User, user);

            this.controller.Logout();

            Assert.AreEqual(this.controller.User, null);
        }
        public void LogoutShouldReturnViewDifferentFromNull()
        {
            var user = new User("Pesho", "435321", Role.Lecturer);
            this.controller.User = user;

            Assert.AreEqual(this.controller.User, user);

            var actualResult = this.controller.Logout().Model;
            //var expectedResult = string.Format("User {0} logged out successfully.", user.Username);

            Assert.AreNotEqual(null, actualResult);
        }
        public void TestLogout_InitializeGuestUser_ShouldThrow()
        {
            // Arrange
            User user = new User("gosho", "123456", Role.Guest);
            var database = new BangaloreUniversityData();
            var controller = new UsersController(database, user);

            // Act
            controller.Logout();

            // Assert
        }
Example #14
0
        public void Get_WithManyEntries_ShouldReturnCorrectEntry()
        {
            var repository = new Repository<User>();
            var user = new User("Ivan Ivanov", "123456", Role.Student);
            var user2 = new User("Ivan Ivanov2", "123456", Role.Student);
            var user3 = new User("Ivan Ivanov3", "123456", Role.Student);
            repository.Add(user);
            repository.Add(user2);
            repository.Add(user3);
            var result = repository.Get(2);

            Assert.AreEqual(user2, result);
        }
        public void TestLogout_LogoutValidUser_CurrentUserShouldBeNull()
        {
            // Arrange
            User user = new User("gosho", "123456", Role.Lecturer);
            var database = new BangaloreUniversityData();
            var controller = new UsersController(database, user);

            // Act
            controller.Logout();

            // Assert
            Assert.IsNull(controller.User, "User should be null.");
        }
        public void Get_Existing_Element()
        {
            var repository = new Repository<User>();
            var list = new List<User>();

            for (var i = 0; i < 100; i++)
            {
                var user = new User("user" + i, "123456" + i, Role.Student);

                repository.Add(user);
                list.Add(user);
            }

            var expected = repository.Get(1);

            Assert.AreEqual(expected, list[0]);
        }
        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);
        }
Example #18
0
        public void TestWithOneCourseAndNoStudents()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Lecturer);
            CoursesController controller = new CoursesController(data, user);

            controller.Create("Object-Oriented+Programming");
            string message = controller.All().Display();

            var courses = new StringBuilder();
            courses.AppendLine("All courses:");
            foreach (var course in data.Courses.Items)
            {
                courses.AppendFormat("{0} ({1} students)", course.CourseName, course.Students.Count);
            }

            Assert.AreEqual(courses.ToString(), message);
        }
Example #19
0
        /// <summary>
        /// Registers an user in the database if he passes validation for having no currently logged in user, having a matching password and having an existing user with given username in the database.
        /// </summary>
        /// <param name="username">The username of the user who wants to register.</param>
        /// <param name="password">The password of the user who wants to register.</param>
        /// <param name="confirmPassword">Conformation the password(same password typed again).</param>
        /// <param name="role">The role that the user will have (ex.Lecturer, Student).</param>
        /// <returns>Returns an IView of the user.</returns>
        public IView Register(string username, string password, string confirmPassword, string role)
        {
            if (password != confirmPassword)
            {
                throw new ArgumentException(Constants.PasswordsDoNotMatch);
            }

            this.EnsureNoLoggedInUser();

            var existingUser = this.Database.Users.GetByUsername(username);
            if (existingUser != null)
            {
                throw new ArgumentException(string.Format(Constants.UserAlreadyExists, username));
            }

            Role userRole = (Role)Enum.Parse(typeof(Role), role, true);
            var user = new User(username, password, userRole);
            this.Database.Users.Add(user);
            return this.CreateView(user);
        }
Example #20
0
        public void TestWithSeveralCoursesAndDifferentStudents()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Lecturer);
            CoursesController controller = new CoursesController(data, user);

            controller.Create("Object-Oriented+Programming");
            controller.Create("High-Quality+Code");
            controller.Create("Java+Basics");
            controller.Enroll(1);
            controller.Enroll(3);
            string message = controller.All().Display();

            var courses = new StringBuilder();
            courses.AppendLine("All courses:");
            foreach (var course in data.Courses.Items.OrderBy(c => c.CourseName).ThenByDescending(c => c.Students.Count))
            {
                courses.AppendFormat("{0} ({1} students)", course.CourseName, course.Students.Count).AppendLine();
            }

            Assert.AreEqual(courses.ToString().Trim(), message);
        }
Example #21
0
        public IView Register(string username, string password, string confirmPassword, string role)
        {
            // The two passwords do not match
            if (password != confirmPassword)
            {
                throw new ArgumentException("The provided passwords do not match.");
            }

            // There is already a logged in currentUser in the system
            this.EnsureNoLoggedInUser();

            // There is already a currentUser with the specified username in the system
            var existingUser = this.Data.Users.GetByUsername(username);
            if (existingUser != null)
            {
                throw new ArgumentException(string.Format("A currentUser with username {0} already exists.", username));
            }

            // Success 
            var userRole = (Role)Enum.Parse(typeof(Role), role, true);
            var user = new User(username, password, userRole);
            this.Data.Users.Add(user);
            return this.View(user);
        }
 public Login(User user)
 : base(user)
 {
 }
Example #23
0
 public UsersController(IBangaloreUniversityDatabase data, User user)
     : base(data, user)
 {
     this.Database = data;
     this.CurrentUser = user;
 }
Example #24
0
 public void AddStudent(User student)
 {
     this.Students.Add(student);
 }
 public UsersController(IBangaloreUniversityData data, User user)
 {
     this.Data = data;
     this.User = user;
 }
 public Register(User user)
 : base(user)
 {
 }
Example #27
0
 protected Controller(IBangaloreUniversityData data, User user)
 {
     this.Data = data;
     this.User = user;
 }
Example #28
0
 public CoursesController(IBangaloreUniversityDatabase data, User user)
     : base(data, user)
 {
 }
Example #29
0
 public void AddStudent(User student)
 {
     this.Students.Add(student);
     student.Courses.Add(this);
 }
 public CoursesController(IBangaloreUniversityData data, User user)
 {
     this.Data = data;
     this.CurrentUser = user;
 }