Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
getfitter-core / spec / controllers / core / tokens_controller_spec.rb
Size: Mime:
require 'rails_helper'

RSpec.describe Core::TokensController, type: :controller do
  let(:json) { JSON.parse(response.body) }

  before do
    @routes = Core::Engine.routes
  end

  context 'parameters' do
    it 'requires a provider' do
      post :exchange, token: 'abc'

      expect(response).not_to be_success
      expect(response).to have_http_status(400)
      expect(json['message']).to match(/Missing parameter/)
    end

    it 'requires a token' do
      post :exchange, provider: 'facebook'

      expect(response).not_to be_success
      expect(response).to have_http_status(400)
      expect(json['message']).to match(/Missing parameter/)
    end

    it 'requires a valid provider' do
      post :exchange, provider: 'none', token: 'abc'

      expect(response).to have_http_status(400)
      expect(json['id']).to eq 'bad_request'
      expect(json['message']).to match(/Provider not supported/)
    end
  end

  context 'facebook' do
    it 'can exchange a Facebook access token' do
      VCR.use_cassette('token_exchange_facebook') do
        # if wishing to re-check, replace, A_VALID_TOKEN
        post :exchange, provider: 'facebook', token: 'A_VALID_TOKEN'

        expect(response).to have_http_status(200)
        expect(json).to have_key 'access_token'
        expect(json).to have_key 'refresh_token'
      end
    end

    it 'responds with an error if the token from Facebook is invalid' do
      VCR.use_cassette('token_exchange_facebook_invalid_token') do
        post :exchange, provider: 'facebook', token: 'abc'

        expect(response).to have_http_status(401)
        expect(json['id']).to eq 'unauthorized'
        expect(json['message']).to match(/token provided was invalid/)
      end
    end

    it 'responds with an error if the user exists under a different method' do
      VCR.use_cassette('token_exchange_facebook') do
        # we'll only validate on the email uniqueness
        create(:core_user, email: 'nick@nickcharlton.net')

        # if wishing to re-check, replace A_VALID_TOKEN
        post :exchange, provider: 'facebook', token: 'A_VALID_TOKEN'

        expect(response).to have_http_status(401)
        expect(json['id']).to eq 'unauthorized'
        expect(json['message']).to match(/user already exists/)
      end
    end
  end
end