def test_list(self): """ Test that you can list """ us = User(email="test@localhost", password=crypt('tset')) db_sess = get_session() db_sess.add(us) db_sess.commit() e_a = Entry(userid=us.userid, title="test", completed=True, description="test desc") e_b = Entry(userid=us.userid, title="test2", completed=False, description="test2 desc") db_sess.add(e_a) db_sess.add(e_b) db_sess.commit() response = self.session.post(self._route("/auth/login"), json={ 'email': us.email, 'password': "******" }) self.assertEqual(response.status_code, 200, response.text) response = self.session.get(self._route("/todo/entry"), ) expected = [e_a.as_dict(), e_b.as_dict()] db_sess.close() self.assertEqual(response.json(), expected, response.text) self.assertEqual(response.status_code, 200, response.text)
def test_put(self): """ Test that you can put """ us = User(email="test@localhost", password=crypt('tset')) db_sess = get_session() db_sess.add(us) db_sess.commit() entry = Entry(userid=us.userid, title="test", completed=True, description="test desc") db_sess.add(entry) db_sess.commit() response = self.session.post(self._route("/auth/login"), json={ 'email': us.email, 'password': "******" }) self.assertEqual(response.status_code, 200, response.text) new_entry = Entry(userid=us.userid, title="test_updated", completed=False, description="test desc updated") response = self.session.put(self._route("/todo/entry/{}".format( entry.entryid)), json={ 'title': new_entry.title, 'completed': new_entry.completed, 'description': new_entry.description }) db_sess.refresh(entry) self.assertEqual(response.status_code, 200, response.text) self.assertEqual(entry.as_dict(), response.json(), response.text) db_sess.close()
def post_handler(): """ Post todo entries for logged in user """ try: body = request.json title = body.pop('title') completed = body.pop('completed', None) description = body.pop('description') if body: abort(400) user = current_user entry = Entry(title=title, completed=completed, description=description, userid=user.userid) session = get_session() session.add(entry) session.commit() except (KeyError): abort(400) response = make_response(json.dumps(entry.as_dict()), 201) session.close() return response