diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index 83391b3..db7be96 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -1,11 +1,23 @@ class AdminUser < ApplicationRecord has_secure_password - validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } + validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP } validates :name, presence: true validates :password, length: { minimum: 8 }, if: -> { password.present? } + before_validation :normalize_email + def update_last_login! update!(last_login_at: Time.current) end + + def ntfy_configured? + ntfy_enabled? && ntfy_topic.present? && ntfy_token.present? + end + + private + + def normalize_email + self.email = email&.downcase&.strip + end end diff --git a/db/migrate/20260727180819_add_ntfy_fields_to_admin_users.rb b/db/migrate/20260727180819_add_ntfy_fields_to_admin_users.rb new file mode 100644 index 0000000..35ef306 --- /dev/null +++ b/db/migrate/20260727180819_add_ntfy_fields_to_admin_users.rb @@ -0,0 +1,8 @@ +class AddNtfyFieldsToAdminUsers < ActiveRecord::Migration[8.0] + def change + add_column :admin_users, :ntfy_topic, :string + add_column :admin_users, :ntfy_token, :string + add_column :admin_users, :ntfy_enabled, :boolean, default: false, null: false + add_column :admin_users, :ntfy_server_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 167917c..1e4d97b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_10_20_031401) do +ActiveRecord::Schema[8.0].define(version: 2026_07_27_180819) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -21,6 +21,10 @@ ActiveRecord::Schema[8.0].define(version: 2025_10_20_031401) do t.datetime "last_login_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "ntfy_topic" + t.string "ntfy_token" + t.boolean "ntfy_enabled", default: false, null: false + t.string "ntfy_server_url" t.index ["email"], name: "index_admin_users_on_email", unique: true end diff --git a/test/models/admin_user_test.rb b/test/models/admin_user_test.rb index 718fba3..dc64aa9 100644 --- a/test/models/admin_user_test.rb +++ b/test/models/admin_user_test.rb @@ -1,7 +1,41 @@ require "test_helper" class AdminUserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "email is normalized to lowercase and stripped before save" do + user = AdminUser.create!(name: "Jane", email: " Jane@Example.COM ", password: "supersecret123") + assert_equal "jane@example.com", user.email + end + + test "email uniqueness is case-insensitive" do + AdminUser.create!(name: "First", email: "user@example.com", password: "supersecret123") + dup = AdminUser.new(name: "Second", email: "USER@example.com", password: "supersecret123") + assert_not dup.valid? + assert_includes dup.errors[:email], "has already been taken" + end + + test "password must be at least 8 characters when present" do + user = AdminUser.new(name: "Short", email: "short@example.com", password: "1234567") + assert_not user.valid? + assert_includes user.errors[:password], "is too short (minimum is 8 characters)" + end + + test "authenticate returns the user with a valid password" do + user = AdminUser.create!(name: "Auth", email: "auth@example.com", password: "supersecret123") + assert_equal user, user.authenticate("supersecret123") + assert_not user.authenticate("wrong") + end + + test "ntfy_configured? returns true only when topic and token and enabled are set" do + admin = AdminUser.new(name: "N", email: "n@e.com", password: "password123") + assert_not admin.ntfy_configured? + + admin.ntfy_enabled = true + assert_not admin.ntfy_configured? + + admin.ntfy_topic = "my-topic" + assert_not admin.ntfy_configured? + + admin.ntfy_token = "tk_abc" + assert admin.ntfy_configured? + end end