def test_filter_reactions_service_problems(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = -2 # Filter correctly a time interval reply = client.post('/stories/filter', data=json.dumps({ 'info': { 'userid': 1, 'init_date': '2019-01-01', 'end_date': '2019-12-01', } }), content_type='application/json') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], -6) self.assertEqual( body['message'], 'One or more of the stories written by the user does not exists in the reaction database' )
def test_invalid_post_wrong_format_user(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # post a new story reply = client.post('/stories') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -9) self.assertEqual(body['message'], "The userid is None")
def test_filter_wrong_dates(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # Filter wrong dates reply = client.post('/stories/filter', data=json.dumps( {'info': { 'userid': 1, }}), content_type='application/json') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], -2) self.assertEqual(body['message'], 'Missing params')
def test_filter_negative(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # Filter wrongly a time interval (init_date > end_date) reply = client.post('/stories/filter', data=json.dumps({ 'info': { 'userid': 1, 'init_date': '2019-12-01', 'end_date': '2019-01-01', } }), content_type='application/json') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], -1) self.assertEqual( body['message'], 'The init date is greater than the end date')
def test_invalid_post_wrong_parameters_roll(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # post a new story reply = client.post( '/stories?userid=1', data=json.dumps({ 'created_story': { 'text': "bird whale coffee bananas ladder glasses" } }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -8) self.assertEqual(body['message'], "Wrong parameters")
def test_search_story_error_text_empty(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 reply = client.get('/search_story', data=json.dumps({"story": { "text": '' }}), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -1) self.assertEqual(body['message'], "The text inserted is empty")
def test_search_story_reactions_service_problem(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = -2 reply = client.get('/search_story', data=json.dumps( {"story": { "text": 'example' }}), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -3) self.assertEqual( body['message'], "One or more of the stories does not exists in the reaction database" )
def test_story_list_limit_negative_reactions_service_problems(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = -2 reply = client.get('/story_list/1/1') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -3) self.assertEqual( body['message'], 'One or more of the stories written by the user does not exists in the reaction database' )
def test_get_stories_negative(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch('stories_service.views.stories.send_request_user_service') as user_request_mock: user_request_mock.return_value = 1 with mock.patch('stories_service.views.stories.send_request_reactions_service') as reactions_request_mock: reactions_request_mock.return_value = 1 # remove the story reply = client.post('/stories/remove/1?userid=1') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], 1) self.assertEqual(body['message'], 'Story removed') # return all stories: 0 reply = client.get('/stories') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], 0) self.assertEqual(body['message'], 'No stories')
def test_filter_reactions_service_too_many_parameters(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # Filter correctly a time interval reply = client.post('/stories/filter', data=json.dumps({ 'info': { 'userid': 1, 'init_date': '2019-01-01', 'end_date': '2019-12-01', }, 'prova': 1 }), content_type='application/json') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], -2) self.assertEqual(body['message'], "Missing params")
def test_restart_db_tables(self): global _app tested_app = create_app(debug=True) self.assertEqual(tested_app.config['WTF_CSRF_SECRET_KEY'], "A SECRET KEY") self.assertEqual(tested_app.config['SECRET_KEY'], 'ANOTHER ONE') self.assertEqual(tested_app.config['SQLALCHEMY_DATABASE_URI'], 'sqlite:///storytellers.db') self.assertEqual(tested_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'], False) self.assertEqual(tested_app.config['SQLALCHEMY_ECHO'], False) self.assertEqual(tested_app.config['TESTING'], True) self.assertEqual(tested_app.config['LOGIN_DISABLED'], False) self.assertEqual(tested_app.config['WTF_CSRF_ENABLED'], True) print(tested_app, file=sys.stderr) self.assertNotEqual(create_app(debug=True), None) self.assertNotEqual(tested_app, None) print(tested_app.app_context(), file=sys.stderr)
def test_invalid_post_too_long_story(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # post a new story text = "" for i in range(0, 2000): text = text + " a" roll = [ "bird", "whale", "coffee", "bananas", "ladder", "glasses" ] reply = client.post('/stories?userid=1', data=json.dumps({ 'created_story': { 'text': text, 'roll': roll } }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -4) self.assertEqual( body['message'], "The story is too long. The length is > 1000 characters." ) # check database entry q = db.session.query(Story).order_by( Story.id.desc()).first() self.assertNotEqual( q.text, "bird whale coffee bananas ladder glasses")
def test_delete_story_negative_story_written_by_another_user(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # post a new story roll = [ "bird", "whale", "coffee", "bananas", "ladder", "glasses" ] reply = client.post( '/stories?userid=2', data=json.dumps({ 'created_story': { 'text': "bird whale coffee bananas ladder glasses", 'roll': roll } }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], 1) self.assertEqual(body['message'], "Story created") reply = client.post('/stories/remove/2?userid=1') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], 0) self.assertEqual( body['message'], 'You cannot remove a story that was not written by you' )
def test_delete_story_negative_user_not_registered(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = -1 reply = client.post('/stories/remove/1?userid=1') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], -4) self.assertEqual(body['message'], 'The user does not exists')
def test_reactions_service_story_not_found(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = -2 # post a new story roll = [ "bird", "whale", "coffee", "bananas", "ladder", "glasses" ] reply = client.post( '/stories?userid=1', data=json.dumps({ 'created_story': { 'text': "bird whale coffee bananas ladder glasses", 'roll': roll } }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -10) self.assertEqual( body['message'], "One or more of the stories written by the user does not exists in the reaction database" ) # check database entry q = db.session.query(Story).order_by( Story.id.desc()).first() self.assertNotEqual( q.text, "bird whale coffee bananas ladder glasses")
def test_invalid_story_WrongFormatSingleDiceError(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # post a new story roll = ["bird", "whale", "coffee", "bananas", "ladder", 1] reply = client.post( '/stories?userid=1', data=json.dumps({ 'created_story': { 'text': "bird whale coffee bananas ladder glasses", 'roll': roll } }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -3) self.assertEqual(body['message'], "There was an error. Try again.") # check database entry q = db.session.query(Story).order_by( Story.id.desc()).first() self.assertNotEqual(q.text, 1)
def test_story_exists_negative(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 reply = client.get('/story_exists/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], 0)
def test_get_stories_negative_reaction_service_timeout(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch('stories_service.views.stories.send_request_user_service') as user_request_mock: user_request_mock.return_value = 1 with mock.patch('stories_service.views.stories.send_request_reactions_service') as reactions_request_mock: reactions_request_mock.return_value = -1 reply = client.get('/stories') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -2) self.assertEqual(body['message'], 'Timeout: the reactions service is not responding')
def test_search_story_error_text_none(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 reply = client.get('/search_story') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], -2) self.assertEqual(body['message'], "Story empty")
def test_story_list_limit_positive(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 reply = client.get('/story_list/1/1') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body['result'], 1) self.assertEqual( body['message'], 'Here are the stories written by the user')
def test_filter_reactions_service_no_json(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) with tested_app.test_client() as client: with mock.patch( 'stories_service.views.stories.send_request_user_service' ) as user_request_mock: user_request_mock.return_value = 1 with mock.patch( 'stories_service.views.stories.send_request_reactions_service' ) as reactions_request_mock: reactions_request_mock.return_value = 1 # Filter correctly a time interval reply = client.post('/stories/filter') self.assertEqual(reply.status_code, 200) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], -2) self.assertEqual(body['message'], 'Missing params')
def test_delete_db(self): tested_app = create_app(debug=True) restart_db_tables(db, tested_app) self.assertEqual(delete_all_db(db, tested_app), None)