add tests for the Ruby backend

This commit is contained in:
2026-10-12 08:20:32 +03:00
parent d6efb46f87
commit e612dbfa23
11 changed files with 358 additions and 25 deletions
+66
View File
@@ -0,0 +1,66 @@
require "rails_helper"
RSpec.describe User, type: :model do
let(:valid_attributes) { { nickname: "player_one", password: "secret123" } }
describe "validations" do
it "is valid with a nickname and password" do
expect(User.new(valid_attributes)).to be_valid
end
it "requires a nickname" do
user = User.new(valid_attributes.merge(nickname: nil))
expect(user).not_to be_valid
expect(user.errors[:nickname]).to be_present
end
it "requires a password" do
user = User.new(nickname: "player_one", password: nil)
expect(user).not_to be_valid
expect(user.errors[:password]).to be_present
end
it "rejects nicknames shorter than 3 characters" do
expect(User.new(valid_attributes.merge(nickname: "ab"))).not_to be_valid
end
it "rejects nicknames longer than 24 characters" do
expect(User.new(valid_attributes.merge(nickname: "a" * 25))).not_to be_valid
end
it "rejects passwords shorter than 6 characters" do
expect(User.new(valid_attributes.merge(password: "12345"))).not_to be_valid
end
it "requires nicknames to be unique case-insensitively" do
User.create!(valid_attributes)
duplicate = User.new(valid_attributes.merge(nickname: "PLAYER_ONE"))
expect(duplicate).not_to be_valid
expect(duplicate.errors[:nickname]).to be_present
end
end
describe "nickname normalization" do
it "strips surrounding whitespace" do
user = User.create!(valid_attributes.merge(nickname: " player_one "))
expect(user.nickname).to eq("player_one")
end
end
describe "password authentication" do
it "authenticates with the correct password" do
user = User.create!(valid_attributes)
expect(user.authenticate("secret123")).to eq(user)
end
it "rejects a wrong password" do
user = User.create!(valid_attributes)
expect(user.authenticate("wrong")).to be_falsey
end
it "does not require a password when updating other attributes" do
user = User.create!(valid_attributes)
expect(user.update(nickname: "new_nickname")).to be true
end
end
end