Class: Wfirma::Drivers::Fake

Inherits:
Object
  • Object
show all
Includes:
Wfirma::DeepDup
Defined in:
lib/wfirma/drivers/fake.rb,
lib/wfirma/drivers/fake/catalogue.rb

Overview

Dev/test driver - no HTTP at all. Returns realistic wFirma-shaped responses so the full payload-mapping and Result-parsing code paths run exactly as in production.

It keeps an in-memory contractor catalogue, so the find -> add/edit flow behaves in dev the way it does against the real CRM: the same customer resolves to one record across invoices.

Failure simulation:

  • Magic contractor NIPs (Stripe-test-card style) drive scenarios from the UI or system tests; all-zeros NIPs are checksum-invalid so no real customer can trigger them. They apply to any call carrying a contractor (invoices/add, contractors/add, contractors/edit).
  • Polish postal codes are validated the way wFirma validates them, so the malformed-zip rejection can be exercised without a network.
  • fail_next!/fail_always! for unit tests and console; these apply to every action, download included.

Defined Under Namespace

Classes: Catalogue, Request

Constant Summary collapse

MAGIC_NIPS =
{
  "0000000000" => :validation_error,
  "0000000001" => :auth_error,
  "0000000002" => :connection_error
}.freeze
PL_ZIP =
/\A\d{2}-\d{3}\z/
ZIP_ERROR =

wFirma accepts Polish postal codes only as XX-XXX and rejects the whole contractor otherwise. Message and field taken from a rejection observed in the reference integration.

{ "field" => "zip", "message" => "Niepoprawny format kodu pocztowego." }.freeze
MAGIC_NIP_ERROR =
{
  "field" => "nip",
  "message" => "Symulowany błąd walidacji (magic NIP 0000000000)"
}.freeze
FAKE_PDF =

Smallest thing that still passes a "%PDF-" magic-bytes check.

"%PDF-1.4\n% Wfirma::Drivers::Fake\n%%EOF\n".b.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wfirma::DeepDup

deep_dup

Constructor Details

#initializeFake

Returns a new instance of Fake.



48
49
50
# File 'lib/wfirma/drivers/fake.rb', line 48

def initialize
  reset!
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



46
47
48
# File 'lib/wfirma/drivers/fake.rb', line 46

def requests
  @requests
end

Instance Method Details

#call(module_name, action, payload, params = {}, id = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wfirma/drivers/fake.rb', line 69

def call(module_name, action, payload, params = {}, id = nil)
  record(module_name, action, payload, params, id)

  failure = consume_failure
  return simulate_failure(failure, payload) if failure

  raise_magic_nip_error(payload)
  rejection = contractor_rejection(payload)
  return rejection if rejection

  return contractors_response(action, payload, id) if module_name == "contractors"

  ok_response(payload)
end

#download(module_name, action, payload, params = {}, id = nil) ⇒ Object

Mirrors Drivers::Http#download: raw bytes on success, an exception otherwise - a fake PDF is enough to exercise storage/serving code.



86
87
88
89
90
91
92
93
# File 'lib/wfirma/drivers/fake.rb', line 86

def download(module_name, action, payload, params = {}, id = nil)
  record(module_name, action, payload, params, id)

  failure = consume_failure
  simulate_download_failure(failure) if failure

  FAKE_PDF.dup
end

#fail_always!(code: "ERROR", errors: []) ⇒ Object



65
66
67
# File 'lib/wfirma/drivers/fake.rb', line 65

def fail_always!(code: "ERROR", errors: [])
  @fail_always = { code: code, errors: errors }
end

#fail_next!(code: "ERROR", errors: []) ⇒ Object



61
62
63
# File 'lib/wfirma/drivers/fake.rb', line 61

def fail_next!(code: "ERROR", errors: [])
  @fail_next = { code: code, errors: errors }
end

#reset!Object



52
53
54
55
56
57
58
59
# File 'lib/wfirma/drivers/fake.rb', line 52

def reset!
  @requests = []
  @next_id = 0
  @next_contractor_id = 0
  @fail_next = nil
  @fail_always = nil
  @contractors = Catalogue.new
end