예제 #1
0
        public async Task TestEditOtherUsersTask()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0; //
                IQueryable <Todo> _todo = from t in context.Todo
                                          orderby t.id ascending
                                          select t;
                var dbTodo = await _todo.AsNoTracking().FirstOrDefaultAsync();

                Todo updatedTodo = new Todo()
                {
                    id       = dbTodo.id + 1,
                    task     = tasks[i],
                    complete = completes[i],
                    dueDate  = dueDates[i]
                };

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.UpdateTask(dbTodo.id + 1, updatedTodo) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #2
0
        public async Task TestNullCookiePost()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2; // task and user does not currently exists in db
                Todo newTodo = new Todo()
                {
                    task     = tasks[i],
                    complete = completes[i],
                    dueDate  = dueDates[i]
                };
                i = 3; //for null cookie

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.PostTodo(newTodo) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #3
0
        public async Task TestNullCookiePut()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2; // task does not currently exists in db
                Todo updatedTodo = new Todo()
                {
                    id       = (await context.Todo.AsNoTracking().FirstAsync()).id,
                    task     = tasks[i],
                    complete = completes[i],
                    dueDate  = dueDates[i]
                };

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.UpdateTask((await context.Todo.AsNoTracking().FirstAsync()).id, updatedTodo) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #4
0
        public async Task TestLogin()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0;
                string username = usernames[i];
                string password = passwords[i];
                User   user     = new User
                {
                    username = username,
                    password = password
                };

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             result          = await usersController.login(user);


                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(OkObjectResult));

                var okObject       = result as OkObjectResult;
                var returnedObject = okObject.Value;

                var v = returnedObject?.GetType().GetProperty("authToken")?.GetValue(returnedObject, null).ToString();
                Assert.IsNotNull(v);
                Assert.AreEqual(authTokens[i], v);
            }
        }
예제 #5
0
        public async Task TestDelete()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0;
                IQueryable <User> _user = from u in context.User
                                          orderby u.id ascending
                                          select u;
                var originalUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             result          = await usersController.DeleteUser(originalUser.id) as IActionResult;


                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(OkObjectResult));

                var okObject = result as OkObjectResult;
                originalUser = okObject.Value as User;
                originalUser = await context.User.Where(u => u.id == originalUser.id).FirstOrDefaultAsync();

                Assert.IsNull(originalUser);
            }
        }
예제 #6
0
        public async Task TestDeleteIncorrectUser()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0; //
                IQueryable <User> _user = from u in context.User
                                          orderby u.id ascending
                                          select u;
                var originalUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                int userId = originalUser.id;

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             resultA         = await usersController.DeleteUser(userId + 1);

                // Then
                Assert.IsNotNull(resultA);
                Assert.IsInstanceOfType(resultA, typeof(UnauthorizedResult));

                _user = from u in context.User
                        orderby u.id descending
                        select u;

                var nonDeletedUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                Assert.IsNotNull(nonDeletedUser);
            }
        }
예제 #7
0
        public async Task TestLoginIncorrectPassword()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 1;
                string username = usernames[i];
                string password = "******";
                User   user     = new User
                {
                    username = username,
                    password = password
                };

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             result          = await usersController.login(user);


                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(NotFoundResult));
            }
        }
예제 #8
0
        public async Task TestNonAuthorisedDelete()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2;
                IQueryable <User> _user = from u in context.User
                                          orderby u.id ascending
                                          select u;
                var originalUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             result          = await usersController.DeleteUser(originalUser.id);


                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));

                originalUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                Assert.IsNotNull(originalUser);
            }
        }
예제 #9
0
        public async Task TestRegister()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2;
                string username     = usernames[i];
                string password     = passwords[i];
                string emailAddress = emails[i];
                User   user         = new User
                {
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             result          = await usersController.RegisterUser(user);

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(CreatedResult));

                IQueryable <User> _user = from u in context.User
                                          orderby u.id descending
                                          select u;
                var dbUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                Assert.AreEqual(emails[i], dbUser.emailAddress);
                Assert.AreEqual(usernames[i], dbUser.username);
            }
        }
예제 #10
0
        public async Task TestGet()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0; //
                string username = usernames[i];
                string password = passwords[i];

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.GetTodo() as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(OkObjectResult));

                var okObject = result as OkObjectResult;
                var todo     = okObject.Value as List <Todo>;

                Assert.IsNotNull(todo);
                Assert.IsTrue(todo.Count == 1);
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #11
0
        public async Task TestDelete()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0;                                                                                                            // exisiting user
                var dbTodo = await context.Todo.AsNoTracking().Where(t => t.user.username == usernames[i]).FirstOrDefaultAsync(); // existing todo

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.DeleteTodo(dbTodo.id) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(OkObjectResult));
                Assert.AreEqual(1, context.Todo.Count());
                Assert.AreEqual(2, context.User.Count());

                var okObject    = result as OkObjectResult;
                var deletedItem = okObject.Value as Todo;

                Assert.AreEqual(dbTodo.id, deletedItem.id);
                Assert.AreEqual(dbTodo.task, deletedItem.task);
                Assert.AreEqual(dbTodo.complete, deletedItem.complete);
                Assert.AreEqual(dbTodo.dueDate, deletedItem.dueDate);
            }
        }
예제 #12
0
        public async Task TestUpdateUser()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2; //
                IQueryable <User> _user = from u in context.User
                                          orderby u.id descending
                                          select u;
                var originalUser = await _user.AsNoTracking().FirstOrDefaultAsync();

                int    userId       = originalUser.id;
                string username     = usernames[i];
                string password     = passwords[i];
                string emailAddress = emails[i];
                // new user has id one before user we want to edit
                User userA = new User
                {
                    id           = userId,
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };
                // i needs to be set to 1 to ensure correct
                i = 1;

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             resultA         = await usersController.UpdateUser(userId, userA);

                // Then
                Assert.IsNotNull(resultA);
                Assert.IsInstanceOfType(resultA, typeof(NoContentResult));

                User dbUser = await context.User.FindAsync(originalUser.id);

                Assert.AreEqual(originalUser.id, dbUser.id);
                Assert.AreNotEqual(originalUser.username, dbUser.username);
                Assert.AreNotEqual(originalUser.password, dbUser.password);
                Assert.AreNotEqual(originalUser.emailAddress, dbUser.emailAddress);
                Assert.AreEqual(originalUser.authToken, dbUser.authToken);
                Assert.AreEqual(originalUser.online, dbUser.online);

                userA.password = hashedPasswords[i];

                Assert.AreEqual(userA.id, dbUser.id);
                Assert.AreEqual(userA.username, dbUser.username);
                Assert.AreEqual(userA.password, dbUser.password);
                Assert.AreEqual(userA.emailAddress, dbUser.emailAddress);
                Assert.AreEqual(userA.authToken, dbUser.authToken);
                Assert.AreEqual(userA.online, dbUser.online);
            }
        }
예제 #13
0
        public async Task TestAuthorisedCookieAuthorisation()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 1;

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             result          = await usersController.checkLoggedIn();


                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(OkResult));
            }
        }
예제 #14
0
        public async Task TestNullCookieDelete()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 3; //for null cookie

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.DeleteTodo((await context.Todo.AsNoTracking().FirstOrDefaultAsync()).id) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #15
0
        public async Task TestNoCookieGet()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 3; //
                string username = usernames[i];
                string password = passwords[i];

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.GetTodo();

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #16
0
        public async Task TestDeleteNoCookie()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 3; //
                User originalUser = await context.User.AsNoTracking().FirstAsync();

                int userId = originalUser.id;

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             resultA         = await usersController.DeleteUser(userId);

                // Then
                Assert.IsNotNull(resultA);
                Assert.IsInstanceOfType(resultA, typeof(UnauthorizedResult));
                Assert.IsNotNull(await context.User.AsNoTracking().Where(u => u.id == userId).FirstOrDefaultAsync());
            }
        }
예제 #17
0
        public async Task TestPost()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2; //task 2 doesn't esists in db
                Todo newTodo = new Todo()
                {
                    task     = tasks[i],
                    complete = completes[i],
                    dueDate  = dueDates[i]
                };
                i = 1; // user 1 exists

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.PostTodo(newTodo) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(CreatedResult));

                IQueryable <Todo> _todo = from t in context.Todo
                                          orderby t.id descending
                                          select t;
                var newestTodo = await _todo.AsNoTracking().FirstOrDefaultAsync();

                Assert.AreEqual(newTodo.task, newestTodo.task);
                Assert.AreEqual(newTodo.complete, newestTodo.complete);
                Assert.AreEqual(newTodo.dueDate, newestTodo.dueDate);

                Assert.AreEqual(newTodo.user.emailAddress, users[i].emailAddress);
                Assert.AreEqual(newTodo.user.username, users[i].username);
                Assert.AreEqual(newTodo.user.password, users[i].password);
                Assert.AreEqual(newTodo.user.online, users[i].online);

                Assert.AreEqual(3, context.Todo.Count());
            }
        }
예제 #18
0
        public async Task TestDeleteNonDbAuthToken()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 2; //for non existant user with a valid authtoken
                IQueryable <Todo> _todo = from t in context.Todo
                                          orderby t.id descending
                                          select t;
                var dbTodo = await _todo.AsNoTracking().FirstOrDefaultAsync();

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.DeleteTodo(dbTodo.id) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #19
0
        public async Task TestDeleteOtherUsersTask()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                i = 0; //for authorised user
                IQueryable <Todo> _todo = from t in context.Todo
                                          orderby t.id descending
                                          select t;
                var dbTodo = await _todo.AsNoTracking().FirstOrDefaultAsync(); // todoItem belonging to different user

                //When
                ICookieService   fakeCookie     = new FakeCookieService();
                TodoesController todoController = new TodoesController(context, fakeCookie);
                var result = await todoController.DeleteTodo(dbTodo.id) as IActionResult;

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
예제 #20
0
        public async Task TestEmptyFieldRegister()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                string username     = null;
                string password     = null;
                string emailAddress = null;
                User   userA        = new User
                {
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };

                username     = "";
                password     = null;
                emailAddress = null;
                User userB = new User
                {
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };

                username     = null;
                password     = "";
                emailAddress = null;
                User userC = new User
                {
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };

                username     = null;
                password     = null;
                emailAddress = "";
                User userD = new User
                {
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };

                username     = "";
                password     = "";
                emailAddress = "";
                User userE = new User
                {
                    username     = username,
                    password     = password,
                    emailAddress = emailAddress
                };

                //When
                ICookieService  fakeCookie      = new FakeCookieService();
                UsersController usersController = new UsersController(context, fakeCookie);
                var             resultA         = await usersController.RegisterUser(userA);

                var resultB = await usersController.RegisterUser(userB);

                var resultC = await usersController.RegisterUser(userC);

                var resultD = await usersController.RegisterUser(userD);

                var resultE = await usersController.RegisterUser(userE);


                // Then
                Assert.IsNotNull(resultA);
                Assert.IsInstanceOfType(resultA, typeof(BadRequestResult));
                Assert.IsNotNull(resultB);
                Assert.IsInstanceOfType(resultB, typeof(BadRequestResult));
                Assert.IsNotNull(resultC);
                Assert.IsInstanceOfType(resultC, typeof(BadRequestResult));
                Assert.IsNotNull(resultD);
                Assert.IsInstanceOfType(resultD, typeof(BadRequestResult));
                Assert.IsNotNull(resultE);
                Assert.IsInstanceOfType(resultE, typeof(BadRequestResult));
            }
        }