Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Jan 2, 2012
0 parents commit ce5de60
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.bundle
/tmp
/log
.DS_Store
.rvmrc
*~
coverage/
.idea
selftest
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
23 changes: 23 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
source 'http://rubygems.org'

gem 'rake'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'rack-contrib', :git => '[email protected]:rack/rack-contrib.git'
gem 'yajl-ruby', :require => "yajl"
gem 'petroglyph'
gem 'unicorn', '~> 4.1.1'
gem 'pebblebed', :git => '[email protected]:benglerpebbles/pebblebed.git'
gem 's3'

group :development, :test do
gem 'bengler_test_helper', :git => "[email protected]:origo/bengler_test_helper.git"
gem 'rspec', '~> 2.7.0.rc1'
gem 'rack-test'
gem 'simplecov'
gem 'capistrano', '=2.8.0'
gem 'capistrano-ext', '=1.2.1'
# gem 'vcr'
# gem 'webmock'
# gem 'timecop'
end
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$:.unshift(File.dirname(__FILE__))

require 'bengler_test_helper/tasks'

task :environment do
require 'config/environment'
end
24 changes: 24 additions & 0 deletions api/v1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# encoding: utf-8
require "json"

Dir.glob("#{File.dirname(__FILE__)}/v1/**/*.rb").each{ |file| require file }

class TiramisuV1 < Sinatra::Base
set :root, "#{File.dirname(__FILE__)}/v1"

register Sinatra::Pebblebed
i_am :tiramisu

post '/a_resource' do
require_god
end

put '/a_resource/:resource' do |resource|
require_god
end

get '/a_resource/:resource' do |resource|
require_identity
end

end
Empty file added api/v1/views/.gitkeep
Empty file.
17 changes: 17 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$:.unshift(File.dirname(__FILE__))

require 'config/environment'
require 'api/v1'
require 'config/logging'
require 'rack/contrib'

ENV['RACK_ENV'] ||= 'development'
set :environment, ENV['RACK_ENV'].to_sym

use Rack::CommonLogger

map "/api/tiramisu/v1" do
use Rack::PostBodyContentTypeParser
use Rack::MethodOverride
run TiramisuV1
end
9 changes: 9 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "bundler"
Bundler.require

# $memcached = Dalli::Client.new

Dir.glob('./lib/**/*.rb').each{ |lib| require lib }

ENV['RACK_ENV'] ||= "development"
environment = ENV['RACK_ENV']
15 changes: 15 additions & 0 deletions config/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'logger'

Dir.mkdir('log') unless File.exist?('log')

logfile = File.new("log/#{Sinatra::Application.environment}.log", 'a+')

Log = Logger.new(logfile)
Log.level = Sinatra::Application.environment == :production ? Logger::WARN : Logger::DEBUG
Log.datetime_format = "%Y-%m-%d %H:%M:%S.%L"

unless Sinatra::Application.environment == :test
STDERR.reopen(logfile)
end

TiramisuV1.use Rack::CommonLogger, logfile
3 changes: 3 additions & 0 deletions lib/models/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# A sample model, just for illustration purposes
# class Resource < ActiveRecord::Base
# end
73 changes: 73 additions & 0 deletions spec/api/v1_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'spec_helper'

describe 'API v1' do
include Rack::Test::Methods

def app
TiramisuV1
end

god_endpoints = [
{:method => :post, :endpoint => '/a_resource'},
{:method => :put, :endpoint => '/a_resource/id'},
]

user_endpoints = [
{:method => :get, :endpoint => '/a_resource/id'}
]

let(:json_output) { JSON.parse(last_response.body) }

context "with a logged in god" do
before :each do
Pebblebed::Connector.any_instance.stub(:checkpoint).and_return(DeepStruct.wrap(:me => {:id => 1337, :god => true, :realm => 'rock_and_roll'}))
end

it "does powerful stuff"

end

context "with a logged in user" do
before :each do
Pebblebed::Connector.any_instance.stub(:checkpoint).and_return(DeepStruct.wrap(:me => {:id => 1337, :god => false, :realm => 'rock_and_roll'}))
end

it "accesses all sorts of things"

describe "has no access to god endpoints" do
god_endpoints.each do |forbidden|
it "fails to #{forbidden[:method]} #{forbidden[:endpoint]}" do
self.send(forbidden[:method], forbidden[:endpoint])
last_response.status.should eq(403)
end
end
end
end

describe "with no current user" do
before :each do
Pebblebed::Connector.any_instance.stub(:checkpoint).and_return(DeepStruct.wrap(:me => {}))
end

it "mostly gets harmless stuff"

describe "has no access to god endpoints" do
god_endpoints.each do |forbidden|
it "fails to #{forbidden[:method]} #{forbidden[:endpoint]}" do
self.send(forbidden[:method], forbidden[:endpoint])
last_response.status.should eq(403)
end
end
end

describe "has no access to user endpoints" do
user_endpoints.each do |forbidden|
it "fails to #{forbidden[:method]} #{forbidden[:endpoint]}" do
self.send(forbidden[:method], forbidden[:endpoint])
last_response.status.should eq(403)
end
end
end
end

end
26 changes: 26 additions & 0 deletions spec/mockcached.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Mockcached
def initialize
@store = {}
end

def set(*args)
@store[args[0]] = args[1]
end

def get(*args)
@store[args[0]]
end

def get_multi(*keys)
result = {}
keys.each do |key|
value = get(key)
result[key] = value if value
end
result
end

def delete(*args)
@store.delete(args[0])
end
end
36 changes: 36 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'simplecov'

SimpleCov.add_filter 'spec'
SimpleCov.add_filter 'config'
SimpleCov.start

$:.unshift(File.dirname(File.dirname(__FILE__)))

ENV["RACK_ENV"] = "test"
require 'config/environment'

# require './spec/mockcached'
require 'rack/test'

require 'api/v1'
require 'config/logging'

# require 'vcr'
# VCR.config do |c|
# c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
# c.stub_with :webmock
# end

set :environment, :test

# Run all examples in a transaction
RSpec.configure do |c|
c.around(:each) do |example|
clear_cookies if respond_to?(:clear_cookies)
# $memcached = Mockcached.new
ActiveRecord::Base.connection.transaction do
example.run
raise ActiveRecord::Rollback
end
end
end

0 comments on commit ce5de60

Please sign in to comment.