VRFS-2694 wip user presence model and spec

This commit is contained in:
Brian Smith 2015-02-03 23:37:20 -05:00
parent 80305c05aa
commit 05ba1ea5d3
2 changed files with 46 additions and 6 deletions

View File

@ -1,6 +1,8 @@
module JamRuby
class UserPresence < ActiveRecord::Base
PERMISSION_MSG = "You do not have permission to perform this operation"
attr_accessible :user_id, :type, :username
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
@ -12,14 +14,27 @@ module JamRuby
UserPresence.where("user_id = ?", options[:id])
end
def self.save(current_user, options = {})
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
UserPresence.create(:user => current_user, :type => options[:type], :username => options[:username])
def self.create(current_user, options = {})
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
raise StateError, "Missing required information" if options[:type].nil? || options[:username].nil?
u = UserPresence.new({:user_id => current_user.id, :type => options[:type], :username => options[:username]})
u.save!
end
def self.update(current_user, options = {})
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
end
def self.destroy(current_user, options = {})
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
UserPresence.destroy_all("id = ?", options[:id])
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
id = options[:id]
user_presence = UserPresence.find(user_presence)
unless user_presence.nil?
raise PermissionError, PERMISSION_MSG if user_presence.user_id != current_user.id
UserPresence.destroy(id)
end
end
end
end

View File

@ -38,7 +38,32 @@ describe UserPresence do
end
end
describe "save" do
describe "create" do
context "when request is valid" do
it "should save successfully" do
UserPresence.create(user1, {:user_id => user1.id, :type => "soundcloud", :username => "soundclouduser1"})
# make sure we can save a second UserPresence for same user and type
UserPresence.create(user1, {:user_id => user1.id, :type => "soundcloud", :username => "soundclouduser2"})
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.create(user1, {:user_id => user2.id, :type => "soundcloud", :username => "soundclouduser2"})}.should raise_error(PermissionError)
end
it "should raise error if type is missing" do
lambda{UserPresence.create(user1, {:user_id => user1.id, :username => "soundclouduser2"})}.should raise_error(StateError)
end
it "should raise error if username is missing" do
lambda{UserPresence.create(user1, {:user_id => user1.id, :type => "soundcloud"})}.should raise_error(StateError)
end
end
end
describe "update" do
context "when request is valid" do
it "should save successfully" do
end