* VRFS-2601 - REST API to return keys

This commit is contained in:
Seth Call 2015-01-05 16:53:43 -06:00
parent d677b43c28
commit 6906e4454a
6 changed files with 113 additions and 0 deletions

View File

@ -80,6 +80,16 @@ module JamRuby
self.download_count = self.download_count + count
self.last_downloaded_at = Time.now
end
def self.list_keys(user, jamtracks)
if jamtracks.nil?
return []
end
JamTrack.select('jam_tracks.id, jam_track_rights.private_key AS private_key, jam_track_rights.id AS jam_track_right_id')
.joins("LEFT OUTER JOIN jam_track_rights ON jam_tracks.id = jam_track_rights.jam_track_id AND jam_track_rights.user_id = '#{user.id}'")
.where('jam_tracks.id IN (?)', jamtracks)
end
end
end

View File

@ -97,5 +97,31 @@ describe JamTrackRight do
end
end
describe "list_keys" do
let(:user) {FactoryGirl.create(:user)}
it "empty" do
JamTrackRight.list_keys(user, nil).should eq([])
end
it "bogus key" do
JamTrackRight.list_keys(user, ['a']).should eq([])
end
it "valid track with no rights to it by querying user" do
jam_track = FactoryGirl.create(:jam_track)
keys = JamTrackRight.list_keys(user, [jam_track.id])
keys.length.should == 0
end
it "valid track with rights to it by querying user" do
jam_track_right = FactoryGirl.create(:jam_track_right, private_key: 'keyabc')
keys = JamTrackRight.list_keys(jam_track_right.user, [jam_track_right.jam_track.id])
keys.length.should == 1
keys[0].id.should == jam_track_right.jam_track.id
keys[0]['private_key'].should eq('keyabc')
end
end
end

View File

@ -36,6 +36,19 @@ class ApiJamTracksController < ApiController
end
end
def keys
jamtrack_ids = params[:jamtracks]
unless jamtrack_ids.kind_of?(Array)
render :json => {message: 'jamtracks parameter must be an array'}, :status => 200
return
end
@jam_tracks = JamTrackRight.list_keys(current_user, params[:jamtracks])
render "api_jam_tracks/list_keys", :layout => nil
end
private
def lookup_jam_track_right
@jam_track_right = JamTrackRight.where("jam_track_id=? AND user_id=?", params[:id], current_user.id).first

View File

@ -0,0 +1,9 @@
object @jam_tracks
node do |jam_track|
{
id: jam_track['id'],
private: jam_track['private_key'],
error: jam_track['private_key'] ? nil : ( jam_track['jam_track_right_id'] ? 'no_key' : 'not_purchased' )
}
end

View File

@ -194,6 +194,7 @@ SampleApp::Application.routes.draw do
match '/jamtracks' => 'api_jam_tracks#index', :via => :get, :as => 'api_jam_tracks_list'
match '/jamtracks/downloads' => 'api_jam_tracks#downloads', :via => :get, :as => 'api_jam_tracks_downloads'
match '/jamtracks/download/:id' => 'api_jam_tracks#download', :via => :get, :as => 'api_jam_tracks_download'
match '/jamtracks/keys' => 'api_jam_tracks#keys', :via => :post, :as => 'api_jam_tracks_keys'
# Shopping carts
match '/shopping_carts/add_jamtrack' => 'api_shopping_carts#add_jamtrack', :via => :post

View File

@ -110,4 +110,58 @@ describe ApiJamTracksController do
notifications.count.should == 1
end
end
describe "keys" do
it "empty" do
get :keys, jamtracks: []
response.status.should == 200
json = JSON.parse(response.body)
json.length == 0
end
it "track with no rights" do
get :keys, jamtracks: [@jam_track.id]
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
puts json.inspect
json[0]['id'].should == @jam_track.id
json[0]['private'].should be_nil
json[0]['error'].should == 'not_purchased'
end
it "track with no key" do
right = FactoryGirl.create(:jam_track_right, user: @user, private_key: nil, jam_track: @jam_track)
get :keys, jamtracks: [@jam_track.id]
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
json[0]['id'].should == @jam_track.id
json[0]['private'].should be_nil
json[0]['error'].should == 'no_key'
end
it "track with key" do
right = FactoryGirl.create(:jam_track_right, user: @user, private_key: 'abc', jam_track: @jam_track)
get :keys, jamtracks: [@jam_track.id]
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
json[0]['id'].should == @jam_track.id
json[0]['private'].should eq('abc')
json[0]['error'].should be_nil
end
it "non-owning user asking for a real track" do
right = FactoryGirl.create(:jam_track_right, user: FactoryGirl.create(:user), private_key: 'abc', jam_track: @jam_track)
get :keys, jamtracks: [@jam_track.id]
response.status.should == 200
json = JSON.parse(response.body)
json[0]['id'].should == @jam_track.id
json[0]['private'].should be_nil
json[0]['error'].should == 'not_purchased'
end
end
end