-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adepoju Adebayo
authored and
Osmond Oscar
committed
Mar 15, 2016
1 parent
784ed23
commit 10224c1
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'rails_helper' | ||
RSpec.describe TokenManager do | ||
subject(:manager){TokenManager} | ||
subject(:request_obj){RequestObj} | ||
subject(:bad_obj){BadObj} | ||
|
||
describe "#generate_token" do | ||
it "generates token" do | ||
expect(manager.generate_token(1).length).to eq 160 | ||
end | ||
|
||
it "issues token" do | ||
payload = { user: 1, exp: (24.hours.from_now).to_i } | ||
expect(manager.issue_token(payload).length).to eq 160 | ||
end | ||
|
||
it "issues secret code" do | ||
expect(manager.secret.length).to eq 128 | ||
end | ||
end | ||
|
||
describe "#decode_token" do | ||
it "decodes token" do | ||
token = request_obj.headers["Authorization"] | ||
result = {"typ"=>"JWT", "alg"=>"HS512"} | ||
expect(manager.decode(token)).to be_kind_of(Array) | ||
expect(manager.decode(token).length).to eq 2 | ||
expect(manager.decode(token)[1]).to eql (result) | ||
end | ||
end | ||
|
||
describe "#authenticate" do | ||
it "authenticates valid token" do | ||
result = {"typ"=>"JWT", "alg"=>"HS512"} | ||
expect(manager.authenticate(request_obj)).to be_kind_of(Array) | ||
expect(manager.authenticate(request_obj).length).to eq 2 | ||
expect(manager.authenticate(request_obj)[1]).to eql (result) | ||
end | ||
|
||
it "authenticates invalid token" do | ||
expect(manager.authenticate(bad_obj)).to be_kind_of(Array) | ||
expect(manager.authenticate(bad_obj).length).to eq 2 | ||
expect(manager.authenticate(bad_obj)).to eql ([nil, 401]) | ||
end | ||
|
||
it "checks valid token" do | ||
expect(manager.token(request_obj).length).to eq 160 | ||
end | ||
|
||
it "checks invalid token" do | ||
expect(manager.token(bad_obj)).to be nil | ||
end | ||
end | ||
end |