|
3 | 3 | RSpec.describe Saml::RequestStore do |
4 | 4 | let(:request_id) { "_abc-123" } |
5 | 5 | let(:org_slug) { "some-university" } |
| 6 | + let(:token) { described_class.create(request_id:, org_slug:) } |
6 | 7 |
|
7 | | - describe "create and claim" do |
8 | | - it "round trips the transaction" do |
9 | | - token = described_class.create(request_id:, org_slug:) |
| 8 | + describe "create" do |
| 9 | + it "issues a distinct expiring token that round trips the transaction" do |
10 | 10 | expect(token).to be_present |
| 11 | + expect(RedisPool.conn { |r| r.ttl("saml_request:#{token}") }) |
| 12 | + .to be_within(5).of(described_class::TTL.to_i) |
11 | 13 | expect(described_class.claim(token)).to eq({org_slug:, request_id:}) |
12 | | - end |
13 | | - |
14 | | - it "only claims once" do |
15 | | - token = described_class.create(request_id:, org_slug:) |
16 | | - described_class.claim(token) |
17 | | - expect(described_class.claim(token)).to be_nil |
18 | | - end |
19 | | - |
20 | | - it "issues a distinct token per request" do |
21 | | - tokens = Array.new(3) { described_class.create(request_id:, org_slug:) } |
22 | | - expect(tokens.uniq.count).to eq 3 |
23 | | - end |
24 | | - |
25 | | - it "expires" do |
26 | | - token = described_class.create(request_id:, org_slug:) |
27 | | - ttl = RedisPool.conn { |r| r.ttl("saml_request:#{token}") } |
28 | | - expect(ttl).to be_within(5).of(described_class::TTL.to_i) |
| 14 | + expect(described_class.create(request_id:, org_slug:)).to_not eq token |
29 | 15 | end |
30 | 16 | end |
31 | 17 |
|
32 | 18 | describe "claim" do |
33 | | - it "is nil for a blank token" do |
34 | | - expect(described_class.claim(nil)).to be_nil |
35 | | - expect(described_class.claim("")).to be_nil |
| 19 | + it "succeeds once, the token being single use" do |
| 20 | + expect(described_class.claim(token)).to eq({org_slug:, request_id:}) |
| 21 | + expect(described_class.claim(token)).to be_nil |
36 | 22 | end |
37 | 23 |
|
38 | | - it "is nil for an unknown token" do |
39 | | - expect(described_class.claim("not-a-real-token")).to be_nil |
| 24 | + context "a token we never issued" do |
| 25 | + it "is nil rather than raising" do |
| 26 | + expect(described_class.claim(nil)).to be_nil |
| 27 | + expect(described_class.claim("")).to be_nil |
| 28 | + expect(described_class.claim("not-a-real-token")).to be_nil |
| 29 | + end |
40 | 30 | end |
41 | 31 | end |
42 | 32 | end |
0 commit comments