Skip to content

Instantly share code, notes, and snippets.

@gotar
Created Dec 9, 2017
Embed
What would you like to do?
rack-test with roda
require_relative "db_helper"
require "rack/test"
require SPEC_ROOT.join("../system/boot").realpath
RSpec.configure do |config|
config.include Rack::Test::Methods#, type: :request
config.include TestHelpers
config.before :suite do
TestHelpers.app.freeze
end
end
require "api_app_helper"
RSpec.describe "Authentication" do
it_behaves_like "API over HTTPS with Basic Auth" do
let(:do_request) { request }
context "with masi realm" do
let(:user) { create_admin }
before { header("WWW-Authenticate", 'Token realm="masi"') }
context "when user exists" do
it "returns 200 HTTP code" do
header("X-Token", "Token token=#{user[:token]}, email=#{user[:email]}")
do_request
expect(last_response.status).to eq(200)
end
end
context "when user doesn't exists" do
it "returns 401 HTTP code" do
header("X-Token", "Token token=abcd, email=test@example.com")
do_request
expect(last_response.status).to eq(401)
end
end
end
context "with cms realm" do
let(:user) { create_cms_admin_user }
before { header("WWW-Authenticate", 'Token realm="cms"') }
context "when user exists" do
it "returns 200 HTTP code" do
header("X-Token", "Token token=#{user[:token]}, email=#{user[:email]}")
do_request
expect(last_response.status).to eq(200)
end
end
context "when user doesn't exists" do
it "returns 401 HTTP code" do
header("X-Token", "Token token=abcd, email=test@example.com")
do_request
expect(last_response.status).to eq(401)
end
end
end
end
def request
base_env.merge!("HTTP_ACCEPT" => "application/vnd.api+json", "CONTENT_TYPE" => "application/vnd.api+json")
get "/v2/users", {}, base_env
end
end
require "api_app_helper"
RSpec.describe "POST /v2/users" do
it_behaves_like "JSON API over HTTPS" do
let(:do_request) { request }
context "user" do
context "invalid email" do
let(:do_request) { request(email: "foo_bar", mobile: "1234") }
it_behaves_like "JSON API Content-Type"
it_behaves_like "Response Unprocessable Entity"
it "response with proper error message for invalid param" do
do_request
expect(json_response["errors"][0]).
to eq("status" => 422, "title" => "Unprocessable Entity", "detail" => "invalid",
"source" => { "pointer" => "/data/attributes/email" })
end
end
context "without first name" do
let(:do_request) { request(first_name: nil) }
it_behaves_like "JSON API Content-Type"
it_behaves_like "Response Created"
it "returns proper data" do
do_request
attributes = json_response["data"]["attributes"].keys
expect(attributes).
to match_array(%w(first_name last_name email token alias mobile settings confirmed_at))
end
end
context "without last name" do
let(:do_request) { request(last_name: nil) }
it_behaves_like "JSON API Content-Type"
it_behaves_like "Response Created"
it "returns proper data" do
request(last_name: nil)
attributes = json_response["data"]["attributes"].keys
expect(attributes).
to match_array(%w(first_name last_name email token alias mobile settings confirmed_at))
end
end
context "valid params" do
context "basic params" do
let(:do_request) { request }
it_behaves_like "JSON API Content-Type"
it_behaves_like "Response Created"
it "returns proper data" do
request
attributes = json_response["data"]["attributes"].keys
expect(attributes).
to match_array(%w(first_name last_name email token alias mobile settings confirmed_at))
end
context "when email is not uniq" do
let(:user) { create_user }
let(:do_request) { request(email: user[:email]) }
it_behaves_like "JSON API Content-Type"
it_behaves_like "Response Unprocessable Entity"
it "returns error when email is not uniq" do
do_request
expect(json_response["errors"][0]).
to eq("status" => 422, "title" => "Unprocessable Entity", "detail" => "taken",
"source" => { "pointer" => "/data/attributes/email" })
end
end
end
context "when email contains big letters" do
let(:email) { "EXAMPLE-#{Time.now.to_f}@example.com" }
let(:do_request) { request(email: email) }
it_behaves_like "JSON API Content-Type"
it_behaves_like "Response Created"
it "returns proper data" do
request
attributes = json_response["data"]["attributes"].keys
expect(attributes).
to match_array(%w(first_name last_name email token alias mobile settings confirmed_at))
end
end
context "obsolete params" do
it "allow to create user with alias" do
request(alias: "foo")
expect(json_response["data"]["attributes"]["alias"]).to eq("foo")
end
it "allow to create user with mobile number" do
request(mobile: "0812341234")
expect(json_response["data"]["attributes"]["mobile"]).to eq("0812341234")
end
end
context "when confirmation flag is off" do
before { ENV["USER_CONFIRMATION_FLAG"] = "ON" }
it "sends email" do
expect_any_instance_of(Mandrill::API).to receive_message_chain(:messages, :send) do |options|
expect(options[:from_email]).to eq("no-reply@sngp.co")
expect(options[:to]).to eq([{ email: base_params[:email] }])
expect(options[:subject]).to eq("You've registered an account with Masii.co.th")
end
request
end
end
context "when confirmation flag is off" do
before { ENV["USER_CONFIRMATION_FLAG"] = "OFF" }
it "sends email" do
expect_any_instance_of(Mandrill::API).to receive_message_chain(:messages, :send) do |options|
expect(options[:from_email]).to eq("no-reply@sngp.co")
expect(options[:to]).to eq([{ email: base_params[:email] }])
expect(options[:subject]).to eq("Welcome")
end
request
end
end
it "do not allow to overwrite token" do
request(token: "12345")
expect(json_response["data"]["attributes"]["token"]).to_not eql("12345")
end
it "allows to add settings" do
request(settings: { foo: "bar" })
expect(json_response["data"]["attributes"]["settings"]).to eql("foo" => "bar")
end
end
end
end
def request(h: nil, **params)
header(*h) if h
header("Accept-Language", "thai-en")
json_post "/v2/users", base_params.merge(params), base_env
end
def base_params
@basic_user_params ||= basic_user_params
end
end
require_relative "spec_helper"
Masii::Container.boot! :rom
require "database_cleaner"
DatabaseCleaner[:sequel, { connection: TestHelpers.connection }].
strategy = :truncation, { except: %w(insurers vehicles operators) }
RSpec.configure do |config|
config.before :suite do
DatabaseCleaner.clean_with :truncation
end
config.around :each do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
ENV["RACK_ENV"] = "test"
require "byebug"
SPEC_ROOT = Pathname(__FILE__).dirname
Dir[SPEC_ROOT.join("support/*.rb").to_s].each(&method(:require))
require SPEC_ROOT.join("../system/masii/container")
# Load each app's container
app_names = Dir[SPEC_ROOT.join("../apps/*")].map(&File.method(:basename))
app_names.each do |app|
require SPEC_ROOT.join("../apps/#{app}/system/#{app}/container")
end
RSpec.configure do |config|
config.disable_monkey_patching!
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.include TestHelpers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment