add tests for controllers and ActionCable channels; improve main backend docs

This commit is contained in:
2026-10-27 10:32:40 +03:00
parent b122cbb652
commit 50e8aacfa9
17 changed files with 1127 additions and 63 deletions
@@ -0,0 +1,52 @@
require "rails_helper"
RSpec.describe "Registrations API", type: :request do
describe "POST /api/register" do
let(:valid_attributes) do
{
user: {
nickname: "Sinbad",
password: "password123"
}
}
end
context "with valid parameters" do
it "registers a new user and returns 201 Created with token" do
expect {
post "/api/register", params: valid_attributes, as: :json
}.to change(User, :count).by(1)
expect(response).to have_http_status(:created)
json = JSON.parse(response.body)
expect(json["token"]).to be_present
expect(json["user"]["nickname"]).to eq("Sinbad")
expect(json["user"]["id"]).to eq(User.last.id)
expect(json["user"]["avatar_url"]).to be_nil
expect(json["user"]["created_at"]).to be_present
end
end
context "with invalid parameters" do
it "returns 422 Unprocessable Content when nickname is taken" do
User.create!(nickname: "Sinbad", password: "password123")
post "/api/register", params: valid_attributes, as: :json
expect(response).to have_http_status(422)
json = JSON.parse(response.body)
expect(json["errors"]).to include("Nickname has already been taken")
end
it "returns 422 Unprocessable Content when password is too short" do
invalid_attrs = { user: { nickname: "Sinbad", password: "123" } }
post "/api/register", params: invalid_attrs, as: :json
expect(response).to have_http_status(422)
json = JSON.parse(response.body)
expect(json["errors"]).to include("Password is too short (minimum is 6 characters)")
end
end
end
end
@@ -0,0 +1,61 @@
require "rails_helper"
RSpec.describe "Sessions API", type: :request do
let!(:user) { User.create!(nickname: "Columbus", password: "securepassword") }
let(:token) { JwtService.encode(user_id: user.id) }
describe "POST /api/login" do
it "logs in with valid credentials and returns 200 OK with token" do
post "/api/login", params: { nickname: "Columbus", password: "securepassword" }, as: :json
expect(response).to have_http_status(:ok)
json = JSON.parse(response.body)
expect(json["token"]).to be_present
expect(json["user"]["id"]).to eq(user.id)
expect(json["user"]["nickname"]).to eq("Columbus")
end
it "returns 401 Unauthorized with invalid credentials" do
post "/api/login", params: { nickname: "Columbus", password: "wrongpassword" }, as: :json
expect(response).to have_http_status(:unauthorized)
json = JSON.parse(response.body)
expect(json["error"]).to eq("Invalid nickname or password")
end
end
describe "DELETE /api/logout" do
it "signs out successfully when bearer token is supplied" do
delete "/api/logout", headers: { "Authorization" => "Bearer #{token}" }
expect(response).to have_http_status(:ok)
json = JSON.parse(response.body)
expect(json["message"]).to eq("Signed out successfully")
end
it "returns 401 Unauthorized when unauthenticated" do
delete "/api/logout"
expect(response).to have_http_status(:unauthorized)
end
end
describe "GET /api/me" do
it "returns user profile with Bearer authorization header" do
get "/api/me", headers: { "Authorization" => "Bearer #{token}" }
expect(response).to have_http_status(:ok)
json = JSON.parse(response.body)
expect(json["user"]["id"]).to eq(user.id)
expect(json["user"]["nickname"]).to eq("Columbus")
end
it "returns 401 Unauthorized when Authorization header is missing" do
get "/api/me"
expect(response).to have_http_status(:unauthorized)
json = JSON.parse(response.body)
expect(json["error"]).to eq("Unauthorized")
end
end
end