Skip to content

Commit

Permalink
Basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpigford committed Oct 31, 2024
1 parent b46a062 commit fcc6920
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
19 changes: 8 additions & 11 deletions app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@ def create
end

def accept
@invitation = Invitation.pending.find_by!(token: params[:id])
redirect_to new_registration_path(invitation: @invitation.token)
@invitation = Invitation.find_by!(token: params[:id])

Check failure on line 27 in app/controllers/invitations_controller.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
if @invitation.pending?
redirect_to new_registration_path(invitation: @invitation.token)
else
raise ActiveRecord::RecordNotFound
end
end

private

def invitation_params
base_params = params.require(:invitation).permit(:email)

if params[:invitation][:role].in?(%w[admin member])
base_params[:role] = params[:invitation][:role]
else
base_params[:role] = "member"
end

base_params
params.require(:invitation).permit(:email, :role)
end
end
4 changes: 4 additions & 0 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Invitation < ApplicationRecord
scope :accepted, -> { where.not(accepted_at: nil) }
scope :most_recent_for_email, ->(email) { where(email: email).order(accepted_at: :desc).first }

def pending?
accepted_at.nil? && expires_at > Time.current
end

private

def generate_token
Expand Down
89 changes: 89 additions & 0 deletions test/controllers/invitations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require "test_helper"

class InvitationsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@invitation = invitations(:one)
end

test "should get new" do
get new_invitation_url
assert_response :success
end

test "should create invitation for member" do
assert_difference("Invitation.count") do
assert_enqueued_with(job: ActionMailer::MailDeliveryJob) do
post invitations_url, params: {
invitation: {
email: "[email protected]",
role: "member"
}
}
end
end

invitation = Invitation.order(created_at: :desc).first
assert_equal "member", invitation.role
assert_equal @user, invitation.inviter
assert_equal "[email protected]", invitation.email
assert_redirected_to settings_profile_path
assert_equal I18n.t("invitations.create.success"), flash[:notice]
end

test "non-admin cannot create admin invitation" do
sign_in users(:family_member)

assert_difference("Invitation.count") do
post invitations_url, params: {
invitation: {
email: "[email protected]",
role: "admin"
}
}
end

invitation = Invitation.last
assert_equal "member", invitation.role # Role should be downgraded to member
end

test "admin can create admin invitation" do
assert_difference("Invitation.count") do
post invitations_url, params: {
invitation: {
email: "[email protected]",
role: "admin"
}
}
end

invitation = Invitation.last
assert_equal "admin", invitation.role
assert_equal @user.family, invitation.family
assert_equal @user, invitation.inviter
end

test "should handle invalid invitation creation" do
assert_no_difference("Invitation.count") do
post invitations_url, params: {
invitation: {
email: "",
role: "member"
}
}
end

assert_redirected_to settings_profile_path
assert_equal I18n.t("invitations.create.failure"), flash[:alert]
end

test "should accept invitation and redirect to registration" do
get accept_invitation_url(@invitation.token)
assert_redirected_to new_registration_path(invitation: @invitation.token)
end

test "should not accept invalid invitation token" do
get accept_invitation_url("invalid-token")
assert_response :not_found
end
end
19 changes: 19 additions & 0 deletions test/fixtures/invitations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
one:
email: "[email protected]"
token: "valid-token-123"
role: "member"
inviter: family_admin
family: dylan_family
created_at: <%= Time.current %>
updated_at: <%= Time.current %>
expires_at: <%= 3.days.from_now %>

two:
email: "[email protected]"
token: "valid-token-456"
role: "admin"
inviter: family_admin
family: dylan_family
created_at: <%= Time.current %>
updated_at: <%= Time.current %>
expires_at: <%= 3.days.from_now %>

0 comments on commit fcc6920

Please sign in to comment.