52 lines
1.4 KiB
Ruby
52 lines
1.4 KiB
Ruby
ENV["LIFEPEX_ENV"] = "test"
|
|
require "yaml"
|
|
require_relative "./fixtures_reader"
|
|
require_relative "../src/app"
|
|
|
|
require "rack/test"
|
|
require "test/unit"
|
|
|
|
class LifePexTest < Test::Unit::TestCase
|
|
include Rack::Test::Methods
|
|
|
|
def app
|
|
LifePex::Systems::BaseSystem
|
|
end
|
|
|
|
# check if every key of `minimal_data` is contained in `full_data`
|
|
def assert_hash_include(minimal_data, full_data)
|
|
full_data = full_data.stringify_keys
|
|
minimal_data.stringify_keys.each do |key, value|
|
|
assert_equal value, full_data[key], "#{key} should contains <#{value}>, got <#{full_data[key]}>"
|
|
end
|
|
end
|
|
|
|
%i(get delete head).each do |verb|
|
|
define_method "api_#{verb}" do |uri, **headers|
|
|
self.send(verb, uri, headers.merge({ "CONTENT_TYPE" => "application/json" }))
|
|
end
|
|
end
|
|
|
|
%i(post put patch).each do |verb|
|
|
define_method "api_#{verb}" do |uri, body, **headers|
|
|
self.send(verb, uri, body.to_json, headers.merge({ "CONTENT_TYPE" => "application/json" }))
|
|
end
|
|
end
|
|
|
|
# parse the body as a json
|
|
def api_reponse_body
|
|
JSON.parse last_response.body
|
|
end
|
|
|
|
# check if the body contains a message
|
|
def assert_message_exist
|
|
assert_not_empty api_reponse_body.dig "message"
|
|
end
|
|
|
|
# check if the entity.user_id belongs to the user who sent the last req
|
|
def assert_entity_owned(entity)
|
|
user_id = JWT.decode(last_request.cookies["auth"], nil, false).first["user_id"]
|
|
assert_equal user_id.to_s, entity["user_id"].to_s
|
|
end
|
|
end
|