Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created February 14, 2017 18:38
Show Gist options
  • Save botanicus/62e9ff8fe15366ea2787be1d5cd506a2 to your computer and use it in GitHub Desktop.
Save botanicus/62e9ff8fe15366ea2787be1d5cd506a2 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require_relative 'spec_helper'
describe 'GET /addresses/:id', role: :retailer, model: GiveIt::Models::Address do
include GiveIt::Spec::Endpoints
let(:attrs) do
{email: 'retailer@test.com'}
end
it 'should return JSON with address' do
response_data[:email].should eql('retailer@test.com')
end
it 'should respond with HTTP 403 if the address does not belong to the current retailer'
end
describe 'POST /addresses', role: :retailer, model: GiveIt::Models::Address do
include GiveIt::Spec::Endpoints
let(:request_data) do
{data: model.factory.values.merge(email: 'new@user.com')}
end
it 'should respond with 201' do
response.code.should eql(201)
end
it 'should respond with {id: <id>}' do
response_data.should have_key(:id)
object = GiveIt::Models::Address[response_data[:id]]
object.email.should eql('new@user.com')
end
end
describe 'GET /addresses/:id', role: :user, model: GiveIt::Models::Address do
include GiveIt::Spec::Endpoints
let(:attrs) do
{email: 'user@test.com'}
end
it 'should return JSON with address' do
response_data[:email].should eql('user@test.com')
end
it 'should respond with HTTP 403 if the address does not belong to the current user'
end
describe 'PUT /addresses/:id', role: :user, model: GiveIt::Models::Address do
include GiveIt::Spec::Endpoints
let(:attrs) do
{email: 'user@new_email.com'}
end
it 'should update the attributes' do
response_data[:email].should eql()
end
end
# NOTE: Is there any role-specific behaviour???
describe 'POST /addresses', role: :user, model: GiveIt::Models::Address do
include GiveIt::Spec::Endpoints
let(:request_data) do
{data: model.factory.values.merge(email: 'new@user.com')}
end
it 'should respond with 201' do
response.code.should eql(201)
end
it 'should respond with {id: <id>}' do
response_data.should have_key(:id)
object = GiveIt::Models::Address[response_data[:id]]
object.email.should eql('new@user.com')
end
end
# encoding: utf-8
module GiveIt
module Spec
module Endpoints
def self.included(base)
base.class_eval do
def top_level_desc
@example.metadata[:example_group][:description]
end
def request_method
top_level_desc.split(' ').first
end
def request_path
top_level_desc.split(' ').last.split('/').
map { |fragment| fragment.sub(/^:(.+)$/) { |match|
instance.send(match[1..-1])
} }.join('/')
end
let(:model) do
@example.metadata[:model]
end
let(:attrs) do
Hash.new
end
let(:factory) do
self.model.factory(attrs)
end
let(:instance) do
# TODO: maybe inheritance?
if request_method == "POST"
factory
else
factory.save
puts "~ DB: #{factory.values}"
factory
end
end
let(:url) do
"http://localhost:9990#{request_path}?role=#{@example.metadata[:role]}"
end
# Rest client docs: https://github.com/rest-client/rest-client
let(:response) do
if ['GET', 'DELETE'].include?(request_method)
RestClient.send(request_method.downcase, url)
else
puts "~ #{request_method} data: #{request_data.inspect}"
RestClient.send(request_method.downcase, url, request_data)
end
end
let(:response_data) do
data = JSON.parse(response).reduce(Hash.new) do |buffer, (key, value)|
buffer.merge(key.to_sym => value)
end
puts "Code: #{response.code}"
puts "Data: #{data.inspect}"
data
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment