123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- # encoding: utf-8
- require 'test_helper'
- class TicketArticlesControllerTest < ActionDispatch::IntegrationTest
- setup do
- # set accept header
- @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
- # create agent
- roles = Role.where(name: %w(Admin Agent))
- groups = Group.all
- UserInfo.current_user_id = 1
- @admin = User.create_or_update(
- login: 'tickets-admin',
- firstname: 'Tickets',
- lastname: 'Admin',
- email: 'tickets-admin@example.com',
- password: 'adminpw',
- active: true,
- roles: roles,
- groups: groups,
- )
- # create agent
- roles = Role.where(name: 'Agent')
- @agent = User.create_or_update(
- login: 'tickets-agent@example.com',
- firstname: 'Tickets',
- lastname: 'Agent',
- email: 'tickets-agent@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- )
- # create customer without org
- roles = Role.where(name: 'Customer')
- @customer_without_org = User.create_or_update(
- login: 'tickets-customer1@example.com',
- firstname: 'Tickets',
- lastname: 'Customer1',
- email: 'tickets-customer1@example.com',
- password: 'customer1pw',
- active: true,
- roles: roles,
- )
- end
- test '01.01 ticket create with agent and articles' do
- credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
- params = {
- title: 'a new ticket #1',
- group: 'Users',
- customer_id: @customer_without_org.id,
- article: {
- body: 'some body',
- }
- }
- post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- params = {
- ticket_id: result['id'],
- content_type: 'text/plain', # or text/html
- body: 'some body',
- type: 'note',
- }
- post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(nil, result['subject'])
- assert_equal('some body', result['body'])
- assert_equal('text/plain', result['content_type'])
- assert_equal(@agent.id, result['updated_by_id'])
- assert_equal(@agent.id, result['created_by_id'])
- ticket = Ticket.find(result['ticket_id'])
- assert_equal(2, ticket.articles.count)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- params = {
- ticket_id: result['ticket_id'],
- content_type: 'text/html', # or text/html
- body: 'some body <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
- AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
- 9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />',
- type: 'note',
- }
- post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(nil, result['subject'])
- assert_no_match(/some body <img src="cid:.+?/, result['body'])
- assert_match(%r{some body <img src="/api/v1/ticket_attachment/.+?" alt="Red dot"}, result['body'])
- assert_equal('text/html', result['content_type'])
- assert_equal(@agent.id, result['updated_by_id'])
- assert_equal(@agent.id, result['created_by_id'])
- assert_equal(3, ticket.articles.count)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- assert_equal(1, ticket.articles[2].attachments.count)
- assert(ticket.articles[2].attachments[0]['id'])
- assert_match(/@zammad.example.com/, ticket.articles[2].attachments[0]['filename'])
- assert_equal('21', ticket.articles[2].attachments[0]['size'])
- assert_equal('image/png', ticket.articles[2].attachments[0]['preferences']['Mime-Type'])
- assert_equal('inline', ticket.articles[2].attachments[0]['preferences']['Content-Disposition'])
- assert_match(/@zammad.example.com/, ticket.articles[2].attachments[0]['preferences']['Content-ID'])
- params = {
- ticket_id: result['ticket_id'],
- content_type: 'text/html', # or text/html
- body: 'some body',
- type: 'note',
- attachments: [
- 'filename' => 'some_file.txt',
- 'data' => 'dGVzdCAxMjM=',
- 'mime-type' => 'text/plain',
- ],
- }
- post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(nil, result['subject'])
- assert_equal('some body', result['body'])
- assert_equal('text/html', result['content_type'])
- assert_equal(@agent.id, result['updated_by_id'])
- assert_equal(@agent.id, result['created_by_id'])
- assert_equal(4, ticket.articles.count)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- assert_equal(1, ticket.articles[2].attachments.count)
- assert_equal(1, ticket.articles[3].attachments.count)
- get "/api/v1/ticket_articles/#{result['id']}?expand=true", {}.to_json, @headers.merge('Authorization' => credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(1, result['attachments'].count)
- assert(result['attachments'][0]['id'])
- assert_equal('some_file.txt', result['attachments'][0]['filename'])
- assert_equal('8', result['attachments'][0]['size'])
- assert_equal('text/plain', result['attachments'][0]['preferences']['Mime-Type'])
- end
- test '02.01 ticket create with customer and articles' do
- credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
- params = {
- title: 'a new ticket #2',
- group: 'Users',
- article: {
- body: 'some body',
- }
- }
- post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- params = {
- ticket_id: result['id'],
- content_type: 'text/plain', # or text/html
- body: 'some body',
- type: 'note',
- }
- post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(nil, result['subject'])
- assert_equal('some body', result['body'])
- assert_equal('text/plain', result['content_type'])
- assert_equal(@customer_without_org.id, result['updated_by_id'])
- assert_equal(@customer_without_org.id, result['created_by_id'])
- ticket = Ticket.find(result['ticket_id'])
- assert_equal(2, ticket.articles.count)
- assert_equal('Customer', ticket.articles[1].sender.name)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- params = {
- ticket_id: result['ticket_id'],
- content_type: 'text/plain', # or text/html
- body: 'some body',
- sender: 'Agent',
- type: 'note',
- }
- post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(nil, result['subject'])
- assert_equal('some body', result['body'])
- assert_equal('text/plain', result['content_type'])
- assert_equal(@customer_without_org.id, result['updated_by_id'])
- assert_equal(@customer_without_org.id, result['created_by_id'])
- ticket = Ticket.find(result['ticket_id'])
- assert_equal(3, ticket.articles.count)
- assert_equal('Customer', ticket.articles[2].sender.name)
- assert_equal(false, ticket.articles[2].internal)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- assert_equal(0, ticket.articles[2].attachments.count)
- params = {
- ticket_id: result['ticket_id'],
- content_type: 'text/plain', # or text/html
- body: 'some body 2',
- sender: 'Agent',
- type: 'note',
- internal: true,
- }
- post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(nil, result['subject'])
- assert_equal('some body 2', result['body'])
- assert_equal('text/plain', result['content_type'])
- assert_equal(@customer_without_org.id, result['updated_by_id'])
- assert_equal(@customer_without_org.id, result['created_by_id'])
- ticket = Ticket.find(result['ticket_id'])
- assert_equal(4, ticket.articles.count)
- assert_equal('Customer', ticket.articles[3].sender.name)
- assert_equal(false, ticket.articles[3].internal)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- assert_equal(0, ticket.articles[2].attachments.count)
- assert_equal(0, ticket.articles[3].attachments.count)
- # add internal article
- article = Ticket::Article.create(
- ticket_id: ticket.id,
- from: 'some_sender@example.com',
- to: 'some_recipient@example.com',
- subject: 'some subject',
- message_id: 'some@id',
- body: 'some message 123',
- internal: true,
- sender: Ticket::Article::Sender.find_by(name: 'Agent'),
- type: Ticket::Article::Type.find_by(name: 'note'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- assert_equal(5, ticket.articles.count)
- assert_equal('Agent', ticket.articles[4].sender.name)
- assert_equal(1, ticket.articles[4].updated_by_id)
- assert_equal(1, ticket.articles[4].created_by_id)
- assert_equal(0, ticket.articles[0].attachments.count)
- assert_equal(0, ticket.articles[1].attachments.count)
- assert_equal(0, ticket.articles[2].attachments.count)
- assert_equal(0, ticket.articles[3].attachments.count)
- assert_equal(0, ticket.articles[4].attachments.count)
- get "/api/v1/ticket_articles/#{article.id}", {}.to_json, @headers.merge('Authorization' => credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('Not authorized', result['error'])
- put "/api/v1/ticket_articles/#{article.id}", { internal: false }.to_json, @headers.merge('Authorization' => credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('Not authorized', result['error'])
- end
- end
|