require "rails_helper" RSpec.describe Api::MatchmakingController, type: :controller do routes do ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do delete "destroy", to: "api/matchmaking#destroy" end end end let(:user) { User.create!(nickname: "CaptainHook", password: "password123") } let(:token) { JwtService.encode(user_id: user.id) } describe "DELETE #destroy" do context "when authenticated" do it "cancels matchmaking for current user and returns success message" do request.headers["Authorization"] = "Bearer #{token}" expect(MatchmakingService).to receive(:cancel).with(user.id) delete :destroy expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["message"]).to eq("Matchmaking cancelled") end end context "when unauthenticated" do it "returns 401 unauthorized and does not call cancel" do expect(MatchmakingService).not_to receive(:cancel) delete :destroy expect(response).to have_http_status(:unauthorized) json = JSON.parse(response.body) expect(json["error"]).to eq("Unauthorized") end end end end