Skip to content

Instantly share code, notes, and snippets.

@Ex-Ark
Last active May 24, 2022 19:20
Show Gist options
  • Save Ex-Ark/8e8fa19c9222abeb760cad3c61fbbc2f to your computer and use it in GitHub Desktop.
Save Ex-Ark/8e8fa19c9222abeb760cad3c61fbbc2f to your computer and use it in GitHub Desktop.
How to stub JWT server validation using Google OAuth2 and RSpec
require 'google/apis/oauth2_v2'
shared_context 'stub_google_token_validation' do
before do
allow_any_instance_of(Google::Apis::Oauth2V2::Oauth2Service).to receive(:tokeninfo).and_return(
Google::Apis::Oauth2V2::Tokeninfo.new email: 'your_fake_email_here@rspec.test',
issued_to: Rails.application.secrets[:google_client_id],
audience: Rails.application.secrets[:google_client_id]
)
end
end
# example using rspec-rails and rspec-rails-swagger
require 'swagger_helper'
RSpec.describe 'Authentification', type: :request, capture_examples: true do
path '/auth/google' do
parameter :id_token, in: :query, type: :string, required: true
post summary: "auth request using Google OAuth id_token", tags: ['auth'] do
produces 'application/json'
response 401, description: "id_token is invalid" do
let(:id_token) { 'invalid' }
end
response 200, description: "id token is valid" do
# we suppose it's valid
let(:id_token) { 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImE1NDFkNmVmMDIyZDc3YTIzMThmN2RkNjU3ZjI3NzkzMjAzYmVkNGEiLCJ0eXAiOiJKV1QifQ' }
# let's stub the responses
include_context 'stub_google_token_validation'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment