# Copyright © 2012 The Pennsylvania State University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
describe 'event jobs' do
before(:each) do
@user = FactoryGirl.find_or_create(:user)
@another_user = FactoryGirl.find_or_create(:archivist)
@third_user = FactoryGirl.find_or_create(:curator)
GenericFile.any_instance.stub(:terms_of_service).and_return('1')
@gf = GenericFile.new(pid: 'test:123')
@gf.apply_depositor_metadata(@user.user_key)
@gf.title = 'Hamlet'
@gf.save
end
after(:each) do
@gf.delete
@user.delete
@another_user.delete
@third_user.delete
$redis.keys('events:*').each { |key| $redis.del key }
$redis.keys('User:*').each { |key| $redis.del key }
$redis.keys('GenericFile:*').each { |key| $redis.del key }
end
it "should log user edit profile events" do
# UserEditProfile should log the event to the editor's dashboard and his/her followers' dashboards
@another_user.follow(@user)
count_user = @user.events.length
count_another = @another_user.events.length
Time.should_receive(:now).at_least(:once).and_return(1)
event = { action: 'User jilluser@example.com has edited his or her profile', timestamp: '1' }
UserEditProfileEventJob.new(@user.user_key).run
@user.events.length.should == count_user + 1
@user.events.first.should == event
@another_user.events.length.should == count_another + 1
@another_user.events.first.should == event
end
it "should log user follow events" do
# UserFollow should log the event to the follower's dashboard, the followee's dashboard, and followers' dashboards
@third_user.follow(@user)
@user.events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = { action: 'User jilluser@example.com is now following archivist1@example.com', timestamp: '1' }
UserFollowEventJob.new(@user.user_key, @another_user.user_key).run
@user.events.length.should == 1
@user.events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
end
it "should log user unfollow events" do
# UserUnfollow should log the event to the unfollower's dashboard, the unfollowee's dashboard, and followers' dashboards
@third_user.follow(@user)
@user.follow(@another_user)
@user.events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = { action: 'User jilluser@example.com has unfollowed archivist1@example.com', timestamp: '1' }
UserUnfollowEventJob.new(@user.user_key, @another_user.user_key).run
@user.events.length.should == 1
@user.events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
end
it "should log content deposit events" do
# ContentDeposit should log the event to the depositor's profile, followers' dashboards, and the GF
@another_user.follow(@user)
@third_user.follow(@user)
User.any_instance.stub(:can?).and_return(true)
@user.profile_events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
@gf.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = {action: 'User jilluser@example.com has deposited Hamlet', timestamp: '1' }
ContentDepositEventJob.new('test:123', @user.user_key).run
@user.profile_events.length.should == 1
@user.profile_events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
@gf.events.length.should == 1
@gf.events.first.should == event
end
it "should log content update events" do
# ContentUpdate should log the event to the depositor's profile, followers' dashboards, and the GF
@another_user.follow(@user)
@third_user.follow(@user)
User.any_instance.stub(:can?).and_return(true)
@user.profile_events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
@gf.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = {action: 'User jilluser@example.com has updated Hamlet', timestamp: '1' }
ContentUpdateEventJob.new('test:123', @user.user_key).run
@user.profile_events.length.should == 1
@user.profile_events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
@gf.events.length.should == 1
@gf.events.first.should == event
end
it "should log content new version events" do
# ContentNewVersion should log the event to the depositor's profile, followers' dashboards, and the GF
@another_user.follow(@user)
@third_user.follow(@user)
User.any_instance.stub(:can?).and_return(true)
@user.profile_events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
@gf.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = {action: 'User jilluser@example.com has added a new version of Hamlet', timestamp: '1' }
ContentNewVersionEventJob.new('test:123', @user.user_key).run
@user.profile_events.length.should == 1
@user.profile_events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
@gf.events.length.should == 1
@gf.events.first.should == event
end
it "should log content restored version events" do
# ContentRestoredVersion should log the event to the depositor's profile, followers' dashboards, and the GF
@another_user.follow(@user)
@third_user.follow(@user)
User.any_instance.stub(:can?).and_return(true)
@user.profile_events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
@gf.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = {action: 'User jilluser@example.com has restored a version \'content.0\' of Hamlet', timestamp: '1' }
ContentRestoredVersionEventJob.new('test:123', @user.user_key, 'content.0').run
@user.profile_events.length.should == 1
@user.profile_events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
@gf.events.length.should == 1
@gf.events.first.should == event
end
it "should log content delete events" do
# ContentDelete should log the event to the depositor's profile and followers' dashboards
@another_user.follow(@user)
@third_user.follow(@user)
@user.profile_events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
Time.should_receive(:now).at_least(:once).and_return(1)
event = {action: 'User jilluser@example.com has deleted file \'test:123\'', timestamp: '1' }
ContentDeleteEventJob.new('test:123', @user.user_key).run
@user.profile_events.length.should == 1
@user.profile_events.first.should == event
@another_user.events.length.should == 1
@another_user.events.first.should == event
@third_user.events.length.should == 1
@third_user.events.first.should == event
end
it "should not log content-related jobs to followers who lack access" do
# No Content-related eventjobs should log an event to a follower who does not have access to the GF
@another_user.follow(@user)
@third_user.follow(@user)
@user.profile_events.length.should == 0
@another_user.events.length.should == 0
@third_user.events.length.should == 0
@gf.events.length.should == 0
@now = Time.now
Time.should_receive(:now).at_least(:once).and_return(@now)
event = {action: 'User jilluser@example.com has updated Hamlet', timestamp: @now.to_i.to_s }
ContentUpdateEventJob.new('test:123', @user.user_key).run
@user.profile_events.length.should == 1
@user.profile_events.first.should == event
@another_user.events.length.should == 0
@another_user.events.first.should be_nil
@third_user.events.length.should == 0
@third_user.events.first.should be_nil
@gf.events.length.should == 1
@gf.events.first.should == event
end
end