|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Layer::Api::IdentityToken do |
| 4 | + describe ".new" do |
| 5 | + it "should allow you to set the user_id, nonce and expires_at variables" do |
| 6 | + user_id = "1234" |
| 7 | + nonce = "your_random_nonce" |
| 8 | + expires_at = "12345678" |
| 9 | + |
| 10 | + token = Layer::Api::IdentityToken.new( |
| 11 | + user_id: user_id, |
| 12 | + nonce: nonce, |
| 13 | + expires_at: expires_at |
| 14 | + ) |
| 15 | + |
| 16 | + expect(token.user_id).to eq(user_id) |
| 17 | + expect(token.nonce).to eq(nonce) |
| 18 | + expect(token.expires_at).to eq(expires_at) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe ".layer_key_id" do |
| 23 | + it "should return your ENV['LAYER_KEY_ID']" do |
| 24 | + layer_key_id = Layer::Api::IdentityToken.new.layer_key_id |
| 25 | + expect(layer_key_id).to eq(ENV['LAYER_KEY_ID']) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + describe ".layer_provider_id" do |
| 30 | + it "should return your ENV['LAYER_PROVIDER_ID']" do |
| 31 | + provider_id = Layer::Api::IdentityToken.new.layer_provider_id |
| 32 | + expect(provider_id).to eq(ENV['LAYER_PROVIDER_ID']) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe ".headers" do |
| 37 | + it "should return necessary headers" do |
| 38 | + token = Layer::Api::IdentityToken.new |
| 39 | + |
| 40 | + headers = token.send(:headers) |
| 41 | + |
| 42 | + expect(headers[:kid]).to eq(ENV['LAYER_KEY_ID']) |
| 43 | + expect(headers[:cty]).to eq('layer-eit;v=1') |
| 44 | + expect(headers[:typ]).to eq('JWT') |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe ".claim" do |
| 49 | + it "should return necessary payload" do |
| 50 | + token = Layer::Api::IdentityToken.new( |
| 51 | + user_id: "user_id", |
| 52 | + nonce: "nonce", |
| 53 | + expires_at: 1234567 |
| 54 | + ) |
| 55 | + |
| 56 | + claim = token.send(:claim) |
| 57 | + |
| 58 | + expect(claim[:iss]).to eq(token.layer_provider_id) |
| 59 | + expect(claim[:prn]).to eq(token.user_id) |
| 60 | + expect(claim[:exp]).to eq(token.expires_at) |
| 61 | + expect(claim[:nce]).to eq(token.nonce) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + describe ".private_key" do |
| 66 | + it "should return valid rsa private key" do |
| 67 | + key = Layer::Api::IdentityToken.new.send(:private_key) |
| 68 | + expect(key).to be_instance_of(OpenSSL::PKey::RSA) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + describe ".to_s" do |
| 73 | + it "should return a string representation of the identity token" do |
| 74 | + token = Layer::Api::IdentityToken.new.to_s |
| 75 | + expect(token).to be_instance_of(String) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + describe ".generate_identity_token" do |
| 80 | + it "should return the correct IdentityToken" do |
| 81 | + options = {} |
| 82 | + options[:user_id] = "user_id" |
| 83 | + options[:nonce] = "user_id" |
| 84 | + layer = Layer::Api::Client.new |
| 85 | + expected_token = Layer::Api::IdentityToken.new(options).to_s |
| 86 | + actual_token = layer.generate_identity_token(options) |
| 87 | + |
| 88 | + expect(actual_token).to eq(expected_token) |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments