def test_create(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({ "firstname": "user", }), content_type=content_type.JSON) assert response.status_code == 200 user1_id = int(response.data.decode('utf-8')) response = test_app.post('/articles', data=json.dumps({ "title": "mon article", "text": "blabla", "writer": user1_id }), content_type=content_type.JSON) assert response.status_code == 200 article1_id = int(response.data.decode('utf-8')) response = test_app.post('/articles/' + str(article1_id) + '/comments', data=json.dumps({ "text": "blabla", "writer": user1_id }), content_type=content_type.JSON) assert response.status_code == 200
def test_create(self): time_before = datetime.now() Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({"firstname": "john"}), content_type=content_type.JSON) assert response.status_code == 200 user1_id = response.data.decode("utf-8") response = test_app.post('/users/' + user1_id + '/notifications', data=json.dumps({"text": "hello"}), content_type=content_type.JSON) assert response.status_code == 200 response = test_app.get('/users/' + user1_id + '/inbox') assert response.status_code == 200 messages = json.loads(response.data) assert len(messages) == 1 assert messages[0]['type'] == 'NOTIFICATION' time = datetime.strptime(messages[0]['time'], date_format.CLASSIC) assert time > time_before assert time < datetime.now()
def test_name_validation_number(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({ "firstname": "test1" }), content_type=content_type.JSON) assert response.status_code == 400
def test_date_order(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({"firstname": "john"}), content_type=content_type.JSON) assert response.status_code == 200 user1_id = response.data.decode("utf-8") response = test_app.post('/users', data=json.dumps({"firstname": "jane"}), content_type=content_type.JSON) assert response.status_code == 200 user2_id = response.data.decode("utf-8") response = test_app.post('/messages', data=json.dumps({ "from_user": 1, "to_user": 2, "text": "..." }), content_type=content_type.JSON) assert response.status_code == 200 message1_id = response.data.decode("utf-8") response = test_app.post('/users/' + user2_id + '/notifications', data=json.dumps({ "to_user": 2, "text": "..." }), content_type=content_type.JSON) assert response.status_code == 200 notification1_id = response.data.decode("utf-8") response = test_app.post('/messages', data=json.dumps({ "from_user": 1, "to_user": 2, "text": "..." }), content_type=content_type.JSON) assert response.status_code == 200 message2_id = response.data.decode("utf-8") response = test_app.get('/users/' + user2_id + '/inbox') assert response.status_code == 200 messages = json.loads(response.data) assert len(messages) == 3 assert messages[0]['id'] == int( message1_id) and messages[0]['type'] == message_types.USER assert messages[1]['id'] == int(notification1_id) and messages[1][ 'type'] == message_types.NOTIFICATION assert messages[2]['id'] == int( message2_id) and messages[2]['type'] == message_types.USER
def test_search(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({"firstname": "john"}), content_type=content_type.JSON) assert response.status_code == 200 user1_id = int(response.data.decode('utf-8')) response = test_app.post( '/articles', data=json.dumps({ "title": "article 1", "text": "bla bla des phrases, hop bilbo, du bla bla", "writer": user1_id }), content_type=content_type.JSON) assert response.status_code == 200 article1_id = int(response.data.decode("utf-8")) response = test_app.post( '/articles', data=json.dumps({ "title": "article 2", "text": "bla bla des phrases, hop baggins, du bla bla", "writer": user1_id }), content_type=content_type.JSON) assert response.status_code == 200 article2_id = int(response.data.decode("utf-8")) response = test_app.post('/articles', data=json.dumps({ "title": "article 3", "text": "bilbo", "writer": user1_id }), content_type=content_type.JSON) assert response.status_code == 200 article3_id = int(response.data.decode("utf-8")) response = test_app.get('/articles/search/bilbo') assert response.status_code == 200 search_results = json.loads(response.data) assert len(search_results) == 2 assert article1_id in [result['id'] for result in search_results] assert not article2_id in [result['id'] for result in search_results] assert article3_id in [result['id'] for result in search_results]
def test_create(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({ "firstname": "test" }), content_type=content_type.JSON) assert response.status_code == 200 user_id = response.data.decode('utf-8') response = test_app.get('/users/' + user_id) assert response.status_code == 200 user = json.loads(response.data) assert user['firstname'] == 'test'
def test_delete(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({ "firstname": "test" }), content_type=content_type.JSON) assert response.status_code == 200 user_id = response.data.decode("utf-8") response = test_app.delete('/users/' + user_id) assert response.status_code == 200 response = test_app.get('/users') assert response.status_code == 200 assert len(json.loads(response.data)) == 0
def test_update(self): Database.execute_schema() test_app = app.test_client() user_id = UserDao.create({'firstname': 'toto'}) assert user_id > 0 response = test_app.put('/users/' + str(user_id), data=json.dumps({"firstname": "testupdate"}), content_type=content_type.JSON) assert response.status_code == 200 response = test_app.get('/users/' + str(user_id)) assert response.status_code == 200 user = json.loads(response.data) assert user['firstname'] == 'testupdate'
def test_delete(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({"firstname": "test"}), content_type=content_type.JSON) assert response.status_code == 200 user_id = response.data.decode('utf-8') response = test_app.delete('/users/' + str(user_id)) print(response.status_code) assert response.status_code == 204 response = test_app.get('/users/' + user_id) assert response.status_code == 404
def test_create(self): Database.execute_schema() test_app = app.test_client() time_before = datetime.now() response = test_app.post('/articles', data=json.dumps({ "title": "Titre", "text": "text", "writer": "moi" }), content_type=content_type.JSON) time_after = datetime.now() assert response.status_code == 200 article_id = response.data.decode('utf-8') article = ArticleDao.get(article_id) time = datetime.strptime(article['time'], DATE_FORMAT) assert article['title'] == 'Titre' and article['text'] == 'text' and article['writer'] == 'moi' \ and time_before < time < time_after
def test_create(self): Database.execute_schema() test_app = app.test_client() user_id = UserDao.create({'firstname': 'toto'}) assert user_id > 0 response = test_app.post('/users/' + str(user_id) + '/notifications', data=json.dumps({"text": "test"}), content_type=content_type.JSON) assert response.status_code == 200 notifications_id = json.loads(response.data) response = test_app.get('/users/' + str(user_id) + '/notifications') assert response.status_code == 200 notification = json.loads(response.data) assert notification[0]['id'] == notifications_id assert notification[0]['to_user'] == user_id assert notification[0]['text'] == 'test'
def test_create(self): time_before = datetime.now() Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({"firstname": "john"}), content_type=content_type.JSON) assert response.status_code == 200 user1_id = response.data.decode("utf-8") response = test_app.post('/users', data=json.dumps({"firstname": "jane"}), content_type=content_type.JSON) assert response.status_code == 200 user2_id = response.data.decode("utf-8") response = test_app.post('/messages', data=json.dumps({ "from_user": 1, "to_user": 2, "text": "Hello !" }), content_type=content_type.JSON) assert response.status_code == 200 response = test_app.get('/users/' + user1_id + '/outbox') assert response.status_code == 200 messages = json.loads(response.data) assert len(messages) == 1 assert messages[0]['text'] == 'Hello !' assert messages[0]['to']['firstname'] == 'jane' time = datetime.strptime(messages[0]['time'], date_format.CLASSIC) assert time > time_before assert time < datetime.now() response = test_app.get('/users/' + user2_id + '/inbox') assert response.status_code == 200 messages = json.loads(response.data) assert len(messages) == 1 assert messages[0]['text'] == 'Hello !' assert messages[0]['from']['firstname'] == 'john' assert messages[0]['type'] == 'USER'
def test_comment_notification(self): Database.execute_schema() test_app = app.test_client() response = test_app.post('/users', data=json.dumps({"firstname": "john"}), content_type=content_type.JSON) assert response.status_code == 200 user1_id = int(response.data.decode('utf-8')) response = test_app.post('/users', data=json.dumps({"firstname": "jane"}), content_type=content_type.JSON) assert response.status_code == 200 user2_id = int(response.data.decode('utf-8')) response = test_app.post('/articles', data=json.dumps({ "title": "mon article", "text": "blabla", "writer": user1_id }), content_type=content_type.JSON) assert response.status_code == 200 article1_id = int(response.data.decode('utf-8')) response = test_app.post('/articles/' + str(article1_id) + '/comments', data=json.dumps({ "text": "blabla", "writer": user2_id }), content_type=content_type.JSON) assert response.status_code == 200 response = test_app.get('/users/' + str(user1_id) + '/inbox') assert response.status_code == 200 messages = json.loads(response.data) assert len(messages) == 1 assert messages[0]['type'] == message_types.NOTIFICATION
def test_get_null(self): Database.execute_schema() test_app = app.test_client() response = test_app.get('/users/1') assert response.status_code == 404