diff --git a/app/controllers/identity/emails_controller.rb b/app/controllers/identity/emails_controller.rb index bed9ed5..b0aac76 100644 --- a/app/controllers/identity/emails_controller.rb +++ b/app/controllers/identity/emails_controller.rb @@ -18,6 +18,7 @@ def update private def set_user @user = Current.user + redirect_to root_path, notice: "You can't change the demo email address" if @user.email == "user@demo.test" end def user_params diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index df99cb1..51af5d9 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -16,6 +16,7 @@ def update private def set_user @user = Current.user + redirect_to root_path, notice: "You can't change the demo password" if @user.email == "user@demo.test" end def user_params diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 200b567..9911411 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,6 +3,7 @@ # Sessions Controller class SessionsController < ApplicationController skip_before_action :authenticate, only: %i[new create] + before_action :prep_demo, only: :create before_action :set_session, only: :destroy @@ -13,8 +14,8 @@ def index def new; end def create - if (user = User.authenticate_by(email: params[:email], password: params[:password])) - @session = user.sessions.create! + if @user ||= User.authenticate_by(email: params[:email], password: params[:password]) + @session = @user.sessions.create! cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true } redirect_to workspaces_path, notice: "Signed in successfully" @@ -22,7 +23,6 @@ def create redirect_to sign_in_path(email_hint: params[:email]), notice: "That email or password is incorrect" end end - # rubocop:enable Metrics/AbcSize def destroy @session.destroy @@ -33,4 +33,10 @@ def destroy def set_session @session = Current.user.sessions.find(params[:id]) end + + def prep_demo + if params[:email] == "user@demo.test" + @user = DemoPrep.initialize_demo("user@demo.test") + end + end end diff --git a/app/services/demo_prep.rb b/app/services/demo_prep.rb new file mode 100644 index 0000000..90bd1ce --- /dev/null +++ b/app/services/demo_prep.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class DemoPrep + def self.initialize_demo(email) + user = User.find_by(email: email) + if !user + user = FactoryBot.create(:user_with_workspace, email: email) + populate_workspace(user.workspaces.first) + elsif user.created_at.to_date != Date.today + user.workspaces.each do |workspace| + workspace.destroy + end + workspace = user.workspaces.create(name: "Workspace 1") + populate_workspace(workspace) + end + user + end + + def self.populate_workspace(workspace) + b = workspace.boards.create(name: "Board 1") + b.lists.create(title: "List 1") + b.lists.create(title: "List 2") + end +end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 5aef915..dfea018 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -5,6 +5,13 @@

I can has Kanban?

Yes, you can!

Find out how Kanban boards can help you organize your work and your life. And with our low, low prices you can surely still afford that delicious cheezburger!

+
+ <%= form_with(url: sign_in_path) do |form| %> + <%= form.text_field :email, value: "user@demo.test", readonly: "true", hidden: "true" %> + <%= form.password_field :password, value: 'testtest123456', readonly: "true", hidden: "true" %> + <%= form.button "Sign in as Demo User", class: "inline-flex justify-center items-center py-3 px-5 text-base font-medium text-center text-white rounded-lg bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 dark:focus:ring-blue-900" %> + <% end %> +
Kanban Kat diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index e785c27..7bbdf51 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -31,6 +31,14 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest assert_response :success end + test "should sign in user@demo.test with any password" do + post sign_in_url, params: { email: "user@demo.test", password: "12345abcdefg" } + + assert assert_redirected_to workspaces_url + get workspaces_url + assert_response :success + end + test "should not sign in with wrong credentials" do post sign_in_url, params: { email: @user.email, password: "SecretWrong1*3" } diff --git a/test/models/user_test.rb b/test/models/user_test.rb index ccfac24..0dee35c 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -31,4 +31,22 @@ class UserTest < ActiveSupport::TestCase assert_equal(1, user1.workspaces.others.count) end + + test "deleting a user should purge all owned workspaces" do + user1 = create(:user_with_3_workspaces) + board = user1.workspaces.mine.first.boards.create(name: "Test board") + board.lists.create(title: "Test List") + board.lists.first.cards.create(title: "Test Card") + assert_equal(3, user1.workspaces.mine.count) + + assert_difference("Workspace.count", -3) do + assert_difference("Board.count", -1) do + assert_difference("List.count", -1) do + assert_difference("Card.count", -1) do + user1.destroy + end + end + end + end + end end diff --git a/test/services/demo_prep_test.rb b/test/services/demo_prep_test.rb new file mode 100644 index 0000000..80dfd6d --- /dev/null +++ b/test/services/demo_prep_test.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "test_helper" + +class DemoPrepTest < ActiveSupport::TestCase + test "initialize demo if no users exist" do + user = User.find_by(email: "user@demo.test") + assert_nil user + user = DemoPrep.initialize_demo("user@demo.test") + assert user + end + + test "if user exists and was created today, do nothing" do + user = create(:user_with_workspace, email: "user@demo.test") + user.workspaces.create(name: "Workspace 2") + assert_equal 2, user.workspaces.count + demo_user = DemoPrep.initialize_demo("user@demo.test") + assert_equal 2, demo_user.workspaces.count + end + + test "if user exists and was not created today, create empty workspace" do + user = create(:user_with_workspace, email: "user@demo.test") + user.workspaces.create(name: "Workspace 2") + assert_equal 2, user.workspaces.count + travel 1.day do + demo_user = DemoPrep.initialize_demo("user@demo.test") + assert_equal 1, demo_user.workspaces.count + end + end +end diff --git a/test/system/sessions_test.rb b/test/system/sessions_test.rb index 8973c9b..c6f7f44 100644 --- a/test/system/sessions_test.rb +++ b/test/system/sessions_test.rb @@ -25,6 +25,13 @@ class SessionsTest < ApplicationSystemTestCase assert_text "Signed in successfully" end + test "sign in as demo user" do + visit root_url + click_on "Demo" + + assert_text "Signed in successfully" + end + test "signing out" do sign_in_as @user find_by_id("user-menu-button").click