From 15c1beb460571b489320f58671e99c1f3c5b7e00 Mon Sep 17 00:00:00 2001 From: Vladislav Sokov <62177364+VladislavSokov@users.noreply.github.com> Date: Wed, 30 Aug 2023 19:46:08 +0300 Subject: [PATCH] feat(account invitations): rename account invitation to account share (#42) * feat(account invitations): rename account invitation to account share * feat(account invitations): fixup! rename account invitation to account share * feat(account invitations): fixup! fixup! rename account invitation to account share --------- Co-authored-by: VladislavSokov --- .../accept_account_invitations_controller.rb | 26 --------------- .../accept_account_shares_controller.rb | 26 +++++++++++++++ .../account_invitations_controller.rb | 30 ----------------- app/controllers/account_shares_controller.rb | 32 +++++++++++++++++++ app/controllers/my_accounts_controller.rb | 2 +- app/mailers/account_share_mailer.rb | 9 ++++++ app/mailers/invitation_mailer.rb | 9 ------ app/models/account.rb | 6 ++-- ...account_invitation.rb => account_share.rb} | 2 +- .../show.html.slim | 6 ++-- .../_account_invitation.html.slim | 7 ---- app/views/account_invitations/new.html.erb | 16 ---------- .../account_share.html.erb | 7 ++++ .../account_shares/_account_share.html.slim | 7 ++++ .../index.html.slim | 6 ++-- app/views/account_shares/new.html.erb | 16 ++++++++++ app/views/accounts/show.html.slim | 2 +- .../account_invitation.html.erb | 7 ---- config/routes.rb | 4 +-- ...131000_rename_account_invitations_table.rb | 7 ++++ db/schema.rb | 8 ++--- ..._spec.rb => accept_account_shares_spec.rb} | 20 ++++++------ ...tations_spec.rb => account_shares_spec.rb} | 18 ++++++----- spec/controllers/my_accounts_spec.rb | 14 ++++---- ...count_invitations.rb => account_shares.rb} | 4 ++- spec/mailers/account_share_spec.rb | 18 +++++++++++ spec/mailers/invitations_spec.rb | 18 ----------- 27 files changed, 170 insertions(+), 157 deletions(-) delete mode 100644 app/controllers/accept_account_invitations_controller.rb create mode 100644 app/controllers/accept_account_shares_controller.rb delete mode 100644 app/controllers/account_invitations_controller.rb create mode 100644 app/controllers/account_shares_controller.rb create mode 100644 app/mailers/account_share_mailer.rb delete mode 100644 app/mailers/invitation_mailer.rb rename app/models/{account_invitation.rb => account_share.rb} (84%) rename app/views/{accept_account_invitations => accept_account_shares}/show.html.slim (56%) delete mode 100644 app/views/account_invitations/_account_invitation.html.slim delete mode 100644 app/views/account_invitations/new.html.erb create mode 100644 app/views/account_share_mailer/account_share.html.erb create mode 100644 app/views/account_shares/_account_share.html.slim rename app/views/{account_invitations => account_shares}/index.html.slim (62%) create mode 100644 app/views/account_shares/new.html.erb delete mode 100644 app/views/invitation_mailer/account_invitation.html.erb create mode 100644 db/migrate/20230830131000_rename_account_invitations_table.rb rename spec/controllers/{accept_account_invitations_spec.rb => accept_account_shares_spec.rb} (61%) rename spec/controllers/{account_invitations_spec.rb => account_shares_spec.rb} (69%) rename spec/factories/{account_invitations.rb => account_shares.rb} (80%) create mode 100644 spec/mailers/account_share_spec.rb delete mode 100644 spec/mailers/invitations_spec.rb diff --git a/app/controllers/accept_account_invitations_controller.rb b/app/controllers/accept_account_invitations_controller.rb deleted file mode 100644 index 6ba0d020..00000000 --- a/app/controllers/accept_account_invitations_controller.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -class AcceptAccountInvitationsController < ApplicationController - def show - return if user_signed_in? - - session[:after_sign_in_url] = request.fullpath - - invitee_email = AccountInvitation.find_by!(token: ps.fetch(:token)).email - user = User.find_by(email: invitee_email) - redirect_url = user ? new_user_session_url(email: invitee_email) : new_user_registration_url(email: invitee_email) - - redirect_to redirect_url - end - - def update - received_invitation.update!(accepted_at: Time.current) - redirect_to account_path(received_invitation.account_id) - end - - private - - helper_method memoize def received_invitation - AccountInvitation.unaccepted.for(current_user).find_by!(token: ps.fetch(:token)) - end -end diff --git a/app/controllers/accept_account_shares_controller.rb b/app/controllers/accept_account_shares_controller.rb new file mode 100644 index 00000000..572b73be --- /dev/null +++ b/app/controllers/accept_account_shares_controller.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class AcceptAccountSharesController < ApplicationController + def show + return if user_signed_in? + + session[:after_sign_in_url] = request.fullpath + + shared_email = AccountShare.find_by!(token: ps.fetch(:token)).email + user = User.find_by(email: shared_email) + redirect_url = user ? new_user_session_url(email: shared_email) : new_user_registration_url(email: shared_email) + + redirect_to redirect_url + end + + def update + received_share.update!(accepted_at: Time.current) + redirect_to account_path(received_share.account_id) + end + + private + + helper_method memoize def received_share + AccountShare.unaccepted.for(current_user).find_by!(token: ps.fetch(:token)) + end +end diff --git a/app/controllers/account_invitations_controller.rb b/app/controllers/account_invitations_controller.rb deleted file mode 100644 index 470fb1e2..00000000 --- a/app/controllers/account_invitations_controller.rb +++ /dev/null @@ -1,30 +0,0 @@ -class AccountInvitationsController < ApplicationController - before_action :authenticate_user! - - def index; end - - def new; end - - def create - account_invitation = AccountInvitation.create!(user_id: current_user.id, - account_id: account.id, - token: SecureRandom.urlsafe_base64(32), - **account_invitation_params) - InvitationMailer.account_invitation(account_invitation).deliver - redirect_to account_invitations_path account - end - - def destroy - invitation = AccountInvitation.find(ps.fetch(:id)) - invitation.destroy - respond_to do |format| - format.html { redirect_to account_invitations_url, notice: 'Account invitation was successfully destroyed.' } - end - end - - private - - def account_invitation_params - params.require(:account_invitation).permit(:name, :email) - end -end diff --git a/app/controllers/account_shares_controller.rb b/app/controllers/account_shares_controller.rb new file mode 100644 index 00000000..b0457eb9 --- /dev/null +++ b/app/controllers/account_shares_controller.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +class AccountSharesController < ApplicationController + before_action :authenticate_user! + + def index; end + + def new; end + + def create + personal_account_share = AccountShare.create!(user_id: current_user.id, + account_id: account.id, + token: SecureRandom.urlsafe_base64(32), + **account_share_params) + AccountShareMailer.account_share(personal_account_share).deliver + redirect_to account_shares_path account + end + + def destroy + personal_account_share = AccountShare.find(ps.fetch(:id)) + personal_account_share.destroy + respond_to do |format| + format.html { redirect_to account_shares_url, notice: 'Account share was successfully destroyed.' } + end + end + + private + + def account_share_params + params.require(:account_share).permit(:name, :email) + end +end diff --git a/app/controllers/my_accounts_controller.rb b/app/controllers/my_accounts_controller.rb index 11c6f050..e31a6500 100644 --- a/app/controllers/my_accounts_controller.rb +++ b/app/controllers/my_accounts_controller.rb @@ -12,6 +12,6 @@ def show; end end helper_method memoize def shared_accounts - Account.invitees(current_user) + Account.shared_for(current_user) end end diff --git a/app/mailers/account_share_mailer.rb b/app/mailers/account_share_mailer.rb new file mode 100644 index 00000000..a59ee20a --- /dev/null +++ b/app/mailers/account_share_mailer.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AccountShareMailer < ApplicationMailer + def account_share(account_share) + @account_share = account_share + + mail to: account_share.email, subject: 'Share to manage an account!' + end +end diff --git a/app/mailers/invitation_mailer.rb b/app/mailers/invitation_mailer.rb deleted file mode 100644 index af822048..00000000 --- a/app/mailers/invitation_mailer.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -class InvitationMailer < ApplicationMailer - def account_invitation(account_invitation) - @account_invitation = account_invitation - - mail to: account_invitation.email, subject: 'Invitation to manage an account!' - end -end diff --git a/app/models/account.rb b/app/models/account.rb index 51c21cdb..868dc139 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -4,7 +4,7 @@ class Account < ApplicationRecord has_many :income_transactions, class_name: 'Transaction', foreign_key: :to_account_id has_many :outcome_transactions, class_name: 'Transaction', foreign_key: :from_account_id has_many :children, class_name: 'Account', foreign_key: :parent_id - has_many :account_invitations + has_many :account_shares # an account has user optionally has_one :user @@ -19,10 +19,10 @@ class Account < ApplicationRecord scope :visible_for, lambda { |current_user| where(id: [current_user.account_id] + current_user.account.child_ids + - invitees(current_user).pluck(:id)) + shared_for(current_user).pluck(:id)) } - scope :invitees, ->(user) { where(id: AccountInvitation.accepted.for(user).pluck(:account_id)) } + scope :shared_for, ->(user) { where(id: AccountShare.accepted.for(user).pluck(:account_id)) } memoize def balance income_transactions.sum(:amount) - outcome_transactions.sum(:amount) diff --git a/app/models/account_invitation.rb b/app/models/account_share.rb similarity index 84% rename from app/models/account_invitation.rb rename to app/models/account_share.rb index db7a3c3c..56260489 100644 --- a/app/models/account_invitation.rb +++ b/app/models/account_share.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class AccountInvitation < ApplicationRecord +class AccountShare < ApplicationRecord belongs_to :user belongs_to :account diff --git a/app/views/accept_account_invitations/show.html.slim b/app/views/accept_account_shares/show.html.slim similarity index 56% rename from app/views/accept_account_invitations/show.html.slim rename to app/views/accept_account_shares/show.html.slim index 4edcd4da..30bfc854 100644 --- a/app/views/accept_account_invitations/show.html.slim +++ b/app/views/accept_account_shares/show.html.slim @@ -1,13 +1,13 @@ div.columns.is-centered.is-vcentered div.column.is-two-third h4.subtitle.has-text-centered - | Dear #{received_invitation.name} + | Dear #{received_share.name} h6.subtitle.has-text-centered - | I am delighted to invite you to manage the account + | You've been shared access to manage the account br br div.buttons.is-centered - = link_to 'Accept', accept_account_invitation_path, method: :patch, class: 'button is-success is-medium' + = link_to 'Accept', accept_account_share_path, method: :patch, class: 'button is-success is-medium' span.has-text-grey-light = link_to 'Cancel', root_path, class: 'button is-light is-medium' diff --git a/app/views/account_invitations/_account_invitation.html.slim b/app/views/account_invitations/_account_invitation.html.slim deleted file mode 100644 index e5e6a6a9..00000000 --- a/app/views/account_invitations/_account_invitation.html.slim +++ /dev/null @@ -1,7 +0,0 @@ -tr - td= account_invitation.name - td= account_invitation.email - td= account_invitation.created_at.to_formatted_s(:short) - td= account_invitation.accepted_at&.to_formatted_s(:short) - td= link_to 'link', accept_account_invitation_url(token: account_invitation.token) - td= link_to :delete, account_invitation_path(account, account_invitation), method: :delete, data: {confirm: 'Delete this invitation?'} diff --git a/app/views/account_invitations/new.html.erb b/app/views/account_invitations/new.html.erb deleted file mode 100644 index 4c443f46..00000000 --- a/app/views/account_invitations/new.html.erb +++ /dev/null @@ -1,16 +0,0 @@ -
-
- <%= link_to 'Back', account_invitations_path, class: 'button is-light' %> -
-
-
-
-<%= form_with url: account_invitations_path, method: :post do |form| %> -
- <%= form.label :name %> - <%= form.text_field :name, name: 'account_invitation[name]', type:"text", class: 'input'%> - <%= form.label :email %> - <%= form.email_field :email, name: 'account_invitation[email]', type: "email"%> -
- <%= form.submit :Create, class: 'button is-light' %> -<% end %> diff --git a/app/views/account_share_mailer/account_share.html.erb b/app/views/account_share_mailer/account_share.html.erb new file mode 100644 index 00000000..a86e8f93 --- /dev/null +++ b/app/views/account_share_mailer/account_share.html.erb @@ -0,0 +1,7 @@ +

Dear <%= @account_share.name%>, welcome to managing my account!

+
+

Click the link to approve the share

+<%= link_to 'Go and approve the share', accept_account_share_url(token: @account_share.token) %> +
+ +

Sincerely,

diff --git a/app/views/account_shares/_account_share.html.slim b/app/views/account_shares/_account_share.html.slim new file mode 100644 index 00000000..152637df --- /dev/null +++ b/app/views/account_shares/_account_share.html.slim @@ -0,0 +1,7 @@ +tr + td= account_share.name + td= account_share.email + td= account_share.created_at.to_formatted_s(:short) + td= account_share.accepted_at&.to_formatted_s(:short) + td= link_to 'link', accept_account_share_url(token: account_share.token) + td= link_to :delete, account_share_path(account, account_share), method: :delete, data: {confirm: 'Delete this share?'} diff --git a/app/views/account_invitations/index.html.slim b/app/views/account_shares/index.html.slim similarity index 62% rename from app/views/account_invitations/index.html.slim rename to app/views/account_shares/index.html.slim index 406ed511..92aef9e7 100644 --- a/app/views/account_invitations/index.html.slim +++ b/app/views/account_shares/index.html.slim @@ -1,6 +1,6 @@ h2.title - | Account invitations - = link_to 'New invitation', new_account_invitation_path, class: 'button is-light' + | Account shares + = link_to 'New share', new_account_share_path, class: 'button is-light' .columns .column @@ -18,4 +18,4 @@ h2.title th Accepted th Link th Actions - = render partial: 'account_invitation', collection: account.account_invitations.order(created_at: :desc) + = render partial: 'account_share', collection: account.account_shares.order(created_at: :desc) diff --git a/app/views/account_shares/new.html.erb b/app/views/account_shares/new.html.erb new file mode 100644 index 00000000..000794e9 --- /dev/null +++ b/app/views/account_shares/new.html.erb @@ -0,0 +1,16 @@ +
+
+ <%= link_to 'Back', account_shares_path, class: 'button is-light' %> +
+
+
+
+<%= form_with url: account_shares_path, method: :post do |form| %> +
+ <%= form.label :name %> + <%= form.text_field :name, name: 'account_share[name]', type:"text", class: 'input'%> + <%= form.label :email %> + <%= form.email_field :email, name: 'account_share[email]', type: "email"%> +
+ <%= form.submit :Create, class: 'button is-light' %> +<% end %> diff --git a/app/views/accounts/show.html.slim b/app/views/accounts/show.html.slim index 91c04cd9..b3d071b4 100644 --- a/app/views/accounts/show.html.slim +++ b/app/views/accounts/show.html.slim @@ -4,7 +4,7 @@ | Account details = link_to 'Edit', edit_account_path(account), class: 'button is-light' .column.is-flex.is-justify-content-flex-end - = link_to 'Account invitations', account_invitations_path(account), class: 'button is-light' + = link_to 'Account shares', account_shares_path(account), class: 'button is-light' .columns .column diff --git a/app/views/invitation_mailer/account_invitation.html.erb b/app/views/invitation_mailer/account_invitation.html.erb deleted file mode 100644 index 9f570879..00000000 --- a/app/views/invitation_mailer/account_invitation.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -

Dear <%= @account_invitation.name%>, welcome to managing my account!

-
-

Click the link to approve the invitation

-<%= link_to 'Go and approve the invitation', accept_account_invitation_url(token: @account_invitation.token) %> -
- -

Sincerely,

diff --git a/config/routes.rb b/config/routes.rb index 87ad71b7..338afd37 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,10 +9,10 @@ resource :topup, only: %i[new create] resource :spend, only: %i[new create] resources :objectives, only: %i[new create] - resources :invitations, only: %i[index new create destroy], controller: 'account_invitations' + resources :shares, only: %i[index new create destroy], controller: 'account_shares' end - resources :accept_account_invitations, param: :token, only: %i[show update] + resources :accept_account_shares, param: :token, only: %i[show update] resources :transactions, only: [:destroy] resources :objectives, only: [:destroy] end diff --git a/db/migrate/20230830131000_rename_account_invitations_table.rb b/db/migrate/20230830131000_rename_account_invitations_table.rb new file mode 100644 index 00000000..f531c9c7 --- /dev/null +++ b/db/migrate/20230830131000_rename_account_invitations_table.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class RenameAccountInvitationsTable < ActiveRecord::Migration[6.1] + def change + rename_table :account_invitations, :account_shares + end +end diff --git a/db/schema.rb b/db/schema.rb index a388e61a..32a48a92 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_08_18_090609) do +ActiveRecord::Schema.define(version: 2023_08_30_131000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -25,7 +25,7 @@ t.index ["to_account_id"], name: "index_account_automatic_topup_configs_on_to_account_id" end - create_table "account_invitations", force: :cascade do |t| + create_table "account_shares", force: :cascade do |t| t.bigint "user_id", null: false t.string "email" t.string "name" @@ -86,8 +86,8 @@ add_foreign_key "account_automatic_topup_configs", "accounts", column: "from_account_id" add_foreign_key "account_automatic_topup_configs", "accounts", column: "to_account_id" - add_foreign_key "account_invitations", "accounts", on_delete: :cascade - add_foreign_key "account_invitations", "users", on_delete: :cascade + add_foreign_key "account_shares", "accounts", on_delete: :cascade + add_foreign_key "account_shares", "users", on_delete: :cascade add_foreign_key "accounts", "accounts", column: "parent_id" add_foreign_key "objectives", "accounts" add_foreign_key "transactions", "accounts", column: "from_account_id" diff --git a/spec/controllers/accept_account_invitations_spec.rb b/spec/controllers/accept_account_shares_spec.rb similarity index 61% rename from spec/controllers/accept_account_invitations_spec.rb rename to spec/controllers/accept_account_shares_spec.rb index 5201dbcb..27a018b6 100644 --- a/spec/controllers/accept_account_invitations_spec.rb +++ b/spec/controllers/accept_account_shares_spec.rb @@ -2,31 +2,31 @@ require 'rails_helper' -RSpec.describe AcceptAccountInvitationsController, type: :controller do +RSpec.describe AcceptAccountSharesController, type: :controller do describe '#show' do let(:account) { create(:account, :parent) } let(:user) { create(:user, account: account) } let(:second_user) { create(:user) } - let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) } + let!(:account_share) { create(:account_share, user: user, account: account, email: second_user.email) } subject do - get :show, params: { token: account_invitation.token } + get :show, params: { token: account_share.token } end context 'when the user not signed in' do it 'redirects to new user session url with email' do subject - expect(response).to redirect_to(new_user_session_url(email: account_invitation.email)) + expect(response).to redirect_to(new_user_session_url(email: account_share.email)) end end context 'when the user not signed up' do - let(:account_invitation) { create(:account_invitation, user: user, account: account) } + let(:account_share) { create(:account_share, user: user, account: account) } it 'redirects to registration url with email' do subject - expect(response).to redirect_to(new_user_registration_url(email: account_invitation.email)) + expect(response).to redirect_to(new_user_registration_url(email: account_share.email)) end end @@ -43,19 +43,19 @@ let(:user) { create(:user, account: account) } let(:second_user) { create(:user, account: account) } - let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) } + let!(:account_share) { create(:account_share, user: user, account: account, email: second_user.email) } subject do - patch :update, params: { token: account_invitation.token } + patch :update, params: { token: account_share.token } end before { sign_in second_user } it 'set the taime' do subject - expect(AccountInvitation.find(account_invitation.id).accepted_at).not_to be_nil + expect(AccountShare.find(account_share.id).accepted_at).not_to be_nil end it { is_expected.to have_http_status(302) } - it { is_expected.to redirect_to(account_path(account_invitation.account_id)) } + it { is_expected.to redirect_to(account_path(account_share.account_id)) } end end diff --git a/spec/controllers/account_invitations_spec.rb b/spec/controllers/account_shares_spec.rb similarity index 69% rename from spec/controllers/account_invitations_spec.rb rename to spec/controllers/account_shares_spec.rb index 169b6a15..d18df3e6 100644 --- a/spec/controllers/account_invitations_spec.rb +++ b/spec/controllers/account_shares_spec.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe AccountInvitationsController, type: :controller do +RSpec.describe AccountSharesController, type: :controller do describe '#index' do let(:account) { create(:account, :parent) } let(:user) { create(:user, account: account) } @@ -30,7 +32,7 @@ let(:user) { create(:user, account: account) } subject do - post :create, params: { account_invitation: { + post :create, params: { account_share: { name: FFaker::Name.first_name, email: FFaker::Internet.email, token: SecureRandom.urlsafe_base64(32) @@ -39,20 +41,20 @@ before { sign_in user } - it { is_expected.to redirect_to(account_invitations_path) } + it { is_expected.to redirect_to(account_shares_path) } it { is_expected.to have_http_status(302) } - it { expect { subject }.to change { AccountInvitation.where(user_id: user.id).count }.by(1) } - it { expect { subject }.to change { AccountInvitation.count }.by(1) } + 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_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) } + let!(:account_share) { create(:account_share, user: user, account: account, email: second_user.email) } - subject { delete :destroy, params: { account_id: account.id, id: account_invitation } } + subject { delete :destroy, params: { account_id: account.id, id: account_share } } before { sign_in user } - it { expect { subject }.to change { AccountInvitation.count }.from(1).to(0) } + it { expect { subject }.to change { AccountShare.count }.from(1).to(0) } end end diff --git a/spec/controllers/my_accounts_spec.rb b/spec/controllers/my_accounts_spec.rb index 691cea4b..6ae70a3f 100644 --- a/spec/controllers/my_accounts_spec.rb +++ b/spec/controllers/my_accounts_spec.rb @@ -17,19 +17,19 @@ end describe '#shared_accounts' do - let(:invitee_user) { create(:user) } + let(:shared_user) { create(:user) } before do - sign_in invitee_user - create(:account_invitation, user: user, account: account, email: invitee_user.email, accepted_at: accepted_at) + sign_in shared_user + create(:account_share, user: user, account: account, email: shared_user.email, accepted_at: accepted_at) end - context 'when invitation accepted' do + context 'when account share accepted' do let(:accepted_at) { Time.current } - it { expect(controller.send(:shared_accounts, invitee_user)).to include(account) } + it { expect(controller.send(:shared_accounts, shared_user)).to include(account) } end - context 'when invitation unaccepted' do + context 'when account share unaccepted' do let(:accepted_at) { nil } - it { expect(controller.send(:shared_accounts, invitee_user)).to be_empty } + it { expect(controller.send(:shared_accounts, shared_user)).to be_empty } end end end diff --git a/spec/factories/account_invitations.rb b/spec/factories/account_shares.rb similarity index 80% rename from spec/factories/account_invitations.rb rename to spec/factories/account_shares.rb index 891f3422..8cb3c3c7 100644 --- a/spec/factories/account_invitations.rb +++ b/spec/factories/account_shares.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + FactoryBot.define do - factory :account_invitation do + factory :account_share do association :user, factory: :user name { FFaker::Name.first_name } email { FFaker::Internet.email } diff --git a/spec/mailers/account_share_spec.rb b/spec/mailers/account_share_spec.rb new file mode 100644 index 00000000..5f33f1ec --- /dev/null +++ b/spec/mailers/account_share_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountShareMailer, type: :mailer do + describe 'account share' do + let(:account) { create(:account, :parent) } + let(:user) { create(:user, account: account) } + let(:shared_user) { create(:user) } + let!(:account_share) { create(:account_share, user: user, account: account, email: shared_user.email) } + + subject(:mail) { AccountShareMailer.account_share(account_share).deliver } + + it { expect(mail.to).to eq([shared_user.email]) } + it { expect(mail.subject).to eq('Share to manage an account!') } + it { expect(mail.body.encoded).to match(account_share.name) } + end +end diff --git a/spec/mailers/invitations_spec.rb b/spec/mailers/invitations_spec.rb deleted file mode 100644 index 8058a21b..00000000 --- a/spec/mailers/invitations_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe InvitationMailer, type: :mailer do - describe 'account invitation' do - let(:account) { create(:account, :parent) } - let(:user) { create(:user, account: account) } - let(:invitee_user) { create(:user) } - let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: invitee_user.email) } - - subject(:mail) { InvitationMailer.account_invitation(account_invitation).deliver } - - it { expect(mail.to).to eq([invitee_user.email]) } - it { expect(mail.subject).to eq('Invitation to manage an account!') } - it { expect(mail.body.encoded).to match(account_invitation.name) } - end -end