feat(ntfy): add ntfy fields to admin_users and ntfy_configured? method

This commit is contained in:
Min Zeya Phyo
2026-07-28 02:09:42 +08:00
parent 7c432941f1
commit 8f25dc057b
4 changed files with 63 additions and 5 deletions

View File

@@ -1,11 +1,23 @@
class AdminUser < ApplicationRecord class AdminUser < ApplicationRecord
has_secure_password 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 :name, presence: true
validates :password, length: { minimum: 8 }, if: -> { password.present? } validates :password, length: { minimum: 8 }, if: -> { password.present? }
before_validation :normalize_email
def update_last_login! def update_last_login!
update!(last_login_at: Time.current) update!(last_login_at: Time.current)
end end
def ntfy_configured?
ntfy_enabled? && ntfy_topic.present? && ntfy_token.present?
end
private
def normalize_email
self.email = email&.downcase&.strip
end
end end

View File

@@ -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

6
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql" 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 "last_login_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_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 t.index ["email"], name: "index_admin_users_on_email", unique: true
end end

View File

@@ -1,7 +1,41 @@
require "test_helper" require "test_helper"
class AdminUserTest < ActiveSupport::TestCase class AdminUserTest < ActiveSupport::TestCase
# test "the truth" do test "email is normalized to lowercase and stripped before save" do
# assert true user = AdminUser.create!(name: "Jane", email: " Jane@Example.COM ", password: "supersecret123")
# end 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 end