def test_create_account_password_incorrect_length(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "create_account": { "username": "******", "password": "******", } }, indent=4)) expected_response = json.dumps( { "error": "The password isn't of the correct length (0 < len(password) <= 20)." }, indent=4) result = server.create_account(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_new_message_invalid_json(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "new_message": { "channel_receiving_": "Client Team", "user": "******", "timestamp": "2017-03-14 14:11:30", "message": "the actual message that the user posted" } }, indent=4)) expected_response = json.dumps( {"error": "The JSON file sent didn't contain valid information."}, indent=4) mydb.create_account('username', 'password') server, mydb = login(server, mydb, 'username', 'password') mydb.create_channel('Client Team', None) mydb.add_channels_to_user_info('username', ['Client Team']) result = server.new_message(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_leave_channel_success(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"leave_channel": "Client Team"}, indent=4)) expected_response = json.dumps( { "leave_channel": { "channel": "Client Team", "user": "******", "message": "username has left the channel." } }, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, 'username', 'password') mydb.create_channel("Client Team", None) mydb.add_channels_to_user_info("username", ["Client Team"]) result = server.leave_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_change_password_account_does_not_exist(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "change_password": { "username": "******", "current_password": "******", "new_password": "******" } }, indent=4)) expected_response = json.dumps( { "error": "The username/password combination do not exist in the database." }, indent=4) result = server.change_password(mydb, client_request) assert expected_response == result mydb.empty_tables()
def __init__(self, conn, addr): threading.Thread.__init__(self) self.conn = conn self.addr = addr self.server = Camelot_Server() self.mydb = Camelot_Database() self.unauthorized_function_calls = ['__init__', 'login_required']
def test_new_message_user_cant_send_message_to_channel_theyre_not_in(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "new_message": { "channel_receiving_message": "Client Team", "user": "******", "timestamp": "2017-03-14 14:11:30", "message": "the actual message that the user posted" } }, indent=4)) expected_response = json.dumps( { "error": "The user is trying to send a message to a channel they haven't joined yet." }, indent=4) mydb.create_account('username', 'password') server, mydb = login(server, mydb, 'username', 'password') mydb.create_channel('Client Team', None) result = server.new_message(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_delete_account_success(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "delete_account": { "username": "******", "password": "******" } }, indent=4)) expected_response = json.dumps( { "account_deleted": { "username": "******", "channels_being_deleted": ["TestChannel"] } }, indent=4) mydb.create_account("username", "password") mydb.create_channel("TestChannel", "username") result = server.delete_account(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_logout_not_logged_in(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads(json.dumps({"logout": "logout"}, indent=4)) expected_response = json.dumps( {"error": "A user must be signed in to access this function."}, indent=4) result = server.logout(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_join_channel_not_logged_in(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"join_channel": ["Client Team", "Server Team"]}, indent=4)) expected_response = json.dumps( {"error": "A user must be signed in to access this function."}, indent=4) result = server.join_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_delete_channel_not_logged_in(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"delete_channel": "Non-existent channel"}, indent=4)) expected_response = json.dumps( {"error": "A user must be signed in to access this function."}, indent=4) result = server.delete_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_create_account_success_with_default_channels_added_to_account(): server = Camelot_Server() mydb = Camelot_Database() mydb.insert_data('data.sql') expected_response = json.dumps( {"channels": ["Server Team", "Client Team", "Software Eng. Group"]}, indent=4) mydb.create_account("username", "password") result = mydb.get_channels_for_user("username") assert expected_response == result mydb.empty_tables()
def test_create_channel_not_logged_in(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "create_channel": "Test Channel Name with incorrect length-----------" }, indent=4)) expected_response = json.dumps( {"error": "A user must be signed in to access this function."}, indent=4) result = server.create_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_create_account_success(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "create_account": { "username": "******", "password": "******", } }, indent=4)) expected_response = json.dumps( {"success": "Successfully created username's account."}, indent=4) result = server.create_account(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_new_message_send_message_to_channel_that_doesnt_exist(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "new_message": { "channel_receiving_message": "dummy channel", "user": "******", "timestamp": "2017-03-14 14:11:30", "message": "the actual message that the user posted" } }, indent=4)) expected_response = json.dumps( {"error": "The specified channel was not found."}, indent=4) mydb.create_account('username', 'password') server, mydb = login(server, mydb, 'username', 'password') result = server.new_message(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_change_password_invalid_new_password(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "change_password": { "username": "******", "current_password": "******", "new_password": "******" } }, indent=4)) expected_response = json.dumps( { "error": "The password isn't of the correct length (0 < len(password) <= 20)." }, indent=4) mydb.create_account("username", "password") result = server.change_password(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_create_channel_channel_name_incorrect_length(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "create_channel": "Test Channel Name with incorrect length-----------" }, indent=4)) expected_response = json.dumps( { "error": "The name of the channel isn't of the correct length (0 < len(channel_name) <= 40)." }, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, 'username', 'password') result = server.create_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_create_account_username_already_taken(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "create_account": { "username": "******", "password": "******", } }, indent=4)) expected_response = json.dumps( {"error": "That username is already taken."}, indent=4) server.create_account(mydb, client_request) result = server.create_account(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_delete_account_invalid_json(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "delete_account": { "username_invalid": "username", "password": "******" } }, indent=4)) expected_response = json.dumps( {"error": "The JSON file sent didn't contain valid information."}, indent=4) result = server.delete_account(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_change_password_invalid_json(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "change_password": { "username_invalid": "username", "current_password": "******", "new_password": "******" } }, indent=4)) expected_response = json.dumps( {"error": "The JSON file sent didn't contain valid information."}, indent=4) result = server.change_password(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_login_user_password_combination_not_in_database(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( {"login": { "username": "******", "password": "******", }}, indent=4)) expected_response = json.dumps( { "error": "The username/password combination do not exist in the database." }, indent=4) result = server.login(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_delete_channel_user_not_authorized_to_delete_channel(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"delete_channel": "TestChannel"}, indent=4)) expected_response = json.dumps( { "error": "The user trying to delete the channel isn't the admin of the channel." }, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, 'username', 'password') mydb.create_account("admin user", "password") mydb.create_channel("TestChannel", "admin user") result = server.delete_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_new_message_not_logged_in(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( { "new_message": { "channel_receiving_message": "Client Team", "user": "******", "timestamp": "2017-03-14 14:11:30", "message": "the actual message that the user posted" } }, indent=4)) expected_response = json.dumps( {"error": "A user must be signed in to access this function."}, indent=4) result = server.new_message(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_get_channels_for_user(): server = Camelot_Server() mydb = Camelot_Database() mydb.insert_data('data.sql') client_request = json.dumps( {"get_channels_for_user": "******"}, indent=4) expected_response = json.dumps( {"channels": ["Server Team", "Client Team", "Software Eng. Group"]}, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, 'username', 'password') result = server.get_channels_for_user(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_delete_channel_channel_not_found(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"delete_channel": "Non-existent channel"}, indent=4)) expected_response = json.dumps( {"error": "The specified channel was not found."}, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, 'username', 'password') mydb.create_channel("TestChannel", "username") result = server.delete_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_join_channel_user_tries_to_send_json_containing_zero_channels_to_join( ): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads(json.dumps({"join_channel": []}, indent=4)) expected_response = json.dumps( {"error": "No channels were given for the user to join."}, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, "username", "password") mydb.create_channel("Client Team", "username") mydb.create_channel("Server Team", "username") result = server.join_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_join_channel_that_doesnt_exist(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"join_channel": ["Client Team", "Server Team"]}, indent=4)) expected_response = json.dumps( {"error": "The user is trying to join a channel that doesn't exist."}, indent=4) mydb.create_account('username', 'password') server, mydb = login(server, mydb, 'username', 'password') mydb.create_channel("TestChannel", "username") result = server.join_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_join_channel_trying_to_join_channel_that_you_are_already_a_part_of(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"join_channel": ["Client Team", "Server Team"]}, indent=4)) expected_response = json.dumps({ "error": "The user has already joined one or more of the channels they were trying to join again." }) mydb.create_account("username", "password") server, mydb = login(server, mydb, "username", "password") mydb.create_channel("Client Team", "username") mydb.create_channel("Server Team", "username") server.join_channel(mydb, client_request) result = server.join_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_login_success(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps( {"login": { "username": "******", "password": "******", }}, indent=4)) expected_response = json.dumps({"channels": ["ChannelTest"]}, indent=4) mydb.create_account("username", "password") mydb.create_channel("ChannelTest", "username") result = server.login(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_join_channel_success(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads( json.dumps({"join_channel": ["Client Team", "Server Team"]}, indent=4)) expected_response = json.dumps( { "channels_joined": ["Client Team", "Server Team"], "user": "******" }, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, "username", "password") mydb.create_channel("Client Team", "username") mydb.create_channel("Server Team", "username") result = server.join_channel(mydb, client_request) assert expected_response == result mydb.empty_tables()
def test_logout_success(): server = Camelot_Server() mydb = Camelot_Database() client_request = json.loads(json.dumps({"logout": "logout"}, indent=4)) expected_response = json.dumps( {"success": "username has successfully logged out."}, indent=4) mydb.create_account("username", "password") server, mydb = login(server, mydb, 'username', 'password') result = server.logout(mydb, client_request) assert expected_response == result mydb.empty_tables()