master
 1require "rails_helper"
 2
 3describe ProfilesController do
 4  describe "authenticated" do
 5    include_context "stronglifts_program"
 6    let(:user) { create(:user) }
 7
 8    before :each do
 9      http_login(user)
10    end
11
12    describe "#show" do
13      let(:other_user) { create(:user) }
14
15      it "loads the user's profile" do
16        get :show, params: { id: user.to_param }
17        expect(assigns(:user)).to eql(user)
18        expect(assigns(:profile)).to eql(user.profile)
19        expect(assigns(:program)).to eql(Program.stronglifts)
20      end
21
22      it "loads the other user's profile" do
23        get :show, params: { id: other_user.to_param }
24        expect(assigns(:user)).to eql(other_user)
25        expect(assigns(:profile)).to eql(other_user.profile)
26        expect(assigns(:program)).to eql(Program.stronglifts)
27      end
28    end
29
30    describe "#edit" do
31      let(:other_user) { create(:user) }
32
33      it "loads the user's profile into an edit view" do
34        get :edit, params: { id: user.to_param }
35        expect(assigns(:profile)).to eql(user.profile)
36        expect(assigns(:program)).to eql(Program.stronglifts)
37      end
38
39      it "will not load the other user's profile into an edit view" do
40        get :edit, params: { id: other_user.to_param }
41        expect(assigns(:profile)).to eql(user.profile)
42        expect(assigns(:program)).to eql(Program.stronglifts)
43      end
44    end
45
46    describe "#update" do
47      it "updates the user profile" do
48        patch :update, params: {
49          id: user.to_param, profile: { gender: "male" }
50        }
51        user.reload
52        expect(user.profile.male?).to be_truthy
53        expect(response).to redirect_to(profile_path(user.profile))
54      end
55
56      it 'saves the users home gym' do
57        gym = create(:gym)
58
59        patch :update, params: {
60          id: user.to_param, profile: { gym_id: gym.id }
61        }
62
63        expect(user.reload.profile.gym).to eql(gym)
64      end
65    end
66  end
67
68  describe "unauthenticated" do
69    include_context "stronglifts_program"
70    let(:user) { create(:user) }
71
72    describe "#show" do
73      it "loads the user's profile" do
74        get :show, params: { id: user.to_param }
75        expect(assigns(:user)).to be_nil
76        expect(assigns(:program)).to be_nil
77      end
78    end
79
80    describe "#edit" do
81      it "loads the user's profile into an edit view" do
82        get :edit, params: { id: user.to_param }
83        expect(assigns(:user)).to be_nil
84        expect(assigns(:program)).to be_nil
85      end
86    end
87  end
88end