Created
September 11, 2012 21:19
-
-
Save theSociableme/3702148 to your computer and use it in GitHub Desktop.
Posting to api JSON Test
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
class Api::V1::BaseApiController < ApplicationController | |
include Api::V1::BaseApiHelper | |
respond_to :json | |
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
module Api::V1::BaseApiHelper | |
def render_user_json(resource) | |
render :json=> {:success=>true, :upload_credentials => resource.upload_credentials, :account_name => resource.account_name, :media_fields => resource.media_fields, :auth_token=>resource.authentication_token, :email=>resource.email, :recent_media=>resource.recent_media_urls(params[:recent_media_count])} | |
end | |
def ensure_params_exist | |
return unless params[:user].blank? | |
render :json=>{:success=>false, :message=>"missing user parameters"}, :status=>422 | |
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
Testing started at 4:30 PM ... | |
Run options: --seed 23632 | |
# Running tests: | |
FF | |
Finished tests in 0.368849s, 5.4223 tests/s, 5.4223 assertions/s. | |
1) Failure: | |
test: Set devise namespace mapping creating a valid user should create user on POST to create. (Api::V1::RegistrationsControllerTest) [/Users/mwagner72/Dropbox/rails_projects/biterator/test/functional/api/v1/registrations_controller_test.rb:21]: | |
<""> expected but was | |
<"<html><body>You are being <a href=\"https://test.host/api/v1/users.json?user[email]=email%40example.com&user[password]=secret&user[password_confirmation]=secret&user[plan]=11&user[signup_code]=coderowz11\">redirected</a>.</body></html>">. | |
2) Failure: | |
test: Set devise namespace mapping creating a valid user should respond with 200. (Api::V1::RegistrationsControllerTest) [/Users/mwagner72/.rvm/gems/ruby-1.9.2-p290@biterator-photos/gems/shoulda-2.11.3/lib/shoulda/context.rb:324]: | |
Expected response to be a 200, but was 302 | |
2 tests, 2 assertions, 2 failures, 0 errors, 0 skips | |
Process finished with exit code 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
class Api::V1::RegistrationsController < Api::V1::BaseApiController | |
before_filter :ensure_params_exist | |
def create | |
resource = User.new(params[:user]) | |
if resource.save | |
render_user_json(resource) | |
return | |
else | |
warden.custom_failure! | |
render :json=> resource.errors, :status=>422 | |
end | |
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
require 'test_helper' | |
class Api::V1::RegistrationsControllerTest < ActionController::TestCase | |
include Devise::TestHelpers | |
context "Set devise namespace mapping" do | |
setup do | |
@request.env["devise.mapping"] = Devise.mappings[:user] | |
end | |
context "creating a valid user" do | |
setup do | |
@plan = Factory(:plan) | |
post :create, {:user => { :email => 'email@example.com', :password => 'secret', :password_confirmation => 'secret', :signup_code => "coderowz11", :plan => @plan}, :format => 'json'} | |
end | |
should respond_with(:success) | |
should "create user on POST to create" do | |
assert_difference "User.count", +1 do | |
body = ActiveSupport::JSON.decode(response.body) | |
assert_equal "", body | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment