Created
February 14, 2017 18:38
-
-
Save botanicus/62e9ff8fe15366ea2787be1d5cd506a2 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
# 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: '[email protected]'} | |
end | |
it 'should return JSON with address' do | |
response_data[:email].should eql('[email protected]') | |
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: '[email protected]')} | |
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('[email protected]') | |
end | |
end | |
describe 'GET /addresses/:id', role: :user, model: GiveIt::Models::Address do | |
include GiveIt::Spec::Endpoints | |
let(:attrs) do | |
{email: '[email protected]'} | |
end | |
it 'should return JSON with address' do | |
response_data[:email].should eql('[email protected]') | |
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: '[email protected]')} | |
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('[email protected]') | |
end | |
end |
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
# 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