//  [ruby]
    it 'does ticket with correct ticket id (02.04)' do
      title = "ticket with corret ticket id testagent#{rand(999_999_999)}"
      ticket = create(
        :ticket,
        title:       title,
        group:       ticket_group,
        customer_id: customer.id,
        preferences: {
          some_key1: 123,
        },
      )
      authenticated_as(agent)
      get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
      expect(response).to have_http_status(:ok)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['id']).to eq(ticket.id)
      expect(json_response['title']).to eq(title)
      expect(json_response['customer_id']).to eq(ticket.customer_id)
      expect(json_response['updated_by_id']).to eq(1)
      expect(json_response['created_by_id']).to eq(1)
      expect(json_response['preferences']['some_key1']).to eq(123)

      params = {
        title:       "#{title} - 2",
        customer_id: agent.id,
        preferences: {
          some_key2: 'abc',
        },
      }
      put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
      expect(response).to have_http_status(:ok)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['id']).to eq(ticket.id)
      expect(json_response['title']).to eq("#{title} - 2")
      expect(json_response['customer_id']).to eq(agent.id)
      expect(json_response['updated_by_id']).to eq(agent.id)
      expect(json_response['created_by_id']).to eq(1)
      expect(json_response['preferences']['some_key1']).to eq(123)
      expect(json_response['preferences']['some_key2']).to eq('abc')

      params = {
        ticket_id: ticket.id,
        subject:   'some subject',
        body:      'some body',
      }
      post '/api/v1/ticket_articles', params: params, as: :json
      expect(response).to have_http_status(:created)
      article_json_response = json_response
      expect(article_json_response).to be_a_kind_of(Hash)
      expect(article_json_response['ticket_id']).to eq(ticket.id)
      expect(article_json_response['from']).to eq('Tickets Agent')
      expect(article_json_response['subject']).to eq('some subject')
      expect(article_json_response['body']).to eq('some body')
      expect(article_json_response['content_type']).to eq('text/plain')
      expect(article_json_response['internal']).to eq(false)
      expect(article_json_response['created_by_id']).to eq(agent.id)
      expect(article_json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
      expect(article_json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)

      Scheduler.worker(true)
      get "/api/v1/tickets/search?query=#{CGI.escape(title)}", params: {}, as: :json
      expect(response).to have_http_status(:ok)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['tickets'][0]).to eq(ticket.id)
      expect(json_response['tickets_count']).to eq(1)

      params = {
        condition: {
          'ticket.title' => {
            operator: 'contains',
            value:    title,
          },
        },
      }
      post '/api/v1/tickets/search', params: params, as: :json
      expect(response).to have_http_status(:ok)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['tickets'][0]).to eq(ticket.id)
      expect(json_response['tickets_count']).to eq(1)

      delete "/api/v1/ticket_articles/#{article_json_response['id']}", params: {}, as: :json
      expect(response).to have_http_status(:ok)

      params = {
        from:      'something which should not be changed on server side',
        ticket_id: ticket.id,
        subject:   'some subject',
        body:      'some body',
        type:      'email',
        internal:  true,
      }
      post '/api/v1/ticket_articles', params: params, as: :json
      expect(response).to have_http_status(:created)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['ticket_id']).to eq(ticket.id)
      expect(json_response['from']).to eq(%("Tickets Agent via #{ticket_group.email_address.realname}" <#{ticket_group.email_address.email}>))
      expect(json_response['subject']).to eq('some subject')
      expect(json_response['body']).to eq('some body')
      expect(json_response['content_type']).to eq('text/plain')
      expect(json_response['internal']).to eq(true)
      expect(json_response['created_by_id']).to eq(agent.id)
      expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
      expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)

      params = {
        subject: 'new subject',
      }
      put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
      expect(response).to have_http_status(:ok)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['ticket_id']).to eq(ticket.id)
      expect(json_response['from']).to eq(%("Tickets Agent via #{ticket_group.email_address.realname}" <#{ticket_group.email_address.email}>))
      expect(json_response['subject']).to eq('new subject')
      expect(json_response['body']).to eq('some body')
      expect(json_response['content_type']).to eq('text/plain')
      expect(json_response['internal']).to eq(true)
      expect(json_response['created_by_id']).to eq(agent.id)
      expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
      expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)

      params = {
        from:      'something which should not be changed on server side',
        ticket_id: ticket.id,
        subject:   'some subject',
        body:      'some body',
        type:      'email',
        internal:  false,
      }
      post '/api/v1/ticket_articles', params: params, as: :json
      expect(response).to have_http_status(:created)
      expect(json_response['internal']).to eq(false)

      delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
      expect(response).to have_http_status(:unauthorized)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['error']).to eq('Not authorized (communication articles cannot be deleted)!')

      delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
      expect(response).to have_http_status(:unauthorized)
      expect(json_response).to be_a_kind_of(Hash)
      expect(json_response['error']).to eq('Not authorized (admin permission required)!')
    end