Last active
November 16, 2021 19:03
-
-
Save niinyarko/f146f24a50125d55396f63043a2696e7 to your computer and use it in GitHub Desktop.
Request Spec Example for Devise Token Auth Gem with Rails 5.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example Request | |
# My Devise model is named Merchant | |
# Key lines to note are 16, 21, 27, 28, 29 | |
require 'rails_helper' | |
RSpec.describe 'POST /api/v2/customer_groups', type: :request do | |
let(:merchant) { FactoryGirl.create(:merchant) } | |
let(:user) { FactoryGirl.create(:user) } | |
let(:customer) { merchant.customers.create(user_id: user.id)} | |
let(:params) {{ | |
group: { | |
name: 'customers from office', | |
customer_ids: [customer.id] | |
} | |
}} | |
let(:auth_headers) { merchant.create_new_auth_token } | |
before { | |
BusinessType.create(name: 'Restaurants, Bars, & Cafes') | |
FactoryGirl.create(:pricing_plan) | |
sign_in merchant | |
post "/api/v2/customer_groups", | |
params: params.to_json, | |
headers: { | |
'CONTENT_TYPE' => 'application/json', | |
'ACCEPT' => 'application/json', | |
'Uid' => auth_headers['uid'], | |
'Access-Token' => auth_headers['access-token'], | |
'Client' => auth_headers['client'] | |
} | |
} | |
it 'creates groups with members' do | |
expect(response.status).to eq(200) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/rails_helper.rb | |
# uncomment the following line | |
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } | |
# incude the following line under the RSpec.configure block | |
config.include RequestSpecHelper, type: :request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/request_spec_helper.rb | |
module RequestSpecHelper | |
include Warden::Test::Helpers | |
def self.included(base) | |
base.before(:each) { Warden.test_mode! } | |
base.after(:each) { Warden.test_reset! } | |
end | |
def sign_in(resource) | |
login_as(resource, scope: warden_scope(resource)) | |
end | |
def sign_out(resource) | |
logout(warden_scope(resource)) | |
end | |
private | |
def warden_scope(resource) | |
resource.class.name.underscore.to_sym | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment