Skip to content

Commit

Permalink
Add Demo User
Browse files Browse the repository at this point in the history
  • Loading branch information
jbigler committed Mar 15, 2024
1 parent e0fc476 commit 38397fd
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/controllers/identity/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/controllers/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,16 +14,15 @@ 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"
else
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
Expand All @@ -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
24 changes: 24 additions & 0 deletions app/services/demo_prep.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
<h1 class="my-8 text-3xl font-extrabold text-gray-900 dark:text-white md:text-5xl lg:text-6xl">I can has <span class="text-transparent bg-clip-text bg-gradient-to-r to-emerald-600 from-sky-400">Kanban?</span></h1>
<h2 class="mb-4 text-3xl font-extrabold tracking-tight leading-none text-gray-900 md:text-4xl lg:text-5xl dark:text-white">Yes, you can!</h1>
<p class="py-5 mb-8 text-lg font-normal text-gray-500 lg:text-xl sm:px-16 lg:px-48 dark:text-gray-400">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!</p>
<div class="flex flex-col space-y-4 sm:flex-row sm:justify-center sm:space-y-0 mb-8">
<%= 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 %>
</div>
</div>
<img class="basis-1/3 object-scale-down" src="/images/i_kan_has_kanban.png" alt="Kanban Kat">
</div>
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
18 changes: 18 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions test/services/demo_prep_test.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/system/sessions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38397fd

Please sign in to comment.