Commit d12b910d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'remove-observers' into 'master'

Remove observers from application

Part of #1133
parents 88659eeb 9ef5898e
...@@ -11,7 +11,6 @@ end ...@@ -11,7 +11,6 @@ end
gem "rails", "~> 4.1.0" gem "rails", "~> 4.1.0"
gem "protected_attributes" gem "protected_attributes"
gem 'rails-observers'
# Make links from text # Make links from text
gem 'rails_autolink', '~> 1.1' gem 'rails_autolink', '~> 1.1'
......
...@@ -365,8 +365,6 @@ GEM ...@@ -365,8 +365,6 @@ GEM
bundler (>= 1.3.0, < 2.0) bundler (>= 1.3.0, < 2.0)
railties (= 4.1.1) railties (= 4.1.1)
sprockets-rails (~> 2.0) sprockets-rails (~> 2.0)
rails-observers (0.1.2)
activemodel (~> 4.0)
rails_autolink (1.1.6) rails_autolink (1.1.6)
rails (> 3.1) rails (> 3.1)
rails_best_practices (1.14.4) rails_best_practices (1.14.4)
...@@ -644,7 +642,6 @@ DEPENDENCIES ...@@ -644,7 +642,6 @@ DEPENDENCIES
rack-cors rack-cors
rack-mini-profiler rack-mini-profiler
rails (~> 4.1.0) rails (~> 4.1.0)
rails-observers
rails_autolink (~> 1.1) rails_autolink (~> 1.1)
rails_best_practices rails_best_practices
raphael-rails (~> 2.1.2) raphael-rails (~> 2.1.2)
......
...@@ -21,7 +21,7 @@ class Projects::NotesController < Projects::ApplicationController ...@@ -21,7 +21,7 @@ class Projects::NotesController < Projects::ApplicationController
end end
def create def create
@note = Notes::CreateService.new(project, current_user, params).execute @note = Notes::CreateService.new(project, current_user, params[:note]).execute
respond_to do |format| respond_to do |format|
format.json { render_note_json(@note) } format.json { render_note_json(@note) }
......
...@@ -98,8 +98,7 @@ class ProjectsController < ApplicationController ...@@ -98,8 +98,7 @@ class ProjectsController < ApplicationController
def destroy def destroy
return access_denied! unless can?(current_user, :remove_project, project) return access_denied! unless can?(current_user, :remove_project, project)
project.team.truncate ::Projects::DestroyService.new(@project, current_user, {}).execute
project.destroy
respond_to do |format| respond_to do |format|
format.html { redirect_to root_path } format.html { redirect_to root_path }
......
...@@ -41,6 +41,9 @@ class Event < ActiveRecord::Base ...@@ -41,6 +41,9 @@ class Event < ActiveRecord::Base
# For Hash only # For Hash only
serialize :data serialize :data
# Callbacks
after_create :reset_project_activity
# Scopes # Scopes
scope :recent, -> { order("created_at DESC") } scope :recent, -> { order("created_at DESC") }
scope :code_push, -> { where(action: PUSHED) } scope :code_push, -> { where(action: PUSHED) }
...@@ -303,4 +306,10 @@ class Event < ActiveRecord::Base ...@@ -303,4 +306,10 @@ class Event < ActiveRecord::Base
target.respond_to? :title target.respond_to? :title
end end
end end
def reset_project_activity
if project
project.update_column(:last_activity_at, self.created_at)
end
end
end end
...@@ -57,6 +57,7 @@ class Note < ActiveRecord::Base ...@@ -57,6 +57,7 @@ class Note < ActiveRecord::Base
serialize :st_diff serialize :st_diff
before_create :set_diff, if: ->(n) { n.line_code.present? } before_create :set_diff, if: ->(n) { n.line_code.present? }
after_update :set_references
class << self class << self
def create_status_change_note(noteable, project, author, status, source) def create_status_change_note(noteable, project, author, status, source)
...@@ -314,4 +315,8 @@ class Note < ActiveRecord::Base ...@@ -314,4 +315,8 @@ class Note < ActiveRecord::Base
order('id DESC').limit(100). order('id DESC').limit(100).
update_all(updated_at: Time.now) update_all(updated_at: Time.now)
end end
def set_references
notice_added_references(project, author)
end
end end
...@@ -132,6 +132,10 @@ class User < ActiveRecord::Base ...@@ -132,6 +132,10 @@ class User < ActiveRecord::Base
before_validation :sanitize_attrs before_validation :sanitize_attrs
before_save :ensure_authentication_token before_save :ensure_authentication_token
after_save :ensure_namespace_correct
after_create :post_create_hook
after_destroy :post_destroy_hook
alias_attribute :private_token, :authentication_token alias_attribute :private_token, :authentication_token
...@@ -490,4 +494,36 @@ class User < ActiveRecord::Base ...@@ -490,4 +494,36 @@ class User < ActiveRecord::Base
GravatarService.new.execute(email, size) GravatarService.new.execute(email, size)
end end
end end
def ensure_namespace_correct
# Ensure user has namespace
self.create_namespace!(path: self.username, name: self.username) unless self.namespace
if self.username_changed?
self.namespace.update_attributes(path: self.username, name: self.username)
end
end
def post_create_hook
log_info("User \"#{self.name}\" (#{self.email}) was created")
notification_service.new_user(self)
system_hook_service.execute_hooks_for(self, :create)
end
def post_destroy_hook
log_info("User \"#{self.name}\" (#{self.email}) was removed")
system_hook_service.execute_hooks_for(self, :destroy)
end
def notification_service
NotificationService.new
end
def log_info message
Gitlab::AppLogger.info message
end
def system_hook_service
SystemHooksService.new
end
end end
...@@ -33,6 +33,9 @@ class UsersGroup < ActiveRecord::Base ...@@ -33,6 +33,9 @@ class UsersGroup < ActiveRecord::Base
scope :with_group, ->(group) { where(group_id: group.id) } scope :with_group, ->(group) { where(group_id: group.id) }
scope :with_user, ->(user) { where(user_id: user.id) } scope :with_user, ->(user) { where(user_id: user.id) }
after_create :notify_create
after_update :notify_update
validates :group_access, inclusion: { in: UsersGroup.group_access_roles.values }, presence: true validates :group_access, inclusion: { in: UsersGroup.group_access_roles.values }, presence: true
validates :user_id, presence: true validates :user_id, presence: true
validates :group_id, presence: true validates :group_id, presence: true
...@@ -43,4 +46,18 @@ class UsersGroup < ActiveRecord::Base ...@@ -43,4 +46,18 @@ class UsersGroup < ActiveRecord::Base
def access_field def access_field
group_access group_access
end end
def notify_create
notification_service.new_group_member(self)
end
def notify_update
if group_access_changed?
notification_service.update_group_member(self)
end
end
def notification_service
NotificationService.new
end
end end
...@@ -37,6 +37,10 @@ class UsersProject < ActiveRecord::Base ...@@ -37,6 +37,10 @@ class UsersProject < ActiveRecord::Base
scope :in_projects, ->(projects) { where(project_id: projects.map { |p| p.id }) } scope :in_projects, ->(projects) { where(project_id: projects.map { |p| p.id }) }
scope :with_user, ->(user) { where(user_id: user.id) } scope :with_user, ->(user) { where(user_id: user.id) }
after_create :post_create_hook
after_update :post_update_hook
after_destroy :post_destroy_hook
class << self class << self
# Add users to project teams with passed access option # Add users to project teams with passed access option
...@@ -114,4 +118,37 @@ class UsersProject < ActiveRecord::Base ...@@ -114,4 +118,37 @@ class UsersProject < ActiveRecord::Base
def owner? def owner?
project.owner == user project.owner == user
end end
def post_create_hook
Event.create(
project_id: self.project.id,
action: Event::JOINED,
author_id: self.user.id
)
notification_service.new_team_member(self)
system_hook_service.execute_hooks_for(self, :create)
end
def post_update_hook
notification_service.update_team_member(self) if self.project_access_changed?
end
def post_destroy_hook
Event.create(
project_id: self.project.id,
action: Event::LEFT,
author_id: self.user.id
)
system_hook_service.execute_hooks_for(self, :destroy)
end
def notification_service
NotificationService.new
end
def system_hook_service
SystemHooksService.new
end
end end
class BaseObserver < ActiveRecord::Observer
def notification
NotificationService.new
end
def event_service
EventCreateService.new
end
def log_info message
Gitlab::AppLogger.info message
end
end
class NoteObserver < BaseObserver
def after_create(note)
notification.new_note(note)
# Skip system notes, like status changes and cross-references.
unless note.system
event_service.leave_note(note, note.author)
# Create a cross-reference note if this Note contains GFM that names an
# issue, merge request, or commit.
note.references.each do |mentioned|
Note.create_cross_reference_note(mentioned, note.noteable, note.author, note.project)
end
end
end
def after_update(note)
note.notice_added_references(note.project, note.author)
end
end
class ProjectActivityCacheObserver < BaseObserver
observe :event
def after_create(event)
event.project.update_column(:last_activity_at, event.created_at) if event.project
end
end
class ProjectObserver < BaseObserver
def after_create(project)
log_info("#{project.owner.name} created a new project \"#{project.name_with_namespace}\"")
end
def after_update(project)
project.send_move_instructions if project.namespace_id_changed?
project.rename_repo if project.path_changed?
end
def before_destroy(project)
project.repository.expire_cache unless project.empty_repo?
end
def after_destroy(project)
GitlabShellWorker.perform_async(
:remove_repository,
project.path_with_namespace
)
GitlabShellWorker.perform_async(
:remove_repository,
project.path_with_namespace + ".wiki"
)
project.satellite.destroy
log_info("Project \"#{project.name}\" was removed")
end
end
class SystemHookObserver < BaseObserver
observe :user, :project, :users_project
def after_create(model)
system_hook_service.execute_hooks_for(model, :create)
end
def after_destroy(model)
system_hook_service.execute_hooks_for(model, :destroy)
end
private
def system_hook_service
SystemHooksService.new
end
end
class UserObserver < BaseObserver
def after_create(user)
log_info("User \"#{user.name}\" (#{user.email}) was created")
notification.new_user(user)
end
def after_destroy user
log_info("User \"#{user.name}\" (#{user.email}) was removed")
end
def after_save user
# Ensure user has namespace
user.create_namespace!(path: user.username, name: user.username) unless user.namespace
if user.username_changed?
user.namespace.update_attributes(path: user.username, name: user.username)
end
end
end
class UsersGroupObserver < BaseObserver
def after_create(membership)
notification.new_group_member(membership)
end
def after_update(membership)
notification.update_group_member(membership) if membership.group_access_changed?
end
end
class UsersProjectObserver < BaseObserver
def after_create(users_project)
Event.create(
project_id: users_project.project.id,
action: Event::JOINED,
author_id: users_project.user.id
)
notification.new_team_member(users_project)
end
def after_update(users_project)
notification.update_team_member(users_project) if users_project.project_access_changed?
end
def after_destroy(users_project)
Event.create(
project_id: users_project.project.id,
action: Event::LEFT,
author_id: users_project.user.id
)
end
end
...@@ -28,4 +28,8 @@ class BaseService ...@@ -28,4 +28,8 @@ class BaseService
def log_info message def log_info message
Gitlab::AppLogger.info message Gitlab::AppLogger.info message
end end
def system_hook_service
SystemHooksService.new
end
end end
module Notes module Notes
class CreateService < BaseService class CreateService < BaseService
def execute def execute
note = project.notes.new(params[:note]) note = project.notes.new(params)
note.author = current_user note.author = current_user
note.system = false note.system = false
note.save
if note.save
notification_service.new_note(note)
# Skip system notes, like status changes and cross-references.
unless note.system
event_service.leave_note(note, note.author)
# Create a cross-reference note if this Note contains GFM that names an
# issue, merge request, or commit.
note.references.each do |mentioned|
Note.create_cross_reference_note(mentioned, note.noteable, note.author, note.project)
end
end
end
note note
end end
end end
......
...@@ -51,6 +51,9 @@ module Projects ...@@ -51,6 +51,9 @@ module Projects
@project.creator = current_user @project.creator = current_user
if @project.save if @project.save
log_info("#{@project.owner.name} created a new project \"#{@project.name_with_namespace}\"")
system_hook_service.execute_hooks_for(@project, :create)
unless @project.group unless @project.group
@project.users_projects.create( @project.users_projects.create(
project_access: UsersProject::MASTER, project_access: UsersProject::MASTER,
......
module Projects
class DestroyService < BaseService
def execute
return false unless can?(current_user, :remove_project, project)
project.team.truncate
project.repository.expire_cache unless project.empty_repo?
if project.destroy
GitlabShellWorker.perform_async(
:remove_repository,
project.path_with_namespace
)
GitlabShellWorker.perform_async(
:remove_repository,
project.path_with_namespace + ".wiki"
)
project.satellite.destroy
log_info("Project \"#{project.name}\" was removed")
system_hook_service.execute_hooks_for(project, :destroy)
true
end
end
end
end
...@@ -13,7 +13,15 @@ module Projects ...@@ -13,7 +13,15 @@ module Projects
project.change_head(new_branch) project.change_head(new_branch)
end end
project.update_attributes(params[:project], as: role) if project.update_attributes(params[:project], as: role)
if project.previous_changes.include?('namespace_id')
project.send_move_instructions
end
if project.previous_changes.include?('path')
project.rename_repo
end
end
end end
end end
end end
...@@ -18,15 +18,6 @@ module Gitlab ...@@ -18,15 +18,6 @@ module Gitlab
# :all can be used as a placeholder for all plugins not explicitly named. # :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
config.active_record.observers = :project_activity_cache_observer,
:note_observer,
:project_observer,
:system_hook_observer,
:user_observer,
:users_group_observer,
:users_project_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)' # config.time_zone = 'Central Time (US & Canada)'
......
...@@ -50,12 +50,15 @@ module API ...@@ -50,12 +50,15 @@ module API
post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
required_attributes! [:body] required_attributes! [:body]
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"]) opts = {
@note = @noteable.notes.new(note: params[:body]) note: params[:body],
@note.author = current_user noteable_type: noteables_str.classify,
@note.project = user_project noteable_id: params[noteable_id_str]
}
@note = ::Notes::CreateService.new(user_project, current_user, opts).execute
if @note.save if @note.valid?
present @note, with: Entities::Note present @note, with: Entities::Note
else else
not_found! not_found!
......
...@@ -47,20 +47,16 @@ describe "Admin::Users", feature: true do ...@@ -47,20 +47,16 @@ describe "Admin::Users", feature: true do
it "should call send mail" do it "should call send mail" do
Notify.should_receive(:new_user_email) Notify.should_receive(:new_user_email)
User.observers.enable :user_observer do click_button "Create user"
click_button "Create user"
end
end end
it "should send valid email to user with email & password" do it "should send valid email to user with email & password" do
User.observers.enable :user_observer do click_button "Create user"
click_button "Create user" user = User.last
user = User.last email = ActionMailer::Base.deliveries.last
email = ActionMailer::Base.deliveries.last email.subject.should have_content("Account was created")
email.subject.should have_content("Account was created") email.text_part.body.should have_content(user.email)
email.text_part.body.should have_content(user.email) email.text_part.body.should have_content('password')
email.text_part.body.should have_content('password')
end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe "Profile account page", feature: true do describe "Profile account page", feature: true do
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
before do before do
......
require 'spec_helper' require 'spec_helper'
describe "Projects", feature: true do describe "Projects", feature: true do
before(:each) { enable_observers }
after(:each) {disable_observers}
before { login_as :user } before { login_as :user }
describe "DELETE /projects/:id" do describe "DELETE /projects/:id" do
......
...@@ -2,7 +2,6 @@ require 'spec_helper' ...@@ -2,7 +2,6 @@ require 'spec_helper'
describe "Search", feature: true do describe "Search", feature: true do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
login_as :user login_as :user
@project = create(:project, namespace: @user.namespace) @project = create(:project, namespace: @user.namespace)
@project.team << [@user, :reporter] @project.team << [@user, :reporter]
......
...@@ -65,26 +65,4 @@ describe Event do ...@@ -65,26 +65,4 @@ describe Event do
it { @event.branch_name.should == "master" } it { @event.branch_name.should == "master" }
it { @event.author.should == @user } it { @event.author.should == @user }
end end
describe 'Team events' do
let(:user_project) { double.as_null_object }
let(:observer) { UsersProjectObserver.instance }
before {
Event.should_receive :create
observer.stub(notification: double.as_null_object)
}
describe "Joined project team" do
it "should create event" do
observer.after_create user_project
end
end
describe "Left project team" do
it "should create event" do
observer.after_destroy user_project
end
end
end
end end
...@@ -27,9 +27,6 @@ ...@@ -27,9 +27,6 @@
require 'spec_helper' require 'spec_helper'
describe Project do describe Project do
before { enable_observers }
after { disable_observers }
describe "Associations" do describe "Associations" do
it { should belong_to(:group) } it { should belong_to(:group) }
it { should belong_to(:namespace) } it { should belong_to(:namespace) }
......
...@@ -19,21 +19,20 @@ require "spec_helper" ...@@ -19,21 +19,20 @@ require "spec_helper"
describe SystemHook do describe SystemHook do
describe "execute" do describe "execute" do
before(:each) { ActiveRecord::Base.observers.enable(:all) }
before(:each) do before(:each) do
@system_hook = create(:system_hook) @system_hook = create(:system_hook)
WebMock.stub_request(:post, @system_hook.url) WebMock.stub_request(:post, @system_hook.url)
end end
it "project_create hook" do it "project_create hook" do
project = create(:project) Projects::CreateService.new(create(:user), name: 'empty').execute
WebMock.should have_requested(:post, @system_hook.url).with(body: /project_create/).once WebMock.should have_requested(:post, @system_hook.url).with(body: /project_create/).once
end end
it "project_destroy hook" do it "project_destroy hook" do
project = create(:project) user = create(:user)
project.destroy project = create(:empty_project, namespace: user.namespace)
Projects::DestroyService.new(project, user, {}).execute
WebMock.should have_requested(:post, @system_hook.url).with(body: /project_destroy/).once WebMock.should have_requested(:post, @system_hook.url).with(body: /project_destroy/).once
end end
......
...@@ -145,7 +145,6 @@ describe User do ...@@ -145,7 +145,6 @@ describe User do
describe 'projects' do describe 'projects' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@project = create :project, namespace: @user.namespace @project = create :project, namespace: @user.namespace
@project_2 = create :project, group: create(:group) # Grant MASTER access to the user @project_2 = create :project, group: create(:group) # Grant MASTER access to the user
...@@ -168,7 +167,6 @@ describe User do ...@@ -168,7 +167,6 @@ describe User do
describe 'groups' do describe 'groups' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@group = create :group @group = create :group
@group.add_owner(@user) @group.add_owner(@user)
...@@ -181,7 +179,6 @@ describe User do ...@@ -181,7 +179,6 @@ describe User do
describe 'group multiple owners' do describe 'group multiple owners' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@user2 = create :user @user2 = create :user
@group = create :group @group = create :group
...@@ -195,7 +192,6 @@ describe User do ...@@ -195,7 +192,6 @@ describe User do
describe 'namespaced' do describe 'namespaced' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@project = create :project, namespace: @user.namespace @project = create :project, namespace: @user.namespace
end end
...@@ -339,7 +335,7 @@ describe User do ...@@ -339,7 +335,7 @@ describe User do
user.all_ssh_keys.should include(key.key) user.all_ssh_keys.should include(key.key)
end end
end end
describe :avatar_type do describe :avatar_type do
let(:user) { create(:user) } let(:user) { create(:user) }
......
...@@ -37,4 +37,32 @@ describe UsersGroup do ...@@ -37,4 +37,32 @@ describe UsersGroup do
it { should respond_to(:user_name) } it { should respond_to(:user_name) }
it { should respond_to(:user_email) } it { should respond_to(:user_email) }
end end
context 'notification' do
describe "#after_create" do
it "should send email to user" do
membership = build(:users_group)
membership.stub(notification_service: double('NotificationService').as_null_object)
membership.should_receive(:notification_service)
membership.save
end
end
describe "#after_update" do
before do
@membership = create :users_group
@membership.stub(notification_service: double('NotificationService').as_null_object)
end
it "should send email to user" do
@membership.should_receive(:notification_service)
@membership.update_attribute(:group_access, UsersGroup::MASTER)
end
it "does not send an email when the access level has not changed" do
@membership.should_not_receive(:notification_service)
@membership.update_attribute(:group_access, UsersGroup::OWNER)
end
end
end
end end
require 'spec_helper'
describe NoteObserver do
subject { NoteObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
let(:team_without_author) { (1..2).map { |n| double :user, id: n } }
let(:note) { double(:note).as_null_object }
describe '#after_create' do
it 'is called after a note is created' do
subject.should_receive :after_create
Note.observers.enable :note_observer do
create(:note)
end
end
it 'sends out notifications' do
subject.should_receive(:notification)
subject.after_create(note)
end
it 'creates cross-reference notes as appropriate' do
@p = create(:project)
@referenced = create(:issue, project: @p)
@referencer = create(:issue, project: @p)
@author = create(:user)
Note.should_receive(:create_cross_reference_note).with(@referenced, @referencer, @author, @p)
Note.observers.enable :note_observer do
create(:note, project: @p, author: @author, noteable: @referencer,
note: "Duplicate of ##{@referenced.iid}")
end
end
it "doesn't cross-reference system notes" do
Note.should_receive(:create_cross_reference_note).once
Note.observers.enable :note_observer do
Note.create_cross_reference_note(create(:issue), create(:issue))
end
end
end
describe '#after_update' do
it 'checks for new cross-references' do
note.should_receive(:notice_added_references)
subject.after_update(note)
end
end
end
require 'spec_helper'
describe UserObserver do
before(:each) { enable_observers }
after(:each) {disable_observers}
subject { UserObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
it 'calls #after_create when new users are created' do
new_user = build(:user)
subject.should_receive(:after_create).with(new_user)
new_user.save
end
context 'when a new user is created' do
it 'sends an email' do
subject.should_receive(:notification)
create(:user)
end
it 'trigger logger' do
user = double(:user, id: 42, password: 'P@ssword!', name: 'John', email: 'u@mail.local', extern_uid?: false)
Gitlab::AppLogger.should_receive(:info)
create(:user)
end
end
end
require 'spec_helper'
describe UsersGroupObserver do
before(:each) { enable_observers }
after(:each) { disable_observers }
subject { UsersGroupObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
describe "#after_create" do
it "should send email to user" do
subject.should_receive(:notification)
create(:users_group)
end
end
describe "#after_update" do
before do
@membership = create :users_group
end
it "should send email to user" do
subject.should_receive(:notification)
@membership.update_attribute(:group_access, UsersGroup::MASTER)
end
it "does not send an email when the access level has not changed" do
subject.should_not_receive(:notification)
@membership.update_attribute(:group_access, UsersGroup::OWNER)
end
end
end
require 'spec_helper'
describe UsersProjectObserver do
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) }
let(:project) { create(:project) }
subject { UsersProjectObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
describe "#after_update" do
before do
@users_project = create :users_project
end
it "should called when UsersProject updated" do
subject.should_receive(:after_update)
@users_project.update_attribute(:project_access, UsersProject::MASTER)
end
it "should send email to user" do
subject.should_receive(:notification)
@users_project.update_attribute(:project_access, UsersProject::OWNER)
end
it "should not called after UsersProject destroyed" do
subject.should_not_receive(:after_update)
@users_project.destroy
end
end
describe "#after_destroy" do
before do
@users_project = create :users_project
end
it "should called when UsersProject destroyed" do
subject.should_receive(:after_destroy)
@users_project.destroy
end
it "should create new event" do
Event.should_receive(:create)
@users_project.destroy
end
end
describe "#after_create" do
it "should send email to user" do
subject.should_receive(:notification)
Event.stub(create: true)
create(:users_project)
end
it "should create new event" do
Event.should_receive(:create)
create(:users_project)
end
end
end
...@@ -3,8 +3,6 @@ require 'mime/types' ...@@ -3,8 +3,6 @@ require 'mime/types'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
......
...@@ -3,9 +3,6 @@ require 'mime/types' ...@@ -3,9 +3,6 @@ require 'mime/types'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let!(:project) { create(:project, creator_id: user.id) } let!(:project) { create(:project, creator_id: user.id) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
before { project.team << [user, :developer] } before { project.team << [user, :developer] }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:key) { create(:key, user: user) } let(:key) { create(:key, user: user) }
let(:project) { create(:project) } let(:project) { create(:project) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:issue) { create(:issue, author: user, assignee: user, project: project) } let!(:issue) { create(:issue, author: user, assignee: user, project: project) }
......
...@@ -2,8 +2,6 @@ require "spec_helper" ...@@ -2,8 +2,6 @@ require "spec_helper"
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) } let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") } let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:milestone) { create(:milestone, project: project) } let!(:milestone) { create(:milestone, project: project) }
...@@ -92,9 +89,6 @@ describe API::API, api: true do ...@@ -92,9 +89,6 @@ describe API::API, api: true do
end end
describe "PUT /projects/:id/milestones/:milestone_id to test observer on close" do describe "PUT /projects/:id/milestones/:milestone_id to test observer on close" do
before { enable_observers }
after { disable_observers }
it "should create an activity event when an milestone is closed" do it "should create an activity event when an milestone is closed" do
Event.should_receive(:create) Event.should_receive(:create)
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let!(:group1) { create(:group) } let!(:group1) { create(:group) }
let!(:group2) { create(:group) } let!(:group2) { create(:group) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:issue) { create(:issue, project: project, author: user) } let!(:issue) { create(:issue, project: project, author: user) }
...@@ -128,14 +125,10 @@ describe API::API, api: true do ...@@ -128,14 +125,10 @@ describe API::API, api: true do
end end
describe "POST /projects/:id/noteable/:noteable_id/notes to test observer on create" do describe "POST /projects/:id/noteable/:noteable_id/notes to test observer on create" do
before { enable_observers }
after { disable_observers }
it "should create an activity event when an issue note is created" do it "should create an activity event when an issue note is created" do
Event.should_receive(:create) Event.should_receive(:create)
post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!' post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
end end
end end
end end
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, 'ProjectHooks', api: true do describe API::API, 'ProjectHooks', api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user3) { create(:user) } let(:user3) { create(:user) }
let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) } let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:user3) { create(:user) } let(:user3) { create(:user) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:user3) { create(:user) } let(:user3) { create(:user) }
......
...@@ -3,9 +3,6 @@ require 'mime/types' ...@@ -3,9 +3,6 @@ require 'mime/types'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let!(:project) { create(:project, creator_id: user.id) } let!(:project) { create(:project, creator_id: user.id) }
......
...@@ -2,9 +2,6 @@ require "spec_helper" ...@@ -2,9 +2,6 @@ require "spec_helper"
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) {create(:project, creator_id: user.id, namespace: user.namespace) } let(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
......
require 'spec_helper' require 'spec_helper'
describe Issues::BulkUpdateService do describe Issues::BulkUpdateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:issue) { let(:issue) {
create(:issue, project: @project) create(:issue, project: @project)
} }
before do before do
@user = create :user @user = create :user
...@@ -23,7 +20,7 @@ describe Issues::BulkUpdateService do ...@@ -23,7 +20,7 @@ describe Issues::BulkUpdateService do
@issues = 5.times.collect do @issues = 5.times.collect do
create(:issue, project: @project) create(:issue, project: @project)
end end
@params = { @params = {
update: { update: {
status: 'closed', status: 'closed',
issues_ids: @issues.map(&:id) issues_ids: @issues.map(&:id)
...@@ -48,7 +45,7 @@ describe Issues::BulkUpdateService do ...@@ -48,7 +45,7 @@ describe Issues::BulkUpdateService do
@issues = 5.times.collect do @issues = 5.times.collect do
create(:closed_issue, project: @project) create(:closed_issue, project: @project)
end end
@params = { @params = {
update: { update: {
status: 'reopen', status: 'reopen',
issues_ids: @issues.map(&:id) issues_ids: @issues.map(&:id)
...@@ -71,7 +68,7 @@ describe Issues::BulkUpdateService do ...@@ -71,7 +68,7 @@ describe Issues::BulkUpdateService do
before do before do
@new_assignee = create :user @new_assignee = create :user
@params = { @params = {
update: { update: {
issues_ids: [issue.id], issues_ids: [issue.id],
assignee_id: @new_assignee.id assignee_id: @new_assignee.id
...@@ -93,7 +90,7 @@ describe Issues::BulkUpdateService do ...@@ -93,7 +90,7 @@ describe Issues::BulkUpdateService do
before do before do
@milestone = create :milestone @milestone = create :milestone
@params = { @params = {
update: { update: {
issues_ids: [issue.id], issues_ids: [issue.id],
milestone_id: @milestone.id milestone_id: @milestone.id
......
require 'spec_helper'
describe Notes::CreateService do
let(:project) { create(:empty_project) }
let(:issue) { create(:issue, project: project) }
let(:user) { create(:user) }
describe :execute do
context "valid params" do
before do
project.team << [user, :master]
opts = {
note: 'Awesome comment',
description: 'please fix',
noteable_type: 'Issue',
noteable_id: issue.id
}
@note = Notes::CreateService.new(project, user, opts).execute
end
it { @note.should be_valid }
it { @note.note.should == 'Awesome comment' }
end
end
end
require 'spec_helper' require 'spec_helper'
describe Projects::CreateService do describe Projects::CreateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
describe :create_by_user do describe :create_by_user do
before do before do
@user = create :user @user = create :user
...@@ -66,11 +63,8 @@ describe Projects::CreateService do ...@@ -66,11 +63,8 @@ describe Projects::CreateService do
@settings.stub(:merge_requests) { true } @settings.stub(:merge_requests) { true }
@settings.stub(:wiki) { true } @settings.stub(:wiki) { true }
@settings.stub(:snippets) { true } @settings.stub(:snippets) { true }
stub_const("Settings", Class.new) Gitlab.config.gitlab.stub(restricted_visibility_levels: [])
@restrictions = double("restrictions") Gitlab.config.gitlab.stub(:default_projects_features).and_return(@settings)
@restrictions.stub(:restricted_visibility_levels) { [] }
Settings.stub_chain(:gitlab).and_return(@restrictions)
Settings.stub_chain(:gitlab, :default_projects_features).and_return(@settings)
end end
context 'should be public when setting is public' do context 'should be public when setting is public' do
...@@ -109,11 +103,9 @@ describe Projects::CreateService do ...@@ -109,11 +103,9 @@ describe Projects::CreateService do
@settings.stub(:wiki) { true } @settings.stub(:wiki) { true }
@settings.stub(:snippets) { true } @settings.stub(:snippets) { true }
@settings.stub(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE } @settings.stub(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE }
stub_const("Settings", Class.new) @restrictions = [ Gitlab::VisibilityLevel::PUBLIC ]
@restrictions = double("restrictions") Gitlab.config.gitlab.stub(restricted_visibility_levels: @restrictions)
@restrictions.stub(:restricted_visibility_levels) { [ Gitlab::VisibilityLevel::PUBLIC ] } Gitlab.config.gitlab.stub(:default_projects_features).and_return(@settings)
Settings.stub_chain(:gitlab).and_return(@restrictions)
Settings.stub_chain(:gitlab, :default_projects_features).and_return(@settings)
end end
context 'should be private when option is public' do context 'should be private when option is public' do
...@@ -158,4 +150,3 @@ describe Projects::CreateService do ...@@ -158,4 +150,3 @@ describe Projects::CreateService do
Projects::CreateService.new(user, opts).execute Projects::CreateService.new(user, opts).execute
end end
end end
require 'spec_helper' require 'spec_helper'
describe Projects::ImageService do describe Projects::ImageService do
before(:each) { enable_observers }
after(:each) { disable_observers }
describe 'Image service' do describe 'Image service' do
before do before do
@user = create :user @user = create :user
......
require 'spec_helper' require 'spec_helper'
describe Projects::TransferService do describe Projects::TransferService do
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group) } let(:group) { create(:group) }
let(:group2) { create(:group) } let(:group2) { create(:group) }
......
require 'spec_helper' require 'spec_helper'
describe Projects::UpdateService do describe Projects::UpdateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
describe :update_by_user do describe :update_by_user do
before do before do
@user = create :user @user = create :user
......
require 'spec_helper' require 'spec_helper'
describe 'Search::GlobalService' do describe 'Search::GlobalService' do
let(:user) { create(:user, namespace: found_namespace) } let(:user) { create(:user) }
let(:public_user) { create(:user, namespace: public_namespace) } let(:public_user) { create(:user) }
let(:internal_user) { create(:user, namespace: internal_namespace) } let(:internal_user) { create(:user) }
let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') } let!(:found_project) { create(:empty_project, :private, name: 'searchable_project') }
let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') } let!(:unfound_project) { create(:empty_project, :private, name: 'unfound_project') }
let(:internal_namespace) { create(:namespace, name: 'searchable internal namespace', path: 'something_internal') } let!(:internal_project) { create(:empty_project, :internal, name: 'searchable_internal_project') }
let(:public_namespace) { create(:namespace, name: 'searchable public namespace', path: 'something_public') } let!(:public_project) { create(:empty_project, :public, name: 'searchable_public_project') }
let!(:found_project) { create(:project, :private, name: 'searchable_project', creator_id: user.id, namespace: found_namespace) } before do
let!(:unfound_project) { create(:project, :private, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace) } found_project.team << [user, :master]
let!(:internal_project) { create(:project, :internal, name: 'searchable_internal_project', creator_id: internal_user.id, namespace: internal_namespace) } end
let!(:public_project) { create(:project, :public, name: 'searchable_public_project', creator_id: public_user.id, namespace: public_namespace) }
describe '#execute' do describe '#execute' do
context 'unauthenticated' do context 'unauthenticated' do
...@@ -38,7 +37,7 @@ describe 'Search::GlobalService' do ...@@ -38,7 +37,7 @@ describe 'Search::GlobalService' do
end end
it 'namespace name should be searchable' do it 'namespace name should be searchable' do
context = Search::GlobalService.new(user, search: "searchable namespace") context = Search::GlobalService.new(user, search: found_project.namespace.path)
results = context.execute results = context.execute
results[:projects].should match_array [found_project] results[:projects].should match_array [found_project]
end end
......
...@@ -43,7 +43,7 @@ RSpec.configure do |config| ...@@ -43,7 +43,7 @@ RSpec.configure do |config|
# instead of true. # instead of true.
config.before(:suite) do config.before(:suite) do
TestEnv.init(observers: false, init_repos: true, repos: false) TestEnv.init(init_repos: true, repos: false)
end end
config.before(:each) do config.before(:each) do
TestEnv.setup_stubs TestEnv.setup_stubs
......
...@@ -3,9 +3,7 @@ module LoginHelpers ...@@ -3,9 +3,7 @@ module LoginHelpers
# #
# role - User role (e.g., :admin, :user) # role - User role (e.g., :admin, :user)
def login_as(role) def login_as(role)
ActiveRecord::Base.observers.enable(:user_observer) do @user = create(role)
@user = create(role)
end
login_with(@user) login_with(@user)
end end
......
...@@ -17,14 +17,6 @@ module TestEnv ...@@ -17,14 +17,6 @@ module TestEnv
def init(opts = {}) def init(opts = {})
RSpec::Mocks::setup(self) RSpec::Mocks::setup(self)
# Disable observers to improve test speed
#
# You can enable it in whole test case where needed by next string:
#
# before(:each) { enable_observers }
#
disable_observers if opts[:observers] == false
# Disable mailer for spinach tests # Disable mailer for spinach tests
disable_mailer if opts[:mailer] == false disable_mailer if opts[:mailer] == false
setup_stubs setup_stubs
...@@ -33,14 +25,6 @@ module TestEnv ...@@ -33,14 +25,6 @@ module TestEnv
setup_test_repos(opts) if opts[:repos] == true setup_test_repos(opts) if opts[:repos] == true
end end
def enable_observers
ActiveRecord::Base.observers.enable(:all)
end
def disable_observers
ActiveRecord::Base.observers.disable(:all)
end
def disable_mailer def disable_mailer
NotificationService.any_instance.stub(mailer: double.as_null_object) NotificationService.any_instance.stub(mailer: double.as_null_object)
end end
...@@ -89,10 +73,6 @@ module TestEnv ...@@ -89,10 +73,6 @@ module TestEnv
Repository.any_instance.stub( Repository.any_instance.stub(
size: 12.45 size: 12.45
) )
BaseObserver.any_instance.stub(
current_user: double("current_user", id: 1)
)
end end
def clear_repo_dir(namespace, name) def clear_repo_dir(namespace, name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment