~ren/magentasso-rb

ref: 4751491c21c5d31117ad9da65ba9f2e9fc454290 magentasso-rb/spec/magentasso_response_spec.rb -rw-r--r-- 2.0 KiB
4751491c — Lauren Jenkinson Keep dependencies in gemspec in alphabetical order 1 year, 4 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
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
# frozen_string_literal: true

RSpec.describe MagentaSSO::Response do # rubocop:disable Metrics/BlockLength
  it "signs a response" do
    response = MagentaSSO::Response.new(
      "test",
      MagentaSSO::TEST_SECRET,
      123_456,
      { "external_id" => 1, "email" => "test@example.com" },
      { "profile" => { "name_combined" => "Test User" } }
    )

    _, signature = response.sign
    expect(signature).to eq "dfVZ0jF8tGlWc7umZVstuqb9Y731-NRyA3J2Ye_CqE8"
  end

  it "verifies a signed response" do
    payload =
      "eyJjbGllbnRfaWQiOiJ0ZXN0Iiwibm9uY2UiOjEyMzQ1NiwidXNlcl9kYXRhIjp7ImV4d" \
      "GVybmFsX2lkIjoxLCJlbWFpbCI6InRlc3RAZXhhbXBsZS5jb20ifSwic2NvcGVfZGF0YS" \
      "I6eyJwcm9maWxlIjp7Im5hbWVfY29tYmluZWQiOiJUZXN0IFVzZXIifX19"
    signature = "dfVZ0jF8tGlWc7umZVstuqb9Y731-NRyA3J2Ye_CqE8"

    response = MagentaSSO::Response.verify(
      payload,
      signature,
      MagentaSSO::TEST_SECRET
    )

    expect(response.nonce).to eq 123_456
  end

  it "returns the external ID" do
    response = MagentaSSO::Response.new(
      "test",
      MagentaSSO::TEST_SECRET,
      123_456,
      { "external_id" => 1, "email" => "test@example.com" },
      { "profile" => { "name_combined" => "Test User" } }
    )

    expect(response.external_id).to eq 1
  end

  it "returns the combined name" do
    response = MagentaSSO::Response.new(
      "test",
      MagentaSSO::TEST_SECRET,
      123_456,
      { "external_id" => 1, "email" => "test@example.com" },
      { "profile" => { "name_combined" => "Test User", "name_first" => "Test", "name_last" => "User" } }
    )

    expect(response.profile_name).to eq(["Test User", nil])
  end

  it "returns separated first and last names if no combined name exists" do
    response = MagentaSSO::Response.new(
      "test",
      MagentaSSO::TEST_SECRET,
      123_456,
      { "external_id" => 1, "email" => "test@example.com" },
      { "profile" => { "name_first" => "Test", "name_last" => "User" } }
    )

    expect(response.profile_name).to eq(%w[Test User])
  end
end