* Install Rspec * Create .aidigestignore * Update rails_helper - Added sections and comments - Add support for loading via canonical.rb - Add FactoryBot syntax methods - Disable SQL logging in test environment * Move gems around * Add canonical.rb and test env CSVs We load these CSVs via canonical.rb when we run tests as a data source for canonical objects. * Remove RBS for now This is too much and we need to find the right solution * Refactor GridSummonsController and add tests * Create GridSummon factory * Refactor GridSummon and add documentation and tests * Create have_error_on.rb * Update .aidigestignore * Fix warnings * Add GridWeapons and Parties factories * Refactor GridWeapon and add documentation and tests * Create .rubocop.yml * Create no_weapon_provided_error.rb * Refactor GridWeaponsController - Refactors controller - Adds YARD documentation - Adds Rspec tests * Refactor GridSummonsController - Refactors controller - Adds YARD documentation - Adds Rspec tests * Enable shoulda/matchers * Update User factory * Update party.rb We moved updating the party's element and extra flag to inside the party. We use an after_commit hook to minimize the amount of queries we're running to do this. * Update party.rb We change setting the edit key to use the conditional assignment operator so that it doesn't get overridden when we're running tests. This shouldn't have an effect in production. * Update api_controller.rb Change render_unprocessable_entity_response to render the errors hash instead of the exception so that we get more helpful errors. * Add new errors Added NoCharacterProvidedError and NoSummonProvidedError * Add tests and docs to GridCharacter We added a factory, spec and documentation to the GridCharacter model * Ensure numericality * Move enums into GranblueEnums We don't use these yet, but it gives us a structured place to pull them from. * Refactor GridCharactersController - Refactors controller - Adds YARD documentation - Adds Rspec tests * Add debug hook and other small changes * Update grid_characters_controller.rb Removes logs * Update .gitignore * Update .aidigestignore * Refactored PartiesController - Split PartiesController into three concerns - Implemented testing for PartiesController and two concerns - Implemented fixes across other files to ensure PartiesController tests pass - Added Favorites factory * Implement SimpleCov * Refactor Party model - Refactors Party model - Adds tests - Adds documentation * Update granblue_enums.rb Remove included block
134 lines
4.1 KiB
Ruby
134 lines
4.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
# Dummy controller that includes the PartyAuthorizationConcern.
|
|
# This allows us to test its instance methods in isolation.
|
|
class DummyAuthorizationController < ActionController::Base
|
|
include PartyAuthorizationConcern
|
|
|
|
attr_accessor :party, :current_user, :edit_key
|
|
|
|
# Override render_unauthorized_response to set a flag.
|
|
def render_unauthorized_response
|
|
@_unauthorized_called = true
|
|
end
|
|
|
|
def unauthorized_called?
|
|
@_unauthorized_called || false
|
|
end
|
|
end
|
|
|
|
RSpec.describe DummyAuthorizationController, type: :controller do
|
|
let(:dummy_controller) { DummyAuthorizationController.new }
|
|
let(:user) { create(:user) }
|
|
let(:other_user) { create(:user) }
|
|
let(:anonymous_party) { create(:party, user: nil, edit_key: 'anonkey') }
|
|
let(:owned_party) { create(:party, user: user) }
|
|
|
|
describe '#authorize_party!' do
|
|
context 'when the party belongs to a logged in user' do
|
|
before do
|
|
dummy_controller.party = owned_party
|
|
end
|
|
|
|
context 'and current_user matches party.user' do
|
|
before { dummy_controller.current_user = user }
|
|
it 'does not call render_unauthorized_response' do
|
|
dummy_controller.authorize_party!
|
|
expect(dummy_controller.unauthorized_called?).to be false
|
|
end
|
|
end
|
|
|
|
context 'and current_user is missing or does not match' do
|
|
before { dummy_controller.current_user = other_user }
|
|
it 'calls render_unauthorized_response' do
|
|
dummy_controller.authorize_party!
|
|
expect(dummy_controller.unauthorized_called?).to be true
|
|
end
|
|
end
|
|
end
|
|
|
|
context 'when the party is anonymous (no user)' do
|
|
before do
|
|
dummy_controller.party = anonymous_party
|
|
end
|
|
|
|
context 'with a valid edit_key' do
|
|
before { dummy_controller.edit_key = 'anonkey' }
|
|
it 'does not call render_unauthorized_response' do
|
|
dummy_controller.authorize_party!
|
|
expect(dummy_controller.unauthorized_called?).to be false
|
|
end
|
|
end
|
|
|
|
context 'with an invalid edit_key' do
|
|
before { dummy_controller.edit_key = 'wrongkey' }
|
|
it 'calls render_unauthorized_response' do
|
|
dummy_controller.authorize_party!
|
|
expect(dummy_controller.unauthorized_called?).to be true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
describe '#not_owner?' do
|
|
context 'when the party belongs to a logged in user' do
|
|
before do
|
|
dummy_controller.party = owned_party
|
|
end
|
|
|
|
context 'and current_user matches party.user' do
|
|
before { dummy_controller.current_user = user }
|
|
it 'returns false' do
|
|
expect(dummy_controller.not_owner?).to be false
|
|
end
|
|
end
|
|
|
|
context 'and current_user does not match party.user' do
|
|
before { dummy_controller.current_user = other_user }
|
|
it 'returns true' do
|
|
expect(dummy_controller.not_owner?).to be true
|
|
end
|
|
end
|
|
end
|
|
|
|
context 'when the party is anonymous' do
|
|
before do
|
|
dummy_controller.party = anonymous_party
|
|
end
|
|
|
|
context 'and the provided edit_key matches' do
|
|
before { dummy_controller.edit_key = 'anonkey' }
|
|
it 'returns false' do
|
|
expect(dummy_controller.not_owner?).to be false
|
|
end
|
|
end
|
|
|
|
context 'and the provided edit_key does not match' do
|
|
before { dummy_controller.edit_key = 'wrongkey' }
|
|
it 'returns true' do
|
|
expect(dummy_controller.not_owner?).to be true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
# Debug block: prints debug info if an example fails.
|
|
after(:each) do |example|
|
|
if example.exception && defined?(response) && response.present?
|
|
error_message = begin
|
|
JSON.parse(response.body)['exception']
|
|
rescue JSON::ParserError
|
|
response.body
|
|
end
|
|
|
|
puts "\nDEBUG: Error Message for '#{example.full_description}': #{error_message}"
|
|
|
|
# Parse once and grab the trace safely
|
|
parsed_body = JSON.parse(response.body)
|
|
trace = parsed_body.dig('traces', 'Application Trace')
|
|
ap trace if trace # Only print if trace is not nil
|
|
end
|
|
end
|
|
end
|