summaryrefslogtreecommitdiff
path: root/spec/lib/json_web_token
diff options
context:
space:
mode:
authorToon Claes <toon@gitlab.com>2019-02-28 19:57:34 +0100
committerToon Claes <toon@gitlab.com>2019-02-28 19:57:34 +0100
commit62d7990b9bb30cf33ed87017c5c633d1cccc75c2 (patch)
treec3e1b69c58a412ba1c6f50a0337a23d9f9d6e1a4 /spec/lib/json_web_token
parentf6453eca992a9c142268e78ac782cef98110d183 (diff)
downloadgitlab-ce-tc-standard-gem.tar.gz
Ran standardrb --fix on the whole codebasetc-standard-gem
Inspired by https://twitter.com/searls/status/1101137953743613952 I decided to try https://github.com/testdouble/standard on our codebase. It's opinionated, but at least it's a _standard_.
Diffstat (limited to 'spec/lib/json_web_token')
-rw-r--r--spec/lib/json_web_token/hmac_token_spec.rb80
-rw-r--r--spec/lib/json_web_token/rsa_token_spec.rb17
-rw-r--r--spec/lib/json_web_token/token_spec.rb6
3 files changed, 52 insertions, 51 deletions
diff --git a/spec/lib/json_web_token/hmac_token_spec.rb b/spec/lib/json_web_token/hmac_token_spec.rb
index f2cbc381967..19fce33ca30 100644
--- a/spec/lib/json_web_token/hmac_token_spec.rb
+++ b/spec/lib/json_web_token/hmac_token_spec.rb
@@ -1,59 +1,59 @@
# frozen_string_literal: true
-require 'json'
-require 'timecop'
+require "json"
+require "timecop"
describe JSONWebToken::HMACToken do
- let(:secret) { 'shh secret squirrel' }
+ let(:secret) { "shh secret squirrel" }
- shared_examples 'a valid, non-expired token' do
- it 'is an Array with two elements' do
+ shared_examples "a valid, non-expired token" do
+ it "is an Array with two elements" do
expect(decoded_token).to be_a(Array)
expect(decoded_token.count).to eq(2)
end
- it 'contains the following keys in the first Array element Hash - jti, iat, nbf, exp' do
- expect(decoded_token[0].keys).to include('jti', 'iat', 'nbf', 'exp')
+ it "contains the following keys in the first Array element Hash - jti, iat, nbf, exp" do
+ expect(decoded_token[0].keys).to include("jti", "iat", "nbf", "exp")
end
- it 'contains the following keys in the second Array element Hash - typ and alg' do
- expect(decoded_token[1]['typ']).to eql('JWT')
- expect(decoded_token[1]['alg']).to eql('HS256')
+ it "contains the following keys in the second Array element Hash - typ and alg" do
+ expect(decoded_token[1]["typ"]).to eql("JWT")
+ expect(decoded_token[1]["alg"]).to eql("HS256")
end
end
- describe '.decode' do
+ describe ".decode" do
let(:leeway) { described_class::IAT_LEEWAY }
let(:decoded_token) { described_class.decode(encoded_token, secret, leeway: leeway) }
- context 'with an invalid token' do
- context 'that is junk' do
- let(:encoded_token) { 'junk' }
+ context "with an invalid token" do
+ context "that is junk" do
+ let(:encoded_token) { "junk" }
it "raises exception saying 'Not enough or too many segments'" do
- expect { decoded_token }.to raise_error(JWT::DecodeError, 'Not enough or too many segments')
+ expect { decoded_token }.to raise_error(JWT::DecodeError, "Not enough or too many segments")
end
end
- context 'that has been fiddled with' do
+ context "that has been fiddled with" do
let(:encoded_token) do
- described_class.new(secret).encoded.tap { |token| token[0] = 'E' }
+ described_class.new(secret).encoded.tap { |token| token[0] = "E" }
end
it "raises exception saying 'Invalid segment encoding'" do
- expect { decoded_token }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
+ expect { decoded_token }.to raise_error(JWT::DecodeError, "Invalid segment encoding")
end
end
- context 'that was generated using a different secret' do
- let(:encoded_token) { described_class.new('some other secret').encoded }
+ context "that was generated using a different secret" do
+ let(:encoded_token) { described_class.new("some other secret").encoded }
it "raises exception saying 'Signature verification raised" do
- expect { decoded_token }.to raise_error(JWT::VerificationError, 'Signature verification raised')
+ expect { decoded_token }.to raise_error(JWT::VerificationError, "Signature verification raised")
end
end
- context 'that is expired' do
+ context "that is expired" do
# Needs the ! so Timecop.freeze() is effective
let!(:encoded_token) { described_class.new(secret).encoded }
@@ -61,72 +61,72 @@ describe JSONWebToken::HMACToken do
# Needs to be 120 seconds, because the default expiry is 60 seconds
# with an additional 60 second leeway.
Timecop.freeze(Time.now + 120) do
- expect { decoded_token }.to raise_error(JWT::ExpiredSignature, 'Signature has expired')
+ expect { decoded_token }.to raise_error(JWT::ExpiredSignature, "Signature has expired")
end
end
end
end
- context 'with a valid token' do
+ context "with a valid token" do
let(:encoded_token) do
hmac_token = described_class.new(secret)
hmac_token.expire_time = Time.now + expire_time
hmac_token.encoded
end
- context 'that has expired' do
+ context "that has expired" do
let(:expire_time) { 0 }
- context 'with the default leeway' do
+ context "with the default leeway" do
Timecop.freeze(Time.now + 1) do
- it_behaves_like 'a valid, non-expired token'
+ it_behaves_like "a valid, non-expired token"
end
end
- context 'with a leeway of 0 seconds' do
+ context "with a leeway of 0 seconds" do
let(:leeway) { 0 }
it "raises exception saying 'Signature has expired'" do
Timecop.freeze(Time.now + 1) do
- expect { decoded_token }.to raise_error(JWT::ExpiredSignature, 'Signature has expired')
+ expect { decoded_token }.to raise_error(JWT::ExpiredSignature, "Signature has expired")
end
end
end
end
- context 'that has not expired' do
+ context "that has not expired" do
let(:expire_time) { described_class::DEFAULT_EXPIRE_TIME }
- it_behaves_like 'a valid, non-expired token'
+ it_behaves_like "a valid, non-expired token"
end
end
end
- describe '#encoded' do
+ describe "#encoded" do
let(:decoded_token) { described_class.decode(encoded_token, secret) }
- context 'without data' do
+ context "without data" do
let(:encoded_token) { described_class.new(secret).encoded }
- it_behaves_like 'a valid, non-expired token'
+ it_behaves_like "a valid, non-expired token"
end
- context 'with data' do
- let(:data) { { secret_key: 'secret value' }.to_json }
+ context "with data" do
+ let(:data) { {secret_key: "secret value"}.to_json }
let(:encoded_token) do
ec = described_class.new(secret)
ec[:data] = data
ec.encoded
end
- it_behaves_like 'a valid, non-expired token'
+ it_behaves_like "a valid, non-expired token"
it "contains the 'data' key in the first Array element Hash" do
- expect(decoded_token[0]).to have_key('data')
+ expect(decoded_token[0]).to have_key("data")
end
- it 'can re-read back the data' do
- expect(decoded_token[0]['data']).to eql(data)
+ it "can re-read back the data" do
+ expect(decoded_token[0]["data"]).to eql(data)
end
end
end
diff --git a/spec/lib/json_web_token/rsa_token_spec.rb b/spec/lib/json_web_token/rsa_token_spec.rb
index a3c54651e80..c0998e2f3d2 100644
--- a/spec/lib/json_web_token/rsa_token_spec.rb
+++ b/spec/lib/json_web_token/rsa_token_spec.rb
@@ -19,27 +19,28 @@ describe JSONWebToken::RSAToken do
allow_any_instance_of(described_class).to receive(:key).and_return(rsa_key)
end
- context 'token' do
- context 'for valid key to be validated' do
+ context "token" do
+ context "for valid key to be validated" do
before do
- rsa_token['key'] = 'value'
+ rsa_token["key"] = "value"
end
- subject { JWT.decode(rsa_encoded, rsa_key, true, { algorithm: 'RS256' }) }
+ subject { JWT.decode(rsa_encoded, rsa_key, true, {algorithm: "RS256"}) }
it { expect {subject}.not_to raise_error }
- it { expect(subject.first).to include('key' => 'value') }
+ it { expect(subject.first).to include("key" => "value") }
it do
expect(subject.second).to eq(
"typ" => "JWT",
"alg" => "RS256",
- "kid" => "OGXY:4TR7:FAVO:WEM2:XXEW:E4FP:TKL7:7ACK:TZAF:D54P:SUIA:P3B2")
+ "kid" => "OGXY:4TR7:FAVO:WEM2:XXEW:E4FP:TKL7:7ACK:TZAF:D54P:SUIA:P3B2"
+ )
end
end
- context 'for invalid key to raise an exception' do
+ context "for invalid key to raise an exception" do
let(:new_key) { OpenSSL::PKey::RSA.generate(512) }
- subject { JWT.decode(rsa_encoded, new_key, true, { algorithm: 'RS256' }) }
+ subject { JWT.decode(rsa_encoded, new_key, true, {algorithm: "RS256"}) }
it { expect {subject}.to raise_error(JWT::DecodeError) }
end
diff --git a/spec/lib/json_web_token/token_spec.rb b/spec/lib/json_web_token/token_spec.rb
index d7e7560d962..c74bab8b3a2 100644
--- a/spec/lib/json_web_token/token_spec.rb
+++ b/spec/lib/json_web_token/token_spec.rb
@@ -1,8 +1,8 @@
describe JSONWebToken::Token do
let(:token) { described_class.new }
- context 'custom parameters' do
- let(:value) { 'value' }
+ context "custom parameters" do
+ let(:value) { "value" }
before do
token[:key] = value
@@ -12,7 +12,7 @@ describe JSONWebToken::Token do
it { expect(token.payload).to include(key: value) }
end
- context 'embeds default payload' do
+ context "embeds default payload" do
subject { token.payload }
let(:default) { token.send(:default_payload) }