Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

andriy-baran

🎯
Focusing
View GitHub Profile
View api_session.rb
RSpec.shared_context 'api session', shared_context: :metadata do
let(:domain) { 'good.co' }
let(:email) { "#{SecureRandom.hex}@#{domain}" }
let(:user) { FactoryBot.create(:user, email: email) }
let(:valid_headers) { {} }
let(:valid_headers) do
{
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-API-Token' => user.api_key
View protected_endpoint.rb
RSpec.shared_examples 'protected endpoint' do |http_method|
context 'when api user unless they haven\'t provided access token' do
it 'renders not logged in message' do
public_send(http_method, path, valid_params) # No valid headers
expect(response.body).to eq 'Not logged in'
end
end
end
View responce_factory.rb
factory :search_results_resps, class: 'OpenStruct' do
variant_id { variant.id }
product_name { variant.product.name }
product_brand_name { variant.product.brand_name.to_s }
flavor { variant.flavor.to_s }
warehouse_category { variant.product.warehouse_category.to_s }
skip_container { variant.skip_container }
transient do
variant nil
View endpoint_generator.rb
module Rspec
class EndpointGenerator < Rails::Generators::NamedBase
desc 'Generate a spec/requests file for specified action of controller'
argument :controller, type: :string, required: true, banner: 'CONTOLLER'
source_root File.expand_path('../templates', __FILE__)
# https://www.rubytapas.com/2012/11/28/episode-029-redirecting-output/
def capture_output
View path_example.rb
include_context 'api session'
let(:path) { "/api/v1/#{order_id}/contact_info" }
let(:order_id) { 456 }
it_behaves_like 'protected endpoint', :get
context 'when order found' do
let(:order) { FactoryBot.create(:order, creator: user) }
let(:order_id) { order.id }
View response_variations.csv
body\code 200 201 403 404 422
raw text/plain | string text/plain | string text/plain | string text/plain | string text/plain | string
object x-json | ostruct x-json | ostruct x-json | ostruct x-json | ostruct x-json | ostruct
collection x-json | hash x-json | hash x-json | hash x-json | hash x-json | hash
View helpers_v2.rb
module Helpers
def assert_body
expect(response.body).to eq message # raw body
end
def assert_response_object
expect(response_object).to have_attributes(resp_attrs) # object
end
def assert_response_body
View typical_example.rb
it 'renders correct response' do
get path, valid_params, valid_headers # do request
expect(response).to have_http_status(:ok) #
expect(response.content_type).to eq 'application/json' # Assert status, mime type and response body
expect(response_object).to have_attributes(valid_response) #
item.reload
expect(item.qc_by).to eq nil #
expect(item.qc_signed_by).to eq nil # Check database state
View action_request_spec.rb
RSpec.describe '', type: :request do
include_context 'api session'
<% @path_params.each do |param| %>
<%= "let(#{param}) { #{rand(99..500)} }" %>
<% end %>
let(:path) { "<%= @path %>" }
let(:valid_params) { {} }
let(:valid_response) { {} }
View helpers_v1.rb
module Helpers
def parse_json(json_data)
JSON.parse(json_data, symbolize_names: true)
end
def response_body
JSON.parse(response.body, symbolize_names: true)
end
def response_object