summaryrefslogtreecommitdiff
path: root/spec/models/user_spec.rb
blob: cd96defcf95d685d39539ac6f050614c78bff922 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require "rails_helper"

describe User do
  describe "#create" do
    it "saves a new user to the database" do
      user = create(:user)

      saved_user = User.find(user.id)
      expect(saved_user.username).to eql(user.username)
      expect(saved_user.email).to eql(user.email)
      expect(saved_user.password).to be_nil
    end

    it "lowercases the username" do
      user = create(:user, username: "UpCASE")
      expect(user.reload.username).to eql("upcase")
    end

    it "lowercases the email" do
      user = create(:user, email: FFaker::Internet.email.upcase)
      expect(user.reload.email).to eql(user.email.downcase)
    end
  end

  describe "validations" do
    context "username" do
      it "is invalid when the username is missing" do
        user = User.new(username: nil)
        expect(user).to_not be_valid
        expect(user.errors[:username]).to_not be_empty
      end

      it "is invalid if invalid characters are in the username" do
        user = User.new(username: "$()")
        expect(user).to_not be_valid
        expect(user.errors[:username]).to_not be_empty
      end

      it "is invalid if the username is already taken" do
        create(:user, username: "blah", email: "blah@example.com")
        user = build(:user, username: "blah", email: "blahblah@example.com")
        expect(user).to_not be_valid
        expect(user.errors[:username]).to_not be_empty
      end
    end

    describe "#email" do
      it "is invalid when the email is missing" do
        user = User.new(email: nil)
        expect(user).to_not be_valid
        expect(user.errors[:email]).to_not be_empty
      end

      it "is invalid when the email is not in the correct format" do
        user = User.new(email: "blah")
        expect(user).to_not be_valid
        expect(user.errors[:email]).to_not be_empty
      end

      it "is invalid if the email address is already registered" do
        create(:user, username: "blahblah", email: "blah@example.com")
        second_user = build(:user, username: "blah", email: "blah@example.com")
        expect(second_user).to_not be_valid
        expect(second_user.errors[:email]).to_not be_empty
      end
    end

    describe "terms_and_conditions" do
      it "is invalid if terms and conditions is unchecked" do
        user = User.new(terms_and_conditions: false)
        expect(user).to_not be_valid
        expect(user.errors[:terms_and_conditions]).to_not be_empty
      end
    end

    it "is valid when it is" do
      user = User.new(
        username: "coolio",
        email: "notblank@example.com",
        password: "legit",
        terms_and_conditions: "1")
      expect(user).to be_valid
    end
  end

  describe "USERNAME_REGEX" do
    it "does not match" do
      expect(User::USERNAME_REGEX).to_not match("$username")
      expect(User::USERNAME_REGEX).to_not match("!username")
      expect(User::USERNAME_REGEX).to_not match("@username")
    end

    it "matches" do
      expect(User::USERNAME_REGEX).to match("username")
      expect(User::USERNAME_REGEX).to match("user.name")
      expect(User::USERNAME_REGEX).to match("user_name")
      expect(User::USERNAME_REGEX).to match("user-name")
      expect(User::USERNAME_REGEX).to match("username1")
    end
  end


  describe "#to_param" do
    it "returns the username as the uniq identifier" do
      user = build(:user)
      expect(user.to_param).to eql(user.username)
    end
  end

  describe "#personal_record_for" do
    include_context "stronglifts_program"
    let(:user) { create(:user) }
    let(:recommendation) { routine_a.recommendations.find_by(exercise: squat) }

    before :each do
      workout = user.workouts.create!(
        routine: routine_a,
        occurred_at: DateTime.now.utc
      )
      workout.train(squat, 201, repetitions: recommendation.repetitions)
      workout.train(squat, 202, repetitions: recommendation.repetitions)
      workout.train(squat, 210, repetitions: recommendation.repetitions - 1)
      workout.train(squat, 204, repetitions: recommendation.repetitions)
      workout.train(squat, 205, repetitions: recommendation.repetitions)
    end

    it "returns the users maximum amount of weight lifted" do
      expect(user.personal_record_for(squat)).to eql(205.0)
    end
  end

  describe "#begin_workout" do
    subject { create(:user) }
    let(:routine) { create(:routine) }
    let(:today) { DateTime.now }

    it "creates a new training session" do
      result = subject.begin_workout(routine, today, 200)
      expect(result).to be_persisted
      expect(subject.workouts.count).to eql(1)
      expect(subject.workouts.first).to eql(result)
      expect(result.routine).to eql(routine)
      expect(result.occurred_at).to eql(today.utc)
      expect(result.body_weight).to eql(200.lbs)
    end

    it "returns the existing workout for that day" do
      result = subject.begin_workout(routine, today, 200)
      expect(subject.begin_workout(routine, today, 200)).to eql(result)
    end

    it "returns different sessions for different days" do
      todays_result = subject.begin_workout(routine, today, 200)
      tomorrows_result = subject.begin_workout(routine, DateTime.tomorrow, 200)
      expect(todays_result).to_not eql(tomorrows_result)
    end
  end

  describe "when destroying a training session" do
    include_context "stronglifts_program"
    subject { create(:user) }

    it "removes all the associations" do
      workout = subject.begin_workout(routine_a, Date.today, 200)
      workout.train(squat, 200, repetitions: 5)
      workout.train(squat, 200, repetitions: 5)
      workout.train(squat, 200, repetitions: 5)
      workout.train(squat, 200, repetitions: 5)
      workout.train(squat, 200, repetitions: 5)

      subject.workouts.destroy_all

      expect(Workout.all).to be_empty
      expect(ExerciseSet.all).to be_empty
    end
  end

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

    it "creates a new profile" do
      expect(user.profile).to be_present
      expect(user.profile.other?).to be_truthy
      expect(user.profile.social_tolerance).to be_nil
    end
  end

  describe "#login" do
    context "when credentials are correct" do
      it "returns true" do
        user = create(:user, password: "password", password_confirmation: "password")
        result = User.login(user.email.upcase, "password")
        expect(result).to be_instance_of(UserSession)
        expect(result.user).to eql(user)
      end

      it "is case in-sensitive for username" do
        user = create(:user, username: "upcase", password: "password", password_confirmation: "password")
        result = User.login("UPcase", "password")
        expect(result).to be_instance_of(UserSession)
        expect(result.user).to eql(user)
      end
    end

    context "when the email is not registered" do
      it { expect(User.login("sofake@noteven.com", "password")).to be_falsey }
    end

    context "when the username is not registered" do
      it { expect(User.login("sofake", "password")).to be_falsey }
    end
  end

  describe "#add_to_inbox" do
    include_context "stronglifts_program"
    subject { create(:user) }
    let(:email) { build(:email, :with_attachment) }

    it "records the email" do
      subject.add_to_inbox(email)
      expect(subject.received_emails.count).to eql(1)
      received_email = subject.received_emails.first
      expect(received_email.to.map(&:symbolize_keys)).to match_array(email.to)
      expect(received_email.from.symbolize_keys).to eql(email.from)
      expect(received_email.subject).to eql(email.subject)
      expect(received_email.body).to eql(email.body)
    end
  end

  describe "#next_workout_for" do
    subject { create(:user) }
    let(:routine) { create(:routine) }
    let(:body_weight) { rand(300) }

    it "includes the body weight from the previous workout" do
      create(:workout, user: subject, body_weight: body_weight)

      workout = subject.next_workout_for(routine)
      expect(workout.body_weight).to eql(body_weight)
    end

    it "uses the correct routine" do
      workout = subject.next_workout_for(routine)
      expect(workout.routine).to eql(routine)
    end

    it "prepares the correct number of sets" do
      squat = create(:squat)
      routine.add_exercise(squat, sets: 3)
      workout = subject.next_workout_for(routine)
      expect(workout.exercise_sets.length).to eql(3)
    end
  end

  describe "#next_routine" do
    include_context "stronglifts_program"
    subject { create(:user) }

    it "routines the next workout" do
      create(:workout, routine: routine_a, user: subject)
      expect(subject.next_routine).to eql(routine_b)
    end

    it "returns the first routine in the program" do
      expect(subject.next_routine).to eql(routine_a)
    end
  end

  describe "#last_workout" do
    include_context "stronglifts_program"
    subject { create(:user) }

    it "returns the last workout" do
      workout = create(:workout, user: subject, routine: routine_a)
      expect(subject.last_workout).to eql(workout)
    end

    it "returns the last workout that included a specific exercise" do
      deadlift_workout = create(:workout, user: subject, routine: routine_b)
      deadlift_workout.train(deadlift, 315.lbs, repetitions: 5)
      bench_workout = create(:workout, user: subject, routine: routine_a)
      bench_workout.train(bench_press, 195.lbs, repetitions: 5)

      expect(subject.last_workout(deadlift)).to eql(deadlift_workout)
    end

    it "returns nil when no workouts have been completed" do
      expect(subject.last_workout).to be_nil
    end

    it "returns nil when the exercise has not been performed" do
      bench_workout = create(:workout, user: subject, routine: routine_a)
      bench_workout.train(bench_press, 195.lbs, repetitions: 5)

      expect(subject.last_workout(deadlift)).to be_nil
    end
  end
end