2013-09-28 00:07:11 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ActivityStreamTest < ActiveSupport::TestCase
|
2016-01-19 22:30:23 +00:00
|
|
|
role = Role.lookup(name: 'Admin')
|
|
|
|
group = Group.lookup(name: 'Users')
|
2013-10-05 12:56:03 +00:00
|
|
|
admin_user = User.create_or_update(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: 'admin',
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
email: 'bob@example.com',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
|
|
|
role_ids: [role.id],
|
|
|
|
group_ids: [group.id],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1
|
2013-09-29 21:37:49 +00:00
|
|
|
)
|
2016-01-19 22:30:23 +00:00
|
|
|
current_user = User.lookup(email: 'nicole.braun@zammad.org')
|
2013-09-29 21:37:49 +00:00
|
|
|
|
2016-04-13 07:30:44 +00:00
|
|
|
activity_record_delay = if ENV['ZAMMAD_ACTIVITY_RECORD_DELAY']
|
|
|
|
ENV['ZAMMAD_ACTIVITY_RECORD_DELAY'].to_i.seconds
|
|
|
|
else
|
|
|
|
90.seconds
|
|
|
|
end
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
test 'ticket+user' do
|
|
|
|
tests = [
|
|
|
|
|
|
|
|
# test 1
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
create: {
|
|
|
|
ticket: {
|
2016-01-19 22:30:23 +00:00
|
|
|
group_id: Group.lookup(name: 'Users').id,
|
2015-04-27 13:42:53 +00:00
|
|
|
customer_id: current_user.id,
|
2016-01-19 22:30:23 +00:00
|
|
|
owner_id: User.lookup(login: '-').id,
|
2015-04-27 13:42:53 +00:00
|
|
|
title: 'Unit Test 1 (äöüß)!',
|
2016-01-19 22:30:23 +00:00
|
|
|
state_id: Ticket::State.lookup(name: 'new').id,
|
|
|
|
priority_id: Ticket::Priority.lookup(name: '2 normal').id,
|
2015-04-27 13:42:53 +00:00
|
|
|
updated_by_id: current_user.id,
|
|
|
|
created_by_id: current_user.id,
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
article: {
|
2015-04-27 15:21:17 +00:00
|
|
|
updated_by_id: current_user.id,
|
2015-04-27 19:24:14 +00:00
|
|
|
created_by_id: current_user.id,
|
2016-01-19 22:30:23 +00:00
|
|
|
type_id: Ticket::Article::Type.lookup(name: 'phone').id,
|
|
|
|
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
|
2015-04-27 19:24:14 +00:00
|
|
|
from: 'Unit Test <unittest@example.com>',
|
|
|
|
body: 'Unit Test 123',
|
|
|
|
internal: false,
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update: {
|
|
|
|
ticket: {
|
|
|
|
title: 'Unit Test 1 (äöüß) - update!',
|
2016-01-19 22:30:23 +00:00
|
|
|
state_id: Ticket::State.lookup(name: 'open').id,
|
|
|
|
priority_id: Ticket::Priority.lookup(name: '1 low').id,
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update2: {
|
|
|
|
ticket: {
|
|
|
|
title: 'Unit Test 2 (äöüß) - update!',
|
2016-01-19 22:30:23 +00:00
|
|
|
priority_id: Ticket::Priority.lookup(name: '2 normal').id,
|
2014-06-26 22:42:27 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
check: [
|
2015-04-27 15:11:04 +00:00
|
|
|
{
|
2015-04-27 15:21:17 +00:00
|
|
|
result: true,
|
2015-04-27 19:24:14 +00:00
|
|
|
object: 'Ticket',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'update',
|
2015-04-27 15:21:17 +00:00
|
|
|
},
|
2013-09-28 00:07:11 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'Ticket::Article',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'create',
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
2014-06-26 22:42:27 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'Ticket',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'create',
|
2014-06-26 22:42:27 +00:00
|
|
|
},
|
2013-09-28 00:07:11 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: false,
|
|
|
|
object: 'User',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'update',
|
2015-04-27 13:42:53 +00:00
|
|
|
o_id: current_user.id,
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
tickets = []
|
|
|
|
tests.each { |test|
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
ticket = Ticket.create(test[:create][:ticket])
|
2014-05-30 07:57:35 +00:00
|
|
|
test[:check][0][:o_id] = ticket.id
|
2014-06-26 22:42:27 +00:00
|
|
|
test[:check][2][:o_id] = ticket.id
|
|
|
|
test[:check][2][:created_at] = ticket.created_at
|
|
|
|
test[:check][2][:created_by_id] = current_user.id
|
2014-05-30 07:57:35 +00:00
|
|
|
sleep 2
|
|
|
|
|
|
|
|
test[:create][:article][:ticket_id] = ticket.id
|
2016-01-19 22:30:23 +00:00
|
|
|
article = Ticket::Article.create(test[:create][:article])
|
2014-05-30 07:57:35 +00:00
|
|
|
test[:check][1][:o_id] = article.id
|
|
|
|
test[:check][1][:created_at] = article.created_at
|
|
|
|
test[:check][1][:created_by_id] = current_user.id
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
assert_equal(ticket.class.to_s, 'Ticket')
|
|
|
|
assert_equal(article.class.to_s, 'Ticket::Article')
|
2014-05-30 07:57:35 +00:00
|
|
|
|
|
|
|
# update ticket
|
|
|
|
if test[:update][:ticket]
|
2016-01-19 22:30:23 +00:00
|
|
|
ticket.update_attributes(test[:update][:ticket])
|
2014-05-30 07:57:35 +00:00
|
|
|
|
|
|
|
# check updated user
|
2014-06-26 22:42:27 +00:00
|
|
|
test[:check][3][:o_id] = current_user.id
|
|
|
|
test[:check][3][:created_at] = ticket.created_at
|
|
|
|
test[:check][3][:created_by_id] = current_user.id
|
|
|
|
end
|
|
|
|
if test[:update2][:ticket]
|
2016-01-19 22:30:23 +00:00
|
|
|
ticket = Ticket.find(ticket.id)
|
|
|
|
ticket.update_attributes(test[:update2][:ticket])
|
2014-05-30 07:57:35 +00:00
|
|
|
end
|
|
|
|
if test[:update][:article]
|
2016-01-19 22:30:23 +00:00
|
|
|
article.update_attributes(test[:update][:article])
|
2013-09-28 00:07:11 +00:00
|
|
|
end
|
|
|
|
|
2016-04-13 07:30:44 +00:00
|
|
|
sleep activity_record_delay + 1
|
2014-06-26 22:42:27 +00:00
|
|
|
if test[:update][:ticket]
|
2016-01-19 22:30:23 +00:00
|
|
|
ticket.update_attributes(test[:update][:ticket])
|
2014-06-26 22:42:27 +00:00
|
|
|
end
|
|
|
|
if test[:update2][:ticket]
|
2016-01-19 22:30:23 +00:00
|
|
|
ticket.update_attributes(test[:update2][:ticket])
|
2014-06-26 22:42:27 +00:00
|
|
|
end
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
# remember ticket
|
|
|
|
tickets.push ticket
|
|
|
|
|
|
|
|
# check activity_stream
|
2016-01-19 22:30:23 +00:00
|
|
|
activity_stream_check(admin_user.activity_stream(3), test[:check])
|
2013-09-28 00:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# delete tickets
|
|
|
|
tickets.each { |ticket|
|
|
|
|
ticket_id = ticket.id
|
|
|
|
ticket.destroy
|
2016-01-19 22:30:23 +00:00
|
|
|
found = Ticket.where(id: ticket_id).first
|
|
|
|
assert_not(found, 'Ticket destroyed')
|
2013-09-28 00:07:11 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'organization' do
|
|
|
|
tests = [
|
|
|
|
|
|
|
|
# test 1
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
create: {
|
|
|
|
organization: {
|
|
|
|
name: 'some name',
|
|
|
|
updated_by_id: current_user.id,
|
|
|
|
created_by_id: current_user.id,
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update1: {
|
|
|
|
organization: {
|
|
|
|
name: 'some name (äöüß)',
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update2: {
|
|
|
|
organization: {
|
|
|
|
name: 'some name 2 (äöüß)',
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
check: [
|
2013-09-28 00:07:11 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'Organization',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'update',
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'Organization',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'create',
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
organizations = []
|
|
|
|
tests.each { |test|
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
organization = Organization.create(test[:create][:organization])
|
2013-09-28 00:07:11 +00:00
|
|
|
test[:check][0][:o_id] = organization.id
|
|
|
|
test[:check][0][:created_at] = organization.created_at
|
2013-10-05 12:56:03 +00:00
|
|
|
test[:check][0][:created_by_id] = current_user.id
|
2014-05-30 07:57:35 +00:00
|
|
|
sleep 2
|
2013-09-28 00:07:11 +00:00
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
assert_equal(organization.class.to_s, 'Organization')
|
2013-09-28 00:07:11 +00:00
|
|
|
|
|
|
|
if test[:update1][:organization]
|
2016-01-19 22:30:23 +00:00
|
|
|
organization.update_attributes(test[:update1][:organization])
|
2013-09-28 00:07:11 +00:00
|
|
|
test[:check][1][:o_id] = organization.id
|
|
|
|
test[:check][1][:updated_at] = organization.updated_at
|
2013-10-05 12:56:03 +00:00
|
|
|
test[:check][1][:created_by_id] = current_user.id
|
2016-04-13 07:30:44 +00:00
|
|
|
sleep activity_record_delay - 1
|
2013-09-28 00:07:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if test[:update2][:organization]
|
2016-01-19 22:30:23 +00:00
|
|
|
organization.update_attributes(test[:update2][:organization])
|
2013-09-28 00:07:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# remember organization
|
|
|
|
organizations.push organization
|
|
|
|
|
|
|
|
# check activity_stream
|
2016-01-19 22:30:23 +00:00
|
|
|
activity_stream_check(admin_user.activity_stream(2), test[:check])
|
2013-09-28 00:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# delete tickets
|
|
|
|
organizations.each { |organization|
|
|
|
|
organization_id = organization.id
|
|
|
|
organization.destroy
|
2016-01-19 22:30:23 +00:00
|
|
|
found = Organization.where(id: organization_id).first
|
2015-04-27 12:51:43 +00:00
|
|
|
assert( !found, 'Organization destroyed')
|
2013-09-28 00:07:11 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-10-05 12:56:03 +00:00
|
|
|
test 'user with update check false' do
|
|
|
|
tests = [
|
|
|
|
|
|
|
|
# test 1
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
create: {
|
|
|
|
user: {
|
|
|
|
login: 'someemail@example.com',
|
|
|
|
email: 'Bob Smith II <someemail@example.com>',
|
|
|
|
updated_by_id: current_user.id,
|
|
|
|
created_by_id: current_user.id,
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update1: {
|
|
|
|
user: {
|
|
|
|
firstname: 'Bob U',
|
|
|
|
lastname: 'Smith U',
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
check: [
|
2013-10-05 12:56:03 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'User',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'create',
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: false,
|
|
|
|
object: 'User',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'update',
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
users = []
|
|
|
|
tests.each { |test|
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
user = User.create(test[:create][:user])
|
2013-10-05 12:56:03 +00:00
|
|
|
test[:check][0][:o_id] = user.id
|
|
|
|
test[:check][0][:created_at] = user.created_at
|
|
|
|
test[:check][0][:created_by_id] = current_user.id
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
assert_equal(user.class.to_s, 'User')
|
2013-10-05 12:56:03 +00:00
|
|
|
|
|
|
|
if test[:update1][:user]
|
2016-01-19 22:30:23 +00:00
|
|
|
user.update_attributes(test[:update1][:user])
|
2013-10-05 12:56:03 +00:00
|
|
|
test[:check][1][:o_id] = user.id
|
|
|
|
test[:check][1][:updated_at] = user.updated_at
|
|
|
|
test[:check][1][:created_by_id] = current_user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# remember organization
|
|
|
|
users.push user
|
|
|
|
|
|
|
|
# check activity_stream
|
2016-01-19 22:30:23 +00:00
|
|
|
activity_stream_check(admin_user.activity_stream(3), test[:check])
|
2013-10-05 12:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# delete tickets
|
|
|
|
users.each { |user|
|
|
|
|
user_id = user.id
|
|
|
|
user.destroy
|
2015-04-27 13:42:53 +00:00
|
|
|
found = User.where( id: user_id ).first
|
2016-01-19 22:30:23 +00:00
|
|
|
assert_not(found, 'User destroyed')
|
2013-10-05 12:56:03 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'user with update check true' do
|
|
|
|
tests = [
|
|
|
|
|
|
|
|
# test 1
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
create: {
|
|
|
|
user: {
|
|
|
|
login: 'someemail@example.com',
|
|
|
|
email: 'Bob Smith II <someemail@example.com>',
|
|
|
|
updated_by_id: current_user.id,
|
|
|
|
created_by_id: current_user.id,
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update1: {
|
|
|
|
user: {
|
|
|
|
firstname: 'Bob U',
|
|
|
|
lastname: 'Smith U',
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update2: {
|
|
|
|
user: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
2014-06-08 22:01:20 +00:00
|
|
|
},
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
check: [
|
2013-10-05 12:56:03 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'User',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'update',
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
result: true,
|
|
|
|
object: 'User',
|
2016-02-22 19:58:23 +00:00
|
|
|
type: 'create',
|
2013-10-05 12:56:03 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
users = []
|
|
|
|
tests.each { |test|
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
user = User.create(test[:create][:user])
|
2013-10-05 12:56:03 +00:00
|
|
|
test[:check][0][:o_id] = user.id
|
|
|
|
test[:check][0][:created_at] = user.created_at
|
|
|
|
test[:check][0][:created_by_id] = current_user.id
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
assert_equal(user.class.to_s, 'User')
|
2013-10-05 12:56:03 +00:00
|
|
|
|
|
|
|
if test[:update1][:user]
|
2016-01-19 22:30:23 +00:00
|
|
|
user.update_attributes(test[:update1][:user])
|
2013-10-05 12:56:03 +00:00
|
|
|
test[:check][1][:o_id] = user.id
|
|
|
|
test[:check][1][:updated_at] = user.updated_at
|
|
|
|
test[:check][1][:created_by_id] = current_user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# to verify update which need to be logged
|
2016-04-13 07:30:44 +00:00
|
|
|
sleep activity_record_delay + 1
|
2013-10-05 12:56:03 +00:00
|
|
|
|
|
|
|
if test[:update2][:user]
|
2016-01-19 22:30:23 +00:00
|
|
|
user.update_attributes(test[:update2][:user])
|
2013-10-05 12:56:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# remember organization
|
|
|
|
users.push user
|
|
|
|
|
|
|
|
# check activity_stream
|
2016-01-19 22:30:23 +00:00
|
|
|
activity_stream_check(admin_user.activity_stream(2), test[:check])
|
2013-10-05 12:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# delete tickets
|
|
|
|
users.each { |user|
|
|
|
|
user_id = user.id
|
|
|
|
user.destroy
|
2016-01-19 22:30:23 +00:00
|
|
|
found = User.where(id: user_id).first
|
|
|
|
assert(!found, 'User destroyed')
|
2013-10-05 12:56:03 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
def activity_stream_check(activity_stream_list, checks)
|
2014-06-26 22:42:27 +00:00
|
|
|
#activity_stream_list = activity_stream_list.reverse
|
2014-05-30 07:57:35 +00:00
|
|
|
#puts 'AS ' + activity_stream_list.inspect
|
2014-06-26 22:42:27 +00:00
|
|
|
check_count = 0
|
2013-09-28 00:07:11 +00:00
|
|
|
checks.each { |check_item|
|
2014-06-26 22:42:27 +00:00
|
|
|
check_count += 1
|
|
|
|
|
2014-05-30 07:57:35 +00:00
|
|
|
#puts '+++++++++++'
|
|
|
|
#puts check_item.inspect
|
2014-06-26 22:42:27 +00:00
|
|
|
check_list = 0
|
2013-09-28 00:07:11 +00:00
|
|
|
activity_stream_list.each { |item|
|
2014-06-26 22:42:27 +00:00
|
|
|
check_list += 1
|
|
|
|
next if check_list != check_count
|
2015-04-27 19:24:14 +00:00
|
|
|
#next if match
|
2014-06-26 22:42:27 +00:00
|
|
|
#puts '--------'
|
|
|
|
#puts item.inspect
|
|
|
|
#puts check_item.inspect
|
|
|
|
if check_item[:result]
|
2016-01-19 22:30:23 +00:00
|
|
|
assert_equal(check_item[:object], item['object'])
|
|
|
|
assert_equal(check_item[:type], item['type'])
|
|
|
|
assert_equal(check_item[:o_id], item['o_id'])
|
2016-01-15 17:22:57 +00:00
|
|
|
elsif check_item[:object] == item['object'] && check_item[:type] == item['type'] && check_item[:o_id] == item['o_id']
|
2016-01-19 22:30:23 +00:00
|
|
|
assert(false, "entry should not exist #{item['object']}/#{item['type']}/#{item['o_id']}")
|
2014-06-26 22:42:27 +00:00
|
|
|
end
|
2013-09-28 00:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|