diff --git a/.DS_Store b/.DS_Store index da07aa6..4f9fb81 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 2f2b04e..da53bb8 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index ff028e0..1d9320e 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/services/token_manager_spec.rb b/spec/services/token_manager_spec.rb new file mode 100644 index 0000000..00a8d16 --- /dev/null +++ b/spec/services/token_manager_spec.rb @@ -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