+ );
+ }
+}
diff --git a/client/app/widgets/QuickCreate/index.jsx b/client/app/widgets/QuickCreate/index.jsx
index 6d530c075f..db5c4cd135 100644
--- a/client/app/widgets/QuickCreate/index.jsx
+++ b/client/app/widgets/QuickCreate/index.jsx
@@ -5,7 +5,7 @@ import { Input } from '../../components/Input';
import type { Checkbox } from '../../components/Input';
import { Utils } from '../../utils';
import css from './QuickCreate.scss';
-import { QuickCreateForm } from './QuickCreateForm';
+import { DynamicForm } from '../../components/Form/DynamicForm';
// value - e.g. category.id
// label - e.g. category.name
@@ -35,7 +35,7 @@ const alpha = (a: string, b: string) => {
return 0;
};
-const sortAlpha = (checkboxes: Checkbox[]) =>
+const sortAlpha = (checkboxes: Checkbox[]): Checkbox[] =>
// eslint-disable-next-line implicit-arrow-linebreak
checkboxes.sort((a: Checkbox, b: Checkbox) => alpha(a.label, b.label));
@@ -71,30 +71,36 @@ export class QuickCreate extends React.Component {
).length;
};
- onCreate = (checkbox: { label: string, value: number, id: string }) => {
- const { label, value, id } = checkbox;
- this.setState((prevState: State) => {
- const { checkboxes } = prevState;
- checkboxes.push({
- id,
- label,
- value,
- checked: true,
- });
- return {
+ addToCheckboxes = (data: { name: string, id: string, slug: string }) => {
+ const { checkboxes } = this.state;
+ const { name, id, slug } = data;
+ const newCheckboxes = checkboxes.slice(0);
+ newCheckboxes.push({
+ id: slug,
+ label: name,
+ value: id,
+ checked: true,
+ });
+ return sortAlpha(newCheckboxes);
+ };
+
+ onCreate = (response: any) => {
+ const { data } = response;
+ if (data && data.success) {
+ this.setState({
open: false,
accordionOpen: true,
modalKey: Utils.randomString(),
tagKey: Utils.randomString(),
- checkboxes: sortAlpha(checkboxes),
- };
- });
+ checkboxes: this.addToCheckboxes(data),
+ });
+ }
};
displayQuickCreateForm = (nameValue: string) => {
const { formProps } = this.props;
return (
- 'errors#not_found'
get '/500' => 'errors#internal_server_error'
- resources :allies, :except => [:show, :new, :create, :edit, :update, :destroy] do
+ resources :allies, except: [:show, :new, :create, :edit, :update, :destroy] do
collection do
post 'add'
post 'remove'
end
end
+ resources :comment, only: [:create, :destroy] do
+ collection do
+ post 'create'
+ delete 'delete'
+ end
+ end
+
resources :medications
resources :moods do
@@ -28,21 +35,14 @@
end
end
- resources :moments do
- collection do
- post 'comment'
- get 'delete_comment'
- end
- end
+ resources :moments
resources :secret_shares, only: [:create, :show, :destroy]
resources :strategies do
collection do
- post 'comment'
post 'premade'
post 'quick_create'
- get 'delete_comment'
end
end
@@ -58,20 +58,18 @@
collection do
get 'join'
get 'leave'
- post 'comment'
- get 'delete_comment'
end
end
- resources :profile, :except => [:show, :new, :create, :edit, :update, :destroy]
+ resources :profile, except: [:show, :new, :create, :edit, :update, :destroy]
- resources :search, :except => [:show, :new, :create, :edit, :update, :destroy] do
+ resources :search, except: [:show, :new, :create, :edit, :update, :destroy] do
collection do
get 'posts'
end
end
- resources :notifications, :except => [:show, :new, :create, :edit, :update] do
+ resources :notifications, except: [:show, :new, :create, :edit, :update] do
collection do
delete 'clear'
get 'fetch_notifications'
@@ -89,10 +87,10 @@
match 'press', to: 'pages#press', via: :get
match 'resources', to: 'pages#resources', via: :get
- devise_for :users, :controllers => { :registrations => :registrations,
- :omniauth_callbacks => 'omniauth_callbacks',
- :invitations => 'users/invitations',
- :sessions => :sessions }
+ devise_for :users, controllers: { registrations: :registrations,
+ omniauth_callbacks: 'omniauth_callbacks',
+ invitations: 'users/invitations',
+ sessions: :sessions }
post 'pusher/auth'
@@ -100,60 +98,5 @@
get locale.to_s => 'locales#set_initial_locale', defaults: { locale: locale.to_s }
end
- # The priority is based upon order of creation: first created -> highest priority.
- # See how all your routes lay out with "rake routes".
-
- # You can have the root of your site routed with "root"
- # root 'welcome#index'
-
- # Example of regular route:
- # get 'products/:id' => 'catalog#view'
-
- # Example of named route that can be invoked with purchase_url(id: product.id)
- # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
-
- # Example resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Example resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Example resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Example resource route with more complex sub-resources:
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', on: :collection
- # end
- # end
-
- # Example resource route with concerns:
- # concern :toggleable do
- # post 'toggle'
- # end
- # resources :posts, concerns: :toggleable
- # resources :photos, concerns: :toggleable
-
- # Example resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
root 'pages#home'
end
diff --git a/spec/controllers/comment_controller_spec.rb b/spec/controllers/comment_controller_spec.rb
new file mode 100644
index 0000000000..17cb8d7986
--- /dev/null
+++ b/spec/controllers/comment_controller_spec.rb
@@ -0,0 +1,319 @@
+# frozen_string_literal: true
+
+describe CommentController do
+ let(:user) { create(:user) }
+
+ describe 'POST create' do
+ let(:comment) { build(:comment, comment_by: user.id, commentable_type: commentable_type) }
+
+ context 'from a moment' do
+ let(:commentable) { create(:moment, user: user) }
+ let(:commentable_type) { 'moment' }
+ let(:valid_comment_params) do
+ { comment: comment.attributes.merge(commentable_id: commentable.id, visibility: 'all') }
+ end
+ let(:invalid_comment_params) { { comment: comment.attributes } }
+
+ context 'when the user is logged in' do
+ include_context :logged_in_user
+
+ context 'when the comment is saved' do
+ it 'responds with an OK status' do
+ post :create, params: valid_comment_params
+ expect(response.status).to eq(200)
+ end
+ end
+
+ context 'when the comment is not saved' do
+ it 'renders correct response' do
+ post :create, params: invalid_comment_params
+ expect(response.body).to eq({}.to_json)
+ end
+ end
+ end
+
+ context 'when the user is not logged in' do
+ before { post :create }
+ it_behaves_like :with_no_logged_in_user
+ end
+ end
+
+ context 'from a strategy' do
+ let(:commentable) { create(:strategy, user: user) }
+ let(:commentable_type) { 'strategy' }
+ let(:valid_comment_params) do
+ { comment: comment.attributes.merge(commentable_id: commentable.id, visibility: 'all') }
+ end
+ let(:invalid_comment_params) { { comment: comment.attributes } }
+
+ context 'when the user is logged in' do
+ include_context :logged_in_user
+
+ context 'when the comment is saved' do
+ it 'responds with an OK status' do
+ post :create, params: valid_comment_params
+ expect(response.status).to eq(200)
+ end
+ end
+
+ context 'when the comment is not saved' do
+ it 'renders correct response' do
+ post :create, params: invalid_comment_params
+ expect(response.body).to eq({}.to_json)
+ end
+ end
+ end
+
+ context 'when the user is not logged in' do
+ before { post :create }
+ it_behaves_like :with_no_logged_in_user
+ end
+ end
+
+ context 'from a meeting' do
+ let(:commentable) { create(:meeting) }
+ let(:commentable_type) { 'meeting' }
+ let(:valid_comment_params) do
+ { comment: comment.attributes.merge(commentable_id: commentable.id) }
+ end
+ let(:invalid_comment_params) { { comment: comment.attributes } }
+
+ context 'when the user is logged in' do
+ include_context :logged_in_user
+
+ context 'when the comment is saved' do
+ it 'responds with an OK status' do
+ post :create, params: valid_comment_params
+ expect(response.status).to eq(200)
+ end
+ end
+
+ context 'when the comment is not saved' do
+ it 'renders correct response' do
+ post :create, params: invalid_comment_params
+ expect(response.body).to eq({}.to_json)
+ end
+ end
+ end
+
+ context 'when the user is not logged in' do
+ before { post :create }
+
+ it_behaves_like :with_no_logged_in_user
+ end
+ end
+ end
+
+ describe 'DELETE delete' do
+ context 'from a moment' do
+ let(:commentable_type) { 'moment' }
+
+ context 'when the user is logged in' do
+ include_context :logged_in_user
+
+ context 'when the comment exists and belongs to the current_user' do
+ let!(:commentable) { create(:moment, user_id: user.id) }
+ let!(:comment) { create(
+ :comment,
+ comment_by: user.id,
+ commentable_id: commentable.id,
+ visibility: 'all',
+ commentable_type: commentable_type
+ ) }
+
+ it 'destroys the comment' do
+ expect { delete :delete, params: { comment_id: comment.id } }.to(
+ change(Comment, :count).by(-1)
+ )
+ end
+
+ it 'renders correct response' do
+ delete :delete, params: { comment_id: comment.id }
+ expect(response.body).to eq({ id: comment.id }.to_json)
+ end
+ end
+
+ context 'when the comment exists and the commentable belongs to the current_user' do
+ let!(:commentable) { create(:moment, user_id: user.id) }
+ let!(:comment) do
+ create(
+ :comment,
+ comment_by: user.id,
+ commentable_id: commentable.id,
+ visibility: 'all',
+ commentable_type: commentable_type
+ )
+ end
+
+ it 'destroys the comment' do
+ expect { delete :delete, params: { comment_id: comment.id } }.to(
+ change(Comment, :count).by(-1)
+ )
+ end
+
+ it 'renders correct response' do
+ comment
+ delete :delete, params: { comment_id: comment.id }
+ expect(response.body).to eq({ id: comment.id }.to_json)
+ end
+ end
+
+ context 'when the comment does not exist' do
+ it 'renders correct response' do
+ delete :delete, params: { comment_id: 0 }
+ expect(response.body).to eq({}.to_json)
+ end
+ end
+ end
+
+ context 'when the user is not logged in' do
+ before do
+ delete :delete
+ end
+ it_behaves_like :with_no_logged_in_user
+ end
+ end
+
+ context 'from a strategy' do
+ let(:commentable_type) { 'strategy' }
+
+ context 'when the user is logged in' do
+ include_context :logged_in_user
+
+ context 'when the comment exists and belongs to the current_user' do
+ let!(:commentable) { create(:strategy, user_id: user.id) }
+ let!(:comment) { create(
+ :comment,
+ comment_by: user.id,
+ commentable_id: commentable.id,
+ visibility: 'all',
+ commentable_type: commentable_type
+ ) }
+
+ it 'destroys the comment' do
+ expect { delete :delete, params: { comment_id: comment.id } }.to(
+ change(Comment, :count).by(-1)
+ )
+ end
+
+ it 'renders correct response' do
+ delete :delete, params: { comment_id: comment.id }
+ expect(response.body).to eq({ id: comment.id }.to_json)
+ end
+ end
+
+ context 'when the comment exists and the commentable belongs to the current_user' do
+ let!(:commentable) { create(:strategy, user_id: user.id) }
+ let!(:comment) do
+ create(
+ :comment,
+ comment_by: user.id,
+ commentable_id: commentable.id,
+ visibility: 'all',
+ commentable_type: commentable_type
+ )
+ end
+
+ it 'destroys the comment' do
+ expect { delete :delete, params: { comment_id: comment.id } }.to(
+ change(Comment, :count).by(-1)
+ )
+ end
+
+ it 'renders correct response' do
+ comment
+ delete :delete, params: { comment_id: comment.id }
+ expect(response.body).to eq({ id: comment.id }.to_json)
+ end
+ end
+
+ context 'when the comment does not exist' do
+ it 'renders correct response' do
+ delete :delete, params: { comment_id: 0 }
+ expect(response.body).to eq({}.to_json)
+ end
+ end
+ end
+
+ context 'when the user is not logged in' do
+ before do
+ delete :delete
+ end
+ it_behaves_like :with_no_logged_in_user
+ end
+ end
+
+ context 'from a meeting' do
+ let(:commentable) { create(:meeting) }
+ let(:commentable_type) { 'meeting' }
+ let!(:meeting_member) do
+ create(:meeting_member, meeting_id: commentable.id, user: user, leader: true)
+ end
+ let!(:comment) do
+ create(
+ :comment,
+ comment_by: user.id,
+ commentable_id: commentable.id,
+ commentable_type: commentable_type,
+ visibility: 'all'
+ )
+ end
+
+ context 'when the user is logged in' do
+ include_context :logged_in_user
+
+ context 'when the comment exists and belongs to the current_user' do
+ it 'destroys the comment' do
+ expect { delete :delete, params: { comment_id: comment.id } }.to(
+ change(Comment, :count).by(-1)
+ )
+ end
+
+ it 'renders correct response' do
+ delete :delete, params: { comment_id: comment.id }
+ expect(response.body).to eq({ id: comment.id }.to_json)
+ end
+ end
+
+ context 'when the comment exists and the commentable belongs to the current_user' do
+ let!(:comment) do
+ create(
+ :comment,
+ comment_by: user.id,
+ commentable_id: commentable.id,
+ visibility: 'all',
+ commentable_type: commentable_type
+ )
+ end
+
+ it 'destroys the comment' do
+ expect { delete :delete, params: { comment_id: comment.id } }.to(
+ change(Comment, :count).by(-1)
+ )
+ end
+
+ it 'renders correct response' do
+ comment
+ delete :delete, params: { comment_id: comment.id }
+ expect(response.body).to eq({ id: comment.id }.to_json)
+ end
+ end
+
+ context 'when the comment does not exist' do
+ it 'renders correct response' do
+ delete :delete, params: { comment_id: 0 }
+ expect(response.body).to eq({}.to_json)
+ end
+ end
+ end
+
+ context 'when the user is not logged in' do
+ before do
+ delete :delete
+ end
+
+ it_behaves_like :with_no_logged_in_user
+ end
+ end
+ end
+end
diff --git a/spec/controllers/meetings_controller_spec.rb b/spec/controllers/meetings_controller_spec.rb
index 52c288f7a4..2d8ee0f555 100644
--- a/spec/controllers/meetings_controller_spec.rb
+++ b/spec/controllers/meetings_controller_spec.rb
@@ -6,10 +6,9 @@
# TODO: implement session controller
# it_behaves_like 'LoggedOut'
describe 'GET' do
- %w[join leave comment delete_comment].each do |action|
+ %w[join leave].each do |action|
it "#{action} redirects to login" do
get action
-
expect(response).to redirect_to('/users/sign_in')
end
end
@@ -70,114 +69,6 @@
end
end
- describe 'POST #comment' do
- let(:user) { create(:user) }
- let(:meeting) { create(:meeting) }
- let(:comment) { build(:comment, comment_by: user.id, commentable_type: 'meeting') }
- let(:valid_comment_params) do
- comment.attributes.merge(commentable_id: meeting.id)
- end
- let(:invalid_comment_params) { comment.attributes }
-
- context 'when the user is logged in' do
- include_context :logged_in_user
-
- context 'when the comment is saved' do
- it 'responds with an OK status' do
- post :comment, params: valid_comment_params
- expect(response.status).to eq(200)
- end
- end
-
- context 'when the comment is not saved' do
- it 'responds with json no_save: true' do
- post :comment, params: invalid_comment_params
- expect(response.body).to eq({ no_save: true }.to_json)
- end
- end
- end
-
- context 'when the user is not logged in' do
- before { post :comment }
-
- it_behaves_like :with_no_logged_in_user
- end
- end
-
- describe 'GET #delete_comment' do
- let(:user) { create(:user, id: 1) }
- let(:meeting) { create(:meeting, id: 1) }
- let!(:meeting_member) do
- create(:meeting_member, meeting: meeting, user: user, leader: true)
- end
- let!(:comment) do
- create(
- :comment,
- id: 1,
- comment_by: 1,
- commentable_id: 1,
- commentable_type: 'meeting',
- visibility: 'all'
- )
- end
-
- context 'when the user is logged in' do
- include_context :logged_in_user
-
- context 'when the comment exists and belongs to the current_user' do
- it 'destroys the comment' do
- expect { get :delete_comment, params: { commentid: 1 } }.to(
- change(Comment, :count).by(-1)
- )
- end
-
- it 'renders nothing' do
- get :delete_comment, params: { commentid: 1 }
-
- expect(response.body).to eq('')
- end
- end
-
- context 'when the comment exists and the strategy belongs to the current_user' do
- let!(:comment) do
- create(
- :comment, id: 1, comment_by: 1, commentable_id: 1, visibility: 'all'
- )
- end
-
- let!(:new_moment) { create(:moment, id: 1, user_id: 1) }
-
- it 'destroys the comment' do
- expect { get :delete_comment, params: { commentid: 1 } }.to(
- change(Comment, :count).by(-1)
- )
- end
-
- it 'renders nothing' do
- comment
- get :delete_comment, params: { commentid: 1 }
-
- expect(response.body).to eq('')
- end
- end
-
- context 'when the comment does not exist' do
- it 'renders nothing' do
- get :delete_comment, params: { commentid: 99 }
- expect(response.body).to eq('')
- end
- end
- end
-
- context 'when the user is not logged in' do
- before do
- get :delete_comment
- end
-
- it_behaves_like :with_no_logged_in_user
- end
- end
-
describe 'GET #new' do
let!(:user) { create(:user) }
let!(:group_member) do
diff --git a/spec/controllers/moments_controller_spec.rb b/spec/controllers/moments_controller_spec.rb
index 3e4d52e786..e064f2affd 100644
--- a/spec/controllers/moments_controller_spec.rb
+++ b/spec/controllers/moments_controller_spec.rb
@@ -78,107 +78,6 @@ def post_create(moment_params)
end
end
- describe 'POST comment' do
- let(:user) { create(:user) }
- let(:moment) { create(:moment, user: user) }
- let(:comment) { build(:comment, comment_by: user.id) }
- let(:valid_comment_params) do
- comment.attributes.merge(commentable_id: moment.id, visibility: 'all')
- end
- let(:invalid_comment_params) { comment.attributes }
-
- context 'when the user is logged in' do
- include_context :logged_in_user
-
- context 'when the comment is saved' do
- it 'responds with an OK status' do
- post :comment, params: valid_comment_params
- expect(response.status).to eq(200)
- end
- end
-
- context 'when the comment is not saved' do
- it 'responds with json no_save: true' do
- post :comment, params: invalid_comment_params
- expect(response.body).to eq({ no_save: true }.to_json)
- end
- end
- end
-
- context 'when the user is not logged in' do
- before { post :comment }
-
- it_behaves_like :with_no_logged_in_user
- end
- end
-
- describe 'GET delete_comment' do
- let(:user) { create(:user, id: 1) }
-
- context 'when the user is logged in' do
- include_context :logged_in_user
-
- context 'when the comment exists and belongs to the current_user' do
- let!(:new_moment) { create(:moment, id: 1, user_id: 1) }
- let!(:comment) do
- create(
- :comment, id: 1, comment_by: 1, commentable_id: 1, visibility: 'all'
- )
- end
-
- it 'destroys the comment' do
- expect { get :delete_comment, params: { commentid: 1 } }.to(
- change(Comment, :count).by(-1)
- )
- end
-
- it 'renders nothing' do
- get :delete_comment, params: { commentid: 1 }
-
- expect(response.body).to eq('')
- end
- end
-
- context 'when the comment exists and the strategy belongs to the current_user' do
- let!(:comment) do
- create(
- :comment, id: 1, comment_by: 1, commentable_id: 1, visibility: 'all'
- )
- end
- let!(:new_moment) { create(:moment, id: 1, user_id: 1) }
-
- it 'destroys the comment' do
- expect { get :delete_comment, params: { commentid: 1 } }.to(
- change(Comment, :count).by(-1)
- )
- end
-
- it 'renders nothing' do
- comment
- get :delete_comment, params: { commentid: 1 }
-
- expect(response.body).to eq('')
- end
- end
-
- context 'when the comment does not exist' do
- it 'renders nothing' do
- get :delete_comment, params: { commentid: 1 }
-
- expect(response.body).to eq('')
- end
- end
- end
-
- context 'when the user is not logged in' do
- before do
- get :delete_comment
- end
-
- it_behaves_like :with_no_logged_in_user
- end
- end
-
describe 'Moment Analytic Charts' do
it 'should contain react analytics objects' do
create_time = Date.current
@@ -188,9 +87,7 @@ def post_create(moment_params)
new_mood = create(:mood, user_id: new_user.id)
create(:moment, user_id: new_user.id, category: Array.new(1, new_category.id),
mood: Array.new(1, new_mood.id), created_at: create_time)
-
get :index
-
expect(assigns(:react_moments)).to have_key(create_time)
expect(assigns(:react_moments)[create_time]).to eq(1)
end
diff --git a/spec/controllers/secret_shares_controller_spec.rb b/spec/controllers/secret_shares_controller_spec.rb
index 5a36887ac2..5cfedb5052 100644
--- a/spec/controllers/secret_shares_controller_spec.rb
+++ b/spec/controllers/secret_shares_controller_spec.rb
@@ -18,6 +18,7 @@ def do_post_create
it 'Creates Secret Share Identifier' do
expect(moment.reload.secret_share_identifier).not_to be_nil
+ expect(response).to redirect_to moment_path(moment, anchor: 'secretShare')
end
end
diff --git a/spec/controllers/strategies_controller_spec.rb b/spec/controllers/strategies_controller_spec.rb
index 04cc3b9be2..ee4ed3aff2 100644
--- a/spec/controllers/strategies_controller_spec.rb
+++ b/spec/controllers/strategies_controller_spec.rb
@@ -73,95 +73,6 @@
end
end
- describe 'POST comment' do
- let(:comment) do
- build(:comment, comment_by: user.id, commentable_type: 'strategy')
- end
- let(:valid_comment_params) do
- comment.attributes.merge(
- 'commentable_id' => strategy.id, 'visibility' => 'all'
- )
- end
- let(:invalid_comment_params) { comment.attributes }
-
- context 'when the user is logged in' do
- include_context :logged_in_user
-
- context 'when the comment is saved' do
- it 'responds with an OK status' do
- post :comment, params: valid_comment_params
- expect(response).to have_http_status(:ok)
- end
- end
-
- context 'when the comment is not saved' do
- it 'responds with json no_save: true' do
- post :comment, params: invalid_comment_params
- expect(response.body).to eq({ no_save: true }.to_json)
- end
- end
- end
-
- context 'when the user is not logged in' do
- before { post :comment }
- it_behaves_like :with_no_logged_in_user
- end
- end
-
- describe 'GET delete_comment' do
- context 'when the user is logged in' do
- include_context :logged_in_user
-
- context 'when the comment exists' do
- let!(:comment) do
- create(
- :comment,
- comment_by: user.id,
- commentable_id: strategy.id,
- visibility: 'all'
- )
- end
-
- context 'when the comment belongs to the current_user' do
- it 'destroys the comment' do
- expect { get :delete_comment, params: { commentid: comment.id } }
- .to change(Comment, :count).by(-1)
- end
-
- it 'renders nothing' do
- get :delete_comment, params: { commentid: comment.id }
- expect(response.body).to be_empty
- end
- end
-
- context 'when the strategy belongs to the current_user' do
- it 'destroys the comment' do
- expect { get :delete_comment, params: { commentid: comment.id } }
- .to change(Comment, :count).by(-1)
- end
-
- it 'renders nothing' do
- comment
- get :delete_comment, params: { commentid: 1 }
- expect(response.body).to be_empty
- end
- end
- end
-
- context 'when the comment does not exist' do
- it 'renders nothing' do
- get :delete_comment, params: { commentid: 1 }
- expect(response.body).to be_empty
- end
- end
- end
-
- context 'when the user is not logged in' do
- before { get :delete_comment }
- it_behaves_like :with_no_logged_in_user
- end
- end
-
describe 'POST premade' do
context 'when the user is logged in' do
include_context :logged_in_user
diff --git a/spec/features/user_creates_a_draft_moment_spec.rb b/spec/features/user_creates_a_draft_moment_spec.rb
index 71a25a7ba6..8afb6547a9 100644
--- a/spec/features/user_creates_a_draft_moment_spec.rb
+++ b/spec/features/user_creates_a_draft_moment_spec.rb
@@ -100,7 +100,7 @@
expect(page).to have_content 'Some New Strategy'
find('.storyActionsViewers').hover
expect(page).to have_content 'Ally 1'
- expect(page).to have_css('#new_comment')
+ expect(page).not_to have_css('#comments')
expect(page).to have_selector '.storyDraft'
back = current_url
@@ -153,6 +153,7 @@
expect(find('.pageTitle')).to have_content 'My New Moment'
expect(page).to have_content moment_why_text
expect(page).not_to have_selector '.storyDraft'
+ expect(page).to have_css('#comments')
# TRYING TO VIEW AS ALLY
login_as ally
diff --git a/spec/features/user_creates_a_draft_strategy_spec.rb b/spec/features/user_creates_a_draft_strategy_spec.rb
index 4399637dc2..36550f8a06 100644
--- a/spec/features/user_creates_a_draft_strategy_spec.rb
+++ b/spec/features/user_creates_a_draft_strategy_spec.rb
@@ -65,7 +65,7 @@
find('.storyActionsViewers').hover
expect(page).to have_content 'Ally 1'
expect(page).to have_content 'Daily reminder email'
- expect(page).to have_css('#new_comment')
+ expect(page).not_to have_css('#comments')
expect(page).to have_selector '.storyDraft'
back = current_url
@@ -104,6 +104,7 @@
expect(find('.pageTitle')).to have_content 'My New Strategy'
expect(page).to have_content strategy_description_text
expect(page).not_to have_selector '.storyDraft'
+ expect(page).to have_css('#comments')
# TRYING TO VIEW AS ALLY
login_as ally
diff --git a/spec/features/user_creates_a_published_moment_spec.rb b/spec/features/user_creates_a_published_moment_spec.rb
index a7d2c4b61a..d464bc81c9 100644
--- a/spec/features/user_creates_a_published_moment_spec.rb
+++ b/spec/features/user_creates_a_published_moment_spec.rb
@@ -108,7 +108,7 @@
expect(page).to have_content 'Some New Strategy'
find('.storyActionsViewers').hover
expect(page).to have_content 'Ally 0, Ally 1, and Ally 2'
- expect(page).to have_css('#new_comment')
+ expect(page).to have_css('#comments')
expect(page).not_to have_selector '.storyDraft'
# EDITING
diff --git a/spec/features/user_creates_a_published_strategy_spec.rb b/spec/features/user_creates_a_published_strategy_spec.rb
index 9f5b114cca..a435bf105b 100644
--- a/spec/features/user_creates_a_published_strategy_spec.rb
+++ b/spec/features/user_creates_a_published_strategy_spec.rb
@@ -70,7 +70,7 @@
find('.storyActionsViewers').hover
expect(page).to have_content 'Ally 0, Ally 1, and Ally 2'
expect(page).to have_content 'Daily reminder email'
- expect(page).to have_css('#new_comment')
+ expect(page).to have_css('#comments')
expect(page).not_to have_selector '.storyDraft'
# EDITING
diff --git a/spec/helpers/comment_form_helper_spec.rb b/spec/helpers/comment_form_helper_spec.rb
new file mode 100644
index 0000000000..aed9bc6cef
--- /dev/null
+++ b/spec/helpers/comment_form_helper_spec.rb
@@ -0,0 +1,228 @@
+# frozen_string_literal: true
+
+describe CommentFormHelper, type: :controller do
+ controller(ApplicationController) do
+ end
+
+ describe '#comment_form_props' do
+ let(:user1) { create(:user1) }
+ let(:user2) { create(:user2) }
+ let(:submit_input) do
+ {
+ id: 'submit',
+ type: 'submit',
+ value: 'Submit',
+ dark: true
+ }
+ end
+
+ def default_inputs(comment_by, commentable_id, commentable_type)
+ [
+ {
+ id: 'comment_commentable_type',
+ name: 'comment[commentable_type]',
+ type: 'hidden',
+ value: commentable_type
+ },
+ {
+ id: 'comment_comment_by',
+ name: 'comment[comment_by]',
+ type: 'hidden',
+ value: comment_by
+ },
+ {
+ id: 'comment_commentable_id',
+ name: 'comment[commentable_id]',
+ type: 'hidden',
+ value: commentable_id
+ },
+ {
+ id: 'comment_comment',
+ name: 'comment[comment]',
+ type: 'textarea',
+ dark: true,
+ value: nil,
+ required: true,
+ label: 'Comment'
+ },
+ ]
+ end
+
+ def non_owner_inputs(commentable_type)
+ {
+ inputs: default_inputs(user1.id, commentable.id, commentable_type).concat([
+ options: [
+ {
+ id: 'comment_visibility_all',
+ label: 'Share with everyone',
+ value: 'all'
+ },
+ {
+ id: 'comment_visibility_private',
+ label: 'Share with Plum Blossom only',
+ value: 'private'
+ }
+ ],
+ id: 'comment_visibility',
+ name: 'comment[visibility]',
+ type: 'select',
+ value: 'all',
+ dark: true
+ ]).concat([submit_input]),
+ action: comment_index_path,
+ noFormTag: true
+ }
+ end
+
+ def owner_has_no_viewers_inputs(commentable_type)
+ {
+ inputs: default_inputs(user1.id, commentable.id, commentable_type).concat([submit_input]),
+ action: comment_index_path,
+ noFormTag: true
+
+ }
+ end
+
+ def owner_has_viewers_inputs(commentable_type)
+ {
+ inputs: default_inputs(user1.id, commentable.id, commentable_type).concat([
+ options: [
+ {
+ id: 'comment_viewers_everyone',
+ label: 'Share with everyone',
+ value: ''
+ },
+ {
+ id: "comment_viewers_#{user2.id}",
+ label: 'Share with Plum Blossom only',
+ value: user2.id
+ }
+ ],
+ id: 'comment_viewers',
+ name: 'comment[viewers]',
+ type: 'select',
+ value: '',
+ dark: true
+ ]).concat([submit_input]),
+ action: comment_index_path,
+ noFormTag: true
+ }
+ end
+
+ subject { controller.comment_form_props(commentable, commentable_type) }
+
+ before do
+ allow(controller).to receive(:current_user).and_return(user1)
+ end
+
+ context 'commentable type is a moment' do
+ let(:commentable_type) { 'moment' }
+
+ context 'user is the commentable owner' do
+ let(:commentable) { create(:moment, user_id: user1.id, comment: true, viewers: viewers) }
+
+ context 'has no viewers' do
+ let(:viewers) { nil }
+
+ it 'returns correct props' do
+ expect(subject).to eq(owner_has_no_viewers_inputs('moment'))
+ end
+ end
+
+ context 'has viewers' do
+ let(:viewers) { [user2.id] }
+
+ it 'returns correct props' do
+ expect(subject).to eq(owner_has_viewers_inputs('moment'))
+ end
+ end
+ end
+
+ context 'user is not the commentable owner' do
+ let(:commentable) { create(:moment, user_id: user2.id, comment: true, viewers: viewers) }
+
+ context 'has no viewers' do
+ let(:viewers) { nil }
+
+ it 'returns correct props' do
+ expect(subject).to eq(non_owner_inputs('moment'))
+ end
+ end
+
+ context 'has viewers' do
+ let(:viewers) { [user2.id] }
+
+ it 'returns correct props' do
+ expect(subject).to eq(non_owner_inputs('moment'))
+ end
+ end
+ end
+ end
+
+ context 'commentable type is a strategy' do
+ let(:commentable_type) { 'strategy' }
+
+ context 'user is the commentable owner' do
+ let(:commentable) { create(:strategy, user_id: user1.id, comment: true, viewers: viewers) }
+
+ context 'has no viewers' do
+ let(:viewers) { nil }
+
+ it 'returns correct props' do
+ expect(subject).to eq(owner_has_no_viewers_inputs('strategy'))
+ end
+ end
+
+ context 'has viewers' do
+ let(:viewers) { [user2.id] }
+
+ it 'returns correct props' do
+ expect(subject).to eq(owner_has_viewers_inputs('strategy'))
+ end
+ end
+ end
+
+ context 'user is not the commentable owner' do
+ let(:commentable) { create(:strategy, user_id: user2.id, comment: true, viewers: viewers) }
+
+ context 'has no viewers' do
+ let(:viewers) { nil }
+
+ it 'returns correct props' do
+ expect(subject).to eq(non_owner_inputs('strategy'))
+ end
+ end
+
+ context 'has viewers' do
+ let(:viewers) { [user2.id] }
+
+ it 'returns correct props' do
+ expect(subject).to eq(non_owner_inputs('strategy'))
+ end
+ end
+ end
+ end
+
+ context 'commentable type is a meeting' do
+ let(:commentable_type) { 'meeting' }
+
+ context 'user is a meeting leader' do
+ let(:commentable) { create(:meeting) }
+ let(:meeting_members) { create(:meeting_members, user_id: user1.id, meeting_id: commentable.id, leader: true)}
+
+ it 'returns correct props' do
+ expect(subject).to eq(owner_has_no_viewers_inputs('meeting'))
+ end
+ end
+
+ context 'user is not a meeting leader' do
+ let(:commentable) { create(:meeting) }
+ let(:meeting_members) { create(:meeting_members, user_id: user1.id, meeting_id: commentable.id, leader: false)}
+
+ it 'returns correct props' do
+ expect(subject).to eq(owner_has_no_viewers_inputs('meeting'))
+ end
+ end
+ end
+ end
+end
diff --git a/spec/helpers/comments_helper_spec.rb b/spec/helpers/comments_helper_spec.rb
index 0bfd157c05..e11cc774b0 100644
--- a/spec/helpers/comments_helper_spec.rb
+++ b/spec/helpers/comments_helper_spec.rb
@@ -7,17 +7,10 @@
controller(ApplicationController) do
end
- describe 'generate_comment' do
+ describe '#generate_comments' do
let(:user3) { create(:user3) }
let(:comment) { 'Hello from the outside' }
-
- def delete_comment(comment_id)
- %(
)
- end
-
- def comment_info(user)
- %(#{user.name} - less than a minute ago)
- end
+ let(:created_at) { 'Created less than a minute ago' }
before do
create(:allyships_accepted, user_id: user1.id, ally_id: user2.id)
@@ -34,26 +27,30 @@ def comment_info(user)
it 'generates a valid comment object when visbility is all' do
new_comment = create(:comment, comment: comment, commentable_type: 'moment', commentable_id: new_moment.id, comment_by: user1.id, visibility: 'all')
- expect(controller.generate_comment(new_comment, 'moment')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user1),
- comment_text: comment,
- visibility: nil,
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user1.uid,
+ commentByName: user1.name,
+ commentByAvatar: user1.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: nil,
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
it 'generates a valid comment object when visbility is private' do
new_comment = create(:comment, comment: comment, commentable_type: 'moment', commentable_id: new_moment.id, comment_by: user1.id, visibility: 'private', viewers: [user2.id])
- expect(controller.generate_comment(new_comment, 'moment')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user1),
- comment_text: comment,
- visibility: "Visible only between you and #{user2.name}",
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user1.uid,
+ commentByName: user1.name,
+ commentByAvatar: user1.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: "Visible only between you and #{user2.name}",
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
end
@@ -64,26 +61,30 @@ def comment_info(user)
it 'generates a valid comment object when visbility is all' do
new_comment = create(:comment, comment: comment, commentable_type: 'moment', commentable_id: new_moment.id, comment_by: user2.id, visibility: 'all')
- expect(controller.generate_comment(new_comment, 'moment')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user2),
- comment_text: comment,
- visibility: nil,
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user2.uid,
+ commentByName: user2.name,
+ commentByAvatar: user2.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: nil,
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
it 'generates a valid comment object when visbility is private' do
new_comment = create(:comment, comment: comment, commentable_type: 'moment', commentable_id: new_moment.id, comment_by: user2.id, visibility: 'private', viewers: [user1.id])
- expect(controller.generate_comment(new_comment, 'moment')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user2),
- comment_text: comment,
- visibility: "Visible only between you and #{user1.name}",
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user2.uid,
+ commentByName: user2.name,
+ commentByAvatar: user2.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: "Visible only between you and #{user1.name}",
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
end
end
@@ -98,26 +99,30 @@ def comment_info(user)
it 'generates a valid comment object when visbility is all' do
new_comment = create(:comment, comment: comment, commentable_type: 'strategy', commentable_id: new_strategy.id, comment_by: user1.id, visibility: 'all')
- expect(controller.generate_comment(new_comment, 'strategy')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user1),
- comment_text: comment,
- visibility: nil,
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user1.uid,
+ commentByName: user1.name,
+ commentByAvatar: user1.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: nil,
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
it 'generates a valid comment object when visbility is private' do
new_comment = create(:comment, comment: comment, commentable_type: 'strategy', commentable_id: new_strategy.id, comment_by: user1.id, visibility: 'private', viewers: [user2.id])
- expect(controller.generate_comment(new_comment, 'strategy')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user1),
- comment_text: comment,
- visibility: "Visible only between you and #{user2.name}",
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user1.uid,
+ commentByName: user1.name,
+ commentByAvatar: user1.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: "Visible only between you and #{user2.name}",
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
end
@@ -128,26 +133,30 @@ def comment_info(user)
it 'generates a valid comment object when visbility is all' do
new_comment = create(:comment, comment: comment, commentable_type: 'strategy', commentable_id: new_strategy.id, comment_by: user2.id, visibility: 'all')
- expect(controller.generate_comment(new_comment, 'strategy')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user2),
- comment_text: comment,
- visibility: nil,
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user2.uid,
+ commentByName: user2.name,
+ commentByAvatar: user2.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: nil,
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
it 'generates a valid comment object when visbility is private' do
new_comment = create(:comment, comment: comment, commentable_type: 'strategy', commentable_id: new_strategy.id, comment_by: user2.id, visibility: 'private', viewers: [user1.id])
- expect(controller.generate_comment(new_comment, 'strategy')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user2),
- comment_text: comment,
- visibility: "Visible only between you and #{user1.name}",
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user2.uid,
+ commentByName: user2.name,
+ commentByAvatar: user2.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: "Visible only between you and #{user1.name}",
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
end
end
@@ -164,14 +173,16 @@ def comment_info(user)
it 'generates a valid comment object' do
sign_in user1
new_comment = create(:comment, comment: comment, commentable_type: 'meeting', commentable_id: new_meeting.id, comment_by: user1.id, visibility: 'all')
- expect(controller.generate_comment(new_comment, 'meeting')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user1),
- comment_text: comment,
- visibility: nil,
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user1.uid,
+ commentByName: user1.name,
+ commentByAvatar: user1.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: nil,
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
end
@@ -179,14 +190,16 @@ def comment_info(user)
it 'generates a valid comment object' do
sign_in user2
new_comment = create(:comment, comment: comment, commentable_type: 'meeting', commentable_id: new_meeting.id, comment_by: user2.id, visibility: 'all')
- expect(controller.generate_comment(new_comment, 'meeting')).to include(
- commentid: new_comment.id,
- comment_info: comment_info(user2),
- comment_text: comment,
- visibility: nil,
- delete_comment: delete_comment(new_comment.id),
- no_save: false
- )
+ expect(controller.generate_comments(Comment.where(id: new_comment.id))).to eq([{
+ id: new_comment.id,
+ commentByUid: user2.uid,
+ commentByName: user2.name,
+ commentByAvatar: user2.avatar.url,
+ comment: comment,
+ createdAt: created_at,
+ viewers: nil,
+ deleteAction: delete_comment_index_path(comment_id: new_comment.id)
+ }])
end
end
end
diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb
index 4336aefbc8..5b88e53918 100644
--- a/spec/models/comment_spec.rb
+++ b/spec/models/comment_spec.rb
@@ -85,6 +85,58 @@
end
end
+ describe 'comments' do
+ before do
+ create(
+ :comment,
+ commentable_id: 0,
+ commentable_type: commentable_type,
+ comment_by: user2.id,
+ comment: short_comment,
+ visibility: 'all'
+ )
+ create(
+ :comment,
+ commentable_id: commentable.id,
+ commentable_type: commentable_type,
+ comment_by: user2.id,
+ comment: short_comment,
+ visibility: 'all'
+ )
+ end
+
+ context 'when commentable type is a moment' do
+ let(:commentable) { create(:moment, comment: true, user_id: user1.id, viewers: [user2.id]) }
+ let(:commentable_type) { 'moment' }
+
+ it 'returns correct number of comments' do
+ expect(Comment.comments_from(commentable).count).to eq(1)
+ expect(Comment.count).to eq(2)
+ end
+ end
+
+ context 'when commentable type is a strategy' do
+ let(:commentable) { create(:strategy, comment: true, user_id: user1.id, viewers: [user2.id]) }
+ let(:commentable_type) { 'strategy' }
+
+ it 'returns correct number of comments' do
+ expect(Comment.comments_from(commentable).count).to eq(1)
+ expect(Comment.count).to eq(2)
+ end
+ end
+
+ context 'when commentable type is a meeting' do
+ let(:commentable) { create(:meeting) }
+ let(:commentable_type) { 'meeting' }
+
+ it 'returns correct number of comments' do
+ create :meeting_member, user_id: user1.id, leader: true, meeting_id: commentable.id
+ expect(Comment.comments_from(commentable).count).to eq(1)
+ expect(Comment.count).to eq(2)
+ end
+ end
+ end
+
describe 'notify_of_creation!' do
context 'Moments' do
it 'does not send a notification when the user created both the Moment and comment' do
diff --git a/spec/services/comment_viewers_service_spec.rb b/spec/services/comment_viewers_service_spec.rb
new file mode 100644
index 0000000000..27b223af3d
--- /dev/null
+++ b/spec/services/comment_viewers_service_spec.rb
@@ -0,0 +1,311 @@
+# frozen_string_literal: true
+
+describe CommentViewersService do
+ let(:owner) { FactoryBot.create(:user2, :with_allies) }
+ let(:ally) { owner.allies.first }
+ let(:ally_commenter) { owner.allies.second }
+ let(:strategy) { FactoryBot.create(:strategy, user_id: owner.id) }
+ let(:moment) { FactoryBot.create(:moment, user_id: owner.id) }
+ let(:commentable) do
+ {
+ strategy: strategy,
+ moment: moment
+ }
+ end
+ %i[strategy moment].each do |commentable_name|
+ let(:my_commentable) { commentable[commentable_name] }
+ let(:comment) do
+ Comment.create!(commentable_type: commentable_name,
+ commentable_id: my_commentable.id,
+ comment_by: commenter.id,
+ comment: 'test comment',
+ visibility: visibility,
+ viewers: viewers)
+ end
+ end
+
+ describe '#viewers' do
+ subject { CommentViewersService.viewers(comment, current_user) }
+
+ context 'private comments (visible to you and 1 ally)' do
+ let(:visibility) { 'private' }
+
+ context 'and comment was made by owner' do
+ let(:commenter) { owner }
+ let(:viewers) { [ally.id] }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it "has the ally's name in visibility" do
+ expect(subject).to eq("Visible only between you and #{ally.name}")
+ end
+ end
+
+ context 'logged in as ally' do
+ let(:current_user) { ally }
+
+ it "has the owner's name in visibility" do
+ expect(subject).to eq("Visible only between you and #{owner.name}")
+ end
+ end
+ end
+
+ context 'and comment was made by an ally' do
+ let(:commenter) { ally_commenter }
+ let(:viewers) { [] }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it "has the ally's name in visibility" do
+ expect(subject).to eq("Visible only between you and #{ally_commenter.name}")
+ end
+ end
+
+ context 'logged in as commenter' do
+ let(:current_user) { ally_commenter }
+
+ it "has the owner's name in visibility" do
+ expect(subject).to eq("Visible only between you and #{owner.name}")
+ end
+ end
+ end
+ end
+
+ context 'public comments (visible to all allies)' do
+ let(:visibility) { 'all' }
+ let(:viewers) { [] }
+
+ context 'and comment was made by owner' do
+ let(:commenter) { owner }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'has nothing for visibility' do
+ expect(subject).to be_nil
+ end
+ end
+
+ context 'logged in as ally' do
+ let(:current_user) { ally }
+
+ it 'has nothing for visibility' do
+ expect(subject).to be_nil
+ end
+ end
+ end
+
+ context 'and comment was made by ally' do
+ let(:commenter) { ally_commenter }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'has nothing for visibility' do
+ expect(subject).to be_nil
+ end
+ end
+
+ context 'logged in as commenter' do
+ let(:current_user) { commenter }
+
+ it 'has nothing for visibility' do
+ expect(subject).to be_nil
+ end
+ end
+ end
+ end
+ end
+
+ describe '#viewable?' do
+ subject { CommentViewersService.viewable?(comment, current_user) }
+
+ context 'private comments (visible to you and 1 ally)' do
+ let(:visibility) { 'private' }
+
+ context 'and comment was made by owner' do
+ let(:commenter) { owner }
+ let(:viewers) { [ally.id] }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as ally' do
+ let(:current_user) { ally }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+ end
+
+ context 'and comment was made by an ally' do
+ let(:commenter) { ally_commenter }
+ let(:viewers) { [] }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as commenter' do
+ let(:current_user) { ally_commenter }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+ end
+ end
+
+ context 'public comments (visible to all allies)' do
+ let(:visibility) { 'all' }
+ let(:viewers) { [] }
+
+ context 'and comment was made by owner' do
+ let(:commenter) { owner }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as ally' do
+ let(:current_user) { ally }
+
+ it 'returns false' do
+ expect(subject).to eq(false)
+ end
+ end
+ end
+
+ context 'and comment was made by ally' do
+ let(:commenter) { ally_commenter }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as commenter' do
+ let(:current_user) { commenter }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+ end
+ end
+ end
+
+ describe '#deletable?' do
+ subject { CommentViewersService.deletable?(comment, current_user) }
+
+ context 'private comments (visible to you and 1 ally)' do
+ let(:visibility) { 'private' }
+
+ context 'and comment was made by owner' do
+ let(:commenter) { owner }
+ let(:viewers) { [ally.id] }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as ally' do
+ let(:current_user) { ally }
+
+ it 'returns false' do
+ expect(subject).to eq(false)
+ end
+ end
+ end
+
+ context 'and comment was made by an ally' do
+ let(:commenter) { ally_commenter }
+ let(:viewers) { [] }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as commenter' do
+ let(:current_user) { ally_commenter }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+ end
+ end
+
+ context 'public comments (visible to all allies)' do
+ let(:visibility) { 'all' }
+ let(:viewers) { [] }
+
+ context 'and comment was made by owner' do
+ let(:commenter) { owner }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as ally' do
+ let(:current_user) { ally }
+
+ it 'returns false' do
+ expect(subject).to eq(false)
+ end
+ end
+ end
+
+ context 'and comment was made by ally' do
+ let(:commenter) { ally_commenter }
+
+ context 'logged in as owner' do
+ let(:current_user) { owner }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+
+ context 'logged in as commenter' do
+ let(:current_user) { commenter }
+
+ it 'returns true' do
+ expect(subject).to eq(true)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/services/comment_visibility_spec.rb b/spec/services/comment_visibility_spec.rb
deleted file mode 100644
index 29b3fc978f..0000000000
--- a/spec/services/comment_visibility_spec.rb
+++ /dev/null
@@ -1,125 +0,0 @@
-# frozen_string_literal: true
-
-describe CommentVisibility do
- describe '#build' do
- let(:owner) { FactoryBot.create(:user2, :with_allies) }
- let(:ally) { owner.allies.first }
- let(:ally_commenter) { owner.allies.second }
-
- let(:strategy) { FactoryBot.create(:strategy, user_id: owner.id) }
- let(:moment) { FactoryBot.create(:moment, user_id: owner.id) }
-
- let(:commentable) do
- {
- strategy: strategy,
- moment: moment
- }
- end
-
- %i[strategy moment].each do |commentable_name|
- let(:commentable_id) { commentable[commentable_name] }
-
- let(:comment) do
- Comment.create!(commentable_type: commentable_name,
- commentable_id: commentable_id.id,
- comment_by: commenter.id,
- comment: 'test comment',
- visibility: visibility,
- viewers: viewers)
- end
-
- subject { CommentVisibility.build(comment, commentable_id, current_user) }
-
- describe 'private comments (visible to you and 1 ally)' do
- let(:visibility) { 'private' }
-
- describe 'and comment was made by owner' do
- let(:commenter) { owner }
- let(:viewers) { [ally.id] }
-
- describe 'logged in as owner' do
- let(:current_user) { owner }
-
- it "has the ally's name in visibility" do
- expect(subject).to eq("Visible only between you and #{ally.name}")
- end
- end
-
- describe 'logged in as ally' do
- let(:current_user) { ally }
-
- it "has the owner's name in visibility" do
- expect(subject).to eq("Visible only between you and #{owner.name}")
- end
- end
- end
-
- describe 'and comment was made by an ally' do
- let(:commenter) { ally_commenter }
- let(:viewers) { [] }
-
- describe 'logged in as owner' do
- let(:current_user) { owner }
-
- it "has the ally's name in visibility" do
- expect(subject).to eq("Visible only between you and #{ally_commenter.name}")
- end
- end
-
- describe 'logged in as commenter' do
- let(:current_user) { ally_commenter }
-
- it "has the owner's name in visibility" do
- expect(subject).to eq("Visible only between you and #{owner.name}")
- end
- end
- end
- end
-
- describe 'public comments (visible to all allies)' do
- let(:visibility) { 'all' }
- let(:viewers) { [] }
-
- describe 'and comment was made by owner' do
- let(:commenter) { owner }
-
- describe 'logged in as owner' do
- let(:current_user) { owner }
-
- it 'has nothing for visibility' do
- expect(subject).to be_nil
- end
- end
-
- describe 'logged in as ally' do
- let(:current_user) { ally }
-
- it 'has nothing for visibility' do
- expect(subject).to be_nil
- end
- end
- end
-
- describe 'and comment was made by ally' do
- let(:commenter) { ally_commenter }
-
- describe 'logged in as owner' do
- let(:current_user) { owner }
-
- it 'has nothing for visibility' do
- expect(subject).to be_nil
- end
- end
-
- describe 'logged in as commenter' do
- let(:current_user) { commenter }
-
- it 'has nothing for visibility' do
- expect(subject).to be_nil
- end
- end
- end
- end
- end
- end
-end