def initial_data(url): # this function would clear all data and create 3 users # user1 would create channel 1 and user3 would create channel2 # user2 would join channel1 requests.delete(url + 'clear') # create 3 users user_data = { 'password' : 'password', 'name_first': 'u', 'name_last' : '1', } for idx in range(3): email = str(idx + 1) + '*****@*****.**' user_data['email'] = email requests.post(url + 'auth/register', json=user_data) requests.post(url + 'auth/login', json={'email': email, 'password': '******'}) # create 2channels and user2 join channel1 data = { 'token' : token_generate(1, 'login'), 'name' : 'channel_1', 'is_public' : True, } requests.post(url + 'channels/create', json=data) data['token'] = token_generate(3, 'login') data['name'] = 'channel_1' requests.post(url + 'channels/create', json=data) requests.post(url + 'channel/join', json={'token': token_generate(2, 'login'), 'channel_id': 1})
def test_valid_setname(url, initial_basics): ''' valid test: ''' #get token data = { 'token': token_generate(1, 'login'), 'name_first': 'first', "name_last": 'last', } resp = requests.put(url + 'user/profile/setname', json=data) assert resp.status_code == 200 ###http test for length 50### long_name = '0123456789' long_name = long_name * 5 data = { 'token': token_generate(1, 'login'), 'name_first': long_name, 'name_last': long_name, } resp = requests.put(url + 'user/profile/setname', json=data) assert resp.status_code == 200 ###http test for length 1### #reset user3 name with data data = { 'token': token_generate(1, 'login'), 'name_first': '1', 'name_last': '1', } resp = requests.put(url + 'user/profile/setname', json=data) assert resp.status_code == 200
def test_search_standard(url, initial_data): """ Test for standard functionality of search() according to spec. :param url: pytest fixture that starts the server and gets its URL :type url: pytest fixture :param initial_data: pytest fixture to create two users and a channel :type initial_data: pytest fixture """ # do send msgs msg_data = { 'token': token_generate(1, 'login'), 'channel_id': 1, 'message': '' } for i in range(6): msg_data['message'] = 'msg' + str(i + 1) requests.post(url + 'message/send', json=msg_data) # user 1 search (1 output) search_data = { 'token': token_generate(1, 'login'), 'query_str': '3', } resp = requests.get(url + 'search', params=search_data) assert resp.status_code == 200 assert len(json.loads(resp.text)['messages']) == 1 assert json.loads(resp.text)['messages'][0]['message_id'] == 10003 assert json.loads(resp.text)['messages'][0]['u_id'] == 1 # user 2 search (no output) search_data['token'] = token_generate(2, 'login') resp = requests.get(url + 'search', params=search_data) assert resp.status_code == 200 assert len(json.loads(resp.text)['messages']) == 0
def test_register_standard(url): ''' A simple test to register standard ''' # create a standard user with len(password) == 6 and # len(first_name) == 1 and len(last_name) == 1 input = { 'email': '*****@*****.**', 'password': '******', 'name_first': 'u', 'name_last': '1', } resp = requests.post(url + 'auth/register', json=input) assert resp.status_code == 200 assert json.loads(resp.text)['u_id'] == 1 assert json.loads(resp.text)['token'] == token_generate(1, 'register') # create a standard user with len(password) == 6 and # len(first_name) == 50 and len(last_name) == 50 input = { 'email': '*****@*****.**', 'password': '******', 'name_first': 'u' * 50, 'name_last': '2' * 50, } resp = requests.post(url + 'auth/register', json=input) assert resp.status_code == 200 assert json.loads(resp.text)['u_id'] == 2 assert json.loads(resp.text)['token'] == token_generate(2, 'register')
def test_send_error_invalid_token(url, initial_data): ''' error test when given token is invalid 1. non-existing 2. logout token ''' # start a standup data = { 'token' : token_generate(1, 'login'), 'channel_id' : 1, 'length' : 1 } requests.post(url + 'standup/start', json=data) # 1. non-existing token data = { 'token' : 'invalid_token', 'channel_id' : 1, 'message' : 'msg', } resp = requests.post(url + 'standup/send', json=data) assert resp.status_code == 400 # 2. user1 logout token data = { 'token' : token_generate(1, 'logout'), 'channel_id' : 1, 'message' : 'msg', } resp = requests.post(url + 'standup/send', json=data) assert resp.status_code == 400
def test_active_standard(url, initial_data): ''' user1 calls start in channel_1 user1 calls active in channel_1 user3 calls active in channel_2 ''' # start standup in channel1 data = { 'token' : token_generate(1, 'login'), 'channel_id' : 1, 'length' : 1 } requests.post(url + 'standup/start', json=data) # chanenl 1 active check curr_time = int(time.time()) data = { 'token' : token_generate(1, 'login'), 'channel_id' : 1, } resp = requests.get(url + '/standup/active', params=data) assert resp.status_code == 200 assert json.loads(resp.text)['is_active'] == True assert json.loads(resp.text)['time_finish'] == curr_time + 1 # channel 2 active check data = { 'token' : token_generate(3, 'login'), 'channel_id' : 2, } resp = requests.get(url + '/standup/active', params=data) assert resp.status_code == 200 assert json.loads(resp.text)['is_active'] == False assert json.loads(resp.text)['time_finish'] is None
def test_handle_incorrect_length(url, initial_basics): ''' incorrect length test for reset handle ''' handle_reps1 = requests.put(url + 'user/profile/sethandle', json={ 'token': token_generate(1, 'login'), 'handle_str': 'u' }) assert handle_reps1.status_code == 400 handle_reps2 = requests.put(url + 'user/profile/sethandle', json={ 'token': token_generate(1, 'login'), 'handle_str': 'a' * 21 }) assert handle_reps2.status_code == 400 handle_reps1 = requests.put(url + 'user/profile/sethandle', json={ 'token': token_generate(1, 'login'), 'handle_str': '' }) assert handle_reps1.status_code == 400 handle_reps1 = requests.put(url + 'user/profile/sethandle', json={ 'token': token_generate(1, 'login'), 'handle_str': '12' }) assert handle_reps1.status_code == 400
def create_channels(url): """ Pytest fixture that creates 6 test channels with tokens from two users via HTTP routing. """ channel_data = { 'token': token_generate(1, 'login'), 'name': 'Channel 01', 'is_public': True, } requests.post(url + 'channels/create', json=channel_data) channel_data = { 'token': token_generate(1, 'login'), 'name': 'Channel 02', 'is_public': False, } requests.post(url + 'channels/create', json=channel_data) channel_data = { 'token': token_generate(2, 'login'), 'name': 'Channel 03 User 02', 'is_public': True, } requests.post(url + 'channels/create', json=channel_data) channel_data = { 'token': token_generate(2, 'login'), 'name': 'Channel 04 User 02', 'is_public': False, } requests.post(url + 'channels/create', json=channel_data)
def test_react_unreact_errors(url, initial_conditions): ''' error checking for react and unreact only check status code ''' # 1. message_id is not a valid message within a channel that the authorised user has joined # user 6 react and unreact to msg 10001 data = { 'token': token_generate(6, 'login'), 'message_id': 10001, 'react_id': 1, } resp = requests.post(url + 'message/react', json=data) assert resp.status_code == 400 resp = requests.post(url + 'message/unreact', json=data) assert resp.status_code == 400 # 2. react_id is not a valid React ID. The only valid react ID the frontend has is 1 # user 1 react and unreact to msg 10001 with react_id 0 data = { 'token': token_generate(1, 'login'), 'message_id': 10001, 'react_id': 0, } resp = requests.post(url + 'message/react', json=data) assert resp.status_code == 400 resp = requests.post(url + 'message/unreact', json=data) assert resp.status_code == 400 # 3. Message with ID message_id already contains an active React # with ID react_id from the authorised user # user 1 react to msg 10001 and do it again # user 1 unreact to msg 10001 and do in again data = { 'token': token_generate(1, 'login'), 'message_id': 10001, 'react_id': 1, } resp = requests.post(url + 'message/react', json=data) assert resp.status_code == 200 resp = requests.post(url + 'message/react', json=data) assert resp.status_code == 400 resp = requests.post(url + 'message/unreact', json=data) assert resp.status_code == 200 resp = requests.post(url + 'message/unreact', json=data) assert resp.status_code == 400 # 4.given token is invalid data = { 'token': 'invalid token', 'message_id': 10001, 'react_id': 1, } resp = requests.post(url + 'message/react', json=data) assert resp.status_code == 400 resp = requests.post(url + 'message/unreact', json=data) assert resp.status_code == 400
def test_remove_message_id(url, initial_conditions): # message not sent by user data = { 'token': token_generate(5, 'login'), 'message_id': 10002, } resp = requests.delete(url + 'message/remove', json=data) assert resp.status_code == 400 # message sent by user data = { 'token': token_generate(2, 'login'), 'message_id': 10002, } resp = requests.delete(url + 'message/remove', json=data) assert resp.status_code == 200
def initial_data(url): """ Pytest fixture that registers and logs in two users and creates a channel via HTTP routing. """ requests.delete(url + 'clear') data = { 'email': '*****@*****.**', 'password': '******', 'name_first': 'name_first', 'name_last': 'name_last', } requests.post(url + 'auth/register', json=data) requests.post(url + 'auth/login', json={ 'email': data['email'], 'password': data['password'] }) data['email'] = '*****@*****.**' requests.post(url + 'auth/register', json=data) requests.post(url + 'auth/login', json={ 'email': data['email'], 'password': data['password'] }) data = { 'token': token_generate(1, 'login'), 'name': 'channel', 'is_public': False, } requests.post(url + 'channels/create', json=data)
def test_upload_photo_invalid_bounds(url, initial_basics): ''' input error when given bounds are invalid user1 upload a img with invalid bounds and check its status_code 1. negative value 2. out of ranges ''' img_url = 'https://img1.looper.com/img/gallery/things-only-adults-notice-in-shrek/intro-1573597941.jpg' # 1. negative value data = { 'token': token_generate(1, 'login'), 'img_url': img_url, 'x_start': -1, 'x_end': 200, 'y_start': -1, 'y_end': 200, } resp = requests.post(url + 'user/profile/uploadphoto', json=data) assert resp.status_code == 400 # 2. out of ranges data['x_start'] = 1000000 data['y_start'] = 0 resp = requests.post(url + 'user/profile/uploadphoto', json=data) assert resp.status_code == 400 data['x_start'] = 0 data['y_start'] = 0 data['x_end'] = 100000 resp = requests.post(url + 'user/profile/uploadphoto', json=data) assert resp.status_code == 400
def test_remove_owner(url, initial_conditions): #user not owner of channel data = { 'token': token_generate(3, 'login'), 'message_id': 10003, } resp = requests.delete(url + 'message/remove', json=data) assert resp.status_code == 400 #user owner of channel data = { 'token': token_generate(1, 'login'), 'message_id': 10001, } resp = requests.delete(url + 'message/remove', json=data) assert resp.status_code == 200
def test_http_listall_standard(url, create_users, create_channels): """ Test for standard functionality of channels_list() according to spec. :param url: pytest fixture that starts the server and gets its URL :type url: pytest fixture :param create_users: pytest fixture to create two test users :type create_users: pytest fixture :param create_channels: pytest fixture to create six test channels :type create_channels: pytest fixture """ resp = requests.get(url + 'channels/listall', params={'token': token_generate(1, 'login')}) payload = resp.json() # test length and accuracy of returned channels list, making sure # channels of User 2 is not listed assert len(payload['channels']) == 4 channel_01_listed = payload['channels'][0] channel_02_listed = payload['channels'][1] channel_03_listed = payload['channels'][2] channel_04_listed = payload['channels'][3] assert channel_01_listed['channel_id'] == 1 assert channel_01_listed['name'] == 'Channel 01' assert channel_02_listed['channel_id'] == 2 assert channel_02_listed['name'] == 'Channel 02' assert channel_03_listed['channel_id'] == 3 assert channel_03_listed['name'] == 'Channel 03 User 02' assert channel_04_listed['channel_id'] == 4 assert channel_04_listed['name'] == 'Channel 04 User 02'
def test_remove_invalid_token(url, initial_conditions): data = { 'token': token_generate(1, 'logout'), 'message_id': 10001, } resp = requests.delete(url + 'message/remove', json=data) assert resp.status_code == 400
def test_setemail_invalid_token(url, initial_basics): data = { 'token': token_generate(1, 'logout'), 'email': '*****@*****.**', } resp = requests.put(url + 'user/profile/setemail', json=data) assert resp.status_code == 400
def test_clear(url): """ Test for standard functionality of clear() according to spec. :param url: pytest fixture that starts the server and gets its URL :type url: pytest fixture """ data = { 'email': '*****@*****.**', 'password': '******', 'name_first': 'u', 'name_last': '1', } requests.post(url + 'auth/register', json=data) data['email'] = '*****@*****.**' requests.post(url + 'auth/register', json=data) requests.post(url + 'auth/login', json={ 'email': data['email'], 'password': '******' }) resp = requests.get(url + 'users/all', params={'token': token_generate(2, 'login')}) assert len(json.loads(resp.text)['users']) == 2 # create a channel requests.post(url + 'channels/create', json={ 'token': token_generate(2, 'login'), 'name': 'name', 'is_public': True }) resp = requests.get(url + 'channels/listall', params={'token': token_generate(2, 'login')}) assert resp.status_code == 200 assert len(json.loads(resp.text)['channels']) == 1 # do clear resp = requests.delete(url + 'clear') assert resp.status_code == 200 # cannot get all users resp = requests.get(url + 'users/all', params={'token': token_generate(2, 'login')}) assert resp.status_code == 400 # cannot get all channels resp = requests.get(url + 'channels/listall', params={'token': token_generate(2, 'login')}) assert resp.status_code == 400
def test_sethandle_valid(url, initial_basics): ''' valid test for sethandle ''' #reset user1 handle with data data = {'token': token_generate(1, 'login'), 'handle_str': 'updatename'} resp = requests.put(url + 'user/profile/sethandle', json=data) assert resp.status_code == 200
def test_edit_not_an_owner(url, initial_conditions): #user not an owner data = { 'token': token_generate(3, 'login'), 'message_id': 10003, 'message': 'new message', } resp = requests.put(url + 'message/edit', json=data) assert resp.status_code == 400 #user is owner data = { 'token': token_generate(2, 'login'), 'message_id': 10002, 'message': 'new message', } resp = requests.put(url + 'message/edit', json=data) assert resp.status_code == 200
def test_profile_setemail_valid(url, initial_basics): ''' invalid test of 0 characters and 26 charaters of firstname ''' #reset email for user data2 = {'token': token_generate(1, 'login'), 'email': '*****@*****.**'} resp = requests.put(url + 'user/profile/setemail', json=data2) assert resp.status_code == 200
def test_edit_messageid(url, initial_conditions): # incorrect message_id data = { 'token': token_generate(5, 'login'), 'message_id': 10002, 'message': 'new message', } resp = requests.put(url + 'message/edit', json=data) assert resp.status_code == 400 # correct message_id data = { 'token': token_generate(2, 'login'), 'message_id': 10002, 'message': 'new message', } resp = requests.put(url + 'message/edit', json=data) assert resp.status_code == 200
def test_profile_setemail_occupied(url, initial_basics): ''' invalid test of incorrect email format ''' #reset user1 name with data data = {'token': token_generate(1, 'login'), 'email': '*****@*****.**'} resp1 = requests.put(url + 'user/profile/setemail', json=data) assert resp1.status_code == 400
def test_profile_invalid_token(url, initial_basics): data = { 'token': 'invalid_token', 'u_id': 1, } profile_resp = requests.get(url + 'user/profile', params=data) assert profile_resp.status_code == 400 data['token'] = token_generate(1, 'logout') assert profile_resp.status_code == 400
def test_handle_being_used(url, initial_basics): ''' invalid test for the handle has being used user1 set a handle and user2 set the same handle ''' requests.put(url + 'user/profile/sethandle', json={ 'token': token_generate(1, 'login'), 'handle_str': 'temp1_handle_str' }) handle_resp2 = requests.put(url + 'user/profile/sethandle', json={ 'token': token_generate(2, 'login'), 'handle_str': 'temp1_handle_str' }) assert handle_resp2.status_code == 400
def test_send_invalid_token(url, initial_conditions): #incorrect channel_id data = { 'token': token_generate(1, 'logout'), 'channel_id': 0, 'message': 'this shouldnt work', } resp = requests.post(url + 'message/send', json=data) assert resp.status_code == 400
def test_edit_invalid_token(url, initial_conditions): data = { 'token': token_generate(1, 'logout'), 'message_id': 10001, 'message': 'new message', } resp = requests.put(url + 'message/edit', json=data) assert resp.status_code == 400
def test_send_bad_message(url, initial_conditions): #message over 1000 characters data = { 'token': token_generate(1, 'login'), 'channel_id': 1, 'message': 'c' * 1001, } resp = requests.post(url + 'message/send', json=data) assert resp.status_code == 400
def test_send_standard(url, initial_conditions): #standard send data = { 'token': token_generate(1, 'login'), 'channel_id': 1, 'message': 'This is the first message.', } resp = requests.post(url + 'message/send', json=data) assert resp.status_code == 200
def initial_conditions(url): # creates 5 users, user 1 creates a channel, the rest write a message to # that channel user 1 and 2 are owners user_data = { 'password': '******', 'name_first': '1', 'name_last': '1', } for idx in range(6): email = str(idx + 1) + '*****@*****.**' user_data['email'] = email requests.post(url + 'auth/register', json=user_data) requests.post(url + 'auth/login', json={ 'email': email, 'password': '******' }) channel_data = { 'token': token_generate(1, 'login'), 'name': 'channel' + str(idx), 'is_public': True, } requests.post(url + 'channels/create', json=channel_data) for idx in range(1, 5): requests.post(url + 'channel/join', json={ 'token': token_generate(idx + 1, 'login'), 'channel_id': 1 }) data = { 'token': token_generate(idx + 1, 'login'), 'channel_id': 1, 'message': 'message ' + str(idx), } requests.post(url + 'message/send', json=data) if idx == 1: data = { 'token': token_generate(1, 'login'), 'channel_id': 1, 'u_id': 2, } requests.post(url + 'channel/addowner', json=data)
def test_send_error_invalid_msg(url, initial_data): ''' error test when msg is too long (1000 characters) user1 calls start in channel1 and send a too long msg ''' # start a standup data = { 'token' : token_generate(1, 'login'), 'channel_id' : 1, 'length' : 1 } requests.post(url + 'standup/start', json=data) # send a too long msg data = { 'token' : token_generate(1, 'login'), 'channel_id' : 1, 'message' : 'm' * 1001 } resp = requests.post(url + 'standup/send', json=data) assert resp.status_code == 400