Skip to content

Instantly share code, notes, and snippets.

@jaz303
Last active May 11, 2020 08:44
Show Gist options
  • Save jaz303/2421e234764f63f59cb97679c3c5d9a9 to your computer and use it in GitHub Desktop.
Save jaz303/2421e234764f63f59cb97679c3c5d9a9 to your computer and use it in GitHub Desktop.
database(:db, adapter: :pg, host: '127.0.0.1', port: 5432) do
# reset logic here?
end
http_endpoint(:api, '127.0.0.1', 8000) do |addr, port|
working_dir ".."
run "build/api-server"
env LISTEN: "#{addr}:#{port}"
end
scenario(:default) do
run_sql :db, glob("scenarios/default/*.sql")
end
DEFAULT_USER = "bob"
USERS = {
"bob": "quux"
}
module APIAware
import_dependency :api
def login(user = DEFAULT_USER)
res = api.post("/public/v1/login", body: {
json: user,
password: USERS[user]
})
raise unless res.ok?
@session_token = res.json[:session_token]
end
def api_request_headers
{}.tap do |h|
h['Authorization'] = "Bearer token=#{@session_token}" if @session_token
end
end
def get(url)
api.get(url, headers: api_request_headers)
end
def post(url, body = nil)
api.post(url, json: body, headers: api_request_headers)
end
end
test_case do
use_scenario :default
include APIAware
test "query results (simple)" do
res = get("/public/v1/query?foo=bar")
assert_equal 200, res.status
assert_equal 20, res.json[:results].length
end
test "create user" do
assert_change -> db.count("users") do
res = post("/public/v1/users/create", json: {
username: "bob",
password: "quux"
})
assert_equal 201, res.status
end
end
test "foobar" do
login "bob"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment