* VRFS-200; invitation to service REST APIs done
This commit is contained in:
parent
7d62c292cb
commit
9666c9e9f8
|
|
@ -0,0 +1,32 @@
|
|||
class ApiInvitedUsersController < ApiController
|
||||
|
||||
# have to be signed in currently to see this screen
|
||||
before_filter :api_signed_in_user
|
||||
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
@invited_users = InvitedUser.index(current_user)
|
||||
end
|
||||
|
||||
def show
|
||||
@invited_user = InvitedUser.find(params[:id])
|
||||
end
|
||||
|
||||
def create
|
||||
@invited_user = InvitedUser.new
|
||||
@invited_user.sender = current_user
|
||||
@invited_user.email = params[:email]
|
||||
@invited_user.autofriend = true
|
||||
@invited_user.note = params[:note].blank? ? nil : params[:note]
|
||||
@invited_user.save
|
||||
|
||||
unless @invited_user.errors.any?
|
||||
respond_with @invited_user, :responder => ApiResponder, :location => api_invited_user_detail_url(@invited_user)
|
||||
else
|
||||
response.status = :unprocessable_entity
|
||||
respond_with @invited_user
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object @invited_user
|
||||
|
||||
extends "api_invited_users/invited_user"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object @invited_users
|
||||
|
||||
extends "api_invited_users/invited_user"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object @invited_user
|
||||
|
||||
attributes :id, :created_at, :updated_at, :email, :note, :accepted
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object @invited_user
|
||||
|
||||
extends "api_invited_users/invited_user"
|
||||
|
|
@ -38,6 +38,7 @@ module SampleApp
|
|||
|
||||
# Activate observers that should always be running.
|
||||
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
||||
config.active_record.observers = "JamRuby::InvitedUserObserver"
|
||||
|
||||
# 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.
|
||||
|
|
|
|||
|
|
@ -169,6 +169,11 @@ SampleApp::Application.routes.draw do
|
|||
match '/invitations' => 'api_invitations#index', :via => :get
|
||||
match '/invitations' => 'api_invitations#create', :via => :post
|
||||
|
||||
# invited users
|
||||
match '/invited_users/:id' => 'api_invited_users#show', :via => :get, :as => 'api_invited_user_detail'
|
||||
match '/invited_users' => 'api_invited_users#index', :via => :get
|
||||
match '/invited_users' => 'api_invited_users#create', :via => :post
|
||||
|
||||
# instruments
|
||||
match '/instruments/:id' => 'api_instruments#show', :via => :get, :as => 'api_instrument_detail'
|
||||
match '/instruments' => 'api_instruments#index', :via => :get
|
||||
|
|
|
|||
|
|
@ -0,0 +1,136 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe "Invited Users API ", :type => :api do
|
||||
|
||||
include Rack::Test::Methods
|
||||
|
||||
subject { page }
|
||||
|
||||
def login(email, password, http_code, success)
|
||||
# login as fan
|
||||
post '/api/auth_session.json', { :email => email, :password => password }.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should == http_code
|
||||
JSON.parse(last_response.body).should == { "success" => success }
|
||||
end
|
||||
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
|
||||
describe "api" do
|
||||
|
||||
before do
|
||||
InvitedUser.delete_all
|
||||
UserMailer.deliveries.clear
|
||||
|
||||
login(user.email, user.password, 200, true)
|
||||
end
|
||||
|
||||
it "create with no note" do
|
||||
post '/api/invited_users.json', {:email => 'tester@jamkazam.com'}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(201)
|
||||
|
||||
UserMailer.deliveries.length.should == 1
|
||||
|
||||
# now fetch it's data
|
||||
location_header = last_response.headers["Location"]
|
||||
get location_header
|
||||
|
||||
# parse json and test
|
||||
body = JSON.parse(last_response.body)
|
||||
body["id"].should_not be_nil
|
||||
body["created_at"].should_not be_nil
|
||||
body["email"].should == "tester@jamkazam.com"
|
||||
body["accepted"].should be_false
|
||||
body["note"].should be_nil
|
||||
end
|
||||
|
||||
it "create with a note" do
|
||||
post '/api/invited_users.json', {:email => 'tester@jamkazam.com', :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(201)
|
||||
|
||||
# now fetch it's data
|
||||
location_header = last_response.headers["Location"]
|
||||
get location_header
|
||||
|
||||
body = JSON.parse(last_response.body)
|
||||
body["id"].should_not be_nil
|
||||
body["created_at"].should_not be_nil
|
||||
body["email"].should == "tester@jamkazam.com"
|
||||
body["accepted"].should be_false
|
||||
body["note"].should_not be_nil
|
||||
end
|
||||
|
||||
it "cant create when not invited" do
|
||||
user.can_invite = false
|
||||
user.save
|
||||
|
||||
post '/api/invited_users.json', {:email => 'tester@jamkazam.com', :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(422)
|
||||
body = JSON.parse(last_response.body)
|
||||
body["errors"].should_not be_nil
|
||||
body["errors"]["sender"].length.should == 1
|
||||
end
|
||||
|
||||
it "cant create with no email" do
|
||||
post '/api/invited_users.json', {:note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(422)
|
||||
body = JSON.parse(last_response.body)
|
||||
body["errors"].should_not be_nil
|
||||
body["errors"]["email"].length.should == 2
|
||||
end
|
||||
|
||||
it "cant create with blank email" do
|
||||
post '/api/invited_users.json', {:email => "", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(422)
|
||||
body = JSON.parse(last_response.body)
|
||||
body["errors"].should_not be_nil
|
||||
body["errors"]["email"].length.should == 2
|
||||
end
|
||||
|
||||
it "cant create with invalid email" do
|
||||
post '/api/invited_users.json', {:email => "blurp", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(422)
|
||||
body = JSON.parse(last_response.body)
|
||||
body["errors"].should_not be_nil
|
||||
body["errors"]["email"].length.should == 1
|
||||
end
|
||||
|
||||
it "list" do
|
||||
post '/api/invited_users.json', {:email => "tester@jamkazam.com", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(201)
|
||||
|
||||
# now fetch it's data
|
||||
location_header = last_response.headers["Location"]
|
||||
get location_header
|
||||
|
||||
body = JSON.parse(last_response.body)
|
||||
id = body["id"]
|
||||
|
||||
get '/api/invited_users.json', "CONTENT_TYPE" => 'application/json'
|
||||
|
||||
last_response.status.should eql(200)
|
||||
|
||||
body = JSON.parse(last_response.body)
|
||||
body.length.should == 1
|
||||
body[0]["id"].should == id
|
||||
end
|
||||
|
||||
it "show" do
|
||||
post '/api/invited_users.json', {:email => "tester@jamkazam.com", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
|
||||
last_response.status.should eql(201)
|
||||
|
||||
# now fetch it's data
|
||||
location_header = last_response.headers["Location"]
|
||||
get location_header
|
||||
|
||||
body = JSON.parse(last_response.body)
|
||||
id = body["id"]
|
||||
|
||||
get "/api/invited_users/#{id}.json", "CONTENT_TYPE" => 'application/json'
|
||||
|
||||
last_response.status.should eql(200)
|
||||
|
||||
body = JSON.parse(last_response.body)
|
||||
body["id"].should == id
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue