Skip to content

Commit

Permalink
add token manager test
Browse files Browse the repository at this point in the history
  • Loading branch information
Adepoju Adebayo authored and Osmond Oscar committed Mar 15, 2016
1 parent 784ed23 commit 10224c1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[![Coverage Status](https://coveralls.io/repos/github/andela-iamadi/zhishi-backend/badge.svg?branch=ft-question-and-socialprov-tests)](https://coveralls.io/github/andela-iamadi/zhishi-backend?branch=ft-question-and-socialprov-tests)

[![Coverage Status](https://coveralls.io/repos/github/andela-iamadi/zhishi-backend/badge.svg?branch=master)](https://coveralls.io/github/andela-iamadi/zhishi-backend?branch=master)
# Zhishi
You've definitely being in this situation before:
> Someone asks you a question;
Expand Down
12 changes: 12 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@
with.test_framework :rspec
with.library :rails
end

class RequestObj
def self.headers
{ "Authorization" => TokenManager.generate_token(1) }
end
end

class BadObj
def self.headers
{ "Authorization" => nil }
end
end
end
54 changes: 54 additions & 0 deletions spec/services/token_manager_spec.rb
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

0 comments on commit 10224c1

Please sign in to comment.