def test_block(): """Make sure block works.""" db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) user2 = User(username="******", first_name="test2", last_name="test2", email="*****@*****.**", password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) login("*****@*****.**", "hello123", client2) # test0 : test if user1 can block user2 rv = block(user2.username, client1) assert rv.status_code == 200 assert rv.data == b'"Success"\n' assert User.query.filter_by(username=user.username).first( ).blocked[0].blocked_user == user2.username # test1 : test if user2 can block user1 rv = block(user.username, client2) assert rv.status_code == 200 assert rv.data == b'"Success"\n' assert User.query.filter_by(username=user2.username).first( ).blocked[0].blocked_user == user.username client1.get("/logout") client2.get("/logout") # test2 : test if redirected to login page for invalid sign in for client1 rv = client1.get("/block") assert rv.status_code == 302 assert rv.location.endswith("/login") # test3 : test if redirected to login page for invalid sign in for client2 rv = client2.get("/block") assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def test_resetrequest(): """Make sure reset request works.""" # testing reset request get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******",first_name="test",last_name="test",email="*****@*****.**",password=password) db.session.add(user) db.session.commit() client1 = app.test_client() # login user login("*****@*****.**","hello123", client1) # test0 : test if request request page renders rv = client1.get("/reset_request") assert rv.status_code == 200 # test1 : test if "Success" is returned as the output for valid request rv = reset_request(user.email, client1) assert b'Success' in rv.data db.reflect() db.drop_all()
def test_logout(): """Make sure logout works.""" password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) db.create_all() user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) db.session.add(user) db.session.commit() client1 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) # test0: Make sure logout is redirected to login page rv = client1.get("/logout") assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def register(username, email, first_name, last_name, password, confirm): return app.test_client().post('/signup', data=dict(username=username, fname=first_name, lname=last_name, email=email, password=password, confirm=confirm), follow_redirects=True)
def test_profile(): """Make sure profile page works.""" # testing profile get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) db.session.add(user) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login user login("*****@*****.**", "hello123", client1) # test0 : test if the profile page loads rv = client1.get("/profile") assert rv.status_code == 200 # test1 : test if redirected to login page for invalid sign in rv = client2.get("/profile") assert rv.status_code == 302 assert rv.location.endswith("/login") # test2 : update Username, Lastname, Password, Username and Bio rv = profile("nShaz", "mShaz", "hello1234", "username new", "New bio", client1) assert b'First Name, Last Name, Password, Username, Bio' in rv.data # test3 : update nothing should return that nothing has been updates rv = profile("", "", "", "", "", client1) assert b'Nothing Updated' in rv.data db.reflect() db.drop_all()
def test_editprofile(): """Make sure edit profile works.""" # testing chat get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) user2 = User(username="******", first_name="test2", last_name="test2", email="*****@*****.**", password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) # test0 : test if edit profile page renders for client1 rv = client1.get("/avi") assert rv.status_code == 200 # test1 : test if redirected to login page for invalid sign in rv = client2.get("/avi") assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def test_search(): """Make sure search works.""" db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******",first_name="test",last_name="test",email="*****@*****.**",password=password) user2 = User(username="******",first_name="test2",last_name="test2",email="*****@*****.**",password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login user login("*****@*****.**","hello123", client1) # testing search post for client1 # test0 : test if all users are retured rv = search(client1) assert rv.status_code == 200 assert b'["test2"]\n' in rv.data # test1 : test if no user is returned for request upon invalid sign in rv = search(client2) assert rv.status_code == 200 assert b'' in rv.data # test2 : test if get request is made 404 is returned rv = client1.get("/search") assert rv.status_code == 404 db.reflect() db.drop_all()
def test_login(): """Make sure login works.""" # testing login get rv = app.test_client().get("/login") assert rv.status_code == 200 # testing login post password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) db.create_all() user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) db.session.add(user) db.session.commit() # test0 : test if valid login is made rv = login('*****@*****.**', 'hello123') assert rv.status_code == 200 assert b'Success' in rv.data # test1 : test if email is absent rv = login('shazm' + 'x' + '@gmail.com', 'hello123') assert rv.status_code == 200 assert b'user_and_email_not_found' in rv.data # test2 : test if password is incorrect rv = login('*****@*****.**', 'hello123' + 'x') assert rv.status_code == 200 assert b'invalid_password' in rv.data # test3 : test if frontend overriden and empty values sent rv = login('', 'hello123') assert rv.status_code == 200 assert b'Please fill out every field' in rv.data # test4 : test if email is valid rv = login('shazm@com', 'hello123' + 'x') assert rv.status_code == 200 assert b'Invalid email' in rv.data db.reflect() db.drop_all()
def test_characterLimit(): """Make sure Character Limit works.""" # testing chat get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) db.session.add(user) db.session.commit() client1 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) # test0 : test correct character count returned rv = character("Hello", client1) assert rv.status_code == 200 assert rv.data == b'{"Success":20,"limit":25}\n' # test1 : test correct character count returned rv = character("Hello " + list(UNICODE_EMOJI.keys())[0], client1) assert rv.status_code == 200 assert rv.data == b'{"Success":20,"limit":25}\n' # test2 : test limit is correctly shown rv = character( "ABCDEFGHIJ " + list(UNICODE_EMOJI.keys())[0] + " KLMNOPQRSTUVWXYZ", client1) assert rv.status_code == 200 assert rv.data == b'{"Exceeded":1,"limit":25}\n' db.reflect() db.drop_all()
def test_session(): """Make sure sessions work.""" db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) user2 = User(username="******", first_name="test2", last_name="test2", email="*****@*****.**", password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login user login("*****@*****.**", "hello123", client1) user2_room = json.loads(homepage(user2.username, client1).data)["Success"] current_room = user2_room # test0 : test if the profile page loads if the user is logged in rv = client1.get("/profile") assert rv.status_code == 200 # test1 : homepage should load if user is signed in rv = client1.get("/homepage") assert rv.status_code == 200 # test2 : test if chat renders for client1 rv = client1.get("/chat/" + current_room) assert rv.status_code == 200 # test3 : test if redirected to login page for invalid sign in I.e, session is missing rv = client2.get("/profile") assert rv.status_code == 302 assert rv.location.endswith("/login") # test4 : test if redirected to login page for invalid sign in I.e, session is missing rv = client2.get("/homepage") assert rv.status_code == 302 assert rv.location.endswith("/login") # test5 : test if redirected to login page for invalid sign in I.e, session is missing rv = client2.get("/chat/" + current_room) assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def test_registration(): """Make sure registration works.""" # testing registration get rv = app.test_client().get("/signup") assert rv.status_code == 200 # testing registration post db.create_all() # test0 : verify if backend returns "fill out every field" if missing field rv = register('', '*****@*****.**', '', 'test', 'hello123', 'hello123') assert rv.status_code == 200 assert b'Please fill out every field' in rv.data # test1 : verify a valid input rv = register('test', '*****@*****.**', 'test', 'test', 'hello123', 'hello123') user = User.query.filter(User.email == "*****@*****.**", User.username == "test", User.first_name == "test", User.last_name == "test").first() password = ("hello123").encode('utf-8') stored_password = user.password.encode('utf-8') assert rv.status_code == 200 assert b'success' in rv.data # check if user can be found assert user != None # check if the password stored is the same as inserted password assert bcrypt.checkpw(password, stored_password) # test2 : verify if backend recognizes a shorter password rv = register('test2', '*****@*****.**', 'test2', 'test2', 'hello13', 'hello13') assert rv.status_code == 200 assert b'password too short' in rv.data # test3 : verify if backend recognizes a existing username rv = register('test', '*****@*****.**', 'test3', 'test3', 'hello123', 'hello123') assert rv.status_code == 200 assert b'username exists' in rv.data # test4 : verify if backend recognizes a existing email rv = register('test4', '*****@*****.**', 'test4', 'test4', 'hello123', 'hello123') assert rv.status_code == 200 assert b'email exists' in rv.data # test5 : verify if backend recognizes if the passwords don't match rv = register('test5', '*****@*****.**', 'test5', 'test5', 'hello123', 'hello1234') assert rv.status_code == 200 assert b'password does not match' in rv.data # test6 : verify if database recognizes invalid email rv = register('test5', 'shazm5.com', 'test5', 'test5', 'hello123', 'hello1234') assert rv.status_code == 200 assert b'Invalid email' in rv.data db.reflect() db.drop_all()
def test_chat(): """Make sure chat works.""" # testing chat get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) user2 = User(username="******", first_name="test2", last_name="test2", email="*****@*****.**", password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() client3 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) login("*****@*****.**", "hello123", client2) user2_room = json.loads(homepage(user2.username, client1).data)["Success"] user_room = json.loads(homepage(user.username, client2).data)["Success"] # Verify both rooms are same assert user2_room == user_room current_room = user2_room conv = Conversation.query.filter_by(room=current_room).first() message = Message(sender=user.username, message="hello") message2 = Message(sender=user2.username, message="world") conv.message = [message, message2] db.session.add(conv) db.session.commit() # test0 : test if chat renders for client1 rv = client1.get("/chat/" + current_room) assert rv.status_code == 200 # test1 : test if chat renders for client2 rv = client2.get("/chat/" + current_room) assert rv.status_code == 200 # test2 : test if redirected to login page for invalid sign in rv = client3.get("/chat/" + current_room) assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def login(email, password): return app.test_client().post('/login', data=dict(email=email, password=password), follow_redirects=True)
def test_unblock(): """Make sure unblock works.""" db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) user2 = User(username="******", first_name="test2", last_name="test2", email="*****@*****.**", password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) login("*****@*****.**", "hello123", client2) # test0 : test if user1 can block user2 rv = block(user2.username, client1) assert rv.status_code == 200 assert rv.data == b'"Success"\n' assert User.query.filter_by(username=user.username).first( ).blocked[0].blocked_user == user2.username # test1 : test if user2 can block user1 rv = block(user.username, client2) assert rv.status_code == 200 assert rv.data == b'"Success"\n' assert User.query.filter_by(username=user2.username).first( ).blocked[0].blocked_user == user.username # test2 : unblock user2 for user1 rv = unblock(user2.username, client1) assert rv.status_code == 200 assert rv.data == b'"Success"\n' assert len( User.query.filter_by(username=user.username).first().blocked) == 0 # test3 : unblock unblocked user for user1 rv = unblock("Should Not work", client1) assert rv.status_code == 200 assert rv.data == b'"Fail"\n' # test4 : unblock user1 for user2 rv = unblock(user.username, client2) assert rv.status_code == 200 assert rv.data == b'"Success"\n' assert len( User.query.filter_by(username=user2.username).first().blocked) == 0 # test5 : unblock unblocked user for user1 rv = unblock("Should Not work", client2) assert rv.status_code == 200 assert rv.data == b'"Fail"\n' db.reflect() db.drop_all()
def test_homepage(): """Make sure homepage works.""" # testing homepage get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******",first_name="test",last_name="test",email="*****@*****.**",password=password) user2 = User(username="******",first_name="test2",last_name="test2",email="*****@*****.**",password=password) db.session.add(user) db.session.add(user2) db.session.commit() client1 = app.test_client() client2 = app.test_client() client3 = app.test_client() # login both users to test end to end login("*****@*****.**","hello123", client1) login("*****@*****.**","hello123", client2) # test7 : test if homepage renders rv = client1.get("/homepage") assert rv.status_code == 200 # testing homepage post for client1 # test0 : test if valid chat room was created rv = homepage('test2', client1) assert rv.status_code == 200 assert b'Success' in rv.data # test1 : test if the user is trying to talk to themself rv = homepage('test', client1) assert rv.status_code == 200 assert b'Cannot_Talk' in rv.data # test2 : test if the user is not present rv = homepage('test_not_existing', client1) assert rv.status_code == 200 assert b'Invalid_user' in rv.data #----- rv = client2.get("/homepage") assert rv.status_code == 200 # testing homepage post for client2 # test3 : test if valid chat room was created rv = homepage('test', client2) assert rv.status_code == 200 assert b'Success' in rv.data # test4 : test if the user is trying to talk to themself rv = homepage('test2', client2) assert rv.status_code == 200 assert b'Cannot_Talk' in rv.data # test5 : test if the user doesnot exist rv = homepage('test_not_existing', client2) assert rv.status_code == 200 assert b'Invalid_user' in rv.data # test6 : test if redirected to login page for invalid sign in rv = client3.get("/homepage") assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def test_deleteacc(): """Make sure Delete Account page works.""" # testing profile get db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) room1 = Rooms(username1="You Do Not Exist", username2=user.username, new_message=False, room="TestingRoom") room2 = Rooms(username1=user.username, username2="You Do Not Exist", new_message=False, room="TestingRoom") profile = Profile(email=user.email, filename="wut.jpeg") conv = Conversation(room=room1.room) message = Message(sender=user.username, message="you are a test") conv.message = [message] db.session.add(user) db.session.commit() db.session.add(room1) db.session.commit() db.session.add(room2) db.session.commit() db.session.add(profile) db.session.commit() db.session.add(conv) db.session.commit() client1 = app.test_client() client2 = app.test_client() # login user login("*****@*****.**", "hello123", client1) # test0 : test if the profile page loads rv = delete(client1) assert rv.status_code == 200 assert rv.data == b'"success"\n' assert User.query.filter_by(username=user.username).first() is None assert Profile.query.filter_by(email=user.email).first() is None assert Rooms.query.filter_by(username1=user.username).first() is None assert Rooms.query.filter_by(username2=user.username).first() is None assert Rooms.query.filter_by(room=room1.room).first() is None assert Conversation.query.filter_by(room=room1.room).first() is None assert Message.query.filter_by(sender=user.username).first() is None # test1 : test if redirected to login page for invalid sign in rv = client2.get("/deleteacc") assert rv.status_code == 302 assert rv.location.endswith("/login") db.reflect() db.drop_all()
def test_suggestedchats(): """Make sure suggestedchats works.""" db.create_all() password = ("hello123").encode('utf-8') password = bcrypt.hashpw(password, bcrypt.gensalt()) user = User(username="******", first_name="test", last_name="test", email="*****@*****.**", password=password) user2 = User(username="******", first_name="test2", last_name="test2", email="*****@*****.**", password=password) user3 = User(username="******", first_name="test3", last_name="test3", email="*****@*****.**", password=password) db.session.add(user) db.session.add(user2) db.session.add(user3) db.session.commit() client1 = app.test_client() client2 = app.test_client() client3 = app.test_client() # login both users to test end to end login("*****@*****.**", "hello123", client1) login("*****@*****.**", "hello123", client2) login("*****@*****.**", "hello123", client3) user2_room = json.loads(homepage(user2.username, client1).data)["Success"] user_room = json.loads(homepage(user.username, client2).data)["Success"] # Verify both rooms are same assert user2_room == user_room current_room = user2_room conv = Conversation.query.filter_by(room=current_room).first() message = Message(sender=user.username, message="hello") message2 = Message(sender=user2.username, message="world") conv.message = [message, message2] db.session.add(conv) db.session.commit() # test0 : test if suggestedchats gives correct users for client1 rv = client1.post("/suggestedchats") assert rv.status_code == 200 assert user3.username.encode('utf-8') in rv.data # test1 : test if suggestedchats gives correct users for client2 rv = client2.post("/suggestedchats") assert rv.status_code == 200 assert user3.username.encode('utf-8') in rv.data db.reflect() db.drop_all()