Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Setup application logger, be able to destroy sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
chischaschos committed Feb 20, 2014
1 parent d699699 commit 79892dd
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
.bundle .bundle
log/*
db/*
2 changes: 2 additions & 0 deletions lib/todo.rb
Expand Up @@ -6,4 +6,6 @@ module Todo
autoload :Application, 'todo/application' autoload :Application, 'todo/application'
autoload :Models, 'todo/models' autoload :Models, 'todo/models'
autoload :Services, 'todo/services' autoload :Services, 'todo/services'
autoload :Middlewares, 'todo/middlewares'
autoload :MyLogger, 'todo/my_logger'
end end
42 changes: 38 additions & 4 deletions lib/todo/application.rb
Expand Up @@ -5,13 +5,19 @@ class Application < Sinatra::Base


set :root, File.realpath(File.join(File.dirname(__FILE__), '..', '..')) set :root, File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
set :logging, true set :logging, true
set :dump_errors, true set :dump_errors, false
set :raise_errors, true
set :show_exceptions, false
set :logger, MyLogger.new


configure do configure do
DataMapper::Logger.new($stdout, :debug) DataMapper::Logger.new(logger, :debug)
DataMapper.setup(:default, "sqlite://#{File.join(Todo::Application.root, 'todos.db')}") DataMapper.setup(:default, "sqlite://#{File.join(Todo::Application.root, 'db', 'todos.db')}")
end end


use Rack::CommonLogger, settings.logger
use Middlewares::ExceptionHandling

get '/' do get '/' do
haml :index haml :index
end end
Expand All @@ -30,7 +36,7 @@ class Application < Sinatra::Base
end end
end end


post '/api/sessions' do post '/api/session' do
content_type :json content_type :json


session = Services::SessionCreator.new(params[:user]) session = Services::SessionCreator.new(params[:user])
Expand All @@ -47,8 +53,36 @@ class Application < Sinatra::Base
status 404 status 404
{ errors: session.errors.to_hash }.to_json { errors: session.errors.to_hash }.to_json
end end
end

post '/api/session' do
content_type :json


session = Services::SessionCreator.new(params[:user])

if session.valid?
cookie_params = {
value: session.access_token,
httponly: true,
secure: true
}
response.set_cookie 'access_token', cookie_params

else
status 404
{ errors: session.errors.to_hash }.to_json
end
end

delete '/api/session' do
content_type :json
session = Models::Session.first(access_token: request.cookies[:access_token])
if !session && session && !session.destroy
status 404
{ errors: session.errors.to_hash }.to_json
end
end end



end end
end end
5 changes: 5 additions & 0 deletions lib/todo/middlewares.rb
@@ -0,0 +1,5 @@
module Todo
module Middlewares
autoload :ExceptionHandling, 'todo/middlewares/exception_handling'
end
end
25 changes: 25 additions & 0 deletions lib/todo/middlewares/exception_handling.rb
@@ -0,0 +1,25 @@
module Todo
module Middlewares
class ExceptionHandling
def initialize(app)
@app = app
end

def call(env)
begin
@app.call env
rescue => ex
env['rack.errors'].puts ex
env['rack.errors'].puts ex.backtrace.join("\n")
env['rack.errors'].flush

hash = { :message => ex.to_s }
hash[:backtrace] = ex.backtrace
Todo::Application.logger.error(JSON.pretty_generate(hash))
[500, {'Content-Type' => 'application/json'}, [MultiJson.dump(hash)]]
end
end
end
end
end

11 changes: 11 additions & 0 deletions lib/todo/my_logger.rb
@@ -0,0 +1,11 @@
require 'logger'

module Todo
class MyLogger < Logger
alias_method :write, :<<

def initialize
super File.join(Todo::Application.root, 'log', 'app.log')
end
end
end
25 changes: 21 additions & 4 deletions spec/api/sessions_spec.rb
Expand Up @@ -2,15 +2,32 @@


describe 'Sessions API', api: true do describe 'Sessions API', api: true do


it 'a user can create a session' do it 'should allow a client to create a user session' do
params = { email: 'test@test.com', password: '123test123' } params = { email: 'test@test.com', password: '123test123' }
user = Todo::Models::User.create! params user = Todo::Models::User.create params


post '/api/sessions', { user: params } post '/api/session', { user: params }


expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8'
expect(last_response.headers['Set-Cookie']).to match /access_token/ expect(last_response.headers['Set-Cookie']).to match /access_token=#{user.session.access_token}/
expect(last_response.body).to eq '' expect(last_response.body).to eq ''
expect(last_response.status).to eq 200 expect(last_response.status).to eq 200
end end

it 'should allow a client to destroy a user session' do
params = { email: 'test@test.com', password: '123test123' }
Todo::Models::User.create params
session = Todo::Services::SessionCreator.new(params)
expect(session.valid?).to be_true

set_cookie "access_token=#{session.access_token}"

delete "/api/session"

expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8'
expect(last_response.headers['Set-Cookie']).to be_nil
expect(last_response.body).to eq ''
expect(last_response.status).to eq 200

end
end end
Binary file removed todos.db
Binary file not shown.

0 comments on commit 79892dd

Please sign in to comment.