summaryrefslogtreecommitdiff
path: root/spec/models/session_spec.rb
blob: 6c20b1cef356d0813f1a41c6454a9393bfc8b8bf (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
require "rails_helper"

describe Session do
  context "#save" do
    it "creates a new session" do
      Session.create!(user_id: 1, ip_address: '127.0.0.1')
      session = Session.last
      expect(session.user_id).to eql(1)
      expect(session.ip_address).to eql("127.0.0.1")
    end
  end

  context ".authenticate" do
    let(:user_session) { create(:session) }

    context "when the session key is legit" do
      it 'returns the session' do
        expect(Session.authenticate!(user_session.id)).to eql(user_session)
      end
    end

    context "when the session key is incorrect" do
      it 'raises an error' do
        expect(-> { Session.authenticate!('blah') }).to raise_error(ActiveRecord::RecordNotFound)
      end
    end

    context "when the session key is revoked" do
      let(:revoked_session) { create(:session, revoked_at: Time.now) }

      it 'raises an error' do
        expect(-> { Session.authenticate(revoked_session.id) }).to raise_error
      end
    end
  end

  context "#revoke!" do
    subject { create(:user_session) }

    it 'marks the time the session was revoked' do
      subject.revoke!
      expect(subject.revoked_at).to_not be_nil
    end
  end
end