Sha256: 25c1179217cc250f25bc02b22e62b36ac4ba82dc1f2993cb362073b89e81be3b

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

require 'test_helper'
require 'invoice_printer/server'

class ApiTest < Minitest::Test
  include Rack::Test::Methods
  include InvoicePrinterHelpers

  def app
    InvoicePrinter::Server.freeze.app
  end

  def setup
    @test_dir = File.absolute_path('./tmp/invoice_printer_api')
    FileUtils.mkdir_p @test_dir
  end

  def teardown
    FileUtils.rm_rf @test_dir if File.exist?(@test_dir)
  end

  # Test POST /print

  def test_print_with_invalid_json
    header 'Content-Type', 'application/json'
    post '/print', nil

    body = JSON.parse last_response.body

    assert !last_response.ok?
    assert_equal body, { 'result' => 'error', 'error' => 'Invalid JSON.' }
  end

  def test_print_with_valid_document
    invoice = InvoicePrinter::Document.new(**default_document_params)

    json = {
      'document' => invoice.to_h,
      'filename' => "#{@test_dir}/test"
    }.to_json

    header 'Content-Type', 'application/json'
    post '/print', json

    body = JSON.parse last_response.body

    assert last_response.ok?
    assert_equal body, { 'result' => 'ok', 'path' => "#{@test_dir}/test" }
  end

  # Test POST /render

  def test_render_with_invalid_json
    header 'Content-Type', 'application/json'
    post '/render', nil

    body = JSON.parse last_response.body

    assert !last_response.ok?
    assert_equal body, { 'result' => 'error', 'error' => 'Invalid JSON.' }
  end

  def test_render_with_valid_document
    invoice  = InvoicePrinter::Document.new(**default_document_params)
    output   = InvoicePrinter.render(document: invoice)

    json = {
      'document' => invoice.to_h
    }.to_json

    header 'Content-Type', 'application/json'
    post '/render', json

    body = JSON.parse last_response.body

    assert last_response.ok?
    assert_equal body, { 'result' => 'ok', 'data' => Base64.strict_encode64(output) }
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
invoice_printer-2.4.0 test/api_test.rb
invoice_printer-2.3.0 test/api_test.rb
invoice_printer-2.2.0 test/api_test.rb
invoice_printer-2.2.0.alpha1 test/api_test.rb
invoice_printer-2.1.0 test/api_test.rb
invoice_printer-2.1.0.rc1 test/api_test.rb