VRFS-2694 user presence model

This commit is contained in:
Brian Smith 2015-02-05 03:34:25 -05:00
parent 3cdcadda94
commit 33aef8a380
4 changed files with 52 additions and 18 deletions

View File

@ -246,4 +246,5 @@ text_message_migration.sql
user_model_about_changes.sql
performance_samples.sql
user_presences.sql
discard_scores_optimized.sql
discard_scores_optimized.sql
alter_type_columns.sql

View File

@ -0,0 +1,2 @@
ALTER TABLE user_presences RENAME COLUMN type to service_type;
ALTER TABLE performance_samples RENAME COLUMN type to service_type;

View File

@ -3,10 +3,10 @@ module JamRuby
PERMISSION_MSG = "You do not have permission to perform this operation"
attr_accessible :user_id, :type, :username
attr_accessible :user_id, :service_type, :username
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
validates :type, presence:true, length: {maximum: 100}
validates :service_type, presence:true, length: {maximum: 100}
validates :username, presence:true, length: {maximum: 100}
def self.index(options = {})
@ -15,26 +15,38 @@ module JamRuby
end
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?
auth_user(current_user, options)
raise StateError, "Missing required information" if options[:service_type].nil? || options[:username].nil?
u = UserPresence.new({:user_id => current_user.id, :type => options[:type], :username => options[:username]})
u.save!
up = UserPresence.new({:user_id => current_user.id, :service_type => options[:service_type], :username => options[:username]})
up.save!
end
def self.update(current_user, options = {})
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
auth_user(current_user, options)
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
end
def self.destroy(current_user, options = {})
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
private
def self.auth_user(current_user, options={})
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
end
end
end

View File

@ -6,14 +6,15 @@ describe UserPresence do
let(:user2) { FactoryGirl.create(:user) }
describe "index" do
before(:all) do
@user1_presence1 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :type => "facebook")
@user1_presence1 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :service_type => "facebook")
@user1_presence1.save!
@user1_presence2 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :type => "twitter")
@user1_presence2 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :service_type => "twitter")
@user1_presence2.save!
@user2_presence1 = UserPresence.new(:user_id => user2.id, :username => "myonlineusername", :type => "soundcloud")
@user2_presence1 = UserPresence.new(:user_id => user2.id, :username => "myonlineusername", :service_type => "soundcloud")
@user2_presence1.save!
end
@ -41,16 +42,16 @@ describe UserPresence 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"})
UserPresence.create(user1, {:user_id => user1.id, :service_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"})
UserPresence.create(user1, {:user_id => user1.id, :service_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)
lambda{UserPresence.create(user1, {:user_id => user2.id, :service_type => "soundcloud", :username => "soundclouduser2"})}.should raise_error(PermissionError)
end
it "should raise error if type is missing" do
@ -58,14 +59,32 @@ describe UserPresence do
end
it "should raise error if username is missing" do
lambda{UserPresence.create(user1, {:user_id => user1.id, :type => "soundcloud"})}.should raise_error(StateError)
lambda{UserPresence.create(user1, {:user_id => user1.id, :service_type => "soundcloud"})}.should raise_error(StateError)
end
end
end
describe "update" 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 save 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.update(user1, {:id => @user_presence.id, :user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser2"})
up_list = UserPresence.index({:id => user1.id})
up_list.count.should == 1
up_list.first.service_type.should == "soundcloud"
up_list.first.username.should == "soundclouduser2"
end
end