From e3f27eeebcc11756564ae59c81bfd80b706326a5 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 10 Feb 2015 02:44:16 -0500 Subject: [PATCH] VRFS-2694 user presence model tests --- ruby/lib/jam_ruby/models/user_presence.rb | 28 ++++++++++--------- .../jam_ruby/models/user_presence_spec.rb | 24 ++++++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/ruby/lib/jam_ruby/models/user_presence.rb b/ruby/lib/jam_ruby/models/user_presence.rb index e4b6cce03..a225039ed 100644 --- a/ruby/lib/jam_ruby/models/user_presence.rb +++ b/ruby/lib/jam_ruby/models/user_presence.rb @@ -16,7 +16,7 @@ module JamRuby def self.create(current_user, options = {}) auth_user(current_user, options) - raise StateError, "Missing required information" if options[:service_type].nil? || options[:username].nil? + raise StateError, "Missing required information" if options[:service_type].blank? || options[:username].blank? up = UserPresence.new({:user_id => current_user.id, :service_type => options[:service_type], :username => options[:username]}) up.save! @@ -24,22 +24,24 @@ module JamRuby def self.update(current_user, options = {}) auth_user(current_user, options) + raise StateError, "Missing required information" if options[:service_type].blank? || options[:username].blank? || options[:id].blank? - id = options[:id] - if options[:username].nil? - UserPresence.destroy(id) - else - up = UserPresence.find(options[:id]) - up.service_type = options[:service_type] - up.username = options[:username] - up.save! - end + up = UserPresence.find(options[:id]) + up.service_type = options[:service_type] + up.username = options[:username] + up.save! end - def self.destroy(current_user, options = {}) - user_presence = UserPresence.find(user_presence) + def self.delete(current_user, options = {}) + id = options[:id] + raise StateError, "Missing required information" if id.blank? + user_presence = UserPresence.find(id) + + if user_presence.user_id != current_user.id + raise PermissionError, PERMISSION_MSG + end + unless user_presence.nil? - raise PermissionError, PERMISSION_MSG if user_presence.user_id != current_user.id UserPresence.destroy(id) end end diff --git a/ruby/spec/jam_ruby/models/user_presence_spec.rb b/ruby/spec/jam_ruby/models/user_presence_spec.rb index e92f28ff3..a3a8fc43d 100644 --- a/ruby/spec/jam_ruby/models/user_presence_spec.rb +++ b/ruby/spec/jam_ruby/models/user_presence_spec.rb @@ -90,27 +90,51 @@ describe UserPresence do context "when request is not valid" do it "should raise PermissionError if requester id does not match id in request" do + lambda{UserPresence.update(user1, {:user_id => user2.id, :id => @user_presence.id, :service_type => "soundcloud", :username => "soundclouduser2"})}.should raise_error(PermissionError) end it "should raise error if type is missing" do + lambda{UserPresence.update(user1, {:user_id => user1.id, :id => @user_presence.id, :username => "soundclouduser2"})}.should raise_error(StateError) end it "should raise error if username is missing" do + lambda{UserPresence.update(user1, {:user_id => user1.id, :id => @user_presence.id, :service_type => "soundcloud"})}.should raise_error(StateError) + end + + it "should raise error if user presence id is missing" do + lambda{UserPresence.update(user1, {:user_id => user1.id, :username => "soundclouduser2", :service_type => "soundcloud"})}.should raise_error(StateError) end end end describe "destroy" do + + before(:all) do + @user_presence = UserPresence.new(:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1") + @user_presence.save! + end + context "when request is valid" do it "should destroy successfully" do + up_list = UserPresence.index({:id => user1.id}) + up_list.count.should == 1 + up_list.first.service_type.should == "soundcloud" + up_list.first.username.should == "soundclouduser1" + + UserPresence.delete(user1, {:user_id => user1.id, :id => @user_presence.id}) + + up_list = UserPresence.index({:id => user1.id}) + up_list.count.should == 0 end end context "when request is not valid" do it "should raise PermissionError if requester id does not match id in request" do + lambda{UserPresence.delete(user2, {:user_id => user1.id, :id => @user_presence.id})}.should raise_error(PermissionError) end it "should raise error if user presence id is missing" do + lambda{UserPresence.delete(user1, {:user_id => user1.id})}.should raise_error(StateError) end end end