Beispiel #1
0
def account():
    username_form = UpdateUsernameForm()
    password_form = UpdatePasswordForm()
    profile_pic_form = UpdateProfilePicForm()

    if password_form.validate_on_submit():
        hashed = bcrypt.generate_password_hash(
            password_form.new_password.data).decode("utf-8")

        msg = Message('Password Change',
                      sender='*****@*****.**',
                      recipients=[str(temp.email)])
        msg.body = "Your password has been updated! Please reply to this e-mail if you did not request this change."
        mail.send(msg)

        current_user.modify(password=hashed)
        current_user.save()

        return redirect(url_for('users.account'))

    if username_form.validate_on_submit():
        temp = User.objects(username=current_user.username).first()
        current_user.username = username_form.username.data

        msg = Message('Username Change',
                      sender='*****@*****.**',
                      recipients=[str(temp.email)])
        msg.body = "Your username has been updated!\nYour new username is: " + str(
            username_form.username.data)
        mail.send(msg)

        current_user.modify(username=username_form.username.data)
        current_user.save()

        return redirect(url_for('users.account'))

    if profile_pic_form.validate_on_submit():
        img = profile_pic_form.propic.data
        filename = secure_filename(img.filename)

        if current_user.profile_pic.get() is None:
            current_user.profile_pic.put(img.stream, content_type='images/png')
        else:
            current_user.profile_pic.replace(img.stream,
                                             content_type='images/png')
        current_user.save()

        return redirect(url_for('users.account'))

    image = images(current_user.username)

    return render_template("account.html",
                           title="Account",
                           username_form=username_form,
                           password_form=password_form,
                           profile_pic_form=profile_pic_form,
                           image=image)
Beispiel #2
0
def test_change_username_input_validation(client, auth, new_username):
    '''
    Test that if we pass in an empty string, we get the error "This field is required."
    Test that if we pass in a string that's too long, we get the error "Field must be 
    between 1 and 40 characters long."
    '''
    # GET /login PAGE, REGISTER, LOGIN, SET SESSION 
    resp = client.get("/login")
    assert resp.status_code == 200
    auth.register()
    response = auth.login()
    with client:
        client.get("/")
        assert session["_user_id"] == "test"
    # FORM FOR CHANGING USERNAME
    change_name = SimpleNamespace(username=new_username, submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=change_name)
    response = client.post("/account", data=form.data, follow_redirects=True)
    # USERNAME TOO SHORT.
    if new_username == "":
        assert b"This field is required" in response.data
    # USERNAME TOO LONG
    if len(new_username) > 40:
        assert b"Field must be between 1 and 40 characters long." in response.data
###########################################################################
###########################################################################
Beispiel #3
0
def test_change_username_taken(client, auth):
    '''
    Test that if we try to change the username to a different user's
    username, then we get the error message "That username is already taken"
    '''
    resp = client.get("/login")
    assert resp.status_code == 200
    # REGISTER 1ST USER.
    register_resp = auth.register(
        username="******", email="*****@*****.**", passwrd="password", confirm="password"
    )
    # REGISTER 2ND USER.
    register_resp = auth.register(
        username="******", email="*****@*****.**", passwrd="password", confirm="password"
    )
    # LOG IN 2ND USER AND CHECK THAT LOGIN IS SUCCESSFUL.
    login_resp = auth.login(username="******", password="******")
    assert login_resp.status_code == 200
    resp = client.get("/account")
    assert resp.status_code == 200
    # CHECK THAT SESSION ID IS SET TO test2.
    with client:
        client.get("/")
        assert session["_user_id"] == "test2"
    # FILL IN FORM FOR USERNAME CHANGE ... IT SHOULD GIVE AN ERROR NOTIFICATION.
    change_name = SimpleNamespace(username="******", submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=change_name)
    response = client.post("/account", data=form.data, follow_redirects=True)
    assert b"That username is already taken" in response.data
Beispiel #4
0
def account():
    username_form = UpdateUsernameForm()

    if username_form.validate_on_submit():
        # current_user.username = username_form.username.data
        mongo_lock.acquire()
        current_user.modify(username=username_form.username.data)
        current_user.save()
        mongo_lock.release()
        return redirect(url_for('users.account'))

    mongo_lock.acquire()
    user = User.objects(username=current_user.username).first()
    mongo_lock.release()

    return render_template("account.html",
                           title="Account",
                           username_form=username_form,
                           user=user)
Beispiel #5
0
def test_change_username_input_validation(client, auth, new_username, message):
    auth.register()
    auth.login()
    resp = client.get("/account")
    assert resp.status_code == 200

    update = SimpleNamespace(username=new_username, submit="Update Username")
    form = UpdateUsernameForm(formdate=None, obj=update)
    response = client.post("/account", data=form.data, follow_redirects=True)

    assert message in response.data
def account():
    username_form = UpdateUsernameForm()
    profile_pic_form = UpdateProfilePicForm()

    if username_form.validate_on_submit():
        # current_user.username = username_form.username.data

        temp = User.objects(username=current_user.username).first()

        msg = Message('Username Change',
                      sender='*****@*****.**',
                      recipients=[str(temp.email)])
        msg.body = "Your username has been updated!\nYour new username is: " + str(
            username_form.username.data)
        mail.send(msg)

        current_user.modify(username=username_form.username.data)
        current_user.save()

        return redirect(url_for('account'))

    if profile_pic_form.validate_on_submit():
        img = profile_pic_form.propic.data
        filename = secure_filename(img.filename)

        if current_user.profile_pic.get() is None:
            current_user.profile_pic.put(img.stream, content_type='images/png')
        else:
            current_user.profile_pic.replace(img.stream,
                                             content_type='images/png')
        current_user.save()

        return redirect(url_for('account'))

    image = images(current_user.username)

    return render_template("account.html",
                           title="Account",
                           username_form=username_form,
                           profile_pic_form=profile_pic_form,
                           image=image)
Beispiel #7
0
def test_change_username_input_validation(client, auth, new_username):
    resp = client.get("/login")
    assert resp.status_code == 200

    auth.register()
    response = auth.login()
    user = SimpleNamespace(username="******", submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=user)
    response = client.post("/", data=form.data, follow_redirects=True)
    if new_username == "":
        assert b"This field is required." in response.data
    elif new_username == "p" * 41:
        assert b"Field must be between 1 and 40 characters long." in response.data
Beispiel #8
0
def test_change_username_taken(client, auth):
    resp = client.get("/login")
    assert resp.status_code == 200

    auth.register()
    response = auth.login()

    resp = client.get("/account")
    user = SimpleNamespace(username="******", submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=user)
    response = client.post("/", data=form.data, follow_redirects=True)
    assert b"That username is already taken" in response.data
    client.get("/")
    assert session["_user_id"] == "test"
Beispiel #9
0
def test_change_username(client, auth):
    auth.register()
    auth.login()
    resp = client.get("/account")
    assert resp.status_code == 200

    update = SimpleNamespace(username="******", submit="Update Username")
    form = UpdateUsernameForm(formdate=None, obj=update)
    response = client.post("/account", data=form.data, follow_redirects=True)

    auth.login(username="******", password="******")

    resp = client.get("/account")
    assert b"update" in resp.data
def test_change_username_input_validation(client, auth, new_username):
    auth.register()
    auth.login()

    resp = client.get("/account")
    assert resp.status_code == 200

    new_username_namespace = SimpleNamespace(username=new_username,
                                             submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=new_username_namespace)
    response = client.post("/account", data=form.data, follow_redirects=True)

    if len(new_username) == 0:
        assert b"This field is required" in response.data
    else:
        assert b"Field must be between 1 and 40 characters long" in response.data
Beispiel #11
0
def test_change_username_taken(client, auth):
    auth.register()
    auth.register(username="******", email="*****@*****.**")

    auth.login()

    with client:
        client.get("/")
        assert session["_user_id"] == "test"
        update = SimpleNamespace(username="******", submit="Update Username")
        form = UpdateUsernameForm(formdate=None, obj=update)
        response = client.post("/account",
                               data=form.data,
                               follow_redirects=True)

        assert b"That username is already taken" in response.data
def test_change_username_taken(client, auth):
    auth.register(username="******",
                  email="*****@*****.**",
                  passwrd="goteem",
                  confirm="goteem")

    auth.register()
    auth.login()

    resp = client.get("/account")
    assert resp.status_code == 200

    new_username = SimpleNamespace(username="******",
                                   submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=new_username)
    response = client.post("/account", data=form.data, follow_redirects=True)

    assert b"That username is already taken" in response.data
Beispiel #13
0
def test_change_username(client, auth):
    '''
    Test that the account page loads successfully and that you can 
    successfully change the username of the logged-in user.
    Test that the new username shows up on the account page
    Test that the new username change is reflected in the database
    '''
    resp = client.get("/login")
    assert resp.status_code == 200
    old_username, new_username = "******", "I CHANGED"
    # REGISTER A NEW USER
    register_resp = auth.register(
        username=old_username, email="*****@*****.**", passwrd="password", confirm="password"
    )
    # LOG IN BRAND NEW USER AND CHECK THAT LOGIN IS SUCCESSFUL.
    login_resp = auth.login(username=old_username, password="******")
    assert login_resp.status_code == 200
    resp = client.get("/account")
    assert resp.status_code == 200
    # CHECK THAT SESSION ID IS SET TO ORIGINAL USERNAME
    with client:
        client.get("/")
        assert session["_user_id"] == old_username
    # FILL IN FORM FOR USERNAME CHANGE
    change_name = SimpleNamespace(username=new_username, submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=change_name)
    response = client.post("/account", data=form.data, follow_redirects=True)
    # FILLING OUT FOR REIDIRECTS BACK TO LOG IN PAGE, SO LOGIN W/ NEW CREDENTIALS.
    login_resp = auth.login(username=new_username, password="******")
    # CHECK THAT NEW LOGIN WORKED
    assert login_resp.status_code == 200
    # CHECK THAT SESSION ID CHANGES TO NEW USERNAME AFTER LOGING IN.
    with client:
        client.get("/")
        assert session["_user_id"] == new_username
    # CHECK THAT NEW USERNAME APPEARS IN ACCOUNT PAGE HTML.
    resp = client.get("/account")
    assert resp.status_code == 200
    assert str.encode(new_username) in resp.data
    # FINALLY, CHECK FOR NEW USERNAME IN DB.
    new_username_check = User.objects(username=new_username).first().username
    assert new_username == new_username_check
def test_change_username(client, auth):
    auth.register()
    auth.login()

    resp = client.get("/account")
    assert resp.status_code == 200

    new_username = SimpleNamespace(username="******",
                                   submit="Update Username")
    form = UpdateUsernameForm(formdata=None, obj=new_username)
    response = client.post("/account", data=form.data, follow_redirects=True)

    auth.login(username="******")

    response = client.get("/account")

    assert b"peepeehands" in response.data

    users = User.objects(username="******")
    assert len(users) == 1