master
1require "rails_helper"
2
3describe UserSession do
4 subject { build(:user_session) }
5
6 describe "#revoke!" do
7 it "records the time the session was revoked" do
8 subject.revoke!
9 expect(subject.revoked_at).to_not be_nil
10 end
11 end
12
13 describe "#access" do
14 let(:request) { double(ip: "192.168.1.1", user_agent: "blah") }
15 let(:location) { build(:location) }
16 let(:because) { subject.access(request) }
17
18 before :each do
19 allow(Location).to receive(:build_from_ip).with("192.168.1.1").and_return(location)
20 because
21 end
22
23 it { expect(subject.accessed_at).to_not be_nil }
24 it { expect(subject.ip).to eql(request.ip) }
25 it { expect(subject.user_agent).to eql(request.user_agent) }
26 it { expect(subject.location).to_not be_nil }
27 it { expect(because).to eql(subject.id) }
28 it { expect(subject).to be_persisted }
29 end
30
31 describe ".active" do
32 let!(:active_session) { create(:user_session, accessed_at: Time.now) }
33 let!(:expired_session) { create(:user_session, accessed_at: 15.days.ago) }
34 let!(:revoked_session) { create(:user_session, accessed_at: 1.day.ago, revoked_at: Time.now) }
35
36 it "returns active sessions" do
37 expect(UserSession.active).to include(active_session)
38 end
39
40 it "excludes expired sessions" do
41 expect(UserSession.active).to_not include(expired_session)
42 end
43
44 it "excludes revoked sessions" do
45 expect(UserSession.active).to_not include(revoked_session)
46 end
47 end
48
49 describe ".authenticate" do
50 let!(:active_session) { create(:user_session, accessed_at: Time.now) }
51
52 it "returns the session if the id is active" do
53 expect(UserSession.authenticate(active_session.id)).to eql(active_session)
54 end
55
56 it "returns nil if the id is not active" do
57 expect(UserSession.authenticate("blah")).to be_nil
58 end
59 end
60end