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); } }
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()); } }
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()); } }
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()); } }
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()); } }
public TodoesController GetController() { var optionsBuilder = new DbContextOptionsBuilder <finaltodoContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); _context = new finaltodoContext(optionsBuilder.Options); _controller = new TodoesController(_context); createdata(optionsBuilder.Options); return(new TodoesController(_context)); }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }