class ListModelTests(TestCase): def setUp(self): self.user = User.objects.create_user( 'test', '*****@*****.**', 'test' ) self.todolist = TodoList(title='title', creator=self.user) self.todolist.save() self.todo = Todo( description='description', todolist_id=self.todolist.id, creator=self.user ) self.todo.save() def tearDown(self): self.todo.delete() self.todolist.delete() self.user.delete() def test_count_todos(self): self.assertEqual(self.todolist.count(), 1) new_todo = Todo( description='test', todolist_id=self.todolist.id, creator=self.user ) new_todo.save() self.assertEqual(self.todolist.count(), 2) def test_count_open_todos(self): self.assertEqual(self.todolist.count_open(), 1) new_todo = Todo( description='test', todolist_id=self.todolist.id, creator=self.user ) new_todo.save() self.assertEqual(self.todolist.count_open(), 2) new_todo.close() self.assertEqual(self.todolist.count_open(), 1) def test_count_closed_todos(self): self.assertEqual(self.todolist.count_finished(), 0) new_todo = Todo( description='test', todolist_id=self.todolist.id, creator=self.user ) new_todo.close() self.todo.close() self.assertEqual(self.todolist.count_finished(), 2) self.assertIsNotNone(new_todo.finished_at) self.todo.reopen() self.assertEqual(self.todolist.count_finished(), 1) self.assertIsNone(self.todo.finished_at)
class ListModelTests(TestCase): def setUp(self): self.user = User.objects.create_user( 'test', '*****@*****.**', 'test' ) self.todolist = TodoList(title='title', creator=self.user) self.todolist.save() self.todo = Todo( description='description', todolist_id=self.todolist.id, creator=self.user ) self.todo.save() def tearDown(self): self.todo.delete() self.todolist.delete() self.user.delete() def test_count_todos(self): self.assertEqual(self.todolist.count(), 1) new_todo = Todo( description='test', todolist_id=self.todolist.id, creator=self.user ) new_todo.save() self.assertEqual(self.todolist.count(), 2) def test_count_open_todos(self): self.assertEqual(self.todolist.count_open(), 1) new_todo = Todo( description='test', todolist_id=self.todolist.id, creator=self.user ) new_todo.save() self.assertEqual(self.todolist.count_open(), 2) new_todo.close() self.assertEqual(self.todolist.count_open(), 1) def test_count_closed_todos(self): self.assertEqual(self.todolist.count_finished(), 0) new_todo = Todo( description='test', todolist_id=self.todolist.id, creator=self.user ) new_todo.close() self.todo.close() self.assertEqual(self.todolist.count_finished(), 2) self.assertIsNotNone(new_todo.finished_at) self.todo.reopen() self.assertEqual(self.todolist.count_finished(), 1) self.assertIsNone(self.todo.finished_at)
def add_todolist(request): if request.method == 'POST': form = TodoListForm(request.POST) if form.is_valid(): user = request.user if request.user.is_authenticated() else None todolist = TodoList(title=request.POST['title'], creator=user) todolist.save() return redirect('lists:todolist', todolist_id=todolist.id) else: return render(request, 'lists/overview.html', {'form': form}) return redirect('lists:index')
def add_todolist(request): if request.method == 'POST': form = TodoListForm(request.POST) if form.is_valid(): user = request.user if request.user.is_authenticated() else None todolist = TodoList(title=request.POST['title'], creator=user) todolist.save() return redirect('lists:todolist', todolist_id=todolist.id) else: return render(request, 'lists/overview.html', {'form': form}) return redirect('lists:index')
def add_todolist(request): if request.method == "POST": form = TodoListForm(request.POST) if form.is_valid(): user = request.user if request.user.is_authenticated else None todolist = TodoList(title=request.POST["title"], creator=user) todolist.save() return redirect("lists:todolist", todolist_id=todolist.id) else: return render(request, "lists/overview.html", {"form": form}) return redirect("lists:index")
def new_todolist(request): if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): # create default todolist user = request.user if request.user.is_authenticated else None todolist = TodoList(creator=user) todolist.save() todo = Todo(description=request.POST['description'], todolist_id=todolist.id, creator=user) todo.save() return redirect('lists:todolist', todolist_id=todolist.id) else: return render(request, 'lists/index.html', {'form': form}) return redirect('lists:index')
class ListTests(TestCase): def setUp(self): self.user = User.objects.create_user('test', '*****@*****.**', 'test') self.todolist = TodoList(title='test title', creator=self.user) self.todolist.save() self.todo = Todo(description='save todo', due_date=THE_DATE, todolist_id=self.todolist.id, creator=self.user) self.todo.save() self.client.login(username='******', password='******') def tearDown(self): self.client.logout() self.user.delete() self.todolist.delete() self.todo.delete() def test_get_index_page(self): response = self.client.get(reverse('lists:index')) self.assertTemplateUsed(response, 'lists/index.html') self.assertIsNotNone(response.context['greeting']) def test_get_todolist_view(self): response = self.client.get(reverse('lists:todolist', kwargs={'todolist_id': self.todolist.id})) self.assertTemplateUsed(response, 'lists/todolist.html') self.assertIsInstance(response.context['form'], TodoForm) def test_add_todo_to_todolist_view(self): response = self.client.post(reverse('lists:todolist', kwargs={'todolist_id': self.todolist.id}), {'description': 'test'}) self.assertTemplateUsed(response, 'lists/todolist.html') self.assertIsInstance(response.context['form'], TodoForm) self.assertContains(response, 'test') def test_get_todolist_overview(self): response = self.client.get(reverse('lists:overview')) self.assertTemplateUsed(response, 'lists/overview.html') self.assertIsInstance(response.context['form'], TodoListForm) def test_get_todolist_overview_redirect_when_not_logged_in(self): self.client.logout() response = self.client.get(reverse('lists:overview')) self.assertRedirects(response, '/auth/login/?next=/todolists/') def test_add_todolist_to_todolist_overview(self): response = self.client.post(reverse('lists:overview'), {'title': 'some title'}) self.assertRedirects(response, '/todolist/add/', target_status_code=302, fetch_redirect_response=False) def test_filter_usage(self):
def new_todolist(request): if request.method == "POST": form = TodoListForm(request.POST) print("#" * 40) print(form) print("#" * 40) print(request.POST) if form.is_valid(): # create default todolist user = request.user if request.user.is_authenticated else None print(request) todolist = TodoList(creator=user, title=request.POST['title']) todolist.save() return redirect("lists:todolist", todolist_id=todolist.id) else: return render(request, "lists/index.html", {"form": TodoListForm}) return redirect("lists:index")
def new_todolist(request): if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): # create default todolist user = request.user if request.user.is_authenticated() else None todolist = TodoList(creator=user) todolist.save() todo = Todo( description=request.POST['description'], todolist_id=todolist.id, creator=user ) todo.save() return redirect('lists:todolist', todolist_id=todolist.id) else: return render(request, 'lists/index.html', {'form': form}) return redirect('lists:index')
def new_todolist(request): if request.method == "POST": form = TodoForm(request.POST) if form.is_valid(): # create default todolist user = request.user if request.user.is_authenticated else None todolist = TodoList(creator=user) todolist.save() todo = Todo( description=request.POST["description"], todolist_id=todolist.id, creator=user, ) todo.save() return redirect("lists:todolist", todolist_id=todolist.id) else: return render(request, "lists/index.html", {"form": form}) return redirect("lists:index")
def add_todolist(request): if request.method == 'POST': form = TodoListForm(request.POST) if form.is_valid(): user = request.user title = request.POST['title'] is_group_list = (title in Group.objects.all().values_list("name", flat=True)) title_exists = TodoList.objects.filter(title=title).exists() if is_group_list and title_exists and not user.is_superuser: messages.error( request, "A todolist with title {} already exists!".format(title)) return render(request, 'lists/overview.html', {'form': form}) todolist = TodoList(title=title, creator=user) todolist.save() return redirect('lists:todolist', todolist_id=todolist.id) else: return render(request, 'lists/overview.html', {'form': form}) return redirect('lists:index')
class TodoTests(APITestCase): def setUp(self): self.test_user = User.objects.create_user("test", "*****@*****.**", "test") self.client.login(username="******", password="******") self.test_todolist = TodoList(title="some title", creator=self.test_user) self.test_todolist.save() self.test_data = { "description": "some description", "todolist": self.test_todolist.id, } def tearDown(self): test_user = User.objects.get(username="******") test_user.delete() self.client.logout() def post_new_todo(self, data): url = reverse("api:todo-list") return self.client.post(url, data, format="json") def test_create_todo(self): response = self.post_new_todo(self.test_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data["description"], self.test_data["description"]) def test_get_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # get todo todo_id = post_response.data["id"] self.assertEqual(todo_id, 1) get_response = self.client.get(f"/api/todos/{todo_id}/") self.assertEqual(get_response.status_code, status.HTTP_200_OK) # check todo self.assertEqual(get_response.data, post_response.data) def test_get_when_not_logged_in(self): # add some data post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # make sure the user is logged out self.client.logout() response = self.client.get("/api/todos/1/") # expect 200, because reading is allowed for everybody self.assertEqual(response.status_code, status.HTTP_200_OK) def test_get_non_existent_todo(self): response = self.client.get("/api/todo/0/") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_post_for_anon_users(self): # make sure the user is logged out self.client.logout() # try posting a todo response = self.post_new_todo(self.test_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_put_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # put todo todo_id = post_response.data["id"] put_data = post_response.data put_data["description"] = "changed description" put_response = self.client.put(f"/api/todos/{todo_id}/", put_data, format="json") self.assertEqual(put_response.status_code, status.HTTP_200_OK) get_response = self.client.get(f"/api/todos/{todo_id}/") self.assertEqual(put_response.status_code, status.HTTP_200_OK) self.assertEqual(get_response.data["description"], "changed description") def test_put_todo_changing_status(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # put todo todo_id = post_response.data["id"] put_data = post_response.data put_data["is_finished"] = True put_response = self.client.put(f"/api/todos/{todo_id}/", put_data, format="json") self.assertEqual(put_response.status_code, status.HTTP_200_OK) get_response = self.client.get(f"/api/todos/{todo_id}/") self.assertEqual(put_response.status_code, status.HTTP_200_OK) self.assertEqual(get_response.data["is_finished"], True) def test_delete_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # delete todolist todo_id = post_response.data["id"] delete_response = self.client.delete(f"/api/todos/{todo_id}/") self.assertEqual(delete_response.status_code, status.HTTP_204_NO_CONTENT) # get todolist and expect 404 get_response = self.client.get(f"/api/todos/{todo_id}/") self.assertEqual(get_response.status_code, status.HTTP_404_NOT_FOUND)
class TodoTests(APITestCase): def setUp(self): self.test_user = User.objects.create_user( 'test', '*****@*****.**', 'test' ) self.client.login(username='******', password='******') self.test_todolist = TodoList( title='some title', creator=self.test_user ) self.test_todolist.save() self.test_data = { 'description': 'some description', 'todolist': self.test_todolist.id } def tearDown(self): test_user = User.objects.get(username='******') test_user.delete() self.client.logout() def post_new_todo(self, data): url = reverse('api:todo-list') return self.client.post(url, data, format='json') def test_create_todo(self): response = self.post_new_todo(self.test_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( response.data['description'], self.test_data['description'] ) def test_get_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # get todo todo_id = post_response.data['id'] self.assertEqual(todo_id, 1) get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(get_response.status_code, status.HTTP_200_OK) # check todo self.assertEqual(get_response.data, post_response.data) def test_get_when_not_logged_in(self): # add some data post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # make sure the user is logged out self.client.logout() response = self.client.get('/api/todos/1/') # expect 200, because reading is allowed for everybody self.assertEqual(response.status_code, status.HTTP_200_OK) def test_get_non_existent_todo(self): response = self.client.get('/api/todo/0/') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_post_for_anon_users(self): # make sure the user is logged out self.client.logout() # try posting a todo response = self.post_new_todo(self.test_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_put_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # put todo todo_id = post_response.data['id'] put_data = post_response.data put_data['description'] = 'changed description' put_response = self.client.put( '/api/todos/{0}/'.format(todo_id), put_data, format='json' ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) self.assertEqual( get_response.data['description'], 'changed description' ) def test_put_todo_changing_status(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # put todo todo_id = post_response.data['id'] put_data = post_response.data put_data['is_finished'] = True put_response = self.client.put( '/api/todos/{0}/'.format(todo_id), put_data, format='json' ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) self.assertEqual( get_response.data['is_finished'], True ) def test_delete_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # delete todolist todo_id = post_response.data['id'] delete_response = self.client.delete( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual( delete_response.status_code, status.HTTP_204_NO_CONTENT ) # get todolist and expect 404 get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(get_response.status_code, status.HTTP_404_NOT_FOUND)
class ListTests(TestCase): def setUp(self): self.user = User.objects.create_user("test", "*****@*****.**", "test") self.todolist = TodoList(title="test title", creator=self.user) self.todolist.save() self.todo = Todo( description="save todo", todolist_id=self.todolist.id, creator=self.user ) self.todo.save() self.client.login(username="******", password="******") def tearDown(self): self.client.logout() self.user.delete() self.todolist.delete() self.todo.delete() def test_get_index_page(self): response = self.client.get(reverse("lists:index")) self.assertTemplateUsed(response, "lists/index.html") self.assertIsInstance(response.context["form"], TodoForm) def test_add_todo_to_index_page(self): response = self.client.post(reverse("lists:index"), {"description": "test"}) self.assertTemplateUsed(response, "lists/index.html") self.assertIsInstance(response.context["form"], TodoForm) def test_get_todolist_view(self): response = self.client.get( reverse("lists:todolist", kwargs={"todolist_id": self.todolist.id}) ) self.assertTemplateUsed(response, "lists/todolist.html") self.assertIsInstance(response.context["form"], TodoForm) def test_add_todo_to_todolist_view(self): response = self.client.post( reverse("lists:todolist", kwargs={"todolist_id": self.todolist.id}), {"description": "test"}, ) self.assertTemplateUsed(response, "lists/todolist.html") self.assertIsInstance(response.context["form"], TodoForm) self.assertContains(response, "test") def test_get_todolist_overview(self): response = self.client.get(reverse("lists:overview")) self.assertTemplateUsed(response, "lists/overview.html") self.assertIsInstance(response.context["form"], TodoListForm) def test_get_todolist_overview_redirect_when_not_logged_in(self): self.client.logout() response = self.client.get(reverse("lists:overview")) self.assertRedirects(response, "/auth/login/?next=/todolists/") def test_add_todolist_to_todolist_overview(self): response = self.client.post(reverse("lists:overview"), {"title": "some title"}) self.assertRedirects( response, "/todolist/add/", target_status_code=302, fetch_redirect_response=False, )
class ListTests(TestCase): def setUp(self): self.user = User.objects.create_user( 'test', '*****@*****.**', 'test' ) self.todolist = TodoList(title='test title', creator=self.user) self.todolist.save() self.todo = Todo( description='save todo', todolist_id=self.todolist.id, creator=self.user ) self.todo.save() self.client.login(username='******', password='******') def tearDown(self): self.client.logout() self.user.delete() self.todolist.delete() self.todo.delete() def test_get_index_page(self): response = self.client.get(reverse('lists:index')) self.assertTemplateUsed(response, 'lists/index.html') self.assertIsInstance(response.context['form'], TodoForm) def test_add_todo_to_index_page(self): response = self.client.post( reverse('lists:index'), {'description': 'test'} ) self.assertTemplateUsed(response, 'lists/index.html') self.assertIsInstance(response.context['form'], TodoForm) def test_get_todolist_view(self): response = self.client.get( reverse( 'lists:todolist', kwargs={'todolist_id': self.todolist.id} ) ) self.assertTemplateUsed(response, 'lists/todolist.html') self.assertIsInstance(response.context['form'], TodoForm) def test_add_todo_to_todolist_view(self): response = self.client.post( reverse( 'lists:todolist', kwargs={'todolist_id': self.todolist.id} ), {'description': 'test'} ) self.assertTemplateUsed(response, 'lists/todolist.html') self.assertIsInstance(response.context['form'], TodoForm) self.assertContains(response, 'test') def test_get_todolist_overview(self): response = self.client.get(reverse('lists:overview')) self.assertTemplateUsed(response, 'lists/overview.html') self.assertIsInstance(response.context['form'], TodoListForm) def test_get_todolist_overview_redirect_when_not_logged_in(self): self.client.logout() response = self.client.get(reverse('lists:overview')) self.assertRedirects(response, '/auth/login/?next=/todolists/') def test_add_todolist_to_todolist_overview(self): response = self.client.post( reverse('lists:overview'), {'title': 'some title'} ) self.assertRedirects( response, '/todolist/add/', target_status_code=302, fetch_redirect_response=False )
class TodoTests(APITestCase): def setUp(self): self.test_user = User.objects.create_user( 'test', '*****@*****.**', 'test' ) self.client.login(username='******', password='******') self.test_todolist = TodoList( title='some title', creator=self.test_user ) self.test_todolist.save() self.test_data = { 'description': 'some description', 'todolist': self.test_todolist.id } def tearDown(self): test_user = User.objects.get(username='******') test_user.delete() self.client.logout() def post_new_todo(self, data): url = reverse('api:todo-list') return self.client.post(url, data, format='json') def test_create_todo(self): response = self.post_new_todo(self.test_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( response.data['description'], self.test_data['description'] ) def test_get_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # get todo todo_id = post_response.data['id'] self.assertEqual(todo_id, 1) get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(get_response.status_code, status.HTTP_200_OK) # check todo self.assertEqual(get_response.data, post_response.data) def test_get_when_not_logged_in(self): # add some data post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # make sure the user is logged out self.client.logout() response = self.client.get('/api/todos/1/') # expect 200, because reading is allowed for everybody self.assertEqual(response.status_code, status.HTTP_200_OK) def test_get_non_existent_todo(self): response = self.client.get('/api/todo/0/') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_post_for_anon_users(self): # make sure the user is logged out self.client.logout() # try posting a todo response = self.post_new_todo(self.test_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_put_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # put todo todo_id = post_response.data['id'] put_data = post_response.data put_data['description'] = 'changed description' put_response = self.client.put( '/api/todos/{0}/'.format(todo_id), put_data, format='json' ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) self.assertEqual( get_response.data['description'], 'changed description' ) def test_put_todo_changing_status(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # put todo todo_id = post_response.data['id'] put_data = post_response.data put_data['is_finished'] = True put_response = self.client.put( '/api/todos/{0}/'.format(todo_id), put_data, format='json' ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(put_response.status_code, status.HTTP_200_OK) self.assertEqual( get_response.data['is_finished'], True ) def test_delete_todo(self): # add todo post_response = self.post_new_todo(self.test_data) self.assertEqual(post_response.status_code, status.HTTP_201_CREATED) # delete todolist todo_id = post_response.data['id'] delete_response = self.client.delete( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual( delete_response.status_code, status.HTTP_204_NO_CONTENT ) # get todolist and expect 404 get_response = self.client.get( '/api/todos/{0}/'.format(todo_id) ) self.assertEqual(get_response.status_code, status.HTTP_404_NOT_FOUND)