Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added specs for invitation token, secure for more edge cases
Damian Legawiec81f44db4bad6
diff --git a/admin/app/controllers/spree/admin/invitations_controller.rb b/admin/app/controllers/spree/admin/invitations_controller.rbindex d37a92621f..e76613d8e8 100644--- a/admin/app/controllers/spree/admin/invitations_controller.rb+++ b/admin/app/controllers/spree/admin/invitations_controller.rb@@ -47,7 +47,16 @@ module Spree# GET /admin/invitations/:id?token=:tokendef show- @invitation = Spree::Invitation.pending.not_expired.find_by!(id: params[:id], token: params[:token])+ begin+ @invitation = Spree::Invitation.pending.not_expired.find_by!(id: params[:id], token: params[:token])++ if try_spree_current_user != @invitation.invitee+ raise ActiveRecord::RecordNotFound+ end+ rescue ActiveRecord::RecordNotFound+ redirect_to spree.root_path, alert: Spree.t('invalid_or_expired_invitation')+ return+ end# if there's no current user, redirect to the new admin user path so they can sign upunless try_spree_current_userdiff --git a/admin/spec/controllers/spree/admin/invitations_controller_spec.rb b/admin/spec/controllers/spree/admin/invitations_controller_spec.rbnew file mode 100644index 0000000000..865f64eb42--- /dev/null+++ b/admin/spec/controllers/spree/admin/invitations_controller_spec.rb@@ -0,0 +1,262 @@+require 'spec_helper'++RSpec.describe Spree::Admin::InvitationsController, type: :controller do+ render_views++ let(:store) { @default_store }+ let(:admin_user) { create(:admin_user) }+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store) }+ let(:role) { Spree::Role.find_or_create_by!(name: 'admin') }++ before do+ allow(spree).to receive(:root_path).and_return('/')+ end++ describe 'GET #index' do+ stub_authorization!++ before do+ invitation+ get :index+ end++ it 'returns a successful response' do+ expect(response).to be_successful+ end++ it 'assigns @search' do+ expect(assigns(:search)).to be_a(Ransack::Search)+ end++ it 'assigns @collection' do+ expect(assigns(:collection)).to be_a(ActiveRecord::Relation)+ expect(assigns(:collection)).to include(invitation)+ end+ end++ describe 'GET #new' do+ stub_authorization!++ before { get :new }++ it 'returns a successful response' do+ expect(response).to be_successful+ end++ it 'assigns @invitation' do+ expect(assigns(:invitation)).to be_a_new(Spree::Invitation)+ end++ it 'sets the resource and inviter' do+ expect(assigns(:invitation).resource).to eq(store)+ expect(assigns(:invitation).inviter).to eq(controller.try_spree_current_user)+ end+ end++ describe 'POST #create' do+ stub_authorization!++ let(:valid_params) do+ {+ invitation: {+ email: '[email protected]',+ expires_at: 1.week.from_now,+ role_ids: [role.id]+ }+ }+ end++ context 'with valid parameters' do+ it 'creates a new invitation' do+ expect {+ post :create, params: valid_params+ }.to change(Spree::Invitation, :count).by(1)+ end++ it 'redirects to invitations path' do+ post :create, params: valid_params+ expect(response).to redirect_to(spree.admin_invitations_path)+ end++ it 'sets a flash message' do+ post :create, params: valid_params+ expect(flash[:notice]).not_to be_nil+ end+ end++ context 'with invalid parameters' do+ let(:invalid_params) do+ {+ invitation: {+ email: ''+ }+ }+ end++ it 'does not create a new invitation' do+ expect {+ post :create, params: invalid_params+ }.not_to change(Spree::Invitation, :count)+ end++ it 'renders the new template' do+ post :create, params: invalid_params+ expect(response).to render_template(:new)+ end++ it 'returns unprocessable entity status' do+ post :create, params: invalid_params+ expect(response).to have_http_status(:unprocessable_entity)+ end+ end+ end++ describe 'GET #show' do+ let(:token) { invitation.token }++ context 'when user is logged in' do+ let(:another_user) { create(:user) }++ before do+ allow(controller).to receive(:try_spree_current_user).and_return(another_user)+ end++ context 'when user is the invitee' do+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store, invitee: another_user) }++ before { get :show, params: { id: invitation.id, token: token } }++ it 'returns a successful response' do+ expect(response).to be_successful+ end++ it 'assigns @invitation' do+ expect(assigns(:invitation)).to eq(invitation)+ end++ it 'renders the show template' do+ expect(response).to render_template(:show)+ expect(response.body).to include(invitation.inviter.name)+ expect(response.body).to include(invitation.resource.name)+ end+ end++ context 'when user is not the invitee' do+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store, invitee: nil) }++ before { get :show, params: { id: invitation.id, token: token } }++ it 'redirects to root path' do+ expect(response).to redirect_to(spree.root_path)+ end++ it 'sets an alert flash message' do+ expect(flash[:alert]).to eq(Spree.t('invalid_or_expired_invitation'))+ end+ end+ end++ context 'when user is not logged in' do+ before do+ allow(controller).to receive(:try_spree_current_user).and_return(nil)+ get :show, params: { id: invitation.id, token: token }+ end++ it 'redirects to new admin user path' do+ expect(response).to redirect_to(spree.new_admin_admin_user_path(token: token))+ end++ context 'with invalid token' do+ let(:token) { 'invalid' }++ it 'redirects to root path' do+ expect(response).to redirect_to(spree.root_path)+ end++ it 'sets an alert flash message' do+ expect(flash[:alert]).to eq(Spree.t('invalid_or_expired_invitation'))+ end+ end++ context 'with expired token' do+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store, expires_at: 1.day.ago) }++ it 'redirects to root path' do+ expect(response).to redirect_to(spree.root_path)+ expect(flash[:alert]).to eq(Spree.t('invalid_or_expired_invitation'))+ end+ end++ context 'with already accepted invitation' do+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store, status: 'accepted') }++ it 'redirects to root path' do+ expect(response).to redirect_to(spree.root_path)+ expect(flash[:alert]).to eq(Spree.t('invalid_or_expired_invitation'))+ end+ end+ end+ end++ describe 'PUT #accept' do+ stub_authorization!++ let(:invitation) { create(:invitation, inviter: admin_user, invitee: another_user, resource: store) }+ let(:another_user) { create(:user) }++ before do+ allow(controller).to receive(:try_spree_current_user).and_return(another_user)+ put :accept, params: { id: invitation.id }+ end++ it 'accepts the invitation' do+ expect(invitation.reload.status).to eq('accepted')+ expect(invitation.accepted_at).to be_present+ end++ it 'redirects to invitations path' do+ expect(response).to redirect_to(spree.admin_invitations_path)+ end++ it 'sets a notice flash message' do+ expect(flash[:notice]).to eq(Spree.t('invitation_accepted'))+ end+ end++ describe 'PUT #resend' do+ stub_authorization!++ it 'resends the invitation' do+ invitation+ clear_enqueued_jobs+ expect { put :resend, params: { id: invitation.id } }.to have_enqueued_job(ActionMailer::MailDeliveryJob)+ end++ it 'redirects to invitations path' do+ put :resend, params: { id: invitation.id }+ expect(response).to redirect_to(spree.admin_invitations_path)+ expect(flash[:notice]).to eq(Spree.t('invitation_resent'))+ end+ end++ describe 'DELETE #destroy' do+ stub_authorization!++ it 'destroys the invitation' do+ invitation # ensure invitation exists+ expect {+ delete :destroy, params: { id: invitation.id }+ }.to change(Spree::Invitation, :count).by(-1)+ end++ it 'redirects to invitations path' do+ delete :destroy, params: { id: invitation.id }+ expect(response).to redirect_to(spree.admin_invitations_path)+ end++ it 'sets a notice flash message' do+ delete :destroy, params: { id: invitation.id }+ expect(flash[:notice]).not_to be_nil+ end+ end+enddiff --git a/core/app/models/spree/invitation.rb b/core/app/models/spree/invitation.rbindex 01b4f33699..6c09c8dbe1 100644--- a/core/app/models/spree/invitation.rb+++ b/core/app/models/spree/invitation.rb@@ -22,8 +22,7 @@ module Spree#validates :email, email: true, presence: truevalidates :token, presence: true, uniqueness: true- validates :inviter, presence: true- validates :roles, presence: true+ validates :inviter, :resource, :roles, presence: truevalidate :invitee_is_not_inviter, on: :createvalidate :invitee_already_exists, on: :creatediff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex d212db88e6..26b9e4d0f9 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1130,6 +1130,7 @@ en:internationalization: Internationalizationinvalid_credit_card: Invalid credit card.invalid_exchange_variant: Invalid exchange variant.+ invalid_or_expired_invitation: Invalid or expired invitationinvalid_payment_provider: Invalid payment provider.invalid_promotion_action: Invalid promotion action.invalid_promotion_rule: Invalid promotion rule.