Skip to content

Commit

Permalink
feat: refactor tests and allow sending activation emails
Browse files Browse the repository at this point in the history
  • Loading branch information
amrhossamdev committed Sep 23, 2024
1 parent c35788f commit ef5d44e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 34 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# Rails app lives here
WORKDIR /rails

# Install base packages
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create
@user = User.new(user_params)
if @user.save
reset_session
UserMailer.account_activation(@user).deliver_now
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
Expand Down
11 changes: 11 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ def forget
update_attribute(:remember_digest, nil)
end

# Send activation email
def send_activation_email
UserMailer.account_activation(self).deliver_now
end

# Activate an account
def active
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end

# Returns true if the given token matches the digest.
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
Expand Down
15 changes: 15 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'https://simpleapp-production-5d8a.up.railway.app/'

Check failure on line 21 in config/environments/production.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',

Check failure on line 24 in config/environments/production.rb

View workflow job for this annotation

GitHub Actions / lint

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
:port => '587',
:authentication => :plain,
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:domain => 'railway.app',
:enable_starttls_auto => true
}

#Rails.application.routes.default_url_options = { host: 'simpleapp-production-5d8a.up.railway.app/', protocol: 'https' }

# Ensures that a master key has been made available in ENV["RAILS_MASTER_KEY"], config/master.key, or an environment
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ amr:
amr2:
name: Amr2
email: foo@gmail.com
activated: true
activated_at: <%= Time.zone.now %>
password_digest: <%= User.digest("password") %>

lana:
Expand Down
85 changes: 58 additions & 27 deletions test/integration/users_login_test.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,89 @@
require "test_helper"

class UsersLoginTest < ActionDispatch::IntegrationTest
class UsersLogin < ActionDispatch::IntegrationTest

def setup
@user = users(:amr)
end
end

test "login with invalid information" do
class InvalidPasswordTest < UsersLogin

test "login path" do
get login_path
assert_template "sessions/new"
assert_template 'sessions/new'
end

test "login with invalid information" do
post login_path, params: { session: { email: "", password: "" } }
assert_not is_logged_in?
assert_response :unprocessable_entity
assert_template "sessions/new"
assert_not flash.empty?
get root_path
assert flash.empty?
end
end

test "login with valid information" do
get login_path
post login_path, params: { session: { email: @user.email, password: "password" } }
assert_redirected_to @user
follow_redirect!
assert_template "users/show"
class ValidLogin < UsersLogin
def setup
super
post login_path, params: { session: { email: @user.email,
password: "password" } }
end
end

test "login with valid information followed by logout" do
post login_path, params: { session: { email: @user.email,
password: "password" } }
class ValidLoginTest < ValidLogin

test "valid login" do
assert is_logged_in?
assert_redirected_to @user
end

test "redirect after login" do
follow_redirect!
assert_template "users/show"
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
# Simulate user clicking logout in a second window
delete logout_path
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end

test "login with remembering" do
log_in_as(@user, remember_me: "1")
assert_not_nil cookies["remember_token"]
class Logout < ValidLogin
def setup
super
delete logout_path
end
end

test "login without remembering" do
log_in_as(@user, remember_me: "0")
assert_nil cookies["remember_token"]
class LogoutTest < Logout

test "successful logout" do
assert_not is_logged_in?
assert_redirected_to root_url
end

test "redirect after logout" do
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end

test "should still work after logout in second window" do
delete logout_path
assert_redirected_to root_url
end
end

class RememberingTest < UsersLogin
test "login with remembering" do
log_in_as(@user, remember_me: "1")
assert_not_nil cookies["remember_token"]
end

test "login without remembering" do
log_in_as(@user, remember_me: "0")
assert_nil cookies["remember_token"]
end
end
end
49 changes: 44 additions & 5 deletions test/integration/users_signup_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require "test_helper"

class UsersSignupTest < ActionDispatch::IntegrationTest
class UsersSignup < ActionDispatch::IntegrationTest
def setup
@user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
ActionMailer::Base.deliveries.clear
end

end

class UsersSignupTest < UsersSignup

test "invalid signup information" do
get signup_path
assert_no_difference "User.count" do
Expand All @@ -17,15 +21,50 @@ def setup
assert_template :'users/new'
end

test "valid signup information" do
test "valid signup information with account activation" do
assert_difference "User.count", 1 do
post users_path, params: { user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password" } }
end
assert_equal 1, ActionMailer::Base.deliveries.size
end

class AccountActivationTest < UsersSignup
def setup
super
post users_path, params: { user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password" } }
@user = assigns(:user)
end

test "should not be activated" do
assert_not @user.activated?
end

test "should not be able to log in before account activation" do
log_in_as(@user)
assert_not is_logged_in?
end

test "should not be able to log in with invalid activation token" do
get edit_account_activation_path("invalid token", email: @user.email)
assert_not is_logged_in?
end

test "should not be able to log in with invalid email" do
get edit_account_activation_path(@user.activation_token, email: "wrong")
assert_not is_logged_in?
end

test "should log in succesfully with valid activation token and email" do
get edit_account_activation_path(@user.activation_token, email: @user.email)
follow_redirect!
# assert_template "users/show"
# assert is_logged_in?
assert_template "users/show"
assert is_logged_in?
end
end
end

0 comments on commit ef5d44e

Please sign in to comment.