diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3070a88b..1c1799be 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -36,7 +36,7 @@ def create @user = User.new(user_params) if User.none? - @user.admin = true + @user.role = "admin" end if @user.save diff --git a/test/integration/users_test.rb b/test/integration/users_test.rb new file mode 100644 index 00000000..d2879720 --- /dev/null +++ b/test/integration/users_test.rb @@ -0,0 +1,21 @@ +require "test_helper" + +class UsersTest < ActionDispatch::IntegrationTest + test "when no users exist an unauthenticated user can create an admin" do + User.delete_all + + assert_difference "User.count" do + post users_path, params: { user: FactoryBot.attributes_for(:user) } + + assert_redirected_to root_path + end + + assert User.last.admin? + end + + test "when users exists an unauthenticated user cannot create any users" do + post users_path, params: { user: FactoryBot.attributes_for(:user) } + + assert_redirected_to login_path + end +end