* VRFS-98; apis for join_request and related changes

This commit is contained in:
Seth Call 2012-11-30 09:30:30 -06:00
parent ea0e587d51
commit 591a3ed918
16 changed files with 407 additions and 140 deletions

View File

@ -58,7 +58,7 @@ end
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '1.4.0'
gem 'factory_girl_rails' #, '1.4.0'
gem 'cucumber-rails', '1.2.1', :require => false
gem 'database_cleaner', '0.7.0'
gem 'guard-spork', '0.3.2'

View File

@ -8,7 +8,6 @@ class ApiInvitationsController < ApiController
def initialize
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def index
@ -38,11 +37,13 @@ class ApiInvitationsController < ApiController
music_session = MusicSession.find(params[:music_session])
receiver = User.find(params[:receiver])
sender = current_user
join_request = JoinRequest.find(params[:join_request]) unless params[:join_request].nil?
@invitation = Invitation.new
@invitation.music_session = music_session
@invitation.sender = sender
@invitation.receiver = receiver
@invitation.join_request = join_request
@invitation.save

View File

@ -0,0 +1,50 @@
class ApiJoinRequestsController < ApiController
# have to be signed in currently to see this screen
before_filter :api_signed_in_user
respond_to :json
def initialize
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def index
@join_requests = JoinRequest.index(current_user)
end
def show
@join_request = JoinRequest.show(params[:id], current_user)
end
def create
music_session = MusicSession.find(params[:music_session])
text = params[:text]
sender = current_user
@join_request = JoinRequest.new
@join_request.music_session = music_session
@join_request.user = sender
@join_request.text = text
@join_request.save
if @join_request.errors.any?
response.status = :unprocessable_entity
respond_with @join_request
else
join_request_notification = @message_factory.join_request(music_session.id, @join_request.id, sender.name, text)
@mq_router.server_publish_to_session(music_session, join_request_notification)
respond_with @join_request, :responder => ApiResponder, :location => api_join_request_detail_url(@join_request)
end
end
def delete
@join_request = JoinRequest.show(params[:id], current_user)
@join_request.delete
respond_with @join_request, responder => ApiResponder
end
end

View File

@ -5,75 +5,34 @@ class ApiMusicSessionsController < ApiController
respond_to :json
def initialize
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def index
@music_sessions = MusicSession.index(current_user)
end
def create
ConnectionManager.active_record_transaction do |connection_manager|
client_id = params[:client_id]
band = Band.find(params[:band]) unless params[:band].nil?
client_id = params[:client_id]
if client_id.nil?
raise JamArgumentError, "client_id must be specified"
end
@music_session = MusicSession.new()
@music_session.creator = current_user
@music_session.description = params[:description]
@music_session.musician_access = params[:musician_access]
@music_session.approval_required = params[:approval_required]
@music_session.fan_chat = params[:fan_chat]
@music_session.fan_access = params[:fan_access]
@music_session.band = Band.find(params[:band]) unless params[:band].nil?
genres = params[:genres]
logger.debug "Genres class: " + genres.class.to_s()
unless genres.nil?
genres.each do |genre_id|
loaded_genre = Genre.find(genre_id)
@music_session.genres << loaded_genre
end
end
@music_session.save
unless @music_session.errors.any?
# auto-join this user into the newly created session
connection_manager.join_music_session(current_user.id, client_id, @music_session.id, true)
@connection = Connection.find_by_client_id(client_id)
tracks = params[:tracks]
logger.debug "Tracks class: " + tracks.class.to_s()
associate_tracks(@connection, tracks)
@connection.save
if @connection.errors.any?
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
else
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
if client_id.nil?
raise JamArgumentError, "client_id must be specified"
end
@music_session = MusicSessionManager.new.create(
current_user,
client_id,
params[:description],
params[:musician_access],
params[:approval_required],
params[:fan_chat],
params[:fan_access],
band,
params[:genres],
params[:tracks])
if @music_session.errors.any?
# we have to do this because api_session_detail_url will fail with a bad @music_session
response.status = :unprocessable_entity
respond_with @music_session
elsif @connection.errors.any?
# we have to do this because api_session_detail_url will fail with a bad @music_session
response.status = :unprocessable_entity
respond_with @connection
else
respond_with @music_session, responder: ApiResponder, :location => api_session_detail_url(@music_session)
end
@ -104,77 +63,31 @@ class ApiMusicSessionsController < ApiController
end
def participant_create
@music_session = nil
@connection = nil
ConnectionManager.active_record_transaction do |connection_manager|
@music_session = MusicSession.find(params[:id])
client_id = params[:client_id]
as_musician = params[:as_musician]
connection_manager.join_music_session(current_user.id, client_id, @music_session.id, as_musician)
@connection = Connection.find_by_client_id(client_id)
tracks = params[:tracks]
associate_tracks(@connection, tracks)
@connection.save
@connection = MusicSessionManager.new.participant_create(
current_user,
params[:id],
params[:client_id],
params[:as_musician],
params[:tracks])
if @connection.errors.any?
response.status = :unprocessable_entity
respond_with @connection
else
respond_with @connection, responder: ApiResponder, :location => api_session_participant_detail_url(@connection.client_id)
end
# send out notification to queue to the rest of the session
# TODO: also this isn't necessarily a user leaving; it's a client leaving'
user_joined = @message_factory.user_joined_music_session(@music_session.id, current_user.id, current_user.name)
@mq_router.server_publish_to_session(@music_session, user_joined, sender = {:client_id => @connection.client_id})
respond_with @connection, responder: ApiResponder, :location => api_session_participant_detail_url(@connection.client_id)
end
def participant_delete
@music_session = nil
@connection = nil
client_id = params[:id]
@connection = Connection.find_by_client_id!(client_id)
music_session = MusicSession.find(@connection.music_session_id)
ConnectionManager.active_record_transaction do |connection_manager|
@connection = Connection.find_by_client_id(params[:id])
unless @connection.nil?
@music_session = MusicSession.find(@connection.music_session_id)
end
if @connection.nil?
raise JamArgumentError, "no client found with specified client_id #{id}"
end
if @connection.user.id != current_user.id
raise PermissionError, "you do not own this connection"
end
connection_manager.leave_music_session(current_user.id, @connection.client_id, @connection.music_session_id)
end
unless @music_session.nil?
# send out notification to queue to the rest of the session
# TODO: we should rename the notification to music_session_participants_change or something
# TODO: also this isn't necessarily a user leaving; it's a client leaving'
user_left = @message_factory.user_left_music_session(@music_session.id, current_user.id, current_user.first_name + " " + current_user.last_name)
@mq_router.server_publish_to_session(@music_session, user_left, sender = {:client_id => @connection.client_id})
end
MusicSessionManager.new.participant_delete(current_user, @connection, music_session)
respond_with @connection, responder: ApiResponder
end
private
def associate_tracks(connection, tracks)
logger.debug "Tracks:"
logger.debug tracks
unless tracks.nil?
#tracks.each_pair do |index,track|
tracks.each do |track|
logger.debug "Track"
logger.debug track
instrument = Instrument.find(track["instrument_id"])
connection_track = ConnectionTrack.new
connection_track.instrument = instrument
connection_track.connection = connection
connection_track.sound = track["sound"]
connection_track.save
connection.connection_tracks << connection_track
end
end
end
end

View File

@ -1,6 +1,6 @@
object @invitation
attributes :id
attributes :id, :join_request_id
child(:sender => :sender) {
attributes :id, :name
@ -14,5 +14,8 @@ child(:music_session) {
attributes :id, :description
}
child(:join_request) {
attributes :id
}

View File

@ -0,0 +1,3 @@
object @join_request
extends "api_join_requests/join_request"

View File

@ -0,0 +1,3 @@
object @join_requests
extends "api_join_requests/join_request"

View File

@ -0,0 +1,10 @@
object @join_request
attributes :id, :text
child(:user => :user) {
attributes :id, :name
}

View File

@ -0,0 +1,3 @@
object @join_request
extends "api_join_requests/join_request"

View File

@ -17,14 +17,16 @@ child(:connections => :participants) {
}
}
node :invitations do |music_session|
end
child({:invitations => :invitations}) {
attributes :id, :sender_id
}
child({:fan_invitations => :fan_invitations}) {
attributes :id, :sender_id
}
# only show join_requests if the current_user is in the session
node(:join_requests, :if => lambda { |music_session| music_session.users.exists?(current_user) } ) do |music_session|
child(:join_requests => :join_requests) {
attributes :id, :text
child(:user => :user) {
attributes :id, :name
}
}
end

View File

@ -115,5 +115,11 @@ SampleApp::Application.routes.draw do
# search
match '/search' => 'api_search#index', :via => :get
# join requests
match '/join_requests/:id' => 'api_join_requests#show', :via => :get, :as => 'api_join_request_detail'
match '/join_requests/:id' => 'api_join_requests#delete', :via => :delete
match '/join_requests' => 'api_join_requests#create', :via => :post
match '/join_requests' => 'api_join_requests#index', :via => :get
end
end

View File

@ -0,0 +1,97 @@
require 'recaptcha'
class MusicSessionManager < BaseManager
include Recaptcha::Verify
def initialize(options={})
super(options)
@log = Logging.logger[self]
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def create(user, client_id, description, musician_access, approval_required, fan_chat, fan_access, band, genres, tracks)
return_value = nil
ActiveRecord::Base.transaction do
music_session = MusicSession.new()
music_session.creator = user
music_session.description = description
music_session.musician_access = musician_access
music_session.approval_required = approval_required
music_session.fan_chat = fan_chat
music_session.fan_access = fan_access
music_session.band = band
genres = genres
@log.debug "Genres class: " + genres.class.to_s()
unless genres.nil?
genres.each do |genre_id|
loaded_genre = Genre.find(genre_id)
music_session.genres << loaded_genre
end
end
music_session.save
unless music_session.errors.any?
# auto-join this user into the newly created session
connection = ConnectionManager.new.join_music_session(user.id, client_id, music_session.id, true, tracks)
unless connection.errors.any?
return_value = music_session
else
return_value = connection
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
else
return_value = music_session
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
end
end
return return_value
end
def participant_create(user, music_session_id, client_id, as_musician, tracks)
connection = nil
ActiveRecord::Base.transaction do
music_session = MusicSession.find(music_session_id)
as_musician = as_musician
connection = ConnectionManager.new.join_music_session(user.id, client_id, music_session.id, as_musician, tracks)
if connection.errors.any?
# rollback the transaction to make sure nothing is disturbed in the database
raise ActiveRecord::Rollback
else
# send out notification to queue to the rest of the session
# TODO: also this isn't necessarily a user leaving; it's a client leaving'
user_joined = @message_factory.user_joined_music_session(connection.music_session.id, user.id, user.name)
@mq_router.server_publish_to_session(connection.music_session, user_joined, sender = {:client_id => connection.client_id})
end
end
return connection
end
def participant_delete(user, connection, music_session)
if connection.user.id != user.id
raise PermissionError, "you do not own this connection"
end
ConnectionManager.new.leave_music_session(user.id, connection.client_id, connection.music_session.id)
unless music_session.nil?
# send out notification to queue to the rest of the session
# TODO: we should rename the notification to music_session_participants_change or something
# TODO: also this isn't necessarily a user leaving; it's a client leaving'
user_left = @message_factory.user_left_music_session(music_session.id, user.id, user.first_name + " " + user.last_name)
@mq_router.server_publish_to_session(music_session, user_left, sender = {:client_id => connection.client_id})
end
end
end

View File

@ -11,6 +11,13 @@ FactoryGirl.define do
factory :admin do
admin true
end
factory :single_user_session do
after(:create) do |user, evaluator|
music_session = FactoryGirl.create(:music_session, :creator => user)
connection = FactoryGirl.create(:connection, :user => user, :music_session => music_session)
end
end
end
factory :fan, :class => "JamRuby::User" do
@ -33,6 +40,7 @@ FactoryGirl.define do
factory :connection, :class => JamRuby::Connection do
sequence(:client_id) { |n| "Client#{n}" }
as_musician true
end
factory :friendship, :class => JamRuby::Friendship do
@ -46,4 +54,8 @@ FactoryGirl.define do
factory :band, :class => JamRuby::Band do
end
factory :join_request, :class => JamRuby::JoinRequest do
text 'let me in to the session!'
end
end

View File

@ -0,0 +1,81 @@
require 'spec_helper'
describe "Join Request API", :type => :api do
include Rack::Test::Methods
let(:user) { FactoryGirl.create(:single_user_session) }
let(:other) { FactoryGirl.create(:user) }
let(:other2) { FactoryGirl.create(:user) }
before(:each) do
end
def login(user)
# login as fan
post '/api/auth_session.json', { :email => user.email, :password => user.password }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
JSON.parse(last_response.body).should == { "success" => true }
end
it "create join_request" do
login(other)
post '/api/join_requests.json', { :music_session => user.music_sessions[0].id, :text => "lemme in" }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 201
get last_response.headers["Location"] + '.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
join_request = JSON.parse(last_response.body)
join_request["id"].should_not be_nil
join_request["text"].should == "lemme in"
join_request["user"]["id"].should == other.id
# verify that fetching the music session by the requesting user (isn't in the session!) doesn't show any join_requests
# that's right--they don't even see their own join_requset via searching for sessions
# if a user wants to see their outstanding join_requests, they should fetch GET /api/join_requests
get "/api/sessions/#{user.music_sessions[0].id}.json", "CONTENT_TYPE" => 'application/json'
music_session = JSON.parse(last_response.body)
music_session["join_requests"].should be_nil
# verify that the user can see their own join_requests with the listing of join_requests
get '/api/join_requests', "CONTENT_TYPE" => 'application/json'
join_requests = JSON.parse(last_response.body)
join_requests.length.should == 1
join_requests[0]["id"].should == join_request["id"]
# verify that fetching the music session reflects the join_request by a user *in the session*
login(user)
get "/api/sessions/#{user.music_sessions[0].id}.json", "CONTENT_TYPE" => 'application/json'
music_session = JSON.parse(last_response.body)
music_session["join_requests"].length.should == 1
music_session["join_requests"][0]["id"].should == join_request["id"]
# verify that the user in the session can see it when queried directly
get "/api/join_requests/#{join_request["id"]}.json", "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
JSON.parse(last_response.body).should == join_request
# verify that the random user can't access it
login(other2)
get "/api/join_requests/#{join_request["id"]}.json", "CONTENT_TYPE" => 'application/json'
last_response.status.should == 404
end
it "delete join_request" do
login(other)
post '/api/join_requests.json', { :music_session => user.music_sessions[0].id, :text => "lemme in" }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 201
join_request = JSON.parse(last_response.body)
# delete it; should work since this user created it
delete "/api/join_requests/#{join_request["id"]}.json", "CONTENT_TYPE" => 'application/json'
last_response.status.should == 204
# check that we now get a 404
get "/api/join_requests/#{join_request["id"]}.json", "CONTENT_TYPE" => 'application/json'
last_response.status.should == 404
end
end

View File

@ -39,11 +39,12 @@ describe "Music Session API ", :type => :api do
get '/api/sessions.json'
music_sessions = JSON.parse(last_response.body)
music_session = music_sessions[0]
music_session["id"].should == music_session["id"]
music_session["musician_access"].should be_true
music_session["invitations"].should == []
music_session["fan_invitations"].should == []
music_session["join_requests"].should == []
music_session["approval_required"].should be_false
music_session["fan_chat"].should be_true
music_session["fan_access"].should be_true
@ -256,7 +257,6 @@ describe "Music Session API ", :type => :api do
music_session["id"].should == session["id"]
music_session["musician_access"].should == false
music_session["invitations"].should == []
music_session["fan_invitations"].should == []
music_session["participants"].length.should == 1
participant = music_session["participants"][0]
participant["ip_address"].should == client.ip_address
@ -290,9 +290,7 @@ describe "Music Session API ", :type => :api do
music_session["invitations"].length.should == 1
invitation = music_session["invitations"][0]
invitation["id"].should == invitation_model.id
music_session["fan_invitations"].should == []
music_session["participants"].length.should == 1
participant = music_session["participants"][0]
participant["client_id"].should == client.client_id
@ -309,7 +307,6 @@ describe "Music Session API ", :type => :api do
client = FactoryGirl.create(:connection, :user => user, :ip_address => "1.1.1.1", :client_id => "3")
post '/api/sessions.json', defopts.merge({:client_id => client.client_id, :musician_access => false}).to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(201)
session = JSON.parse(last_response.body)
@ -321,10 +318,10 @@ describe "Music Session API ", :type => :api do
# users are friends, but no invitation... so we shouldn't be able to join as user 2
login(user2)
post "/api/sessions/#{session["id"]}/participants.json", { :client_id => client2.client_id, :as_musician => true }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(500)
post "/api/sessions/#{session["id"]}/participants.json", { :client_id => client2.client_id, :as_musician => true, :tracks => [{"instrument_id" => "bass guitar", "sound" => "mono"}] }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(422)
join_response = JSON.parse(last_response.body)
join_response["type"].should == "PermissionError"
join_response["errors"]["musician_access"].should == [Connection::INVITE_REQUIRED]
# but let's make sure if we then invite, that we can then join'
login(user)
@ -332,7 +329,91 @@ describe "Music Session API ", :type => :api do
last_response.status.should eql(201)
login(user2)
post "/api/sessions/#{session["id"]}/participants.json", { :client_id => client2.client_id, :as_musician => true }.to_json, "CONTENT_TYPE" => 'application/json'
post "/api/sessions/#{session["id"]}/participants.json", { :client_id => client2.client_id, :as_musician => true, :tracks => [{"instrument_id" => "bass guitar", "sound" => "mono"}] }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(201)
end
it "join_requests don't show up in session listing" do
client = FactoryGirl.create(:connection, :user => user, :ip_address => "1.1.1.1", :client_id => "1")
post '/api/sessions.json', defopts.merge({:client_id => client.client_id}).to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(201)
get last_response.headers["Location"]
music_session = JSON.parse(last_response.body)
get '/api/sessions.json'
music_sessions = JSON.parse(last_response.body)
music_session = music_sessions[0]
music_session["id"].should == music_session["id"]
music_session["join_requests"].should == []
user2 = FactoryGirl.create(:user) # in the music session
login(user2)
# create join request
post '/api/join_requests.json', { :music_session => music_session["id"], :text => "lemme in" }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 201
get last_response.headers["Location"] + ".json", "CONTENT_TYPE" => 'application/json'
join_request = JSON.parse(last_response.body)
# now join_requsets should still be empty, because we don't share join_requests to people outside the session
get '/api/sessions.json'
music_sessions = JSON.parse(last_response.body)
music_session = music_sessions[0]
music_session["id"].should == music_session["id"]
music_session["join_requests"].should be_nil
login(user)
# but for people in the session, they can see the join_requests!
get '/api/sessions.json'
music_sessions = JSON.parse(last_response.body)
music_session = music_sessions[0]
music_session["id"].should == music_session["id"]
music_session["join_requests"].length.should == 1
music_session["join_requests"][0]["id"].should == join_request["id"]
end
it "should now allow join of approval_required=true session" do
client = FactoryGirl.create(:connection, :user => user, :ip_address => "1.1.1.1", :client_id => "1")
post '/api/sessions.json', defopts.merge({:client_id => client.client_id, :approval_required => true}).to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(201)
# now fetch it's data
get last_response.headers["Location"]
music_session = JSON.parse(last_response.body)
# try to add 2nd user to session - should fail because approval is required
user2 = FactoryGirl.create(:user)
client2 = FactoryGirl.create(:connection, :user => user2, :ip_address => "2.2.2.2")
login(user2)
post "/api/sessions/#{music_session["id"]}/participants.json", { :client_id => client2.client_id, :as_musician => true, :tracks => [{"instrument_id" => "bass guitar", "sound" => "mono"}]}.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(422)
rejected_join_attempt = JSON.parse(last_response.body)
rejected_join_attempt["errors"]["approval_required"] = [Connection::INVITE_REQUIRED]
# now send up a join_request to try and get in
post '/api/join_requests.json', { :music_session => music_session["id"], :text => "lemme in" }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 201
get last_response.headers["Location"] + ".json", "CONTENT_TYPE" => 'application/json'
join_request = JSON.parse(last_response.body)
# pop back to user1 and allow user2 to get in
login(user)
post '/api/invitations.json', {:music_session => music_session["id"], :receiver => user2.id, :join_request => join_request["id"]}.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(201)
# finally, go back to user2 and attempt to join again
login(user2)
post "/api/sessions/#{music_session["id"]}/participants.json", { :client_id => client2.client_id, :as_musician => true, :tracks => [{"instrument_id" => "bass guitar", "sound" => "mono"}]}.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(201)
end
end

View File

@ -32,6 +32,8 @@ require 'rspec/autorun'
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)
RSpec.configure do |config|
# ## Mock Framework
#