Skip to content

Commit

Permalink
feat(public account share): fixup! fixup! fixup! fixup! fixup! fixup!…
Browse files Browse the repository at this point in the history
… fixup! fixup! fixup! add public account share
  • Loading branch information
VladislavSokov committed Sep 6, 2023
1 parent 4735080 commit 18a395c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 42 deletions.
14 changes: 12 additions & 2 deletions app/controllers/account_shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class AccountSharesController < ApplicationController
before_action :authenticate_user!
before_action :account_share, only: [:destroy]

def index; end

Expand All @@ -17,8 +18,9 @@ def create
end

def destroy
personal_account_share = AccountShare.find(ps.fetch(:id))
personal_account_share.destroy
return unless accessible?

account_share.destroy
respond_to do |format|
format.html { redirect_to account_shares_url, notice: 'Account share was successfully destroyed.' }
end
Expand All @@ -29,4 +31,12 @@ def destroy
def account_share_params
params.require(:account_share).permit(:name, :email)
end

memoize def account_share
AccountShare.find(params.fetch(:id))
end

def accessible?
current_user.id == account_share.user_id || current_user.email == account_share.email
end
end
8 changes: 0 additions & 8 deletions app/controllers/public_account_shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@ def create
AccountShare.create!(user_id: current_user.id, account_id: account.id, accepted_at: Time.current)
redirect_to account_shares_path account
end

def destroy
public_account_share = AccountShare.find(ps.fetch(:id))
public_account_share.destroy
respond_to do |format|
format.html { redirect_to account_shares_url, notice: 'Account share was successfully destroyed.' }
end
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
resource :spend, only: %i[new create]
resources :objectives, only: %i[new create]
resources :shares, only: %i[index new create destroy], controller: 'account_shares'
resources :public_account_shares, only: %i[new create destroy]
resources :public_account_shares, only: %i[new create]
end

resources :accept_account_shares, param: :token, only: %i[show update]
Expand Down
39 changes: 18 additions & 21 deletions spec/controllers/account_shares_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@
require 'rails_helper'

RSpec.describe AccountSharesController, type: :controller do
describe '#index' do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }

subject(:index) { get :index, params: { account_id: account.id } }
before { sign_in user }

before { sign_in user }
describe '#index' do
subject(:index) { get :index, params: { account_id: account.id } }

it { is_expected.to have_http_status(:success) }
it { is_expected.to render_template(:index) }
end

describe '#new' do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }

subject(:new) { get :new, params: { account_id: account.id } }

before { sign_in user }

it { is_expected.to have_http_status(:success) }
it { is_expected.to render_template(:new) }
end

describe '#create' do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }

subject do
post :create, params: { account_share: {
name: FFaker::Name.first_name,
Expand All @@ -39,22 +31,27 @@
}, account_id: account.id }
end

before { sign_in user }

it { is_expected.to redirect_to(account_shares_path) }
it { is_expected.to have_http_status(302) }
it { expect { subject }.to change { AccountShare.where(user_id: user.id).count }.by(1) }
it { expect { subject }.to change { AccountShare.count }.by(1) }
end

describe '#destroy' do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }
let(:second_user) { create(:user) }
let!(:account_share) { create(:account_share, user: user, account: account, email: second_user.email) }
let!(:account_share) { create(:account_share, user: user, account: account, email: FFaker::Internet.email) }

subject { delete :destroy, params: { account_id: account.id, id: account_share } }
before { sign_in user }
it { expect { subject }.to change { AccountShare.count }.from(1).to(0) }

context 'when user have access to destroy the share' do
it { expect { subject }.to change { AccountShare.count }.by(-1) }
it { is_expected.to redirect_to(account_shares_path) }
end

context 'when user not to have access to destroy the share' do
let(:another_user) { create(:user) }
before { sign_in another_user }

it { expect { subject }.not_to(change { AccountShare.count }) }
end
end
end
10 changes: 0 additions & 10 deletions spec/controllers/public_account_shares_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,4 @@
it { expect { subject }.to change { AccountShare.where(user_id: user.id).count }.by(1) }
it { expect { subject }.to change { AccountShare.count }.by(1) }
end

describe '#destroy' do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }
let!(:account_share) { create(:account_share, user: user, account: account) }

subject { delete :destroy, params: { account_id: account.id, id: account_share } }
before { sign_in user }
it { expect { subject }.to change { AccountShare.count }.by(-1) }
end
end

0 comments on commit 18a395c

Please sign in to comment.