Sha256: 20f26552c7ea2921bad69c61ba22109c1222ff55ff2bef1f01216a17b5983474

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 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.exists?(@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.encode64(output) }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
invoice_printer-1.2.0 test/api_test.rb
invoice_printer-1.2.0.alpha1 test/api_test.rb