Skip to content

Instantly share code, notes, and snippets.

@noahhendrix
Created January 4, 2012 21:53
Show Gist options
  • Save noahhendrix/1562378 to your computer and use it in GitHub Desktop.
Save noahhendrix/1562378 to your computer and use it in GitHub Desktop.
API Spec
module Todo
class API < Grape::API
use Rack::Session::Cookie
version 'v1', :format => :json
resource do
http_basic do |username, password|
User.authenticate(username, password)
end
resource '/user' do
get do
current_user
end
end
end
end
end
require 'spec_helper'
require 'api'
require 'rack/test'
set :environment, :test
describe Todo::API do
include Rack::Test::Methods
def app
Todo::API
end
let(:user) { 'valid user' }
describe 'GET /v1/user' do
context 'not authenticated' do
it 'returns unauthorized and empty body' do
get '/v1/user'
last_response.status.should == 401
last_response.body.should be_empty
end
end
end
context 'authenticated' do
before(:each) do
User.should_receive(:authenticate).and_return user
app.stub(:current_user).and_return user
end
describe 'GET /v1/user' do
it 'returns user hash' do
get '/v1/user'
last_response.body.should == user.to_json
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment