Skip to content

Instantly share code, notes, and snippets.

@Impossibeard
Last active August 19, 2016 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Impossibeard/e3385b12077dacb1911ac56f19a73971 to your computer and use it in GitHub Desktop.
Save Impossibeard/e3385b12077dacb1911ac56f19a73971 to your computer and use it in GitHub Desktop.
class ApiConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
@default || req.headers['Accept'].include?("application/vnd.marketplace.v#{@version}")
end
end
require 'api_constraints'
MarketPlaceApi::Application.routes.draw do
devise_for :users
# Api definition
namespace :api, defaults: { format: :json },
constraints: { subdomain: 'api' }, path: '/' do
scope module: :v1,
constraints: ApiConstraints.new(version: 1, default: true) do
# We are going to list our resources here
resources :users, :only => [:show, :create]
end
end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
config.include Devise::Test::ControllerHelpers, type: :controller
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explictly tag your specs with their type, e.g.:
#
# describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/v/3-0/docs
config.infer_spec_type_from_file_location!
end
class Api::V1::UsersController < ApplicationController
respond_to :json
def show
respond_with User.find(params[:id])
end
def create
user = User.new(user_params)
if user.save
render json: user, status: 201, location: [:api, user]
else
render json: { errors: user.errors }, status: 422
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
require 'spec_helper'
describe Api::V1::UsersController do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
@user = FactoryGirl.create :user
get :show, id: @user.id, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql @user.email
end
it { should respond_with 200 }
end
describe "POST #create" do
context "when is successfully created" do
before(:each) do
@user_attributes = FactoryGirl.attributes_for :user
post :create, { user: @user_attributes }, format: :json
end
it "renders the json representation for the user record just created" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql @user_attributes[:email]
end
it { should respond_with 201 }
end
context "when is not created" do
before(:each) do
#notice I'm not including the email
@invalid_user_attributes = { password: "12345678",
password_confirmation: "12345678" }
post :create, { user: @invalid_user_attributes }, format: :json
end
it "renders an errors json" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response).to have_key(:errors)
end
it "renders the json errors on why the user could not be created" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:errors][:email]).to include "can't be blank"
end
it { should respond_with 422 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment