def test_dm_remove_v1_success(user_setup): dm_create_v1(make_token(0), [1]) dm_create_v1(make_token(0), [2]) # removing this dm dm_remove_v1(make_token(0), 1) assert dm_list_v1(make_token(0)) == { 'dms': [{ "dm_id": 0, "name": "gungeetsingh, petertran", "members": [ { 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }, { 'u_id': 1, 'email': "*****@*****.**", 'name_first': 'Peter', 'name_last': 'Tran', 'handle': 'petertran' }, ], "owner_members": [{ 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }] }] }
def test_channel_invite_v2_multiple_private(user_setup, channel_setup): assert channel_invite_v2(make_token(1), 3, 2) == {} assert channel_details_v2(make_token(1), 3) == { 'name': 'PrivateChannel1', 'is_public': False, 'owner_members': [ { 'u_id': 1, 'email': "*****@*****.**", 'name_first': "Peter", 'name_last': "Tran", 'handle': "petertran" }, ], 'all_members': [ { 'u_id': 1, 'email': "*****@*****.**", 'name_first': "Peter", 'name_last': "Tran", 'handle': "petertran" }, { 'u_id': 2, 'email': "*****@*****.**", 'name_first': "Christopher", 'name_last': "Luong", 'handle': "christopherluong" } ] }
def test_standup_active_v1(user_setup, channel_setup, standup_setup): # Test 1: # invalid token with pytest.raises(AccessError): standup_active_v1(make_token(414345), 0) # "Invalid token" # Test 2: # invalid channel id with pytest.raises(InputError): standup_active_v1(make_token(2), 414345) # "Invalid channel" # Test 3: # no active standup assert standup_active_v1(make_token(0), 0) == { "is_active": False, "time_finish": None } # Test 4: # active standup dateTimeObj = datetime.now() timeStampStr = (dateTimeObj + timedelta(seconds=1)).strftime("%d-%b-%Y (%H:%M)") assert standup_active_v1(make_token(3), 2) == { "is_active": True, "time_finish": timeStampStr }
def channel_setup(): ''' Create multiple channels for multiple users ''' channels_create_v2(make_token(0), 'Gchannel', True) channels_create_v2(make_token(1), 'Pchannel', True) channels_create_v2(make_token(2), 'Cchannel', True)
def test_notifications(register_users): channels_create_v2(make_token(1), "PublicChannel1", True) channel_invite_v2(make_token(1), 0, 0) assert notifications_get_v1(make_token(0)) == { "notifications": [{ "channel_id": 0, "dm_id": -1, "notification_message": "petertran invited you to PublicChannel1" }] } dm_create_v1(make_token(0), [1]) # dm_id 0 assert notifications_get_v1(make_token(1)) == { "notifications": [{ "channel_id": -1, "dm_id": 0, "notification_message": "gungeetsingh added you to dm gungeetsingh, petertran" }] } clear_v1()
def test_message_sendlaterdm_errors(user_setup, dm_setup, channel_setup, message_setup): # invalid token with pytest.raises(AccessError): message_sendlater_v1(make_token(100000), 0, "hello", datetime.datetime.now().timestamp() + 1) # DM ID is not a valid DM with pytest.raises(InputError): message_sendlaterdm_v1(make_token(2), 20, "hello", datetime.datetime.now().timestamp() + 1) # Message is more than 1000 characters thousand_string = '' for i in range(1005): thousand_string += str(i) with pytest.raises(InputError): message_sendlaterdm_v1(make_token(2), 0, thousand_string, datetime.datetime.now().timestamp() + 1) # Time sent is a time in the past with pytest.raises(InputError): message_sendlaterdm_v1(make_token(2), 0, "hello", datetime.datetime.now().timestamp() - 5) # the authorised user is not a member of the DM they are trying to post to with pytest.raises(AccessError): message_sendlaterdm_v1(make_token(2), 1, "hello", datetime.datetime.now().timestamp() + 1)
def test_message_sendlater_errors(user_setup, dm_setup, channel_setup, message_setup): # invalid token with pytest.raises(AccessError): message_sendlater_v1(make_token(100000), 0, "hello", datetime.datetime.now().timestamp() + 1) # channel id is not a valid channel with pytest.raises(InputError): message_sendlater_v1(make_token(1), 10, "hello", datetime.datetime.now().timestamp() + 1) # message is more than 1000 characters thousand_string = '' for i in range(1005): thousand_string += str(i) with pytest.raises(InputError): message_sendlater_v1(make_token(2), 0, thousand_string, datetime.datetime.now().timestamp() + 1) # time sent is a time in the past with pytest.raises(InputError): message_sendlater_v1(make_token(2), 0, "hello", datetime.datetime.now().timestamp() - 5) # authorised user has not joined the channel they are trying to post to with pytest.raises(AccessError): message_sendlater_v1(make_token(1), 2, "hello", datetime.datetime.now().timestamp() + 1)
def test_channels_listall_v2_invalid_user(user_setup): ''' Invalid user attempts to call channels listall ''' channels_create_v2(make_token(1), 'PChannel1', True) with pytest.raises(AccessError): channels_list_v2(make_token(1000))
def test_channel_join_v2(user_setup, channel_setup): channel_join_v2(make_token(2), 0) assert channel_details_v2(make_token(2), 0) == { 'name': 'PublicChannel1', 'is_public': True, 'owner_members': [ { 'u_id': 1, 'email': "*****@*****.**", 'name_first': "Peter", 'name_last': "Tran", 'handle': "petertran" }, ], 'all_members': [ { 'u_id': 1, 'email': "*****@*****.**", 'name_first': "Peter", 'name_last': "Tran", 'handle': "petertran" }, { 'u_id': 2, 'email': "*****@*****.**", 'name_first': "Christopher", 'name_last': "Luong", 'handle': "christopherluong" } ] }
def channel_setup_owner(): ''' Create multiple channels for the same user ''' channels_create_v2(make_token(0), 'Gchannel1', True) channels_create_v2(make_token(0), 'Gchannel2', True) channels_create_v2(make_token(0), 'Gchannel3', False)
def test_channel_messages_fiftyone(user_setup, dm_setup): # send 51 messages and check the dms timeStampStr = [] for i in range(51): message_senddm_v1(make_token(0), 1, str(i)) dateTimeObj = datetime.now() timeStampStr.append(dateTimeObj.strftime("%d-%b-%Y (%H:%M)")) messages = [] for i in range(50): message = { "message_id": (49 - i), "u_id": 0, "message": str(49 - i), "time_created": timeStampStr[49 - i], "channel_id": -1, "dm_id": 1, 'reacts': [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False } messages.append(message) assert dm_messages_v1(make_token(0), 1, 0) == { "messages": messages, "start": 0, "end": 50, } # sends the least recent message assert dm_messages_v1(make_token(0), 1, 50) == { "messages": [{ "message_id": 0, "u_id": 0, "message": "0", "time_created": timeStampStr[0], "channel_id": -1, "dm_id": 1, 'reacts': [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False }], "start": 50, "end": -1, }
def test_dm_messages_v1_leave(user_setup, dm_setup): # testing to see if messages works after user leaves message_senddm_v1(make_token(0), 1, "onions cannot be eaten raw") dateTimeObj = datetime.now() timeStampStr = dateTimeObj.strftime("%d-%b-%Y (%H:%M)") dm_leave_v1(make_token(0), 1) assert dm_messages_v1(make_token(1), 1, 0) == { "messages": [ { "message_id": 0, "u_id": 0, "message": "onions cannot be eaten raw", "time_created": timeStampStr, "channel_id": -1, "dm_id": 1, 'reacts': [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False }, ], "start": 0, "end": -1, }
def test_standup_send_v1_errors(user_setup, channel_setup, standup_setup): # Test 1: # invalid token with pytest.raises(AccessError): standup_send_v1(make_token(414345), 0, "invalid token error") # "Invalid token" # Test 2: # auhtorised user is not part of channel with pytest.raises(AccessError): standup_send_v1(make_token(2), 0, "Not part of channel error") # "Authorised user is not part of the channel" # Test 3: # invalid channel id with pytest.raises(InputError): standup_send_v1(make_token(2), 414345, "invalid channel error") # "Invalid channel" # Test 4: # message too long with pytest.raises(AccessError): standup_send_v1(make_token(2), 0, "1" * 999 + "Message too long error") # "Authorised user is not part of the channel" # Test 5: # no active standup is running in channel with pytest.raises(InputError): standup_send_v1(make_token(1), 1, "no active standup error")
def test_standup_send_v1(user_setup, channel_setup): # send some messages to standup standup_start_v1(make_token(0), 3, 1) assert standup_send_v1(make_token(0), 3, "0") == {} assert standup_send_v1(make_token(1), 3, "1") == {} assert standup_send_v1(make_token(2), 3, "2") == {} assert standup_send_v1(make_token(3), 3, "3") == {}
def test_channel_invite_v2_errors(user_setup, channel_setup_single_user): ''' test the errors for the function channel_invite_v2 ''' # Test 5: # invites an invalid user to a private channel with pytest.raises(requests.exceptions.HTTPError): requests.get(config.url + 'channel/invite/v2', json = { 'token': make_token(1), 'channel_id': 5, 'u_id': 10, }).raise_for_status() # "Invalid user" # Test 6: # invites an invalid user to a public channel with pytest.raises(requests.exceptions.HTTPError): requests.post(config.url + 'channel/invite/v2', json = { 'token': make_token(1), 'channel_id': 2, 'u_id': 10, }).raise_for_status() # "Invalid user" # Test 7: # invites a user to an invalid channel with pytest.raises(requests.exceptions.HTTPError): requests.post(config.url + 'channel/invite/v2', json = { 'token': make_token(1), 'channel_id': 10, 'u_id': 2, }).raise_for_status() # "Invalid channel" # Test 8: # invitation to a channel from an invalid user with pytest.raises(requests.exceptions.HTTPError): requests.post(config.url + 'channel/invite/v2', json = { 'token': make_token(10), 'channel_id': 4, 'u_id': 2, }).raise_for_status() # "Invalid authorised user" # Test 9: # the authorised user sending an invite is not a member of the channel requests.post(config.url + 'auth/register/v2', json = { "email": "*****@*****.**", "password": "******", "name_first": "Tal", "name_last": "Avrahami", }) requests.post(config.url + 'auth/login/v2', json = { "email": "*****@*****.**", "password": "******", }) with pytest.raises(requests.exceptions.HTTPError): requests.post(config.url + 'channel/invite/v2', json = { 'token': make_token(0), 'channel_id': 4, 'u_id': 3, }).raise_for_status() # "Authorised user needs to be a member/
def test_proper_setname(): assert user_profile_setname_v1(make_token(0), 'Eyal', 'Dorfan') == {} assert user_profile_v1(make_token(0), 0) == { 'user': { 'u_id': 0, 'email': '*****@*****.**', 'name_first': 'Eyal', 'name_last': 'Dorfan', 'handle_str': 'gungeetsingh', } }
def test_channels_create_v2_name_too_long(user_setup): ''' User attempts to create a channel whose name is too long and fails ''' badname1 = 'brothisnameiswaytoolongicantbelieveyouwouldeventrythisman' badname2 = 'youreallytryingtoputanotherlongnamebroyouneverlearn' # for public channel with pytest.raises(InputError): channels_create_v2(make_token(0), badname1, True) # for private channel with pytest.raises(InputError): channels_create_v2(make_token(0), badname2, False)
def test_dm_remove_v1_errors(user_setup, dm_setup): # invalid token with pytest.raises(AccessError): dm_remove_v1(make_token(123456), 0) # invalid dm with pytest.raises(InputError): dm_remove_v1(make_token(1), 10) # User is not the owner with pytest.raises(AccessError): dm_remove_v1(make_token(0), 2)
def test_valid_email_reset(register_users): result = user_profile_setemail_v1(make_token(0), '*****@*****.**') assert result == {} assert user_profile_v1(make_token(0), 0) == { 'user': { 'u_id': 0, 'email': '*****@*****.**', 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle_str': 'gungeetsingh', } }
def test_dm_invite_v1_errors(user_setup, dm_setup): # invalid token with pytest.raises(AccessError): dm_invite_v1(make_token(123456), 0, 0) # invalid dm with pytest.raises(InputError): dm_invite_v1(make_token(0), 10, 2) # User is not a member of the dm with pytest.raises(AccessError): dm_invite_v1(make_token(3), 1, 2)
def test_dm_leave_v1_errors(user_setup, dm_setup): # invalid token with pytest.raises(AccessError): dm_leave_v1(make_token(123456), 0) # invalid dm with pytest.raises(InputError): dm_leave_v1(make_token(1), 10) # User is not part of the dm with pytest.raises(AccessError): dm_leave_v1(make_token(3), 0)
def test_channel_details_v2_errors(user_setup, channel_setup): # invalid channel with pytest.raises(InputError): channel_details_v2(make_token(1), 10) # invalid authorised user with pytest.raises(AccessError): channel_details_v2(make_token(10), 0) # Authorised user is not a member of channel with channel_id with pytest.raises(AccessError): channel_details_v2(make_token(3), 0)
def test_channel_join_v2_errors(user_setup, channel_setup): # invalid authorised user with pytest.raises(AccessError): channel_join_v2(make_token(10), 2) # invalid channel id with pytest.raises(InputError): channel_join_v2(make_token(1), 10) # channel_id refers to a private channel and the authorised user is not a member with pytest.raises(AccessError): channel_join_v2(make_token(2), 5)
def test_channels_list_v2_invite(user_setup, channel_setup): ''' list channels after inviting another user (Non-owner) ''' channel_invite_v2(make_token(1), 1, 2) assert channels_list_v2(make_token(1)) == { 'channels': [ { 'channel_id': 1, 'name': 'Pchannel', }, ], }
def test_channels_create_v2_works(user_setup): ''' User is able to create a channel ''' assert channels_create_v2(make_token(0), "channel1", True) == { 'channel_id': 0 } assert channels_create_v2(make_token(0), "channel2", True) == { 'channel_id': 1 } assert channels_create_v2(make_token(0), "channel3", True) == { 'channel_id': 2 }
def test_handle_iteration(): clear_v1() auth_register_v2('*****@*****.**', '123abc!@#', 'Tal', 'Avrahami') result = auth_register_v2('*****@*****.**', '123456', 'Tal', 'Avrahami') assert result == {'token': make_token(1), 'auth_user_id': 1} assert user_profile_v1(make_token(1), 1) == { 'user': { 'u_id': 1, 'email': '*****@*****.**', 'name_first': 'Tal', 'name_last': 'Avrahami', 'handle_str': 'talavrahami0', } }
def test_dm_list_v1_success(user_setup, dm_setup): # list out a particular users dms assert dm_list_v1(make_token(3)) == { 'dms': [{ "dm_id": 3, "name": "gungeetsingh, talavrahami", "members": [ { 'u_id': 3, 'email': "*****@*****.**", 'name_first': 'Tal', 'name_last': 'Avrahami', 'handle': 'talavrahami' }, { 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }, ], "owner_members": [{ 'u_id': 3, 'email': "*****@*****.**", 'name_first': 'Tal', 'name_last': 'Avrahami', 'handle': 'talavrahami' }] }] }
def test_dm_details_v1_multiple(user_setup, dm_setup): # Test 2: # Lists the details of dm no.1 with multiple users in the dm assert dm_details_v1(make_token(1), 0) == { "name": "christopherluong, gungeetsingh, petertran", "members": [{ 'u_id': 1, 'email': "*****@*****.**", 'name_first': 'Peter', 'name_last': 'Tran', 'handle': 'petertran' }, { 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }, { 'u_id': 2, 'email': "*****@*****.**", 'name_first': 'Christopher', 'name_last': 'Luong', 'handle': 'christopherluong' }], }
def test_invalid_handle(register_users): assert user_profile_sethandle_v1(make_token(0), 'EEEEyaldorfan@ ') == {} assert user_profile_v1(make_token(0), 0) == { 'user': { 'u_id': 0, 'email': '*****@*****.**', 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle_str': 'eeeeyaldorfan', } } with pytest.raises(InputError): user_profile_sethandle_v1(make_token(0), 'A') user_profile_sethandle_v1(make_token(0), 'A' * 21) user_profile_sethandle_v1(make_token(0), 'eeeeyaldorfan')
def test_users_all(register_users): assert users_all_v1(make_token(0)) == { 'users': [ { 'u_id': 0, 'email': '*****@*****.**', 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle_str': 'gungeetsingh', }, { 'u_id': 1, 'email': '*****@*****.**', 'name_first': 'Peter', 'name_last': 'Tran', 'handle_str': 'petertran', }, { 'u_id': 2, 'email': '*****@*****.**', 'name_first': 'Christopher', 'name_last': 'Luong', 'handle_str': 'christopherluong', }, { 'u_id': 3, 'email': '*****@*****.**', 'name_first': 'Tal', 'name_last': 'Avrahami', 'handle_str': 'talavrahami', }, ] }