~ren/magentasso-rb

ref: b4173bb058ac827bf15f2d71206b1d366c07b6e4 magentasso-rb/spec/magentasso_spec.rb -rw-r--r-- 992 bytes
b4173bb0 — Lauren Jenkinson Add "json" as an explicit dependency 1 year, 5 months ago
                                                                                
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
# frozen_string_literal: true

RSpec.describe MagentaSSO do
  it "has a version number" do
    expect(MagentaSSO::VERSION).not_to be nil
  end

  describe "#encode_and_sign" do
    it "encodes the payload JSON" do
      payload, = MagentaSSO.encode_and_sign({ "test" => true }, MagentaSSO::TEST_SECRET)
      decoded_payload = JSON.parse(Base64.urlsafe_decode64(payload))

      expect(decoded_payload).to eq({ "test" => true })
    end

    it "signs the payload" do
      _, signature = MagentaSSO.encode_and_sign({ "test" => true }, MagentaSSO::TEST_SECRET)
      expect(signature).to eq "AHlrP-r0AtZ_zK-uQTUqdBPH85q-Ezu3mDUdlSnndMQ"
    end
  end

  describe "#verify_and_decode" do
    it "verifies the signature and returns the decoded payload" do
      payload = MagentaSSO.verify_and_decode(
        "eyJ0ZXN0Ijp0cnVlfQ",
        "AHlrP-r0AtZ_zK-uQTUqdBPH85q-Ezu3mDUdlSnndMQ",
        MagentaSSO::TEST_SECRET
      )

      expect(payload).to eq({ "test" => true })
    end
  end
end