From 96516a8ff365e4ac6909adf89fec817dd91dbce4 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 24 Oct 2014 14:12:47 +1100 Subject: [PATCH] User can switch between trialling a shopfront and not trialling a shopfront --- .../admin/enterprises_controller.rb | 11 ++++- .../admin/enterprises_controller_spec.rb | 42 +++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 8db4101075..b3fad9dc5c 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -19,12 +19,19 @@ module Admin def set_sells enterprise = Enterprise.find(params[:id]) attributes = { sells: params[:sells] } - attributes[:producer_profile_only] = !!params[:producer_profile_only] if params[:sells] == 'none' + attributes[:producer_profile_only] = params[:sells] == "none" && !!params[:producer_profile_only] attributes[:shop_trial_start_date] = Time.now if params[:sells] == "own" if %w(none own).include?(params[:sells]) if params[:sells] == 'own' && enterprise.shop_trial_start_date - flash[:error] = "You've already started your trial!" + expiry = enterprise.shop_trial_start_date + Enterprise::SHOP_TRIAL_LENGTH.days + if Time.now > expiry + flash[:error] = "Sorry, but you've already had a trial. Expired on: #{expiry.strftime('%Y-%m-%d')}" + else + attributes.delete :shop_trial_start_date + enterprise.update_attributes(attributes) + flash[:notice] = "Welcome back! Your trial expires on: #{expiry.strftime('%Y-%m-%d')}" + end elsif enterprise.update_attributes(attributes) flash[:success] = "Congratulations! Registration for #{enterprise.name} is complete!" end diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index 60f0d9f736..1e961e3710 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -147,18 +147,44 @@ module Admin end context "setting 'sells' to 'own'" do - it "is disallowed if a trial already been started" do + before do enterprise.sells = 'own' - enterprise.shop_trial_start_date = Date.today.to_time enterprise.save! - spree_post :set_sells, { id: enterprise.id, sells: 'own' } - expect(response).to redirect_to spree.admin_path - expect(flash[:error]).to eq "You've already started your trial!" - expect(enterprise.reload.sells).to eq 'own' - expect(enterprise.reload.shop_trial_start_date).to eq Date.today.to_time end - context "if a trial has not already been started" do + context "if the trial has finished" do + before do + enterprise.shop_trial_start_date = (Date.today - 30.days).to_time + enterprise.save! + end + + it "is disallowed" do + spree_post :set_sells, { id: enterprise.id, sells: 'own' } + expect(response).to redirect_to spree.admin_path + trial_expiry = Date.today.strftime("%Y-%m-%d") + expect(flash[:error]).to eq "Sorry, but you've already had a trial. Expired on: #{trial_expiry}" + expect(enterprise.reload.sells).to eq 'own' + expect(enterprise.reload.shop_trial_start_date).to eq (Date.today - 30.days).to_time + end + end + + context "if the trial has not finished" do + before do + enterprise.shop_trial_start_date = Date.today.to_time + enterprise.save! + end + + it "is allowed, but trial start date is not reset" do + spree_post :set_sells, { id: enterprise.id, sells: 'own' } + expect(response).to redirect_to spree.admin_path + trial_expiry = (Date.today + 30.days).strftime("%Y-%m-%d") + expect(flash[:notice]).to eq "Welcome back! Your trial expires on: #{trial_expiry}" + expect(enterprise.reload.sells).to eq 'own' + expect(enterprise.reload.shop_trial_start_date).to eq Date.today.to_time + end + end + + context "if a trial has not started" do it "is allowed" do spree_post :set_sells, { id: enterprise.id, sells: 'own' } expect(response).to redirect_to spree.admin_path