Refactoring: Migrate ticket_customer_organization_update_test to RSpec

This commit is contained in:
Ryan Lue 2019-02-13 12:19:33 +08:00 committed by Martin Edenhofer
parent b045381d2d
commit d281c1b177
3 changed files with 40 additions and 77 deletions

View file

@ -19,6 +19,10 @@ FactoryBot.define do
factory :customer_user, aliases: %i[customer] do
role_ids { Role.signup_role_ids.sort }
trait :with_org do
organization
end
end
factory :agent_user, aliases: %i[agent] do

View file

@ -247,6 +247,42 @@ RSpec.describe Ticket, type: :model do
end
end
describe 'Associations:' do
describe '#organization' do
subject(:ticket) { build(:ticket, customer: customer, organization: nil) }
let(:customer) { create(:customer, :with_org) }
context 'on creation' do
it 'automatically adopts the organization of its #customer' do
expect { ticket.save }
.to change { ticket.organization }.to(customer.organization)
end
end
context 'on update of #customer.organization' do
context 'to nil' do
it 'automatically updates to #customers new value' do
ticket.save
expect { customer.update(organization: nil) }
.to change { ticket.reload.organization }.to(nil)
end
end
context 'to a different organization' do
let(:new_org) { create(:organization) }
it 'automatically updates to #customers new value' do
ticket.save
expect { customer.update(organization: new_org) }
.to change { ticket.reload.organization }.to(new_org)
end
end
end
end
end
describe 'Callbacks & Observers -' do
describe 'NULL byte handling (via ChecksAttributeValuesAndLength concern):' do
it 'removes them from title on creation, if necessary (postgres doesnt like them)' do

View file

@ -1,77 +0,0 @@
require 'test_helper'
class TicketCustomerOrganizationUpdateTest < ActiveSupport::TestCase
setup do
groups = Group.where(name: 'Users')
roles = Role.where(name: 'Agent')
@agent1 = User.create_or_update(
login: 'ticket-customer-organization-update-agent1@example.com',
firstname: 'Notification',
lastname: 'Agent1',
email: 'ticket-customer-organization-update-agent1@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
updated_at: '2015-02-05 16:37:00',
updated_by_id: 1,
created_by_id: 1,
)
roles = Role.where(name: 'Customer')
@organization1 = Organization.create_if_not_exists(
name: 'Customer Organization Update',
updated_at: '2015-02-05 16:37:00',
updated_by_id: 1,
created_by_id: 1,
)
@customer1 = User.create_or_update(
login: 'ticket-customer-organization-update-customer1@example.com',
firstname: 'Notification',
lastname: 'Customer1',
email: 'ticket-customer-organization-update-customer1@example.com',
password: 'customerpw',
active: true,
organization_id: @organization1.id,
roles: roles,
updated_at: '2015-02-05 16:37:00',
updated_by_id: 1,
created_by_id: 1,
)
end
test 'create ticket, update customers organization later' do
ticket = Ticket.create(
title: "some title1\n äöüß",
group: Group.lookup(name: 'Users'),
customer_id: @customer1.id,
owner_id: @agent1.id,
state: Ticket::State.lookup(name: 'new'),
priority: Ticket::Priority.lookup(name: '2 normal'),
updated_by_id: 1,
created_by_id: 1,
)
assert(ticket, 'ticket created')
assert_equal(@customer1.id, ticket.customer.id)
assert_equal(@organization1.id, ticket.organization.id)
# update customer organization
@customer1.organization_id = nil
@customer1.save!
# verify ticket
ticket.reload
assert_nil(ticket.organization_id)
# update customer organization
@customer1.organization_id = @organization1.id
@customer1.save!
# verify ticket
ticket.reload
assert_equal(@organization1.id, ticket.organization_id)
ticket.destroy
end
end