2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe UpdateTickets do
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:ticket) do
|
|
|
|
allow_any_instance_of(Event).to receive(:geocode).and_return([1, 1])
|
2018-03-25 17:30:42 +00:00
|
|
|
create(:ticket, :has_card, :has_event)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
describe '.update' do
|
|
|
|
include_context :shared_rd_donation_value_context
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:basic_valid_ticket_input) do
|
|
|
|
{ ticket_id: ticket.id, event_id: event.id }
|
|
|
|
end
|
|
|
|
let(:include_fake_token) do
|
|
|
|
basic_valid_ticket_input.merge(token: fake_uuid)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:include_valid_token) do
|
|
|
|
basic_valid_ticket_input.merge(token: source_token.token)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:general_ticket) do
|
2018-03-25 17:30:42 +00:00
|
|
|
{
|
2019-07-30 21:29:24 +00:00
|
|
|
quantity: 1,
|
|
|
|
supporter: supporter,
|
|
|
|
payment: payment,
|
|
|
|
charge: charge,
|
|
|
|
event_discount: event_discount,
|
|
|
|
created_at: Time.now,
|
|
|
|
updated_at: Time.now,
|
|
|
|
checked_in: nil,
|
|
|
|
bid_id: 1,
|
|
|
|
card_id: nil,
|
|
|
|
profile_id: nil,
|
|
|
|
note: nil,
|
|
|
|
deleted: nil,
|
|
|
|
source_token_id: nil,
|
|
|
|
ticket_level_id: nil
|
2018-03-25 17:30:42 +00:00
|
|
|
}
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:general_expected) do
|
2018-03-25 17:30:42 +00:00
|
|
|
{
|
2019-07-30 21:29:24 +00:00
|
|
|
id: ticket.id,
|
|
|
|
quantity: 1,
|
|
|
|
supporter_id: supporter.id,
|
|
|
|
payment_id: payment.id,
|
|
|
|
charge_id: charge.id,
|
|
|
|
event_discount_id: event_discount.id,
|
|
|
|
created_at: Time.now,
|
|
|
|
updated_at: Time.now,
|
|
|
|
checked_in: nil,
|
|
|
|
bid_id: 1,
|
|
|
|
card_id: nil,
|
|
|
|
profile_id: nil,
|
|
|
|
note: nil,
|
|
|
|
deleted: nil,
|
|
|
|
source_token_id: nil,
|
|
|
|
event_id: event.id,
|
|
|
|
ticket_level_id: nil
|
2018-03-25 17:30:42 +00:00
|
|
|
}.with_indifferent_access
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:payment) { force_create(:payment) }
|
|
|
|
let(:charge) { force_create(:charge) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:ticket) do
|
|
|
|
force_create(:ticket,
|
|
|
|
general_ticket.merge(event: event))
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:other_ticket) do
|
|
|
|
force_create(:ticket,
|
|
|
|
general_ticket.merge(event: other_event))
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'basic validation' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdateTickets.update(token: 'bhaetiwhet', checked_in: 'blah', bid_id: 'bhalehti') }.to raise_error { |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
|
|
|
|
|
|
|
expect_validation_errors(error.data, [
|
2019-07-30 21:29:24 +00:00
|
|
|
{ key: :event_id, name: :required },
|
|
|
|
{ key: :event_id, name: :is_reference },
|
|
|
|
{ key: :ticket_id, name: :required },
|
|
|
|
{ key: :ticket_id, name: :is_reference },
|
|
|
|
{ key: :token, name: :format },
|
|
|
|
{ key: :bid_id, name: :is_integer },
|
|
|
|
{ key: :checked_in, name: :included_in }
|
|
|
|
])
|
2018-03-25 17:30:42 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'event is invalid' do
|
2019-07-30 21:29:24 +00:00
|
|
|
find_error_event { UpdateTickets.update(event_id: 5555, ticket_id: ticket.id) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ticket is invalid' do
|
2019-07-30 21:29:24 +00:00
|
|
|
find_error_ticket { UpdateTickets.update(event_id: event.id, ticket_id: 5555) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ticket is deleted' do
|
|
|
|
ticket.deleted = true
|
|
|
|
ticket.save!
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdateTickets.update(event_id: event.id, ticket_id: ticket.id) }.to raise_error { |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :ticket_id }])
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error.message).to include 'deleted'
|
|
|
|
expect(error.message).to include "Ticket ID #{ticket.id}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'event is deleted' do
|
|
|
|
event.deleted = true
|
|
|
|
event.save!
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdateTickets.update(event_id: event.id, ticket_id: ticket.id) }.to raise_error { |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :event_id }])
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error.message).to include 'deleted'
|
|
|
|
expect(error.message).to include "Event ID #{event.id}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'event and ticket dont match' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdateTickets.update(event_id: event.id, ticket_id: other_ticket.id) }.to raise_error { |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :ticket_id }])
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error.message).to include "Ticket ID #{other_ticket.id} does not belong to event #{event.id}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'token is invalid' do
|
2019-07-30 21:29:24 +00:00
|
|
|
validation_invalid_token { UpdateTickets.update(include_fake_token) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors out if token is unauthorized' do
|
2019-07-30 21:29:24 +00:00
|
|
|
validation_unauthorized { UpdateTickets.update(include_fake_token) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors out if token is expired' do
|
2019-07-30 21:29:24 +00:00
|
|
|
validation_expired { UpdateTickets.update(include_fake_token) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'card doesnt belong to supporter' do
|
2019-07-30 21:29:24 +00:00
|
|
|
validation_card_not_with_supporter { UpdateTickets.update(include_fake_token.merge(token: other_source_token.token)) }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'success editing note' do
|
2019-07-30 21:29:24 +00:00
|
|
|
result = UpdateTickets.update(basic_valid_ticket_input.merge(note: 'noteedited'))
|
|
|
|
expected = general_expected.merge(note: 'noteedited')
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(result.attributes).to eq expected
|
|
|
|
ticket.reload
|
|
|
|
expect(ticket.attributes).to eq expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'success editing bid_id' do
|
2019-07-30 21:29:24 +00:00
|
|
|
result = UpdateTickets.update(basic_valid_ticket_input.merge(bid_id: 50))
|
|
|
|
expected = general_expected.merge(bid_id: 50)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(result.attributes).to eq expected
|
|
|
|
ticket.reload
|
|
|
|
expect(ticket.attributes).to eq expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'success editing checked_in' do
|
2019-07-30 21:29:24 +00:00
|
|
|
result = UpdateTickets.update(basic_valid_ticket_input.merge(checked_in: 'true'))
|
2018-03-25 17:30:42 +00:00
|
|
|
expected = general_expected.merge(checked_in: true)
|
|
|
|
|
|
|
|
expect(result.attributes).to eq expected
|
|
|
|
ticket.reload
|
|
|
|
expect(ticket.attributes).to eq expected
|
|
|
|
end
|
|
|
|
|
2018-10-23 15:43:58 +00:00
|
|
|
it 'success editing checked_in as a boolean' do
|
2019-07-30 21:29:24 +00:00
|
|
|
result = UpdateTickets.update(basic_valid_ticket_input.merge(checked_in: true))
|
2018-10-23 15:43:58 +00:00
|
|
|
expected = general_expected.merge(checked_in: true)
|
|
|
|
|
|
|
|
expect(result.attributes).to eq expected
|
|
|
|
ticket.reload
|
|
|
|
expect(ticket.attributes).to eq expected
|
|
|
|
end
|
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
it 'success editing token' do
|
2019-07-30 21:29:24 +00:00
|
|
|
result = UpdateTickets.update(basic_valid_ticket_input.merge(token: source_token.token))
|
2018-03-25 17:30:42 +00:00
|
|
|
expected = general_expected.merge(source_token_id: source_token.id)
|
|
|
|
|
|
|
|
expect(result.attributes).to eq expected
|
|
|
|
ticket.reload
|
|
|
|
expect(ticket.attributes).to eq expected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.delete' do
|
|
|
|
it 'marks the given ticket as deleted=true' do
|
|
|
|
UpdateTickets.delete(ticket['event_id'], ticket['id'])
|
|
|
|
ticket.reload
|
|
|
|
expect(ticket['deleted']).to eq(true)
|
|
|
|
|
|
|
|
expect(Ticket.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.delete_card_for_ticket' do
|
|
|
|
it 'deletes the card from the ticket' do
|
|
|
|
Timecop.freeze(2020, 1, 5) do
|
|
|
|
original_ticket = ticket
|
|
|
|
card = ticket.card
|
|
|
|
expect(ticket.card).to_not be_nil
|
|
|
|
ret = UpdateTickets.delete_card_for_ticket(ticket['event_id'], ticket['id'])
|
|
|
|
expect(ret[:status]).to eq :ok
|
|
|
|
expect(ret[:json]).to eq({})
|
|
|
|
ticket.reload
|
|
|
|
expect(Card.find(card.id)).to eq(card)
|
|
|
|
expect(ticket.card).to be_nil
|
|
|
|
expect(Ticket.count).to eq(1)
|
2019-07-30 21:29:24 +00:00
|
|
|
skip_attribs = %i[updated_at card]
|
|
|
|
expect(ticket.attributes.reject { |k, _| skip_attribs.include?(k) }).to eq original_ticket.attributes.reject { |k, _| skip_attribs.include?(k) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(ticket.updated_at).to eq Time.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'parameter validation' do
|
|
|
|
it 'validates parameters' do
|
|
|
|
result = UpdateTickets.delete_card_for_ticket(nil, nil)
|
|
|
|
errors = result[:json][:errors]
|
|
|
|
expect(errors.length).to eq(4)
|
|
|
|
expect(result[:status]).to eq :unprocessable_entity
|
|
|
|
expect_validation_errors(errors, [
|
2019-07-30 21:29:24 +00:00
|
|
|
{ key: :event_id, name: :required },
|
|
|
|
{ key: :event_id, name: :is_integer },
|
|
|
|
{ key: :ticket_id, name: :required },
|
|
|
|
{ key: :ticket_id, name: :is_integer }
|
|
|
|
])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'invalid event_id causes no problem' do
|
|
|
|
ticket
|
|
|
|
tickets = Ticket.all
|
|
|
|
events = Event.all
|
|
|
|
result = UpdateTickets.delete_card_for_ticket(444, ticket.id)
|
|
|
|
expect(result[:status]).to eq :unprocessable_entity
|
|
|
|
expect(Ticket.all).to match_array(tickets)
|
|
|
|
expect(Event.all).to match_array(events)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'invalid ticket_id causes no problem' do
|
|
|
|
ticket
|
|
|
|
tickets = Ticket.all
|
|
|
|
events = Event.all
|
|
|
|
result = UpdateTickets.delete_card_for_ticket(ticket.event.id, 444)
|
|
|
|
expect(result[:status]).to eq :unprocessable_entity
|
|
|
|
expect(Ticket.all).to match_array(tickets)
|
|
|
|
expect(Event.all).to match_array(events)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|