Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. require 'rspec'
  2.  
  3. module Authenticable
  4. def current_user
  5. @current_user ||= 'Pedro'
  6. end
  7.  
  8. def authenticate_with_token!
  9. render json: { errors: "Not authenticated" },
  10. status: :unauthorized unless user_signed_in?
  11. end
  12.  
  13. def user_signed_in?
  14. current_user != nil
  15. end
  16. end
  17.  
  18. class Authentication
  19. include Authenticable
  20. end
  21.  
  22. RSpec.describe Authenticable do
  23. let(:authentication) { Authentication.new }
  24. subject { authentication }
  25.  
  26. describe '.authenticate_with_token' do
  27. before do
  28. allow(authentication).to receive(:current_user).and_return(nil)
  29. allow(authentication).to receive(:render) do |args|
  30. args
  31. end
  32. end
  33.  
  34. it 'returns error' do
  35. expect(authentication.authenticate_with_token![:json][:errors]).to eq 'Not authenticated'
  36. end
  37.  
  38. it 'returns unauthorized status' do
  39. expect(authentication.authenticate_with_token![:status]).to eq :unauthorized
  40. end
  41. end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement