Module: Webhookdb::SpecHelpers

Defined in:
lib/webhookdb/spec_helpers.rb

Defined Under Namespace

Modules: Async, Citest, Message, Postgres, Sentry, Service, Whdb

Constant Summary collapse

TEST_DATA_DIR =

The directory to look in for fixture data

Pathname("spec/data").expand_path

Class Method Summary collapse

Class Method Details

.included(context) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/webhookdb/spec_helpers.rb', line 18

def self.included(context)
  context.before(:all) do
    Webhookdb::Customer.password_hash_cost = 1
  end
  context.before(:each) do
    allow(Kernel).to receive(:sleep) do |n|
      raise "Never sleep with > 0 during tests" if n.positive?
    end
  end
  super
end

.json_headers(**more) ⇒ Object



34
35
36
# File 'lib/webhookdb/spec_helpers.rb', line 34

module_function def json_headers(**more)
  return {"Content-Type" => "application/json"}.merge(**more)
end

.json_response(body, status: 200, headers: {}) ⇒ Object



38
39
40
# File 'lib/webhookdb/spec_helpers.rb', line 38

module_function def json_response(body, status: 200, headers: {})
  return {status:, body: body.to_json, headers: json_headers(**headers)}
end

.load_fixture_data(name, raw: false) ⇒ Object

Load data from the spec/data directory with the specified name, deserializing it if it’s YAML or JSON, and returning it.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webhookdb/spec_helpers.rb', line 44

module_function def load_fixture_data(name, raw: false)
  name = name.to_s
  path = TEST_DATA_DIR + name
  path = TEST_DATA_DIR + "#{name}.json" unless path.exist? || !File.extname(name).empty?
  path = TEST_DATA_DIR + "#{name}.yaml" unless path.exist? || !File.extname(name).empty?
  path = TEST_DATA_DIR + "#{name}.yml" unless path.exist? || !File.extname(name).empty?

  rawdata = path.read(encoding: "utf-8")

  return rawdata if raw

  return case path.extname
    when ".json"
      Oj.load(rawdata)
    when ".yml", ".yaml"
      YAML.safe_load(rawdata)
    else
      rawdata
  end
end

.test_data_dirObject



30
31
32
# File 'lib/webhookdb/spec_helpers.rb', line 30

module_function def test_data_dir
  return TEST_DATA_DIR
end