Skip to content

Commit

Permalink
Link up a new user to an existing deploy only user
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhammond committed Oct 30, 2024
1 parent 18de46f commit 276c663
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def edit
end

def create
@user = User.new(user_params)
@user = User.find_or_initialize_by(username: user_params[:username])
@user.assign_attributes(user_params)

invite_link = InviteLink.active.find_by(code: params[:code])

respond_to do |format|
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class User < ApplicationRecord
has_secure_password validations: false
validates :password, length: {minimum: 10, maximum: 72}, if: -> { password.present? }

validates :username, presence: true, uniqueness: true

after_create_commit :queue_import_avatar

scope :has_role, -> { where.not(role: nil) }
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241030224628_add_index_to_users_username.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIndexToUsersUsername < ActiveRecord::Migration[7.2]
def change
add_index :users, :username, unique: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity
end

test "should create with existing deploy user" do
invite_link = InviteLink.create!(role: :admin)
user = create(:user, password: nil, username: "nickhammond")

assert_no_difference("User.count") do
post users_url, params: {code: invite_link.code, user: {email: user.email, name: user.name, password: "secretsecret", username: user.username}}
end

assert_redirected_to root_url
assert_equal user, User.last
assert User.last.admin?
end

test "should create user" do
invite_link = InviteLink.create!(role: :user)
user = build(:user)
Expand Down
1 change: 1 addition & 0 deletions test/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do
factory :user do
sequence(:email) { |n| "person#{n}@example.com" }
sequence(:username) { |n| "user#{n}" }
name { "MyString" }
password { "secretsecret" }
end
Expand Down

0 comments on commit 276c663

Please sign in to comment.