# frozen_string_literal: true require "test_helper" require "tmpdir" require "fileutils" class UsersTest < Minitest::Test def setup @dir = Dir.mktmpdir @path = File.join(@dir, "users.toml") end def teardown FileUtils.remove_entry(@dir) end def test_bootstrap_user_from_hash hash = Volumen::Password.hash("secret") users = Volumen::Users.new(path: @path, bootstrap_hash: hash) assert_equal 1, users.all.length assert users.authenticate("admin", "secret") end def test_no_users_without_file_or_hash refute_predicate Volumen::Users.new(path: @path), :any? end def test_add_and_authenticate users = Volumen::Users.new(path: @path) assert users.add("alice", "wonderland") assert_path_exists @path assert users.authenticate("alice", "wonderland") refute users.authenticate("alice", "nope") end def test_add_rejects_duplicate users = Volumen::Users.new(path: @path) users.add("alice", "x") assert_nil users.add("alice", "y") end def test_update_password users = Volumen::Users.new(path: @path) users.add("alice", "old") users.update_password("alice", "new") refute users.authenticate("alice", "old") assert users.authenticate("alice", "new") end def test_rename users = Volumen::Users.new(path: @path) users.add("alice", "x") assert users.rename("alice", "alicia") assert_nil users.find("alice") assert users.find("alicia") end def test_delete_keeps_last_user users = Volumen::Users.new(path: @path) users.add("alice", "x") assert_nil users.delete("alice") users.add("bob", "y") assert users.delete("bob") assert_nil users.find("bob") end def test_default_role_is_author users = Volumen::Users.new(path: @path) users.add("alice", "x") assert_equal "author", users.find("alice").role end def test_add_with_explicit_role users = Volumen::Users.new(path: @path) users.add("boss", "x", "admin") assert_equal "admin", users.find("boss").role end def test_invalid_role_falls_back_to_default users = Volumen::Users.new(path: @path) users.add("alice", "x", "wizard") assert_equal "author", users.find("alice").role end def test_bootstrap_user_is_admin users = Volumen::Users.new(path: @path, bootstrap_hash: Volumen::Password.hash("s")) assert_equal "admin", users.find("admin").role end def test_set_role users = Volumen::Users.new(path: @path) users.add("alice", "x", "admin") users.add("bob", "y", "author") assert users.set_role("bob", "admin") assert_equal "admin", users.find("bob").role end def test_cannot_demote_last_admin users = Volumen::Users.new(path: @path) users.add("alice", "x", "admin") users.add("bob", "y", "author") assert_nil users.set_role("alice", "author") assert_equal "admin", users.find("alice").role end def test_cannot_delete_last_admin users = Volumen::Users.new(path: @path) users.add("alice", "x", "admin") users.add("bob", "y", "author") assert_nil users.delete("alice") users.add("carol", "z", "admin") assert users.delete("alice") end end