Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Social Features in Ruby on Rails Building the Friendship UI Integration Testing Friendships

Lisa Rossiter
Lisa Rossiter
3,630 Points

Getting 302 even though I have added an encrypted password

I cant get rid of this error any help is much appreciated.

Finished tests in 0.227997s, 4.3860 tests/s, 4.3860 assertions/s.

  1) Failure:
AddAFriendTest#test_that_adding_a_friend_works [test/integration/add_a_friend_test.rb:11]:
Expected response to be a <success>, but was <302>

1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
admins-imac:ujunto-app admin$ 

I feel I may have a typo in my user_friendhips_controller but I am not sure.

class UserFriendshipsController < ApplicationController
  before_filter :authenticate_user!, only: [:new]

  def new
    if params[:friend_id]
      @friend = User.where(profile_name: params[:friend_id]).first
      raise ActiveRecord::RecordNotFound if @friend.nil?
      @user_friendship = current_user.user_friendships.new(friend: @friend)
    else
      flash[:error] = "Friend required"
    end
    rescue ActiveRecord::RecordNotFound
      render file: 'public/404', status: :not_found
  end

  def create
    if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
      @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
      @user_friendship = current_user.user_friendships.new(friend: @friend)
      @user_friendship.save
      flash[:success] = "You are now friends with #{@friend.full_name}"
      redirect_to profile_path(@friend)
    else
        flash[:error] = "Friend required"
        redirect_to root_path
    end
  end
end
Stone Preston
Stone Preston
42,016 Points

can you post the assertion thats on line 11 of your test file as well?

Lisa Rossiter
Lisa Rossiter
3,630 Points

Yes sure here is the whole thing.

require 'test_helper'

class AddAFriendTest < ActionDispatch::IntegrationTest
  def sign_in_as(user, password)
    post login_path, user: { email: user.email, password: password}
  end
  test "that adding a friend works" do
    sign_in_as users(:sam), "testing"

    get "/user_friendships/new?friend_id=#{users(:bob).profile_name}"
    assert_response :success # line 11

    assert_difference 'UserFriendship.count' do
      post "/user_friendships", user_friendship: { friend_id: users(:bob).profile_name }
      assert_response :redirect
      assert_equal "You are now friends with #{users(:bob).full_name}", flash[:success]
    end
  end
end

4 Answers

Lisa Rossiter
Lisa Rossiter
3,630 Points

Fixed it I had an error in my routes.rb, Woohoo!

Im stuck with the exact same error and can't figure it out, do you by chance remember what the problem in your routes was??

EDIT: so i was able to fix it, by changing the hash i had for my encrypted password.

from: encrypted_password: "$2a$10$m9.1lruzmkyIDxMlQx4mreYxJ2.NtF3fgmYUHBdti/sBl14ohbc2a" to encrypted_password: "$2a$10$Fh/Hm8RxDsuPTeBUr6864.GARlcmt7zF2WspaXGr3BIv2C27N.osq"

which makes no sense to me, as the first one was the actual encryption that i took from the database of my real account with the same password, while the other is just one i found from someone else's git of the same project. WHY????

Also all my tests are passing but i still can't get a successful add friend form

any thoughts Jason Seifer? Im using rails 4

EDIT 2: my git url is https://github.com/calderJamNet/JamNet-2, please someone help me figure this out i really need to get this working before i move on to the next stage

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Lisa Rossiter let's try and see what it really happening. Where you have assert_response :success on line 11, try moving that down a line and then do puts response.body to see where the test is going. You can also check the log/test.log file to see what it's doing for that particular part of the test.

Lisa Rossiter
Lisa Rossiter
3,630 Points

Ok did all of this and here is my results.

puts response.body

# Running tests:

<html><body>You are being <a href="http://www.example.com/login">redirected</a>.</body></html>
F

Log files

-----------------------------------------------
AddAFriendTest: test_that_adding_a_friend_works
-----------------------------------------------
  [1m[35mUser Load (0.3ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 638797851]]
Started POST "/login" for 127.0.0.1 at 2014-05-09 19:47:27 +0100
Processing by Devise::SessionsController#new as HTML
  Parameters: {"user"=>{"email"=>"modernoozedesign@gmail.com", "password"=>"[FILTERED]"}}
  Rendered devise/shared/_links.erb (1.6ms)
  Rendered devise/sessions/new.html.erb within layouts/application (77.5ms)
Completed 200 OK in 149ms (Views: 101.6ms | ActiveRecord: 0.0ms)
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m  [["id", 902541635]]
Started GET "/user_friendships/new?friend_id=bobb" for 127.0.0.1 at 2014-05-09 19:47:27 +0100
Processing by UserFriendshipsController#new as HTML
  Parameters: {"friend_id"=>"bobb"}
Completed 401 Unauthorized in 1ms
  [1m[35m (0.1ms)[0m  rollback transaction
Lisa Rossiter
Lisa Rossiter
3,630 Points

Also as another note I am not able to login on my real app.

Any help would be appreciated

Lisa Rossiter
Lisa Rossiter
3,630 Points

I still need an answer as I am working on it tonight. I have tried everything I know of.