Last active
May 11, 2020 08:44
-
-
Save jaz303/2421e234764f63f59cb97679c3c5d9a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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