コード例 #1
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());
            }
        }
コード例 #2
0
 public void ClearDb()
 {
     using (var context = new SocialBackendContext(options))
     {
         context.User.RemoveRange(context.User);
         context.SaveChanges();
     };
     i = 999;
 }
コード例 #3
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);
            }
        }
コード例 #4
0
        public void SetupDb()
        {
            using (var context = new SocialBackendContext(options))
            {
                User userA = new User()
                {
                    username     = usernames[0],
                    emailAddress = emails[0],
                    password     = hashedPasswords[0],
                    authToken    = authTokens[0]
                };
                users.Add(userA);

                User userB = new User()
                {
                    username     = usernames[1],
                    emailAddress = emails[1],
                    password     = hashedPasswords[1],
                    authToken    = authTokens[1]
                };
                users.Add(userB);

                context.User.Add(userA);
                context.User.Add(userB);
                context.SaveChanges();

                Todo todoA = new Todo()
                {
                    task     = tasks[0],
                    dueDate  = dueDates[0],
                    complete = completes[0],
                    user     = context.User.Where(u => u.username == users[0].username).AsNoTracking().FirstOrDefault()
                };
                todos.Add(todoA);

                Todo todoB = new Todo()
                {
                    task     = tasks[1],
                    dueDate  = dueDates[1],
                    complete = completes[1],
                    user     = context.User.Where(u => u.username == users[1].username).AsNoTracking().FirstOrDefault()
                };
                todos.Add(todoB);

                context.Todo.Add(todoA);
                context.Todo.Add(todoB);
                context.SaveChanges();
            }
        }
コード例 #5
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));
            }
        }
コード例 #6
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());
            }
        }
コード例 #7
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());
            }
        }
コード例 #8
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());
            }
        }
コード例 #9
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());
            }
        }
コード例 #10
0
 public static void Initialize(IServiceProvider serviceProvider)
 {
     using (var context = new SocialBackendContext(
                serviceProvider.GetRequiredService <DbContextOptions <SocialBackendContext> >()))
     {
         if (context.User.Count() == 0)
         {
             context.User.AddRange(
                 new User
             {
                 username     = "******",
                 password     = "******",
                 emailAddress = "*****@*****.**",
                 authToken    = Guid.NewGuid().ToString(),
                 online       = false
             });
         }
         context.SaveChanges();
         if (context.Todo.Count() == 0)
         {
             context.Todo.AddRange(
                 new Todo
             {
                 task     = "Finish coding todo API",
                 dueDate  = DateTime.Now.AddDays(1),
                 complete = false,
                 user     = context.User.FirstOrDefault()
             },
                 new Todo
             {
                 task     = "Finish social work app",
                 dueDate  = DateTime.Now.AddDays(2),
                 complete = false,
                 user     = context.User.FirstOrDefault()
             });
         }
         context.SaveChanges();
     }
 }
コード例 #11
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());
            }
        }
コード例 #12
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());
            }
        }
コード例 #13
0
        public async Task TestEditTodo()
        {
            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,
                    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, updatedTodo) as IActionResult;

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

                var updatedDbTodo = await _todo.AsNoTracking().FirstOrDefaultAsync();

                Assert.AreEqual(dbTodo.id, updatedDbTodo.id);
                Assert.AreEqual(dbTodo.task, updatedDbTodo.task);
                Assert.AreEqual(dbTodo.dueDate, updatedDbTodo.dueDate);
                Assert.AreNotEqual(dbTodo.complete, updatedDbTodo.complete);

                Assert.AreEqual(2, context.Todo.Count());
            }
        }
コード例 #14
0
        public async Task TestLoginNonExistant()
        {
            using (var context = new SocialBackendContext(options))
            {
                // Given
                string username = "******";
                string password = "******";
                User   userA    = new User
                {
                    username = username,
                    password = password
                };

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

                // Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(NotFoundResult));
            }
        }
コード例 #15
0
        public async Task TestNonExistantUserPost()
        {
            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]
                };

                //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(BadRequestResult));
                Assert.AreEqual(2, context.Todo.Count());
            }
        }
コード例 #16
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));
            }
        }