From 6906e4454ab3d0ca289d2ad43f8414ef692972c1 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 5 Jan 2015 16:53:43 -0600 Subject: [PATCH] * VRFS-2601 - REST API to return keys --- ruby/lib/jam_ruby/models/jam_track_right.rb | 10 ++++ .../jam_ruby/models/jam_track_right_spec.rb | 26 +++++++++ .../controllers/api_jam_tracks_controller.rb | 13 +++++ web/app/views/api_jam_tracks/list_keys.rabl | 9 ++++ web/config/routes.rb | 1 + .../api_jam_tracks_controller_spec.rb | 54 +++++++++++++++++++ 6 files changed, 113 insertions(+) create mode 100644 web/app/views/api_jam_tracks/list_keys.rabl diff --git a/ruby/lib/jam_ruby/models/jam_track_right.rb b/ruby/lib/jam_ruby/models/jam_track_right.rb index 1593971a8..fbd97bc78 100644 --- a/ruby/lib/jam_ruby/models/jam_track_right.rb +++ b/ruby/lib/jam_ruby/models/jam_track_right.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/jam_track_right_spec.rb b/ruby/spec/jam_ruby/models/jam_track_right_spec.rb index 6670ba996..2936fd8d4 100644 --- a/ruby/spec/jam_ruby/models/jam_track_right_spec.rb +++ b/ruby/spec/jam_ruby/models/jam_track_right_spec.rb @@ -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 diff --git a/web/app/controllers/api_jam_tracks_controller.rb b/web/app/controllers/api_jam_tracks_controller.rb index cd21ecbf6..959d0f4ae 100644 --- a/web/app/controllers/api_jam_tracks_controller.rb +++ b/web/app/controllers/api_jam_tracks_controller.rb @@ -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 diff --git a/web/app/views/api_jam_tracks/list_keys.rabl b/web/app/views/api_jam_tracks/list_keys.rabl new file mode 100644 index 000000000..43a33b24e --- /dev/null +++ b/web/app/views/api_jam_tracks/list_keys.rabl @@ -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 \ No newline at end of file diff --git a/web/config/routes.rb b/web/config/routes.rb index e633e339f..962c18c62 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -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 diff --git a/web/spec/controllers/api_jam_tracks_controller_spec.rb b/web/spec/controllers/api_jam_tracks_controller_spec.rb index 2459e5bcf..b6ae5932b 100644 --- a/web/spec/controllers/api_jam_tracks_controller_spec.rb +++ b/web/spec/controllers/api_jam_tracks_controller_spec.rb @@ -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