def test_invalid_post_short_story(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') assert b'Hi Admin!' in reply.data reply = client.get('/stories') assert b'Trial story of example admin user' in reply.data # post a new story roll = json.dumps( ["bird", "whale", "coffee", "bananas", "ladder", "glasses"]) reply = client.post('/stories', data=dict(text="short story", roll=roll), follow_redirects=True) self.assertEqual(reply.status_code, 200) assert b'The number of words of the story must greater or equal of the number of resulted faces.' in reply.data # check database entry q = db.session.query(Story).order_by(Story.id.desc()).first() self.assertNotEqual(q.text, "short story")
def test_roll(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') self.assertEqual(reply.status_code, 200) # wrong dice number reply = client.get('/rolldice/12/basic') assert b'Error!' in reply.data # non-existing dice set reply = client.get('/rolldice/6/pippo') self.assertEqual(reply.status_code, 404) # correct roll reply = client.get('/rolldice/5/basic') self.assertEqual(reply.status_code, 200)
def test1(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') assert b'Hi Admin!' in reply.data reply = client.get('/stories/filter') self.assertIn(b'Filter Stories', reply.data) # Filter correctly a time interval reply = client.post('/stories/filter', data=dict( init_date='2019-01-01', end_date='2019-12-01' ), follow_redirects=True) self.assertIn(b'Trial story of example', reply.data) # Filter wrongly a time interval (init_date > end_date) reply = client.post('/stories/filter', data=dict( init_date='2019-12-01', end_date='2019-01-01' ), follow_redirects=True) self.assertIn(b'Cant travel back in time', reply.data)
def test_getuser(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: user_base = User.query.first() # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.commit() res = get_users() correct = [user_base, user_a, user_b, user_c] print(res) print(correct) self.assertEqual(res, correct)
def test_get_followed_list(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') user_d = User() user_d.email = '*****@*****.**' user_d.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.add(user_d) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() user_d_id = User.query.filter_by( email=user_d.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_ad = _create_follow(user_a_id, user_d_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_ad) db.session.add(follow_bc) db.session.commit() res = get_followed_list(user_a_id) correct = [user_b_id, user_c_id, user_d_id] self.assertEqual(res, correct)
def test_send_email(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app)
def test2(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app # create 100 Stories restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') assert b'Hi Admin!' in reply.data
def test_delete_story_negative(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') self.assertEqual(reply.status_code, 200) users = User.query.all() self.assertEqual(len(users), 1) # add reaction to a story reply = client.get('/stories/reaction/1/1') self.assertEqual(reply.status_code, 200) reply = client.post('stories/2/remove/stories', follow_redirects=True) self.assertEqual(reply.status_code, 404) story = db.session.query(Story).filter_by(id=1).first() self.assertNotEqual(story, None) reactions = Reaction.query.filter_by(story_id=1).all() self.assertEqual(len(reactions), 1) stories = Story.query.filter_by(id=1).all() self.assertEqual(len(stories), 1) users = User.query.all() self.assertEqual(len(users), 1) reply = client.post('stories/1/remove/stories', follow_redirects=True) self.assertEqual(reply.status_code, 200) assert b'The story has been canceled.' in reply.data reactions = Reaction.query.filter_by(story_id=1).all() self.assertEqual(len(reactions), 0) # logout reply = logout(client) self.assertEqual(reply.status_code, 200)
def test_invalid_post_too_long_story(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') assert b'Hi Admin!' in reply.data reply = client.get('/stories') assert b'Trial story of example admin user' in reply.data # post a new story roll = json.dumps( ["bird", "whale", "coffee", "bananas", "ladder", "glasses"]) text = "" for i in range(0, 2000): text = text + " a" print('ERROR 2', file=sys.stderr) reply = client.post('/stories', data=dict(text=text, roll=roll), follow_redirects=True) self.assertEqual(reply.status_code, 200) assert b'The story is too long' in reply.data # check database entry q = db.session.query(Story).order_by(Story.id.desc()).first() self.assertNotEqual(q.text, text)
def test_email_sender(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: example2 = User() example2.firstname = 'Daniele' example2.lastname = 'Arioli' example2.email = '*****@*****.**' example2.dateofbirth = datetime(2020, 10, 5) example2.is_admin = True example2.set_password('admin') db.session.add(example2) db.session.commit() self.assertTrue(send_emails())
def test_delete_wrong_author_story_index(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: # login reply = login(client, '*****@*****.**', 'admin') self.assertEqual(reply.status_code, 200) # add reaction to a story reply = client.get('/stories/reaction/1/1') self.assertEqual(reply.status_code, 200) users = User.query.all() self.assertEqual(len(users), 1) story = db.session.query(Story).filter_by(id=1).first() self.assertNotEqual(story, None) # logout reply = logout(client) self.assertEqual(reply.status_code, 200) # signup user1 = User() user1.firstname = "Mario" user1.lastname = "Rossi" user1.email = "*****@*****.**" user1.dateofbirth = "1994" user1.password = user1.set_password("12345") reply = signup(client, user1) self.assertEqual(reply.status_code, 200) users = User.query.all() self.assertEqual(len(users), 2) story = db.session.query(Story).filter_by(id=1).first() self.assertNotEqual(story, None) stories = Story.query.filter_by(id=1).all() self.assertEqual(len(stories), 1) reactions = Reaction.query.filter_by(story_id=1).all() self.assertEqual(len(reactions), 1) story = db.session.query(Story).filter_by(id=1).first() self.assertNotEqual(story, None) reactions = Reaction.query.filter_by(story_id=1).all() self.assertEqual(len(reactions), 1) # add reaction to a story reply = client.get('/stories/reaction/1/1') self.assertEqual(reply.status_code, 200) reply = client.post('stories/1/remove/index', follow_redirects=True) self.assertEqual(reply.status_code, 200) story = db.session.query(Story).filter_by(id=1).first() self.assertNotEqual(story, None) # logout reply = logout(client) self.assertEqual(reply.status_code, 200)
def test_followers_list(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.firstname = "Pippo" user_a.lastname = "Pippo" user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.firstname = "Pluto" user_b.lastname = "Mouse" user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() # login as user_1 login(client, user_a.email, 'test') # call /follow/user_id_2 # assert True, redirtect to user_2 wall reply = client.post('/follow/' + str(user_b_id)) data = "/wall/" + str(user_b_id) self.assertTrue(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # get followed list reply = client.get("/followed/list") self.assertIn(user_b.firstname, str(reply.data)) # get followed number reply = client.get("/followed") assert b'"followed_num":1' in reply.data # logout user1 logout(client) # login as user2 login(client, user_b.email, 'test') # get followers list reply = client.get("/followers/list") self.assertIn(user_a.firstname, str(reply.data)) # get followers number reply = client.get("/followers") assert b'"followers_num":1' in reply.data
def test_unfollow_post_user(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_bc) db.session.commit() # login as user_1 login(client, user_a.email, 'test') # call /unfollow/user_1 # assert False data = '/wall/' + str(user_a_id) reply = client.post('/unfollow/' + str(user_a_id)) self.assertFalse(_is_follower(user_a_id, user_a_id)) self.assertIn(data, str(reply.data)) # call /unfollow/user_2 # assert OK data = '/wall/' + str(user_b_id) self.assertTrue(_is_follower(user_a_id, user_b_id)) reply = client.post('/unfollow/' + str(user_b_id)) self.assertFalse(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call unfollow/user_2 # assert EXC data = '/wall/' + str(user_b_id) reply = client.post('/unfollow/' + str(user_b_id)) self.assertFalse(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call unfollow/user_not_exist # assert EXC user_not_exist_id = 999999999999 data = '/wall/' + str(user_not_exist_id) reply = client.post('/unfollow/' + str(user_not_exist_id)) self.assertFalse(_is_follower(user_a_id, user_not_exist_id)) self.assertIn("/stories", str(reply.data)) logout(client)
def test_follow_user(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() # login as user_1 login(client, user_a.email, 'test') # call /follow/user_id_2 # assert True, redirtect to user_2 wall reply = client.post('/follow/' + str(user_b_id)) data = "/wall/" + str(user_b_id) self.assertTrue(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call /follow/user_id_2 # assert True, redirtect to user_2 wall data = "/wall/" + str(user_b_id) reply = client.post('/follow/' + str(user_b_id)) self.assertTrue(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call /follow/user_id_3 # assert True, redirtect to user_3 wall data = "/wall/" + str(user_c_id) reply = client.post('/follow/' + str(user_c_id)) self.assertTrue(_is_follower(user_a_id, user_c_id)) self.assertIn(data, str(reply.data)) # call /follow/user_id_2 # assert True, redirect to user_2 wall data = "/wall/" + str(user_b_id) reply = client.post('/follow/' + str(user_b_id)) self.assertTrue(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call /follow/user_id_1 (himslef) # assert False, redirect to user_1 wall data = "/wall/" + str(user_a_id) reply = client.post('/follow/' + str(user_a_id)) self.assertFalse(_is_follower(user_a_id, user_a_id)) self.assertIn(data, str(reply.data)) # call /follow/user_not_exist # assert False, redirect to stories user_not_exist_id = 99999999999 reply = client.post('/follow/' + str(user_not_exist_id)) self.assertFalse(_is_follower(user_a_id, user_not_exist_id)) self.assertIn("/stories", str(reply.data)) logout(client)
def test_maker_message(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.firstname = 'user_a' user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.firstname = 'user_b' user_b.lastname = 'surname_b' user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.firstname = 'user_c' user_c.lastname = 'surname_c' user_c.email = '*****@*****.**' user_c.set_password('test') user_d = User() user_d.firstname = 'user_d' user_d.lastname = 'surname_d' user_d.email = '*****@*****.**' user_d.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.add(user_d) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() user_d_id = User.query.filter_by( email=user_d.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_ad = _create_follow(user_a_id, user_d_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_ad) db.session.add(follow_bc) story_b_1 = make_story(user_b_id, "story_b_1") story_b_2 = make_story(user_b_id, "story_b_2") story_b_3 = make_story(user_b_id, "story_b_3", 66, 9) story_b_4 = make_story(user_b_id, "story_b_4", 5, 1) story_c_1 = make_story(user_c_id, "story_c_1") story_c_2 = make_story(user_c_id, "story_c_2", 1000, 162) story_c_3 = make_story(user_c_id, "story_c_3") db.session.add(story_b_1) db.session.add(story_b_2) db.session.add(story_b_3) db.session.add(story_b_4) db.session.add(story_c_1) db.session.add(story_c_2) db.session.add(story_c_3) db.session.commit() res = maker_message(user_a) print(res) correct = "Hello user_a,\n\nhere you can find what's new on the wall of Sweaggers' SocialDice!\n\n - user_b surname_b posts 4 new stories.\n - user_c surname_c posts 3 new stories.\n\nSee you on SocialDice,\nSweaggers Team" self.assertEqual(res, correct) res = maker_message(user_d) correct = "Hello user_d,\n\nYou have no news for today, take a look and add new writers on Sweaggers' SocialDice!" self.assertEqual(res, correct)
def test_get_all_stories_by_writer(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') user_d = User() user_d.email = '*****@*****.**' user_d.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.add(user_d) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() user_d_id = User.query.filter_by( email=user_d.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_ad = _create_follow(user_a_id, user_d_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_ad) db.session.add(follow_bc) story_b_1 = make_story(user_b_id, "story_b_1") story_b_2 = make_story(user_b_id, "story_b_2") story_b_3 = make_story(user_b_id, "story_b_3", 66, 9) story_b_4 = make_story(user_b_id, "story_b_4", 5, 1) story_c_1 = make_story(user_c_id, "story_c_1") story_c_2 = make_story(user_c_id, "story_c_2", 1000, 162) story_c_3 = make_story(user_c_id, "story_c_3") db.session.add(story_b_1) db.session.add(story_b_2) db.session.add(story_b_3) db.session.add(story_b_4) db.session.add(story_c_1) db.session.add(story_c_2) db.session.add(story_c_3) db.session.commit() res = get_all_stories_by_writer(user_b_id) self.assertEqual(len(res), 4) res = get_all_stories_by_writer(user_c_id) self.assertEqual(len(res), 3) res = get_all_stories_by_writer(user_d_id) self.assertEqual(len(res), 0)