From 74429ae687c8acc36ce17c307d669a8e0345ed4e Mon Sep 17 00:00:00 2001 From: Stephen Crosby Date: Tue, 1 Sep 2015 08:35:49 -0700 Subject: [PATCH] Refs #561 users can see all apps --- app/assets/stylesheets/errbit.css.erb | 4 ++-- app/controllers/application_controller.rb | 7 ------- app/controllers/apps_controller.rb | 4 +--- app/controllers/comments_controller.rb | 4 ---- app/controllers/deploys_controller.rb | 8 ++------ app/controllers/problems_controller.rb | 13 ++++++------- app/controllers/watchers_controller.rb | 21 +++++++-------------- app/interactors/user_destroy.rb | 7 +++++-- app/models/app.rb | 9 ++++++++- app/models/user.rb | 6 ------ app/views/apps/_fields.html.haml | 15 --------------- app/views/apps/show.html.haml | 7 +++++-- config/locales/en.yml | 7 ++++++- config/routes.rb | 2 +- public/mockup.html | 90 ------------------------------------------------------------------------------------------ spec/acceptance/watch_unwatch_app_spec.rb | 23 ++++++++++++++--------- spec/controllers/apps_controller_spec.rb | 24 ++++++++---------------- spec/controllers/devise_sessions_controller_spec.rb | 6 ------ spec/controllers/problems_controller_spec.rb | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------- spec/controllers/watchers_controller_spec.rb | 60 +++++++++++++++++++++++++++++++++++++++--------------------- spec/models/user_spec.rb | 18 ------------------ spec/views/apps/show.html.haml_spec.rb | 4 ++-- 22 files changed, 197 insertions(+), 376 deletions(-) delete mode 100644 public/mockup.html diff --git a/app/assets/stylesheets/errbit.css.erb b/app/assets/stylesheets/errbit.css.erb index 0688503..9930be2 100644 --- a/app/assets/stylesheets/errbit.css.erb +++ b/app/assets/stylesheets/errbit.css.erb @@ -540,8 +540,8 @@ a.button.active { display: inline-block; } -/* Watchers / Issue Tracker / Notification Forms */ -div.watcher.nested .watcher_params, div.issue_tracker.nested .tracker_params, div.notification_service.nested .notification_params { +/* Issue Tracker / Notification Forms */ +div.issue_tracker.nested .tracker_params, div.notification_service.nested .notification_params { display: none; } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cd42cb3..f4bc107 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,13 +5,6 @@ class ApplicationController < ActionController::Base before_action :authenticate_user! before_action :set_time_zone - # Devise override - After login, if there is only one app, - # redirect to that app's path instead of the root path (apps#index). - def stored_location_for(resource) - location = super || root_path - (location == root_path && current_user.apps.count == 1) ? app_path(current_user.apps.first) : location - end - rescue_from ActionController::RedirectBackError, :with => :redirect_to_root class StrongParametersWithEagerAttributesStrategy < DecentExposure::StrongParametersStrategy diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb index 7664948..1acc3f5 100644 --- a/app/controllers/apps_controller.rb +++ b/app/controllers/apps_controller.rb @@ -7,9 +7,7 @@ class AppsController < ApplicationController before_action :parse_notice_at_notices_or_set_default, :only => [:create, :update] respond_to :html - expose(:app_scope) { - (current_user.admin? ? App : current_user.apps) - } + expose(:app_scope) { App } expose(:apps) { app_scope.asc(:name).map { |app| AppDecorator.new(app) } diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 3b16728..4572599 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -27,10 +27,6 @@ class CommentsController < ApplicationController protected def find_app @app = App.find(params[:app_id]) - - # Mongoid Bug: could not chain: current_user.apps.find_by_id! - # apparently finding by 'watchers.email' and 'id' is broken - raise(Mongoid::Errors::DocumentNotFound.new(App,@app.id)) unless current_user.admin? || current_user.watching?(@app) end def find_problem diff --git a/app/controllers/deploys_controller.rb b/app/controllers/deploys_controller.rb index a376a38..da8d9d3 100644 --- a/app/controllers/deploys_controller.rb +++ b/app/controllers/deploys_controller.rb @@ -12,13 +12,9 @@ class DeploysController < ApplicationController end def index - # See AppsController#find_app for the reasoning behind this code. - app = App.find(params[:app_id]) - raise Mongoid::Errors::DocumentNotFound.new(App, app.id) unless current_user.admin? || current_user.watching?(app) - - @deploys = Kaminari.paginate_array(app.deploys.order_by(:created_at.desc)). + @app = App.find(params[:app_id]) + @deploys = Kaminari.paginate_array(@app.deploys.order_by(:created_at.desc)). page(params[:page]).per(10) - @app = app end private diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb index 866bb34..71b77fd 100644 --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -13,8 +13,7 @@ class ProblemsController < ApplicationController ] expose(:app_scope) { - apps = current_user.admin? ? App.all : current_user.apps - params[:app_id] ? apps.where(:_id => params[:app_id]) : apps + params[:app_id] ? App.where(:_id => params[:app_id]) : App.all } expose(:app) { @@ -34,11 +33,11 @@ class ProblemsController < ApplicationController } expose(:problems) { - pro = Problem.for_apps( - app_scope - ).in_env( - params_environement - ).all_else_unresolved(all_errs).ordered_by(params_sort, params_order) + pro = Problem + .for_apps(app_scope) + .in_env(params_environement) + .all_else_unresolved(all_errs) + .ordered_by(params_sort, params_order) if request.format == :html pro.page(params[:page]).per(current_user.per_page) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 28be9d8..3fb8f32 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -5,23 +5,16 @@ class WatchersController < ApplicationController App.find(params[:app_id]) end - expose(:watcher) do - app.watchers.where(:user_id => params[:id]).first - end - - before_action :require_watcher_edit_priviledges, :only => [:destroy] - def destroy + watcher = app.watchers.where(:user_id => params[:id]).first app.watchers.delete(watcher) - flash[:success] = "That's sad. #{watcher.label} is no longer watcher." - redirect_to root_path + flash[:success] = t('.success', app: app.name) + redirect_to app_path(app) end - private - - def require_watcher_edit_priviledges - redirect_to(root_path) unless current_user == watcher.user || current_user.admin? + def update + app.watchers.create(user_id: current_user.id) + flash[:success] = t('.success', app: app.name) + redirect_to app_path(app) end - end - diff --git a/app/interactors/user_destroy.rb b/app/interactors/user_destroy.rb index 8453962..ba99655 100644 --- a/app/interactors/user_destroy.rb +++ b/app/interactors/user_destroy.rb @@ -4,8 +4,11 @@ class UserDestroy end def destroy + App.watched_by(@user).each do |app| + watcher = app.watchers.where(user_id: @user.id).first + app.watchers.delete(watcher) + end + @user.destroy - @user.watchers.each(&:destroy) end - end diff --git a/app/models/app.rb b/app/models/app.rb index 0ff1203..c581ba1 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -23,7 +23,6 @@ class App pre_processed: true, default: ->{ BSON::ObjectId.new.to_s } - embeds_many :watchers embeds_many :deploys embeds_one :issue_tracker, :class_name => 'IssueTracker' @@ -48,6 +47,14 @@ class App accepts_nested_attributes_for :notification_service, :allow_destroy => true, :reject_if => proc { |attrs| !NotificationService.subclasses.map(&:to_s).include?(attrs[:type].to_s) } + scope :watched_by, ->(user) do + where watchers: { "$elemMatch" => { "user_id" => user.id } } + end + + def watched_by?(user) + watchers.pluck("user_id").include? user.id + end + # Acceps a hash with the following attributes: # # * :error_class - the class of error (required to create a new Problem) diff --git a/app/models/user.rb b/app/models/user.rb index 15d8de1..5ff7273 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,17 +41,11 @@ class User validates_presence_of :name validates_uniqueness_of :github_login, :allow_nil => true - has_many :apps, :foreign_key => 'watchers.user_id' - if Errbit::Config.user_has_username field :username validates_presence_of :username end - def watchers - apps.map(&:watchers).flatten.select {|w| w.user_id.to_s == id.to_s} - end - def per_page super || PER_PAGE end diff --git a/app/views/apps/_fields.html.haml b/app/views/apps/_fields.html.haml index 0d43a25..d44de9b 100644 --- a/app/views/apps/_fields.html.haml +++ b/app/views/apps/_fields.html.haml @@ -44,21 +44,6 @@ = f.check_box :notify_all_users = f.label :notify_all_users, 'Send notifications to all users' - -%fieldset.watchers.nested-wrapper{:style => app_decorate.notify_user_display} - %legend Watchers - = f.fields_for :watchers do |w| - %div.watcher.nested - %div.choose - = w.radio_button :watcher_type, :user - = w.label :watcher_type_user, 'User' - = w.radio_button :watcher_type, :email - = label_tag :watcher_type_email, 'Email Address', :for => label_for_attr(w, 'watcher_type_email') - %div.watcher_params.user{:class => w.object.email.blank? ? 'chosen' : nil} - = w.select :user_id, users.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --' - %div.watcher_params.email{:class => w.object.email.present? ? 'chosen' : nil} - = w.text_field :email - %div.checkbox = f.check_box :resolve_errs_on_deploy = f.label :resolve_errs_on_deploy, 'Resolve errs on deploy' diff --git a/app/views/apps/show.html.haml b/app/views/apps/show.html.haml index 71b19b1..afdd893 100644 --- a/app/views/apps/show.html.haml +++ b/app/views/apps/show.html.haml @@ -19,8 +19,11 @@ - else = link_to t('.all_errs'), app_path(app, :all_errs => true), :class => 'button' - - if current_user.watching?(app) - = link_to t('.unwatch'), app_watcher_path({app_id: app, id: current_user.id}), method: :delete, class: 'button', data: {confirm: t('.are_you_sure')} + - if app.watched_by?(current_user) + = link_to t('.unwatch'), app_watcher_path(app_id: app, id: current_user.id), method: :delete, class: 'button' + - else + = link_to t('.watch'), app_watcher_path(app_id: app, id: current_user.id), method: :put, class: 'button' + %h3#watchers_toggle =t('.watchers') %span.click_span=t('.show_hide') diff --git a/config/locales/en.yml b/config/locales/en.yml index a7fa455..803ec19 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -124,6 +124,7 @@ en: unresolved_errs: unresolved errors unwatch: unwatch user_or_email: User or Email + watch: watch watchers: Watchers when: When who: Who @@ -139,4 +140,8 @@ en: cancel: 'cancel' seriously: 'Seriously?' update: 'Update App' - + watchers: + destroy: + success: "You are no longer watching %{app}" + update: + success: "You are now watching %{app}" diff --git a/config/routes.rb b/config/routes.rb index f385464..63c58b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,7 +42,7 @@ Rails.application.routes.draw do end end resources :deploys, :only => [:index] - resources :watchers, :only => [:destroy] + resources :watchers, :only => [:destroy, :update] member do post :regenerate_api_key end diff --git a/public/mockup.html b/public/mockup.html deleted file mode 100644 index 917949b..0000000 --- a/public/mockup.html +++ /dev/null @@ -1,90 +0,0 @@ - - - - - Errbit — - Add App - - - - - - - - - - - - - - - - - - - -
-
-

Add App

- -
- cancel -
-
-
- -
-
- - -
-
- - -
-
- Watchers -
-
-    -

-
-
- - -
-
-
-
-
-
-
- - - - diff --git a/spec/acceptance/watch_unwatch_app_spec.rb b/spec/acceptance/watch_unwatch_app_spec.rb index c718cdc..cc06b5a 100644 --- a/spec/acceptance/watch_unwatch_app_spec.rb +++ b/spec/acceptance/watch_unwatch_app_spec.rb @@ -1,20 +1,25 @@ require 'acceptance/acceptance_helper' feature 'A user can watch and unwatch an application' do - let!(:app) { Fabricate(:app) } - let!(:user) do - user = Fabricate(:user) - app.watchers.create!( - :user_id => user.id - ) + let!(:user) { Fabricate(:user) } + + scenario 'log in and unwatch a project' do + app.watchers.create!(user_id: user.id) user.reload - end - scenario 'log in watch a project and unwatch it' do log_in user + click_on app.name click_on I18n.t('apps.show.unwatch') - expect(page).to have_content(I18n.t('apps.index.no_apps')) + expect(page).to have_content( + I18n.t('watchers.destroy.success', app: app.name)) end + scenario 'log in and watch a project' do + log_in user + click_on app.name + click_on I18n.t('apps.show.watch') + expect(page).to have_content( + I18n.t('watchers.update.success', app: app.name)) + end end diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb index f839181..e364184 100644 --- a/spec/controllers/apps_controller_spec.rb +++ b/spec/controllers/apps_controller_spec.rb @@ -39,12 +39,11 @@ describe AppsController, type: 'controller' do end context 'when logged in as a regular user' do - it 'finds apps the user is watching' do - sign_in(user) - watched_app1 && watched_app2 && unwatched_app + it 'finds all apps' do + sign_in user + unwatched_app && watched_app1 && watched_app2 get :index - expect(controller.apps).to include(watched_app1, watched_app2) - expect(controller.apps).to_not include(unwatched_app) + expect(controller.apps.entries).to eq App.all.sort.entries end end end @@ -163,19 +162,12 @@ describe AppsController, type: 'controller' do end context 'logged in as a user' do - it 'finds the app if the user is watching it' do - watcher - sign_in user - get :show, :id => app.id - expect(controller.app).to eq app - end - - it 'does not find the app if the user is not watching it' do + it 'finds the app even when not watching it' do sign_in Fabricate(:user) app = Fabricate(:app) - expect{ - get :show, :id => app.id - }.to raise_error(Mongoid::Errors::DocumentNotFound) + + get :show, :id => app.id + expect(controller.app).to eq app end end end diff --git a/spec/controllers/devise_sessions_controller_spec.rb b/spec/controllers/devise_sessions_controller_spec.rb index f0814f3..4bf53a0 100644 --- a/spec/controllers/devise_sessions_controller_spec.rb +++ b/spec/controllers/devise_sessions_controller_spec.rb @@ -14,12 +14,6 @@ describe Devise::SessionsController, type: 'controller' do expect(response).to redirect_to(root_path) end - it 'redirects to app page if there is app for the user' do - Fabricate(:user_watcher, :app => app, :user => user) - post :create, { :user => { 'email' => user.email, 'password' => user.password } } - expect(response).to redirect_to(app_path(app)) - end - it 'displays a friendly error when credentials are invalid' do post :create, { :user => { 'email' => 'whatever', 'password' => 'somethinginvalid' } } expect(request.flash["alert"]).to eq(I18n.t 'devise.failure.user.email_invalid') diff --git a/spec/controllers/problems_controller_spec.rb b/spec/controllers/problems_controller_spec.rb index 06bc965..78bbbab 100644 --- a/spec/controllers/problems_controller_spec.rb +++ b/spec/controllers/problems_controller_spec.rb @@ -6,122 +6,94 @@ describe ProblemsController, type: 'controller' do let(:app) { Fabricate(:app) } let(:err) { Fabricate(:err, :problem => problem) } - let(:admin) { Fabricate(:admin) } + let(:user) { Fabricate(:user) } let(:problem) { Fabricate(:problem, :app => app, :environment => "production") } describe "GET /problems" do - context 'when logged in as an admin' do + before(:each) do + sign_in user + @problem = Fabricate(:notice, :err => Fabricate(:err, :problem => Fabricate(:problem, :app => app, :environment => "production"))).problem + end + + context "pagination" do before(:each) do - sign_in admin - @problem = Fabricate(:notice, :err => Fabricate(:err, :problem => Fabricate(:problem, :app => app, :environment => "production"))).problem + 35.times { Fabricate :err } end - context "pagination" do - before(:each) do - 35.times { Fabricate :err } - end - - it "should have default per_page value for user" do - get :index - expect(controller.problems.to_a.size).to eq User::PER_PAGE - end - - it "should be able to override default per_page value" do - admin.update_attribute :per_page, 10 - get :index - expect(controller.problems.to_a.size).to eq 10 - end + it "should have default per_page value for user" do + get :index + expect(controller.problems.to_a.size).to eq User::PER_PAGE end - context 'with environment filters' do - before(:each) do - environments = ['production', 'test', 'development', 'staging'] - 20.times do |i| - Fabricate(:problem, :environment => environments[i % environments.length]) - end - end + it "should be able to override default per_page value" do + user.update_attribute :per_page, 10 + get :index + expect(controller.problems.to_a.size).to eq 10 + end + end - context 'no params' do - it 'shows problems for all environments' do - get :index - expect(controller.problems.size).to eq 21 - end + context 'with environment filters' do + before(:each) do + environments = ['production', 'test', 'development', 'staging'] + 20.times do |i| + Fabricate(:problem, :environment => environments[i % environments.length]) end + end - context 'environment production' do - it 'shows problems for just production' do - get :index, :environment => 'production' - expect(controller.problems.size).to eq 6 - end + context 'no params' do + it 'shows problems for all environments' do + get :index + expect(controller.problems.size).to eq 21 end + end - context 'environment staging' do - it 'shows problems for just staging' do - get :index, :environment => 'staging' - expect(controller.problems.size).to eq 5 - end + context 'environment production' do + it 'shows problems for just production' do + get :index, :environment => 'production' + expect(controller.problems.size).to eq 6 end + end - context 'environment development' do - it 'shows problems for just development' do - get :index, :environment => 'development' - expect(controller.problems.size).to eq 5 - end + context 'environment staging' do + it 'shows problems for just staging' do + get :index, :environment => 'staging' + expect(controller.problems.size).to eq 5 end + end - context 'environment test' do - it 'shows problems for just test' do - get :index, :environment => 'test' - expect(controller.problems.size).to eq 5 - end + context 'environment development' do + it 'shows problems for just development' do + get :index, :environment => 'development' + expect(controller.problems.size).to eq 5 end end - end - context 'when logged in as a user' do - it 'gets a paginated list of unresolved problems for the users apps' do - sign_in(user = Fabricate(:user)) - unwatched_err = Fabricate(:err) - watched_unresolved_err = Fabricate(:err, :problem => Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => false)) - watched_resolved_err = Fabricate(:err, :problem => Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => true)) - get :index - expect(controller.problems).to include(watched_unresolved_err.problem) - expect(controller.problems).to_not include(unwatched_err.problem, watched_resolved_err.problem) + context 'environment test' do + it 'shows problems for just test' do + get :index, :environment => 'test' + expect(controller.problems.size).to eq 5 + end end end end describe "GET /problems - previously all" do - context 'when logged in as an admin' do - it "gets a paginated list of all problems" do - sign_in admin - problems = Kaminari.paginate_array((1..30).to_a) - 3.times { problems << Fabricate(:err).problem } - 3.times { problems << Fabricate(:err, :problem => Fabricate(:problem, :resolved => true)).problem } - expect(Problem).to receive(:ordered_by).and_return( - double('proxy', :page => double('other_proxy', :per => problems)) - ) - get :index, :all_errs => true - expect(controller.problems).to eq problems - end - end - - context 'when logged in as a user' do - it 'gets a paginated list of all problems for the users apps' do - sign_in(user = Fabricate(:user)) - unwatched_problem = Fabricate(:problem) - watched_unresolved_problem = Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => false) - watched_resolved_problem = Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => true) - get :index, :all_errs => true - expect(controller.problems).to include(watched_resolved_problem, watched_unresolved_problem) - expect(controller.problems).to_not include(unwatched_problem) - end + it "gets a paginated list of all problems" do + sign_in Fabricate(:user) + problems = Kaminari.paginate_array((1..30).to_a) + 3.times { problems << Fabricate(:err).problem } + 3.times { problems << Fabricate(:err, :problem => Fabricate(:problem, :resolved => true)).problem } + expect(Problem).to receive(:ordered_by).and_return( + double('proxy', :page => double('other_proxy', :per => problems)) + ) + get :index, :all_errs => true + expect(controller.problems).to eq problems end end describe "GET /problems/search" do before do - sign_in Fabricate(:admin) + sign_in user @app = Fabricate(:app) @problem1 = Fabricate(:problem, :app=>@app, message: "Most important") @problem2 = Fabricate(:problem, :app=>@app, message: "Very very important") @@ -145,73 +117,49 @@ describe ProblemsController, type: 'controller' do end describe "GET /apps/:app_id/problems/:id" do - context 'when logged in as an admin' do - before do - sign_in admin - end - - it "finds the app" do - get :show, :app_id => app.id, :id => err.problem.id - expect(controller.app).to eq app - end - - it "finds the problem" do - get :show, :app_id => app.id, :id => err.problem.id - expect(controller.problem).to eq err.problem - end - - it "successfully render page" do - get :show, :app_id => app.id, :id => err.problem.id - expect(response).to be_success - end - - context 'pagination' do - let!(:notices) do - 3.times.reduce([]) do |coll, i| - coll << Fabricate(:notice, :err => err, :created_at => (Time.now + i)) - end - end + before do + sign_in user + end - it "paginates the notices 1 at a time, starting with the most recent" do - get :show, :app_id => app.id, :id => err.problem.id - expect(assigns(:notices).entries.count).to eq 1 - expect(assigns(:notices)).to include(notices.last) - end + it "finds the app" do + get :show, :app_id => app.id, :id => err.problem.id + expect(controller.app).to eq app + end - it "paginates the notices 1 at a time, based on then notice param" do - get :show, :app_id => app.id, :id => err.problem.id, :notice => 3 - expect(assigns(:notices).entries.count).to eq 1 - expect(assigns(:notices)).to include(notices.first) - end - end + it "finds the problem" do + get :show, :app_id => app.id, :id => err.problem.id + expect(controller.problem).to eq err.problem + end + it "successfully render page" do + get :show, :app_id => app.id, :id => err.problem.id + expect(response).to be_success end - context 'when logged in as a user' do - before do - sign_in(@user = Fabricate(:user)) - @unwatched_err = Fabricate(:err) - @watched_app = Fabricate(:app) - @watcher = Fabricate(:user_watcher, :user => @user, :app => @watched_app) - @watched_err = Fabricate(:err, :problem => Fabricate(:problem, :app => @watched_app)) + context 'pagination' do + let!(:notices) do + 3.times.reduce([]) do |coll, i| + coll << Fabricate(:notice, :err => err, :created_at => (Time.now + i)) + end end - it 'finds the problem if the user is watching the app' do - get :show, :app_id => @watched_app.to_param, :id => @watched_err.problem.id - expect(controller.problem).to eq @watched_err.problem + it "paginates the notices 1 at a time, starting with the most recent" do + get :show, :app_id => app.id, :id => err.problem.id + expect(assigns(:notices).entries.count).to eq 1 + expect(assigns(:notices)).to include(notices.last) end - it 'raises a DocumentNotFound error if the user is not watching the app' do - expect { - get :show, :app_id => @unwatched_err.problem.app_id, :id => @unwatched_err.problem.id - }.to raise_error(Mongoid::Errors::DocumentNotFound) + it "paginates the notices 1 at a time, based on then notice param" do + get :show, :app_id => app.id, :id => err.problem.id, :notice => 3 + expect(assigns(:notices).entries.count).to eq 1 + expect(assigns(:notices)).to include(notices.first) end end end describe "PUT /apps/:app_id/problems/:id/resolve" do before do - sign_in admin + sign_in user @err = Fabricate(:err) end @@ -245,7 +193,7 @@ describe ProblemsController, type: 'controller' do end describe "POST /apps/:app_id/problems/:id/create_issue" do - before { sign_in admin } + before { sign_in user } context "when app has a issue tracker" do let(:notice) { NoticeDecorator.new(Fabricate :notice) } @@ -259,7 +207,7 @@ describe ProblemsController, type: 'controller' do before do problem.app.issue_tracker = issue_tracker allow(controller).to receive(:problem).and_return(problem) - allow(controller).to receive(:current_user).and_return(admin) + allow(controller).to receive(:current_user).and_return(user) end it "should redirect to problem page" do @@ -316,7 +264,7 @@ describe ProblemsController, type: 'controller' do describe "DELETE /apps/:app_id/problems/:id/unlink_issue" do before(:each) do - sign_in admin + sign_in user end context "problem with issue" do @@ -352,7 +300,7 @@ describe ProblemsController, type: 'controller' do describe "Bulk Actions" do before(:each) do - sign_in admin + sign_in user @problem1 = Fabricate(:err, :problem => Fabricate(:problem, :resolved => true)).problem @problem2 = Fabricate(:err, :problem => Fabricate(:problem, :resolved => false)).problem end @@ -434,7 +382,7 @@ describe ProblemsController, type: 'controller' do describe "POST /apps/:app_id/problems/destroy_all" do before do - sign_in Fabricate(:admin) + sign_in user @app = Fabricate(:app) @problem1 = Fabricate(:problem, :app=>@app) @problem2 = Fabricate(:problem, :app=>@app) diff --git a/spec/controllers/watchers_controller_spec.rb b/spec/controllers/watchers_controller_spec.rb index f0545fb..3e8d251 100644 --- a/spec/controllers/watchers_controller_spec.rb +++ b/spec/controllers/watchers_controller_spec.rb @@ -1,32 +1,50 @@ describe WatchersController, type: 'controller' do - let(:app) do - a = Fabricate(:app) - Fabricate(:user_watcher, :app => a) - a - end + let(:user) { Fabricate(:user) } + let(:problem) { Fabricate(:problem) } + + before(:each) { sign_in user } + + describe "#destroy" do + let(:app) do + a = Fabricate(:app) + Fabricate(:user_watcher, app: a, user: user) + a + end + + context "successful watcher deletion" do + let(:watcher) { app.watchers.first } - describe "DELETE /apps/:app_id/watchers/:id/destroy" do - context "with admin user" do before(:each) do - sign_in Fabricate(:admin) + delete :destroy, :app_id => app.id, :id => watcher.user.id.to_s + problem.reload + end + + it "should delete the watcher" do + expect(app.watchers.detect{|w| w.id.to_s == watcher.id }).to be nil end - context "successful watcher deletion" do - let(:problem) { Fabricate(:problem_with_comments) } - let(:watcher) { app.watchers.first } + it "should redirect to app page" do + expect(response).to redirect_to(app_path(app)) + end + end + end - before(:each) do - delete :destroy, :app_id => app.id, :id => watcher.user.id.to_s - problem.reload - end + describe "#update" do + let(:app) { Fabricate(:app) } - it "should delete the watcher" do - expect(app.watchers.detect{|w| w.id.to_s == watcher.id }).to be nil - end + context "successful watcher update" do + before(:each) do + put :update, :app_id => app.id, :id => user.id.to_s + problem.reload + end + + it "should be watching" do + app.reload + expect(app.watchers.first.user_id).to eq user.id + end - it "should redirect to index page" do - expect(response).to redirect_to(root_path) - end + it "should redirect to app page" do + expect(response).to redirect_to(app_path(app)) end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2f65512..e41a798 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -37,24 +37,6 @@ describe User do end end - context 'Watchers' do - it 'has many watchers' do - user = Fabricate(:user) - watcher = Fabricate(:user_watcher, :user => user) - expect(user.watchers).to_not be_empty - expect(user.watchers).to include(watcher) - end - - it "has many apps through watchers" do - user = Fabricate(:user) - watched_app = Fabricate(:app) - unwatched_app = Fabricate(:app) - Fabricate(:user_watcher, :app => watched_app, :user => user) - expect(user.apps.all).to include(watched_app) - expect(user.apps.all).to_not include(unwatched_app) - end - end - context "First user" do it "should be created this admin access via db:seed" do expect { diff --git a/spec/views/apps/show.html.haml_spec.rb b/spec/views/apps/show.html.haml_spec.rb index b8f88e3..cbec694 100644 --- a/spec/views/apps/show.html.haml_spec.rb +++ b/spec/views/apps/show.html.haml_spec.rb @@ -30,7 +30,7 @@ describe "apps/show.html.haml", type: 'view' do context "with user watch application" do before do - allow(user).to receive(:watching?).with(app).and_return(true) + allow(app).to receive(:watched_by?).with(user).and_return(true) end it 'see the unwatch button' do render @@ -40,7 +40,7 @@ describe "apps/show.html.haml", type: 'view' do context "with user not watch application" do before do - allow(user).to receive(:watching?).with(app).and_return(false) + allow(app).to receive(:watched_by?).with(user).and_return(false) end it 'not see the unwatch button' do render -- libgit2 0.21.2