summaryrefslogtreecommitdiff
path: root/spec/controllers/profiles_controller_spec.rb
blob: a5bb00abb8b4977f2c6b12307c58a4ead658faa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require "rails_helper"

describe ProfilesController do
  describe "authenticated" do
    include_context "stronglifts_program"
    let(:user) { create(:user) }

    before :each do
      http_login(user)
    end

    describe "#show" do
      let(:other_user) { create(:user) }

      it "loads the user's profile" do
        get :show, params: { id: user.to_param }
        expect(assigns(:user)).to eql(user)
        expect(assigns(:profile)).to eql(user.profile)
        expect(assigns(:program)).to eql(Program.stronglifts)
      end

      it "loads the other user's profile" do
        get :show, params: { id: other_user.to_param }
        expect(assigns(:user)).to eql(other_user)
        expect(assigns(:profile)).to eql(other_user.profile)
        expect(assigns(:program)).to eql(Program.stronglifts)
      end
    end

    describe "#edit" do
      let(:other_user) { create(:user) }

      it "loads the user's profile into an edit view" do
        get :edit, params: { id: user.to_param }
        expect(assigns(:profile)).to eql(user.profile)
        expect(assigns(:program)).to eql(Program.stronglifts)
      end

      it "will not load the other user's profile into an edit view" do
        get :edit, params: { id: other_user.to_param }
        expect(assigns(:profile)).to eql(user.profile)
        expect(assigns(:program)).to eql(Program.stronglifts)
      end
    end

    describe "#update" do
      it "updates the user profile" do
        patch :update, params: {
          id: user.to_param, profile: { gender: "male" }
        }
        user.reload
        expect(user.profile.male?).to be_truthy
        expect(response).to redirect_to(profile_path(user.profile))
      end

      it 'saves the users home gym' do
        gym = create(:gym)

        patch :update, params: {
          id: user.to_param, profile: { gym_id: gym.id }
        }

        expect(user.reload.profile.gym).to eql(gym)
      end
    end
  end

  describe "unauthenticated" do
    include_context "stronglifts_program"
    let(:user) { create(:user) }

    describe "#show" do
      it "loads the user's profile" do
        get :show, params: { id: user.to_param }
        expect(assigns(:user)).to be_nil
        expect(assigns(:program)).to be_nil
      end
    end

    describe "#edit" do
      it "loads the user's profile into an edit view" do
        get :edit, params: { id: user.to_param }
        expect(assigns(:user)).to be_nil
        expect(assigns(:program)).to be_nil
      end
    end
  end
end