def test_dm(test_data):
    a_data, b_data, c_data, a_ch_id = test_data
    channel_join_v2(b_data["token"], a_ch_id)
    channel_join_v2(c_data["token"], a_ch_id)

    # Create the unscramble user
    message_send_v2(a_data["token"], a_ch_id, "/u start")

    us_id = None
    for user in users_all_v1(a_data["token"])["users"]:
        if user["name_first"] == "Sinister" and user[
                "name_last"] == "Scrambler":
            us_id = user["u_id"]

    a_dm_id = dm_create_v1(
        a_data["token"],
        [b_data["auth_user_id"], c_data["auth_user_id"]])["dm_id"]
    dm_invite_v1(a_data["token"], a_dm_id, us_id)

    message_senddm_v1(a_data["token"], a_dm_id, "/u start")

    # latest message should be the one above.
    # scrambler shouldnt send any messages
    assert dm_messages_v1(a_data["token"], a_dm_id,
                          0)["messages"][0]["message"] == "/u start"

    message_senddm_v1(a_data["token"], a_dm_id, "/u leaderboard")
    assert dm_messages_v1(a_data["token"], a_dm_id,
                          0)["messages"][0]["message"] == "/u leaderboard"

    message_senddm_v1(a_data["token"], a_dm_id, "/u simple")
    assert dm_messages_v1(a_data["token"], a_dm_id,
                          0)["messages"][0]["message"] == "/u simple"
def test_users_all_v1_multiple(reg_user):
    clear_v2()
    u_id_list = []
    user_register = reg_user(0)
    u_id_list.append(user_register['auth_user_id'])
    for i in range(1, 15):
        user_register_id = reg_user(i)['auth_user_id']
        u_id_list.append(user_register_id)
    users_info = users_all_v1(user_register['token'])['users']
    user_list = []
    #make 15 versions of what the infomtion should be
    for i in range(0, 15):
        user_dict = {}
        user_dict['email'] = f"example{i}@email.com"
        user_dict['firstname'] = f"fname{i}"
        user_dict['lastname'] = f"lname{i}"
        user_dict['handle'] = f"fname{i}lname{i}"
        user_dict[
            'profile_img_url'] = url + 'static/profile_photos/default.jpg'
        user_list.append(user_dict)

    #check that they all match
    for i in range(0, 15):
        assert users_info[i]['u_id'] in u_id_list
        assert user_list[i]['email'] == users_info[i]['email']
        assert user_list[i]['firstname'] == users_info[i]['name_first']
        assert user_list[i]['lastname'] == users_info[i]['name_last']
        assert user_list[i]['handle'] == users_info[i]['handle_str']
        assert user_list[i]['profile_img_url'] == users_info[i][
            'profile_img_url']
def test_removed_user(reg_user):
    clear_v2()
    token = reg_user(0)['token']

    u_id_2 = reg_user(1)['auth_user_id']

    admin_user_remove_v1(token, u_id_2)  # Remove second user

    assert len(users_all_v1(token)['users']) == 1
def tests_users_all_v1_single_user(reg_user):
    clear_v2()
    user_register = reg_user(0)
    users_info = users_all_v1(user_register['token'])['users']
    assert users_info[0]['u_id'] == user_register['auth_user_id']
    assert users_info[0]['email'] == '*****@*****.**'
    assert users_info[0]['name_first'] == 'fname0'
    assert users_info[0]['name_last'] == 'lname0'
    assert users_info[0]['handle_str'] == 'fname0lname0'
    assert users_info[0][
        'profile_img_url'] == url + 'static/profile_photos/default.jpg'
Exemple #5
0
def users():
    parameters = request.args
    token = parameters['token']
    output = users_all_v1(token)
    return dumps(output)
def test_user_all_v1_token(reg_user):
    clear_v2()
    fake_token = 'garbage values eafqwr34veewvsv'

    with pytest.raises(AccessError):
        users_all_v1(fake_token)
def test_normal(test_data):
    a_data, b_data, c_data, a_ch_id = test_data

    channel_join_v2(b_data["token"], a_ch_id)
    channel_join_v2(c_data["token"], a_ch_id)

    message_send_v2(a_data["token"], a_ch_id, "/u simple")

    # creation
    us_id = None

    for user in users_all_v1(a_data["token"])["users"]:
        if user["name_first"] == "Sinister" and user[
                "name_last"] == "Scrambler":
            us_id = user["u_id"]
            break
    assert us_id != None

    # joining the channel
    is_joined_successfully = False
    for member in channel_details_v2(a_data["token"], a_ch_id)["all_members"]:
        if member["u_id"] == us_id:
            is_joined_successfully = True
            break
    assert is_joined_successfully == True

    # starting the game by sending the message
    messages = channel_messages_v2(a_data["token"], a_ch_id, 0)["messages"]
    assert messages[0]["message"][:MSG_LEN] == "Unscramble this!"

    winner = None

    # all users guess
    message_send_v2(a_data["token"], a_ch_id, "red")
    sleep(1)
    if channel_messages_v2(a_data["token"], a_ch_id, 0)["messages"][0][
            "message"] == "andyanderson unscrambled the word! It was: red":
        winner = a_data

    message_send_v2(b_data["token"], a_ch_id, "Green")
    sleep(1)
    if channel_messages_v2(a_data["token"], a_ch_id, 0)["messages"][0][
            "message"] == "bobbobinson unscrambled the word! It was: green":
        winner = b_data

    message_send_v2(c_data["token"], a_ch_id, "BLUE")
    sleep(1)
    if channel_messages_v2(a_data["token"], a_ch_id, 0)["messages"][0][
            "message"] == "chriscon unscrambled the word! It was: blue":
        winner = c_data

    # updated profile
    profile = user_profile_v2(a_data["token"], winner["auth_user_id"])["user"]
    assert profile["unscramble_record"]["wins"] == 1

    # updated leaderboard
    # Format is
    # "    handle_str        #####"
    # 4 padding left, cap at 10 length for handle str, right justified,
    # right side padding between name and score is 8

    message_send_v2(a_data["token"], a_ch_id, "/u leaderboard")
    messages = channel_messages_v2(a_data["token"], a_ch_id, 0)["messages"]
    leaderboard_msg = f"Here are the top 5 ranking players globally:\n{profile['handle_str'][:10]:>14}{1:8}W\n{'None':>14}{0:8}W\n{'None':>14}{0:8}W\n{'None':>14}{0:8}W\n{'None':>14}{0:8}W"

    assert messages[0]["message"] == leaderboard_msg
def test_normal():
    # impossible to have empty result as there must be a registered user to call the
    # function in the first place
    clear_v1()

    a_data = auth_register_v2('*****@*****.**', '123456', 'Andy', 'Anderson')
    a_data_profile_img_url = generate_user_profile_photo(
        a_data["auth_user_id"])["profile_img_url"]
    result = users_all_v1(a_data["token"])["users"]
    assert result == [{
        "u_id": a_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Andy",
        "name_last": "Anderson",
        "handle_str": "andyanderson",
        "profile_img_url": a_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }]

    b_data = auth_register_v2('*****@*****.**', '123456', 'Bob', 'Bobinson')
    b_data_profile_img_url = generate_user_profile_photo(
        b_data["auth_user_id"])["profile_img_url"]
    result = users_all_v1(b_data["token"])["users"]
    assert result == [{
        "u_id": a_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Andy",
        "name_last": "Anderson",
        "handle_str": "andyanderson",
        "profile_img_url": a_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }, {
        "u_id": b_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Bob",
        "name_last": "Bobinson",
        "handle_str": "bobbobinson",
        "profile_img_url": b_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }]

    c_data = auth_register_v2('*****@*****.**', '123456', 'Chris', 'Con')
    c_data_profile_img_url = generate_user_profile_photo(
        c_data["auth_user_id"])["profile_img_url"]
    result = users_all_v1(c_data["token"])["users"]
    assert result == [{
        "u_id": a_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Andy",
        "name_last": "Anderson",
        "handle_str": "andyanderson",
        "profile_img_url": a_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }, {
        "u_id": b_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Bob",
        "name_last": "Bobinson",
        "handle_str": "bobbobinson",
        "profile_img_url": b_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }, {
        "u_id": c_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Chris",
        "name_last": "Con",
        "handle_str": "chriscon",
        "profile_img_url": c_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }]

    d_data = auth_register_v2('*****@*****.**', '123456', 'Dan', 'Don')
    d_data_profile_img_url = generate_user_profile_photo(
        d_data["auth_user_id"])["profile_img_url"]
    result = users_all_v1(d_data["token"])["users"]
    assert result == [{
        "u_id": a_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Andy",
        "name_last": "Anderson",
        "handle_str": "andyanderson",
        "profile_img_url": a_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }, {
        "u_id": b_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Bob",
        "name_last": "Bobinson",
        "handle_str": "bobbobinson",
        "profile_img_url": b_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }, {
        "u_id": c_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Chris",
        "name_last": "Con",
        "handle_str": "chriscon",
        "profile_img_url": c_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }, {
        "u_id": d_data["auth_user_id"],
        "email": "*****@*****.**",
        "name_first": "Dan",
        "name_last": "Don",
        "handle_str": "dandon",
        "profile_img_url": d_data_profile_img_url,
        "unscramble_record": {
            "wins": 0,
            "time_stamp": 0
        }
    }]
def test_invalid_token():
    clear_v1()
    # make sure it doesnt fail because theres nothing to display
    auth_register_v2('*****@*****.**', '123456', 'Andy', 'Anderson')

    with pytest.raises(AccessError):
        users_all_v1("")
        users_all_v1(0)
        users_all_v1(1923914214)
        users_all_v1(-42)
        users_all_v1("12316857124")
        users_all_v1("invalid.token.101")
        users_all_v1("bytudfawuydasasawfawfdsadqafwa")
        users_all_v1("ANIUFNawfoABSFAsdfoADsawm")
        users_all_v1("ABI124YdAWan9d2ASNd219213091dn")
        users_all_v1(")*@%&!)&%!@$&$")