123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- require 'test_helper'
- class HistoryTest < ActiveSupport::TestCase
- test 'ticket' do
- current_user = User.lookup(email: 'nicole.braun@zammad.org')
- tests = [
- # test 1
- {
- ticket_create: {
- ticket: {
- group_id: Group.lookup(name: 'Users').id,
- customer_id: current_user.id,
- owner_id: User.lookup(login: '-').id,
- title: 'Unit Test 1 (äöüß)!',
- state_id: Ticket::State.lookup(name: 'new').id,
- priority_id: Ticket::Priority.lookup(name: '2 normal').id,
- updated_by_id: current_user.id,
- created_by_id: current_user.id,
- },
- article: {
- updated_by_id: current_user.id,
- created_by_id: current_user.id,
- type_id: Ticket::Article::Type.lookup(name: 'phone').id,
- sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
- from: 'Unit Test <unittest@example.com>',
- body: 'Unit Test 123',
- internal: false,
- },
- },
- ticket_update: {
- ticket: {
- title: 'Unit Test 1 (äöüß) - update!',
- state_id: Ticket::State.lookup(name: 'open').id,
- priority_id: Ticket::Priority.lookup(name: '1 low').id,
- },
- },
- history_check: [
- {
- result: true,
- history_object: 'Ticket',
- history_type: 'created',
- },
- {
- result: true,
- history_object: 'Ticket',
- history_type: 'updated',
- history_attribute: 'title',
- value_from: 'Unit Test 1 (äöüß)!',
- value_to: 'Unit Test 1 (äöüß) - update!',
- },
- {
- result: true,
- history_object: 'Ticket',
- history_type: 'updated',
- history_attribute: 'state',
- value_from: 'new',
- value_to: 'open',
- id_from: Ticket::State.lookup(name: 'new').id,
- id_to: Ticket::State.lookup(name: 'open').id,
- },
- {
- result: true,
- history_object: 'Ticket::Article',
- history_type: 'created',
- },
- {
- result: false,
- history_object: 'User',
- history_type: 'updated',
- },
- ]
- },
- # test 2
- {
- ticket_create: {
- ticket: {
- group_id: Group.lookup(name: 'Users').id,
- customer_id: current_user.id,
- owner_id: User.lookup(login: '-').id,
- title: 'Unit Test 2 (äöüß)!',
- state_id: Ticket::State.lookup(name: 'new').id,
- priority_id: Ticket::Priority.lookup(name: '2 normal').id,
- updated_by_id: current_user.id,
- created_by_id: current_user.id,
- },
- article: {
- created_by_id: current_user.id,
- updated_by_id: current_user.id,
- type_id: Ticket::Article::Type.lookup(name: 'phone').id,
- sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
- from: 'Unit Test <unittest@example.com>',
- body: 'Unit Test 123',
- internal: false,
- },
- },
- ticket_update: {
- ticket: {
- title: 'Unit Test 2 (äöüß) - update!',
- state_id: Ticket::State.lookup(name: 'open').id,
- owner_id: current_user.id,
- },
- article: {
- body: 'Unit Test 123 - 2',
- },
- },
- history_check: [
- {
- result: true,
- history_object: 'Ticket',
- history_type: 'created',
- },
- {
- result: true,
- history_object: 'Ticket',
- history_type: 'updated',
- history_attribute: 'title',
- value_from: 'Unit Test 2 (äöüß)!',
- value_to: 'Unit Test 2 (äöüß) - update!',
- },
- {
- result: false,
- history_object: 'Ticket',
- history_type: 'updated',
- history_attribute: 'owner',
- value_from: '-',
- value_to: 'Nicole Braun',
- id_from: User.lookup(login: '-').id,
- id_to: current_user.id,
- },
- {
- result: true,
- history_object: 'Ticket::Article',
- history_type: 'created',
- },
- {
- result: true,
- history_object: 'Ticket::Article',
- history_type: 'updated',
- history_attribute: 'body',
- value_from: 'Unit Test 123',
- value_to: 'Unit Test 123 - 2',
- },
- ]
- },
- ]
- tickets = []
- tests.each do |test|
- ticket = nil
- article = nil
- # use transaction
- ActiveRecord::Base.transaction do
- ticket = Ticket.create!(test[:ticket_create][:ticket])
- test[:ticket_create][:article][:ticket_id] = ticket.id
- article = Ticket::Article.create!(test[:ticket_create][:article])
- assert_equal(ticket.class, Ticket)
- assert_equal(article.class, Ticket::Article)
- # update ticket
- if test[:ticket_update][:ticket]
- ticket.update!(test[:ticket_update][:ticket])
- end
- if test[:ticket_update][:article]
- article.update!(test[:ticket_update][:article])
- end
- end
- # execute object transaction
- Observer::Transaction.commit
- # execute background jobs
- Scheduler.worker(true)
- # remember ticket
- tickets.push ticket
- # check history
- history_check(ticket.history_get, test[:history_check])
- end
- # delete tickets
- tickets.each(&:destroy!)
- end
- test 'user' do
- current_user = User.lookup(email: 'nicole.braun@zammad.org')
- name = rand(999_999)
- tests = [
- # test 1
- {
- user_create: {
- user: {
- login: "some_login_test-#{name}",
- firstname: 'Bob',
- lastname: 'Smith',
- email: "somebody-#{name}@example.com",
- active: true,
- updated_by_id: current_user.id,
- created_by_id: current_user.id,
- },
- },
- user_update: {
- user: {
- firstname: 'Bob',
- lastname: 'Master',
- email: "master-#{name}@example.com",
- active: false,
- },
- },
- history_check: [
- {
- result: true,
- history_object: 'User',
- history_type: 'created',
- },
- {
- result: true,
- history_object: 'User',
- history_type: 'updated',
- history_attribute: 'lastname',
- value_from: 'Smith',
- value_to: 'Master',
- },
- {
- result: true,
- history_object: 'User',
- history_type: 'updated',
- history_attribute: 'email',
- value_from: "somebody-#{name}@example.com",
- value_to: "master-#{name}@example.com",
- },
- {
- result: true,
- history_object: 'User',
- history_type: 'updated',
- history_attribute: 'active',
- value_from: 'true',
- value_to: 'false',
- },
- ],
- },
- ]
- users = []
- tests.each do |test|
- user = nil
- # user transaction
- ActiveRecord::Base.transaction do
- user = User.create!(test[:user_create][:user])
- assert_equal(user.class, User)
- # update user
- if test[:user_update][:user]
- test[:user_update][:user][:active] = false
- user.update!(test[:user_update][:user])
- end
- end
- # remember user
- users.push user
- # check history
- history_check(user.history_get, test[:history_check])
- end
- # delete user
- users.each(&:destroy!)
- end
- test 'organization' do
- current_user = User.lookup(email: 'nicole.braun@zammad.org')
- tests = [
- # test 1
- {
- organization_create: {
- organization: {
- name: 'Org äöüß',
- note: 'some note',
- updated_by_id: current_user.id,
- created_by_id: current_user.id,
- },
- },
- organization_update: {
- organization: {
- name: 'Org 123',
- note: 'some note',
- },
- },
- history_check: [
- {
- result: true,
- history_object: 'Organization',
- history_type: 'created',
- },
- {
- result: true,
- history_object: 'Organization',
- history_type: 'updated',
- history_attribute: 'name',
- value_from: 'Org äöüß',
- value_to: 'Org 123',
- },
- ],
- },
- ]
- organizations = []
- tests.each do |test|
- organization = nil
- # user transaction
- ActiveRecord::Base.transaction do
- organization = Organization.create!(test[:organization_create][:organization])
- assert_equal(organization.class, Organization)
- # update organization
- if test[:organization_update][:organization]
- organization.update!(test[:organization_update][:organization])
- end
- end
- # remember user
- organizations.push organization
- # check history
- history_check(organization.history_get, test[:history_check])
- end
- # delete user
- organizations.each(&:destroy!)
- end
- test 'ticket assets' do
- UserInfo.current_user_id = 1
- agent1 = User.create!(
- login: 'agent1@example.com',
- firstname: 'agent',
- lastname: '1',
- email: 'agent1@example.com',
- password: 'agentpw',
- active: true,
- roles: Role.where(name: %w[Agent Admin]),
- groups: Group.all,
- )
- current_user = User.lookup(email: 'nicole.braun@zammad.org')
- UserInfo.current_user_id = current_user.id
- ticket = Ticket.create!(
- title: 'test 1',
- group: Group.first,
- customer_id: current_user.id,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- )
- article = Ticket::Article.create!(
- ticket_id: ticket.id,
- from: 'some_customer_com-1@example.com',
- to: 'some_zammad_com-1@example.com',
- subject: 'com test 1',
- message_id: 'some@id_com_1',
- body: 'some message 123',
- internal: false,
- sender: Ticket::Article::Sender.find_by(name: 'Customer'),
- type: Ticket::Article::Type.find_by(name: 'email'),
- )
- # verify if user of history record is in assets
- UserInfo.current_user_id = agent1.id
- ticket.state = Ticket::State.find_by(name: 'closed')
- ticket.save!
- # update updated_by (to not include agent1 in assets by ticket)
- UserInfo.current_user_id = current_user.id
- ticket.priority = Ticket::Priority.find_by(name: '3 high')
- ticket.save!
- history = ticket.history_get(true)
- assert(history[:assets][:User][current_user.id])
- assert(history[:assets][:User][agent1.id])
- assert(history[:assets][:Ticket][ticket.id])
- assert(history[:assets][:TicketArticle][article.id])
- end
- def history_check(history_list, history_check)
- history_check.each do |check_item|
- match = false
- history_list.each do |history_item|
- next if match
- next if history_item['object'] != check_item[:history_object]
- next if history_item['type'] != check_item[:history_type]
- if check_item[:history_attribute]
- next if check_item[:history_attribute] != history_item['attribute']
- end
- match = true
- if history_item['type'] == check_item[:history_type]
- assert(true, "History type #{history_item['type']} found!")
- end
- if check_item[:history_attribute]
- assert_equal(check_item[:history_attribute], history_item['attribute'], "check history attribute #{check_item[:history_attribute]}")
- end
- if check_item[:value_from]
- assert_equal(check_item[:value_from], history_item['value_from'], "check history :value_from #{history_item['value_from']} ok")
- end
- if check_item[:value_to]
- assert_equal(check_item[:value_to], history_item['value_to'], "check history :value_to #{history_item['value_to']} ok")
- end
- if check_item[:id_from]
- assert_equal(check_item[:id_from], history_item['id_from'], "check history :id_from #{history_item['id_from']} ok")
- end
- if check_item[:id_to]
- assert_equal(check_item[:id_to], history_item['id_to'], "check history :id_to #{history_item['id_to']} ok")
- end
- end
- if check_item[:result]
- assert(match, "history check not matched! #{check_item.inspect}")
- else
- assert_not(match, "history check matched but should not! #{check_item.inspect}")
- end
- end
- end
- end
|