kurenn / market_place_api Goto Github PK
View Code? Open in Web Editor NEWThe API on Rails tutorial application
The API on Rails tutorial application
I'm getting this warning for the line above in user_spec.rb:
Using stub
from rspec-mocks' old :should
syntax without explicitly enabling the syntax is deprecated ...
How can I get rid of it?
Thanks
Hi,
In chapter 3 in your book, i'm trying to integrate the sabisu and i'm getting the following screen:
Ruby ver. : 2.1.2
Rails ver. : 4.0.2
This are the gems i'm using:
Using rake 10.4.2
Using i18n 0.7.0
Using minitest 4.7.5
Using multi_json 1.11.0
Using thread_safe 0.3.5
Using tzinfo 0.3.43
Using activesupport 4.0.2
Using builder 3.1.4
Using erubis 2.7.0
Using rack 1.5.2
Using rack-test 0.6.3
Using actionpack 4.0.2
Using mime-types 1.25.1
Using polyglot 0.3.5
Using treetop 1.4.15
Using mail 2.5.4
Using actionmailer 4.0.2
Using activemodel 4.0.2
Using active_model_serializers 0.9.3
Using activerecord-deprecated_finders 1.0.3
Using arel 4.0.2
Using activerecord 4.0.2
Using bcrypt 3.1.10
Using chunky_png 1.3.4
Using coffee-script-source 1.9.1
Using execjs 2.4.0
Using coffee-script 2.3.0
Using thor 0.19.1
Using railties 4.0.2
Using coffee-rails 4.0.1
Using fssm 0.2.10
Using sass 3.2.19
Using compass 0.12.7
Using hike 1.2.3
Using tilt 1.4.1
Using sprockets 2.11.0
Using compass-rails 1.1.7
Using orm_adapter 0.5.0
Using responders 1.1.2
Using warden 1.2.3
Using devise 3.4.1
Using diff-lcs 1.2.5
Using factory_girl 4.5.0
Using factory_girl_rails 4.5.0
Using faker 1.4.3
Using font-awesome-rails 4.3.0.0
Using furatto 1.3.7
Using json 1.8.2
Using multi_xml 0.5.5
Using httparty 0.13.3
Using jquery-rails 3.1.2
Using bundler 1.9.0
Using sprockets-rails 2.0.1
Using rails 4.0.2
Using rdoc 4.2.0
Using rspec-core 2.14.8
Using rspec-expectations 2.14.5
Using rspec-mocks 2.14.6
Using rspec-rails 2.14.0
Using sabisu_rails 0.0.2.beta from git://github.com/IcaliaLabs/sabisu-rails.git (at master)
Using sass-rails 4.0.4
Using sdoc 0.4.1
Using shoulda-matchers 2.8.0
Using simple_form 3.1.0
Using sqlite3 1.3.10
Using uglifier 2.7.1
any idea why its happening?
thanks.
I've used your app to make similar API for myself. Thank you! It looks very promising.
If I am trying to make a request to the API outside of sabisu what URL should I hit? I've deployed my application using AWS and can make requests through sabisu just fine. When I'm trying to make a request from somewhere else though I don't know what URL to make it through to just get JSON responses.
I am using this URL which is probably not the correct one since is not including authorizatio.
Don't know how to generate the right one:
http://api.marketplaceapi.dev/users/1/orders
{"errors":"Not authenticated"}
I am trying an URL similar to that used for Products:
http://localhost:3000/sabisu_rails/explorer?explorer%5Bresource%5D=orders
The error I am getting is: NoMethodError in SabisuRails::Explorer#index
Showing .../sabisu_rails/explorer/index.html.erb where line #36 raised:
undefined method `attributes' for #<EnoughProductsValidator:0x007ffe5da72930 @options={}>
def required_attributes
@required_attributes ||= resource_class.validators.map(&:attributes).flatten.uniq
end
I'd recommend using:
'sabisu_rails', git: "https://github.com/icaliaLabs/sabisu-rails"
Not sure if this issue should be opened within here or SabisuRails:
During the course of the book, after I do this step within sabisu_rails.rb:
config.base_api_uri = "api.marketplace.dev"
I get the following error when I visit http://localhost:3000/sabisu_rails/explorer :
I do not see the API Explorer in Figure 3.2.
Here is my gemfile setup:
source 'https://rubygems.org'
gem 'rails', '4.2.0'
gem 'sass-rails', '> 5.0'> 4.1.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '
gem 'jquery-rails'
gem 'active_model_serializers'
gem 'sabisu_rails', github: "IcaliaLabs/sabisu-rails"
gem 'compass-rails'
gem 'furatto'
gem 'font-awesome-rails'
gem 'simple_form'
group :doc do
gem 'sdoc', require: false
end
group :development do
gem 'sqlite3'
gem 'web-console', '~> 2.0'
end
group :development, :test do
gem 'byebug'
gem "rspec-rails"
gem "factory_girl_rails"
gem "faker"
gem 'shoulda-matchers'
gem 'spring'
end
gem 'devise'
And here is my sabisu_rails.rb:
SabisuRails.setup do |config|
config.base_api_uri = "api.marketplaceapi.dev"
config.api_headers = { "Accept" => "application/vnd.marketplace.v1" }
config.resources = [:users]
config.default_resource = :users
end
In Chapter 9.1.1 : Extending the Placement model, we should extend the Placement model with a decrement_product_quantity!
but we can't commit our changes without testing before, but test fails.
require 'spec_helper'
describe Placement do
.
.
.
it { should respond_to :quantity }
.
.
.
describe "#decrement_product_quantity!" do
it "decreases the product quantity by the placement quantity" do
product = placement.product # placement doesn't have a product
expect{placement.decrement_product_quantity!}.to change{product.quantity}.by(-placement.quantity)
end
end
end
product
is nil because we don't set a product
to placement
so launching the test complains about undefined method 'quantity' for nil:NilClass
.
In Chapter 8 : Listing 8.4, we write this Placement specs :
require 'spec_helper'
describe Placement do
let(:placement) { FactoryGirl.build :placement }
subject { placement }
it { should respond_to :order_id }
it { should respond_to :product_id }
it { should belong_to :order }
it { should belong_to :product }
end
We don't touch at this spec before Listing 9.2. So placement
doesn't receive a product
.
describe Placement do
let(:product) { FactoryGirl.create(:product, quantity: 4) }
let(:placement) { FactoryGirl.create(:placement, product_id: product.id, quantity: 2) }
end
Now, product
is define. It responds to quantity
. And test pass.
Modify the factory placement
works too. When we generate the model, the factory placement
generated by FactoryGirl sets to nil
each attribute.
FactoryGirl.define do
factory :placement do
order
product
quantity 1
end
end
I am at the end of chapter 6. Everything works well, all tests are green. Here is the question. How I can use the Sabisu browser interface http://localhost:3000/sabisu_rails/explorer to delete, or update, or create an user? Im selecting DELETE instead of GET, I am entering the user id 2 and press Enter and no user is deleted. Also I am getting the message:
{
"errors": "Not authenticated"
}
When using postman to create a session, I'm getting undefined method
valid_password?' for false:FalseClass`.
Here is my sessions_controller.rb
class Api::V1::SessionsController < ApplicationController
respond_to :json
def create
user_password = params[:session][:password]
user_email = params[:session][:email]
user = user_email.present? && User.find_by(email: user_email)
if user.valid_password? user_password
sign_in user, store: false
user.generate_authentication_token!
user.save
render json: user, status: 200, location: [:api, user]
else
render json: { errors: 'Invalid email or password' }, status: 422
end
end
def destroy
user = User.find_by(auth_token: params[:id])
user.generate_authentication_token!
user.save
head 204
end
end
Hi,
I was following your tutorial about how to build an api with rails
but a got an error :
[...]/dependencies.rb:247:in
require': cannot load such file -- lib/api_constraints (LoadError)`
routes.rb
require 'api_constraints'
Rails.application.routes.draw do
#Api definition
namespace :api, defaults: {format: :json}, constraints: {subdomain: 'api'}, path: '/' do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
# code
end
end
end
config/appliaction.rb
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_model/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module MarketPlaceApi
class Application < Rails::Application
#don't generate RSpec tests fot views and helpers
config.generators do |g|
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
g.view_specs false
g.helper_specs false
g.stylsheets = false
g.javascripts = false
g.helper = false
end
config.autoload_paths += %W(#{config.root}/lib)
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
lib/api_constraints.rb
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
After adding embed :ids to user_serializer.rb
class UserSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :email, :created_at, :updated_at, :auth_token
has_many :products
end
Two problems arise. the depreciated embed ids attribute is causing 10 test related to the user model to fail
DEPRECATION WARNING: ** Notice: embed is deprecated. **
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
Please use the global .setup method instead:
ActiveModel::Serializer.setup do |config|
config.embed = :ids
config.embed_in_root = true
end
Showing full backtrace because every line was filtered out.
See docs for RSpec::Configuration#backtrace_exclusion_patterns and
RSpec::Configuration#backtrace_inclusion_patterns for more information.
I created a active_model_serializers.rb with the configuration included, but it didn't make a difference.
After spending a few hours troubleshooting, I'm stuck. I followed along with the book except to change the class level embed: :ids
to embed on association. I even tried cloning this repo down and I still have these errors. Am I missing something @kurenn?
Test failing after adding the code for destroy as it is in Listing 5.8: Destroy action sessions (app/controllers/api/v1/sessions_controller.rb)
rspec spec/controllers/api/v1/sessions_controller_spec.rb
....F
Failures:
Finished in 0.15741 seconds
5 examples, 1 failure
Failed examples:
rspec ./spec/controllers/api/v1/sessions_controller_spec.rb:44 # Api::V1::SessionsController DELETE #destroy
Randomized with seed 203
Hello kuri,
I m stuck with a problem in Listing 8.9, which I guess is because of the current_user addition to the location: ,the method is below;
Listing 8.9: Orders controller create action(app/controllers/api/v1/orders_controlller.rb)
def create
order = current_user.orders.build(order_params)
if order.save
render json: order, status: 201, location: [:api, current_user, order]
else
render json: { errors: order.errors }, status: 422
end
end
FAILURE
Hi
I keep getting Routing error for [GET] user/:id & sabisu_rails/explorer 404 response.
I followed everything in the tutorial, created users using console, and tried doing it by cloning https://github.com/kurenn/market_place_api.git -b chapter3, but none of them work.
$ rspec spec/controllers/api/v1/users_controller_spec.rb
Finished in 1.05 seconds
14 examples, 0 failures
$ rspec spec/controllers/api/v1/products_controller_spec.rb
Failures:
...
11) Api::V1::ProductsController POST #create when is successfully created renders the json representation for the product record just created
Failure/Error: post :create, { user_id: user.id, product: @product_attributes }
SystemStackError:
stack level too deep
# ./app/controllers/api/v1/products_controller.rb:17:in create' # ./spec/controllers/api/v1/products_controller_spec.rb:72:in
block (4 levels) in <top (required)>'
Finished in 2.73 seconds
18 examples, 11 failures
Hello.
I'm using Rails 4.1.7 and my Devise is 3.4.1, and I'm following this book and stuck on Chapter 5, after completing the chapter and running rspec
, I get following errors:
โก rspec
..................FFF..............
Failures:
1) Authenticable#current_user returns the user from the authorization header
Failure/Error: authentication.stub(:request).and_return(request)
#<Authentication:0x007fd33459ad80> does not implement: request
# ./spec/controllers/concerns/authenticable_spec.rb:15:in `block (3 levels) in <top (required)>'
2) Authenticable#authenticate_with_token render a json error message
Failure/Error: authentication.stub(:response).and_return(response)
#<Authentication:0x007fd3344ced20> does not implement: response
# ./spec/controllers/concerns/authenticable_spec.rb:28:in `block (3 levels) in <top (required)>'
3) Authenticable#authenticate_with_token
Failure/Error: authentication.stub(:response).and_return(response)
#<Authentication:0x007fd33440e228> does not implement: response
# ./spec/controllers/concerns/authenticable_spec.rb:28:in `block (3 levels) in <top (required)>'
Finished in 0.41007 seconds (files took 1.81 seconds to load)
35 examples, 3 failures
Failed examples:
rspec ./spec/controllers/concerns/authenticable_spec.rb:17 # Authenticable#current_user returns the user from the authorization header
rspec ./spec/controllers/concerns/authenticable_spec.rb:31 # Authenticable#authenticate_with_token render a json error message
rspec ./spec/controllers/concerns/authenticable_spec.rb:35 # Authenticable#authenticate_with_token
Can someone help me out as to what could be causing this?
Hi kurenn, i am following your APIs on Rails tutorial, and when i try to get sabisu-rails index view, i get a undefined mixing 'display-flex' error, reading your past answers, changed the version of compass-rails to 1.1.7 and now it works, so i suggest to include that on your book because now the version installed if we don't especify it is the 2.0.0 and this generates the error
I'm using rails 4.2 ruby 2.2 & mongoid and encountering a strange error when following your tutorial - section 3.2.3 (Updating users - I use recruiter class instead).
It looks like it failed to update email however it passed when I tweak it to update other information such as first name (see the screenshot).
Below are my API controller & Routes
Any thoughts & suggestions will be very appreciated!!!
In Chapter 6, you set the user_id manually.
post :create, { user_id: user.id, product: @product_attributes }
This should automatically be set using the current_user attribute in the controller.
1) Api::V1::UsersController GET #show
Failure/Error: it { should respond_with 200 }
NoMethodError:
undefined method `respond_with' for #<RSpec::ExampleGroups::ApiV1UsersController::GETShow:0x007f86a721ac70>
# /Users/jenks/.gem/ruby/2.2.2/gems/actionpack-4.2.4/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
# ./spec/controllers/api/v1/users_controller_spec.rb:15:in `block (3 levels) in <top (required)>'
Receiving this error when trying to run the tests for the Users controller. I am using Rspec-rails 3.0 and shoulda-matchers 3.0. Any idea of why this is failing?
Hi!
I'm getting the following errors when running the authenticable_spec.rb tests.
Failures:
1) Authenticable #user_signed_in? when there is a user on 'session' should be user signed in
Failure/Error: it { should be_user_signed_in }
NoMethodError:
undefined method `user_signed_in?' for nil:NilClass
# ./spec/controllers/concerns/authenticable_spec.rb:46:in `block (4 levels) in <top (required)>'
2) Authenticable #user_signed_in? when there is no user on 'session' should not be user signed in
Failure/Error: it { should_not be_user_signed_in }
NoMethodError:
undefined method `user_signed_in?' for nil:NilClass
# ./spec/controllers/concerns/authenticable_spec.rb:56:in `block (4 levels) in <top (required)>'
What could be happening?
Thank you.
It would be nice to see at a quick glance what changes to the users_controller_spec.rb were made at the start of Chapter Seven. However, you do get a better understanding of the code by needing to closely compare the code in chapter seven to that in chapter six.
I have been successful to send a POST from my test iOS app to the Session controller and able to get the Authorization in return. However, I have been running into issue when sending a GET method to my Rails API. The routing seems not to be an issue as it works with cURL and Postman.
Been banging my head on this issue for months. Any helps would be greatly appreciated!
***iOS
Response:
<NSHTTPURLResponse: 0x7fe06a842d60> { URL: http://localhost:3000/api/recruiters/3/events } { status code: 404, headers {
Connection = "keep-alive";
"Content-Length" = 36;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sat, 11 Jul 2015 02:21:27 GMT";
Server = Cowboy;
Via = "1.1 vegur";
"X-Request-Id" = "92137097-5fd1-4bc8-a495-d299a51b55af";
"X-Runtime" = "0.002844";
} }
Body: Optional({"status":"404","error":"Not Found"})
So I've been working through the book updating code samples to use the latest version of Rspec as some of the methods have been deprecated. For eg, this spec from chapter 5 fails,
require 'rails_helper'
class Authentication
include Authenticable
end
describe Authenticable do
let(:authentication) { Authentication.new }
describe '#current_user' do
before do
@user = create(:user)
request.headers['Authorization'] = @user.auth_token
authentication.stub(:request).and_return(request)
#allow(authentication).to receive(:request).and_return(request)
end
it 'returns the user specified from the authorization header' do
expect(authentication.current_user.auth_token).to eq(@user.auth_token)
end
end
end
Result:
Failures:
1) Authenticable#current_user returns the user specified from the authorization header
Failure/Error: authentication.stub(:request).and_return(request)
NoMethodError:
undefined method `stub' for #<Authentication:0x007f812c81b438>
# ./spec/controllers/concerns/authenticable_spec.rb:14:in `block (3 levels) in <top (required)>'
Finished in 0.06153 seconds (files took 3.28 seconds to load)
1 example, 1 failure
I refactored to use allow(authentication).to receive(:request).and_return(request)
instead of authentication.stub(:request).and_return(request)
but still borks:
Failures:
1) Authenticable#current_user returns the user specified from the authorization header
Failure/Error: allow(authentication).to receive(:request).and_return(request)
#<Authentication:0x007ff3d405deb0> does not implement: request
# ./spec/controllers/concerns/authenticable_spec.rb:15:in `block (3 levels) in <top (required)>'
Finished in 0.07007 seconds (files took 3.29 seconds to load)
1 example, 1 failure
Any ideas?
Enter http://localhost:3000/sabisu_rails/explorer
Click cancel then show this:
HTTP Basic: Access denied.
I tried to add a user in rails console but not work
Any suggestion๏ผ
when i'm trying to access sabisu_rails/explorer, i had this error
Connection refused - connect(2) for "api.marketplaceapi.dev" port 80
i had move the project market_place_api to marketplaceapi folder. But the error is keep coming.
I really don't understand the meaning of this code.
Listing 5.11: Authenticable authenticate with token method (spec/controllers/concerns/authenticable_spec.rb)
describe Authenticable do
.
.
.
describe "#authenticate_with_token" do
before do
@user = FactoryGirl.create :user
authentication.stub(:current_user).and_return(nil)
response.stub(:response_code).and_return(401)
response.stub(:body).and_return({"errors" => "Not authenticated"}.to_json)
authentication.stub(:response).and_return(response)
end
it "render a json error message" do
expect(json_response[:errors]).to eql "Not authenticated"
end
it { should respond_with 401 }
end
end
Explain, please:
authentication.stub(:current_user).and_return(nil)
into this
authentication.stub(:current_user).and_return(@user)
all tests also should be passing. WTF?
Thanks.
Hi,
@kurenn, if you find useful, below are some flaws and corrections that can improve the quality of your book.
where is:
Listing 4.1: Users controller spec (app/controllers/api/v1/users_controller.rb)
should be:
... spec/controllers/api/v1/users_controller_spec.rb)
ch5-pg3
where to add the new code?
ch5-pg10
where is:
$ bundle exec rspec spec/controllers/api/v1/users_controller.rb
should be:
$ bundle exec rspec spec/controllers/api/v1/users_controller_spec.rb
ch5-pg11
where is:
Listing 5.15: Destroy action with currentuser (app/controllers/api/v1/users_controller.rb)
should be:
Listing 5.15: Destroy action with current_user (app/controllers/api/v1/users_controller.rb)
ch5-pg12
where is:
bundle exec rspec/spec/controllers/concerns/authenticable.rb
should be:
bundle exec rspec spec/controllers/concerns/authenticable_spec.rb
ch6-pg2
where is:
describe Product do
...
it { should not_be_published }
end
should be:
describe Product do
...
it { should_not be_published }
end
ch6-pg2
where is:
bundle exec rspec spec/modes/product_spec.rb
should be:
bundle exec rspec spec/models/product_spec.rb
ch6-pg5
where is:
$ bundle exec rspec spec/
should be:
$ bundle exec rspec spec/models/user_spec.rb
ch6-pg10
where is:
Listing 6.12: Products create action (spec/controllers/api/v1/products_controller.rb)
should be:
Listing 6.12: Products create action (app/controllers/api/v1/products_controller.rb)
ch6-pg11
where is:
it "renders the json representation for the updated user" do
shouldn't it be
it "renders the json representation for the updated product" do
ch6-pg12
where is:
it "renders the json errors on whye the user could not be created" do
shouldn't it be
it "renders the json errors on why the product could not be updated" do
ch6-pg12
where is:
Listing 6.14: Products update action (spec/controllers/api/v1/products_controller.rb)
should be:
Listing 6.14: Products update action (app/controllers/api/v1/products_controller.rb)
ch6-pg13
where is:
Listing 6.16: Products destroy action (spec/controllers/api/v1/products_controller.rb)
should be:
Listing 6.16: Products destroy action (app/controllers/api/v1/products_controller.rb)
I finished Chapter 3.
GET, PATCH and DELETE are working, but on POST request I get ActionController::UnknownFormat (ActionController::UnknownFormat)
Started POST "/users" for 127.0.0.1 at 2015-03-17 21:29:39 +0100
Processing by Devise::RegistrationsController#create as application/vnd.marketplace.v1
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}
Can't verify CSRF token authenticity
(0.3ms) BEGIN
User Exists (0.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
SQL (0.4ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "[email protected]"], ["encrypted_password", "$2a$10$3YXrXSF423U.ztCVY.hP.uCHb11QJSDqDlm0.WTX.427BIVUnhvw2"], ["created_at", "2015-03-17 20:29:40.052900"], ["updated_at", "2015-03-17 20:29:40.052900"]]
(2.0ms) COMMIT
(0.3ms) BEGIN
SQL (0.3ms) UPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "last_sign_in_ip" = $3, "current_sign_in_ip" = $4, "sign_in_count" = $5, "updated_at" = $6 WHERE "users"."id" = $7 [["last_sign_in_at", "2015-03-17 20:29:40.057666"], ["current_sign_in_at", "2015-03-17 20:29:40.057666"], ["last_sign_in_ip", "127.0.0.1/32"], ["current_sign_in_ip", "127.0.0.1/32"], ["sign_in_count", 1], ["updated_at", "2015-03-17 20:29:40.059548"], ["id", 8]]
(0.4ms) COMMIT
Completed 406 Not Acceptable in 98ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
responders (2.1.0) lib/action_controller/respond_with.rb:205:in `respond_with'
devise (3.4.1) app/controllers/devise/registrations_controller.rb:25:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.0) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:155:in `handle'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:109:in `rescue in block (2 levels) in start'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:106:in `block (2 levels) in start'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:96:in `each'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:96:in `block in start'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:76:in `loop'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:76:in `start'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/lib/nack/server.rb:12:in `run'
/Users/luka/Library/Application Support/Pow/Versions/0.5.0/node_modules/nack/bin/nack_worker:4:in `<main>'
Rendered /usr/local/var/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.7ms)
Rendered /usr/local/var/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7ms)
Rendered /usr/local/var/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered /usr/local/var/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (68.8ms)
Cannot render console with content type application/jsonAllowed content types: [#<Mime::Type:0x007fd8c51f6d78 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x007fd8c51f68c8 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x007fd8c51ecc88 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]
Failures:
Placement#decrement_product_quantity! decreases the product quantity by the placement quantity
Failure/Error: expect{placement.decrement_product_quantity!}.to change{product.quantity}.by(-placement.quantity)
NoMethodError:
undefined method `quantity' for nil:NilClass
Api::V1::OrdersController POST #create returns just user order record
Failure/Error: expect(order_response[:id]).to be_present
NoMethodError:
undefined method `[]' for nil:NilClass
Api::V1::OrdersController POST #create embeds the two product objects related to the order
Failure/Error: expect(order_response[:products].size).to eql 2
NoMethodError:
undefined method `[]' for nil:NilClass
Api::V1::OrdersController POST #create should respond with 201
Failure/Error: it { should respond_with 201 }
Expected response to be a 201, but was 422
Hi,
I finished the whole way of this tutorial and now I'm trying to use it.
For the moment, and although all specs were successful, in Sabisu Explorer I just can use some simple GET ops for users (users/:id; users/orders;products;products/:id). Postman does not work at all (why?) No POST is allowed.
I'm not being able to create new users (which only requires email and password) and so I cannot get the session code to access the API funcs.
I'm building a Ember UI and it fails to. The errors are explicit relating XMLHttpRequest : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 404.
After some research on the web, I made the conclusion that I'm facing a CORS related issue.
What is the role of the API here? How can it handle this requirement?
Does anyone have similar problems?
BR
Hi, I reached the end of Chapter 6, everything went well up to the point to fill DB with products with
20.times { FactoryGirl.create :product }
I'm getting an error as below
I did:
change gemfile to have factory_girl_rails and ffaker in :development, then
bundle install, then
20.times { FactoryGirl.create :product }
I got:
2.2.0 :001 > 20.times { FactoryGirl.create :product }
(0.2ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."auth_token" = '' LIMIT 1
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Auth token has already been taken
Can anyone help here,please?
Thks
Pedro
I cloned the project, run bundle install
and as I say in the title, can't run the tests:
$ bundle exec rspec
invalid option: --no-fail-fast
Please use --help for a listing of valid options
Any idea why?
The destroy spec code is missing.
You have a code snippet (see below) for spec/controllers/api/v1/products_controller.rb but I'm guessing you mean controllers/api/v1/products_controller.rb.
Hence, the (1) destroy spec needs to be added to chapter 6 and the (2) controller code snippet needs to be retitled from spec to app.
def destroy
product = current_user.products.find(params[:id])
product.destroy
head 204
end
Quick note: I'm curious if you recommend adding a failing test to the destroy spec (see below):
describe "DELETE #destroy" do
before(:each) do
@user = FactoryGirl.create :user
@product = FactoryGirl.create :product, user: @user
api_authorization_header @user.auth_token
delete :destroy, { user_id: @user.id, id: @product.id }
end
it { should respond_with 204 }
end
And add the related code in the controller so that it gives a 422 if it's unable to destroy a resource.
if @product.destroy
render nothing: true, status: 204
else
render json: @product.errors, status: 422
end
How do I send the auth_token only on SessionsController requests? Do I need to cread a User Serializer specifically for SessionsController?
This is more of a question out of ignorance, but if I have a before_action :authenticate_with_token! on this project, can someone tell me how I'd properly set up the headers in sabisu or curl to have GET or POST request that works?
I have tried the header:
Authorization : key
And that doesn't seem to be enough. Thanks so much!
def current_user
@current_user ||= User.find_by(auth_token: request.headers['Authorization'])
end
when i check, request.headers['Authorization'] equals null and all my test cases fail. What am I doing wrong?
I'm in rspec 2.99
and when running the authenticable_spec.rb
tests mentioned in chapter 5, I get errors in #authenticate_with_token
and #user_signed_in?
Failures:
1) Authenticable#authenticate_with_token should respond with 401
Failure/Error: it { should respond_with 401 }
NoMethodError:
undefined method `response' for nil:NilClass
# ./spec/controllers/concerns/authenticable_spec.rb:34:in `block (3 levels) in <top (required)>'
2) Authenticable#user_signed_in? when there is a user on 'session' should be user signed in
Failure/Error: it { should be_user_signed_in }
NoMethodError:
undefined method `user_signed_in?' for nil:NilClass
# ./spec/controllers/concerns/authenticable_spec.rb:44:in `block (4 levels) in <top (required)>'
3) Authenticable#user_signed_in? when there is no user on 'session' should not be user signed in
Failure/Error: it { should_not be_user_signed_in }
NoMethodError:
undefined method `user_signed_in?' for nil:NilClass
# ./spec/controllers/concerns/authenticable_spec.rb:53:in `block (4 levels) in <top (required)>'
Finished in 0.06381 seconds
5 examples, 3 failures
I don't know if I'm missing something. I have triple checked the code compared to the guide. My repo is at https://github.com/jorgemoya/achiever_api/tree/698dd4adc31f65b3d9cce48fdbd1d9ff546303d8.
In the end of Chapter 6, the 'rspec-rails' gem is in "group :test". I'd recommend adding it to "group :development, :test" so that it works for rake tasks and generators.
not sure if this is the right place for this, but I'm having an issue. everything is done, tests are passing, and i deployed it to heroku. When it's actually on production, what would the address be? according to the development structure of accessing the app via http://api.repo_name.dev/
, the heroku equivalent should be https://api.heroku_name.herokuapp.com
right?
On development, if I go into my browser and type http://api.marketplaceapi.dev/products
i get a json response. However if I make a curl request to https://api.heroku_name.herokuapp.com/products
I get Heroku | No such app
.
with the api subdomain set, would you know how to go about accessing the endpoints in production?
Sass::SyntaxError in SabisuRails::Explorer#index
Showing /Users/psantos/.rvm/gems/ruby-2.2.0/bundler/gems/sabisu-rails-de6340971e70/app/views/layouts/sabisu_rails.html.erb where line #13 raised:
Mixin appearance doesn't have an argument named $ap.
(in /Users/psantos/.rvm/gems/ruby-2.2.0/bundler/gems/sabisu-rails-de6340971e70/app/assets/stylesheets/sabisu_rails/application.css.scss:132)
Hi,
I'm following the guide and arriving to the final test as the title, I got some Failures around the "authenticable_spec.rb":
$ bundle exec rspec spec/controllers/concerns/authenticable_spec.rb
..FDEPRECATION WARNING: named_routes.helpers
is deprecated, please use route_defined?(route_name)
to see if a named route was defined. (called from block (4 levels) in <top (required)> at /home/pvc/projects/marketplaceapi/spec/controllers/concerns/authenticable_spec.rb:59)
FDEPRECATION WARNING: named_routes.helpers
is deprecated, please use route_defined?(route_name)
to see if a named route was defined. (called from block (4 levels) in <top (required)> at /home/pvc/projects/marketplaceapi/spec/controllers/concerns/authenticable_spec.rb:69)
F
Failures:
Authenticable#authenticate_with_token should respond with 401
Failure/Error: it {should respond_with 401 }
NoMethodError:
undefined method `response' for nil:NilClass
Authenticable#user_signed_in? when there is a user on 'session' should be user signed in
Failure/Error: it { should be_user_signed_in }
NoMethodError:
undefined method `user_signed_in?' for nil:NilClass
Authenticable#user_signed_in? when there is no user on 'session' should not be user signed in
Failure/Error: it { should_not be_user_signed_in }
NoMethodError:
undefined method `user_signed_in?' for nil:NilClass
Finished in 0.09193 seconds
5 examples, 3 failures
Failed examples:
rspec ./spec/controllers/concerns/authenticable_spec.rb:45 # Authenticable#authenticate_with_token should respond with 401
rspec ./spec/controllers/concerns/authenticable_spec.rb:59 # Authenticable#user_signed_in? when there is a user on 'session' should be user signed in
rspec ./spec/controllers/concerns/authenticable_spec.rb:69 # Authenticable#user_signed_in? when there is no user on 'session' should not be user signed in
Randomized with seed 56814
I found the solution here:
http://stackoverflow.com/questions/28786439/undefined-method-response-for-nilnilclass-rspec-test
and the solution is to had:
subject { authentication }
as below:
...
let(:authentication) { Authentication.new }
subject { authentication }
describe '#current_user' do
...
This solved my problem (why is something I cannot say at this moment...)
Pedro
I made the changes in section 7.9 to product_controllers_spec.rb
and product_controllers.rb
and now my tests are failing. Before doing these steps, my tests were green.
Failures:
1) Api::V1::ProductsController GET #show
Failure/Error: it { should respond_with 200 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fd2e33762f8>
# ./spec/controllers/api/v1/products_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
2) Api::V1::ProductsController POST #create when is successfully created
Failure/Error: it { should respond_with 201 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3::Nested_1:0x007fd2e2eb0c28>
# ./spec/controllers/api/v1/products_controller_spec.rb:81:in `block (4 levels) in <top (required)>'
3) Api::V1::ProductsController POST #create when is not created
Failure/Error: it { should respond_with 422 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3::Nested_2:0x007fd2e339d2b8>
# ./spec/controllers/api/v1/products_controller_spec.rb:102:in `block (4 levels) in <top (required)>'
4) Api::V1::ProductsController GET #index when is not receiving any product_ids parameter
Failure/Error: it { should respond_with 200 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x007fd2e80fb1e0>
# ./spec/controllers/api/v1/products_controller_spec.rb:46:in `block (4 levels) in <top (required)>'
5) Api::V1::ProductsController PUT/PATCH #update when is successfully updated
Failure/Error: it { should respond_with 200 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1:0x007fd2e2f00f70>
# ./spec/controllers/api/v1/products_controller_spec.rb:123:in `block (4 levels) in <top (required)>'
6) Api::V1::ProductsController PUT/PATCH #update when is not updated
Failure/Error: it { should respond_with 422 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_2:0x007fd2e1526550>
# ./spec/controllers/api/v1/products_controller_spec.rb:141:in `block (4 levels) in <top (required)>'
7) Api::V1::ProductsController DELETE #destroy
Failure/Error: it { should respond_with 204 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5:0x007fd2e0ce3118>
# ./spec/controllers/api/v1/products_controller_spec.rb:153:in `block (3 levels) in <top (required)>'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.