def test_channels_create_invalid_token(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) with pytest.raises(AccessError): channels_create.channels_create("designedtofail", "Example Channel", True)
def test_channels_create_name_too_long(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) with pytest.raises(ValueError): channels_create.channels_create(TOKEN, "Example Channel <----------", True)
def message_send(token, channel_id, message): users = unbox('users', {}) channels = unbox('channels', {}) messages = unbox('messages', {}) auth = users[str(decode(token)['u_id'])] channel = channels[str(channel_id)] if auth['u_id'] not in channel['all_members']: raise AccessError("Unauthorised") if not (1 <= len(message) <= 1000): raise ValueError("Message is either too long or too short") message_id = 0 if len(messages) == 0 else max([int(id) for id in messages]) + 1 messages[str(message_id)] = { 'message_id': message_id, 'channel_id': channel_id, 'u_id': auth['u_id'], 'message': message, 'time_created': int(time.time()), 'reacts': {}, 'is_pinned': False } box('messages', messages) return {'message_id': message_id}
def test_standup_send_invalid_token(): box('tokens', [TOKEN]) box( 'users', { "1": { "u_id": 1, 'handle_str': 'a', "permission_id": 1 }, "2": { "u_id": 2, 'handle_str': 'b', "permission_id": 1 } }) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [0, 1], 'owner_members': [0] } }) box('channel_id_to_time_finish', {'1': 3}) box('channel_id_to_messages', {'1': []}) with pytest.raises(AccessError): standup_send.standup_send("designedtofail", 1, 'test')
def user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end): if img_url[-4:] != '.jpg' and img_url[-5:] != '.jpeg': raise ValueError(f"Invalid url: '{img_url}'") identifier = str(uuid.uuid4()) PROFILE_IMG_URL = unbox('url_base', '') + '/user/profiles/photo/' + identifier + '.jpg' FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../utility/storage/' + identifier + '.jpg') try: urllib.request.urlretrieve(img_url, FILE) except Exception: raise ValueError(f"Cannot retrieve image: '{img_url}'") try: img = Image.open(FILE) cropped = img.crop((x_start, y_start, x_end, y_end)) cropped.save(FILE) except Exception: os.remove(FILE) raise ValueError("Cannot crop image") users = unbox('users', []) users[str(decode(token)['u_id'])]['profile_img_url'] = PROFILE_IMG_URL box('users', users) return {}
def message_unreact(token, message_id, react_id): users = unbox('users', {}) channels = unbox('channels', {}) messages = unbox('messages', {}) auth = users[str(decode(token)['u_id'])] message = messages[str(message_id)] channel = channels[str(message['channel_id'])] if auth['u_id'] not in channel['all_members']: raise AccessError("Unauthorised") if react_id not in [1]: raise ValueError(f"Invalid ID: react_id - {react_id}") reacts = messages[str(message_id)]['reacts'] if str(react_id) not in reacts or auth['u_id'] not in reacts[str(react_id)]['u_ids']: raise ValueError("React doesn't exist") messages[str(message_id)]['reacts'][str(react_id)]['u_ids'].remove(auth['u_id']) box('messages', messages) return {}
def message_react(token, message_id, react_id): users = unbox('users', {}) channels = unbox('channels', {}) messages = unbox('messages', {}) auth = users[str(decode(token)['u_id'])] message = messages[str(message_id)] channel = channels[str(message['channel_id'])] if auth['u_id'] not in channel['all_members']: raise AccessError("Unauthorised") if react_id not in [1]: raise ValueError(f"Invalid ID: react_id - {react_id}") reacts = message['reacts'] if str(react_id) in reacts and auth['u_id'] in reacts[str( react_id)]['u_ids']: raise ValueError("React already exists") if str(react_id) in reacts: reacts[str(react_id)]['u_ids'].append(auth['u_id']) else: reacts[str(react_id)] = {'react_id': react_id, 'u_ids': [auth['u_id']]} message['reacts'] = reacts messages[str(message_id)] = message box('messages', messages) return {}
def channels_create(token, name, is_public): users = unbox('users', {}) channels = unbox('channels', {}) auth = users[str(decode(token)['u_id'])] if auth['permission_id'] == 3: raise AccessError("Unauthorised") if len(name) > 20: raise ValueError("Invalid name (too long)") channel_id = 0 if len(channels) == 0 else max([int(id) for id in channels]) + 1 channels[str(channel_id)] = { 'channel_id': channel_id, 'name': name, 'is_public': is_public, 'all_members': [auth['u_id']], 'owner_members': [auth['u_id']] } box('channels', channels) return {'channel_id': channel_id}
def test_user_profiles_uploadphoto_success(): box('tokens', [TOKEN]) box('users', {"1" : { "u_id": 1 }}) pre = len(os.listdir(PATH)) user_profiles_uploadphoto.user_profiles_uploadphoto(TOKEN, IMG_URL, 0, 0, 580, 390) post = len(os.listdir(PATH)) users = unbox('users') assert 'profile_img_url' in users['1'] assert post - pre == 1
def test_message_unpin_invalid_token(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [1], 'owner_members': [1] } }) box( 'messages', { '0': { 'message_id': 0, 'channel_id': 1, 'u_id': 1, 'message': "hello, world!", 'time_created': time.asctime(), 'reacts': {}, 'is_pinned': True } }) with pytest.raises(AccessError): message_unpin.message_unpin("designedtofail", 0)
def test_channels_create_success(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) channels_create.channels_create(TOKEN, "Example Channel", True) channels = unbox('channels') assert channels["0"] == { 'channel_id': 0, 'name': "Example Channel", 'is_public': True, 'all_members': [1], 'owner_members': [1] }
def test_message_react_already_reacted(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [1], 'owner_members': [1] } }) box( 'messages', { '0': { 'message_id': 0, 'channel_id': 1, 'u_id': 3, 'message': "hello, world!", 'time_created': time.asctime(), 'reacts': { '1': { 'react_id': 1, 'u_ids': [1] } }, 'is_pinned': True } }) with pytest.raises(ValueError): message_react.message_react(TOKEN, 0, 1)
def test_message_send_success_1(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [1], 'owner_members': [1] } }) box('messages', {}) assert message_send.message_send(TOKEN, 1, "Oh Boy") == {'message_id': 0} messages = unbox('messages') assert messages['0'] == { 'message_id': 0, 'channel_id': 1, 'u_id': 1, 'message': "Oh Boy", 'time_created': int(time.time()), 'reacts': {}, 'is_pinned': False }
def test_message_sendlater_success_1(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [1], 'owner_members': [1] } }) box('messages', {}) timmyboywontyoucomewithme = int(time.time()) + 3 assert message_sendlater.message_sendlater(TOKEN, 1, "Oh Boy", timmyboywontyoucomewithme) == { 'message_id': 0 } time.sleep(4) messages = unbox('messages') assert messages['0'] == { 'message_id': 0, 'channel_id': 1, 'u_id': 1, 'message': "Oh Boy", 'time_created': timmyboywontyoucomewithme, 'reacts': {}, 'is_pinned': False }
def test_message_react_not_authorized(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 3}}) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [], 'owner_members': [] } }) box( 'messages', { '0': { 'message_id': 0, 'channel_id': 1, 'u_id': 1, 'message': "hello, world!", 'time_created': time.asctime(), 'reacts': {}, 'is_pinned': False } }) with pytest.raises(AccessError): message_react.message_react(TOKEN, 0, 1)
def test_message_react_success_1(): box('tokens', [TOKEN]) box('users', {"1": {"u_id": 1, "permission_id": 1}}) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [1], 'owner_members': [1] } }) box( 'messages', { '0': { 'message_id': 0, 'channel_id': 1, 'u_id': 1, 'message': "hello, world!", 'time_created': time.asctime(), 'reacts': {}, 'is_pinned': False } }) message_react.message_react(TOKEN, 0, 1) messages = unbox('messages') assert messages['0']['reacts'] == {'1': {'react_id': 1, 'u_ids': [1]}}
def test_standup_active_success(): box('tokens', [TOKEN]) box( 'users', { "1": { "u_id": 1, 'handle_str': 'a', "permission_id": 1 }, "2": { "u_id": 2, 'handle_str': 'b', "permission_id": 1 } }) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [0, 1], 'owner_members': [0] } }) box('channel_id_to_time_finish', {'1': 3}) assert standup_active.standup_active(TOKEN, 1) == { 'is_active': True, 'time_finish': 3 }
def test_auth_passwordreset_reset_invalid_password(): box('users', {"1" : { "u_id": 1, "email": "*****@*****.**", 'password' : HASH } }) box('email_to_u_id', { "*****@*****.**" : 1}) box('handle_to_u_id', {"janecitizen" : 1}) box('reset_code_to_u_id', {"abcde" : 1}) with pytest.raises(ValueError): auth_passwordreset_reset.auth_passwordreset_reset("abcde", "woops")
def test_auth_passwordreset_reset_unknown_code(): box('users', {"1" : { "u_id": 1, "email": "*****@*****.**", 'password' : HASH } }) box('email_to_u_id', { "*****@*****.**" : 1}) box('handle_to_u_id', {"janecitizen" : 1}) box('reset_code_to_u_id', {"abcde" : 1}) with pytest.raises(ValueError): auth_passwordreset_reset.auth_passwordreset_reset("designedtofail", NEW_PASSWORD)
def test_user_profile_sethandle_length_issues(): box('tokens', [TOKEN]) box('users', {"1": USER}) box('email_to_u_id', {"*****@*****.**": 1}) box('handle_to_u_id', {"janecitizen": 1}) with pytest.raises(ValueError): user_profile_sethandle.user_profile_sethandle(TOKEN, "koolkid" * 50)
def test_users_all_invalid_token(): box('tokens', [TOKEN]) box('users', {"1" : USER}) box('email_to_u_id', { "*****@*****.**" : 1}) box('handle_to_u_id', {"janecitizen" : 1}) with pytest.raises(AccessError): users_all.users_all("designedtofail")
def test_user_profile_setname_invalid_last_name(): box('tokens', [TOKEN]) box('users', {"1": USER}) box('email_to_u_id', {"*****@*****.**": 1}) box('handle_to_u_id', {"janecitizen": 1}) with pytest.raises(ValueError): user_profile_setname.user_profile_setname(TOKEN, "First", "A" * 51)
def test_message_pin_no_such_message(): box('tokens', [TOKEN]) box('users', {"1" : { "u_id": 1, "permission_id": 1 }}) box('channels', {"1" : {'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [1], 'owner_members': [1]}}) box('messages', {}) with pytest.raises(ValueError): message_pin.message_pin(TOKEN, 0)
def test_user_profile_no_such_user(): box('tokens', [TOKEN]) box('users', {"1" : USER}) box('email_to_u_id', { "*****@*****.**" : 1}) box('handle_to_u_id', {"janecitizen" : 1}) with pytest.raises(ValueError): user_profile.user_profile(TOKEN, 3)
def test_user_profile_setname_invalid_token(): box('tokens', [TOKEN]) box('users', {"1": USER}) box('email_to_u_id', {"*****@*****.**": 1}) box('handle_to_u_id', {"janecitizen": 1}) with pytest.raises(AccessError): user_profile_setname.user_profile_setname("designedtofail", "First", "Last")
def test_auth_register_success_general(): box('users', {"1" : { "u_id": 1, "permission_id": 1 }}) auth_register.auth_register("*****@*****.**", "secret", "Jane", "Citizen") users = unbox('users', {}) assert users['2'] == { 'u_id' : 2, 'email' : "*****@*****.**", 'password' : HASH, 'permission_id' : 3, 'name_first' : "Jane", 'name_last' : "Citizen", 'profile_img_url' : '/user/profiles/photo/default.jpg', 'handle_str' : "janecitizen" }
def test_auth_register_success_unique_handle(): box('handle_to_u_id', {"janecitizen" : 1}) auth_register.auth_register("*****@*****.**", "secret", "Jane", "Citizen") users = unbox('users', {}) assert users['0'] == { 'u_id' : 0, 'email' : "*****@*****.**", 'password' : HASH, 'permission_id' : 1, 'name_first' : "Jane", 'name_last' : "Citizen", 'profile_img_url' : '/user/profiles/photo/default.jpg', 'handle_str' : "janecitizen1" }
def test_channel_messages_no_such_channel(): box('tokens', [TOKEN]) box('users', { "1": { "u_id": 1, "permission_id": 1 }, "2": { "u_id": 2, "permission_id": 1 } }) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [0, 1], 'owner_members': [0, 1] } }) box( 'messages', { "1": { 'message_id': 1, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': 1, 'reacts': {}, 'is_pinned': None }, "2": { 'message_id': 2, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': 2, 'reacts': {}, 'is_pinned': None }, "3": { 'message_id': 3, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': 3, 'reacts': {}, 'is_pinned': None } }) with pytest.raises(ValueError): channel_messages.channel_messages(TOKEN, 2, 1)
def test_search_invalid_token(): box('tokens', [TOKEN]) box('users', { "1": { "u_id": 1, "permission_id": 1 }, "2": { "u_id": 2, "permission_id": 1 } }) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [0, 1], 'owner_members': [0, 1] } }) box( 'messages', { "1": { 'message_id': 1, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': None, 'reacts': None, 'is_pinned': None }, "2": { 'message_id': 2, 'channel_id': 1, 'message': "dfjgdfgkfdgdfgtestdlkfglkdfjg", 'u_id': 1, 'time_created': None, 'reacts': {}, 'is_pinned': None }, "3": { 'message_id': 3, 'channel_id': 1, 'message': "dfgjdfgdftestlkdfjgdlfkgjdfg", 'u_id': 1, 'time_created': None, 'reacts': {}, 'is_pinned': None } }) with pytest.raises(AccessError): search.search("designedtofail", "test")
def test_channel_messages_invalid_token(): box('tokens', [TOKEN]) box('users', { "1": { "u_id": 1, "permission_id": 1 }, "2": { "u_id": 2, "permission_id": 1 } }) box( 'channels', { "1": { 'channel_id': 1, 'name': "1st", 'is_public': True, 'all_members': [0, 1], 'owner_members': [0, 1] } }) box( 'messages', { "1": { 'message_id': 1, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': 1, 'reacts': {}, 'is_pinned': None }, "2": { 'message_id': 2, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': 2, 'reacts': {}, 'is_pinned': None }, "3": { 'message_id': 3, 'channel_id': 1, 'message': None, 'u_id': 1, 'time_created': 3, 'reacts': {}, 'is_pinned': None } }) with pytest.raises(AccessError): channel_messages.channel_messages("designedtofail", 1, 1)