Skip to content
Browse files

Merge pull request #8 from shutl/add_user_email_for_audit

Add user email for audit
  • Loading branch information...
2 parents 6077a8a + 8d2fab7 commit c63acde2503d55c612004665ab048b900d5b54f9 @vpacher vpacher committed Nov 18, 2013
View
4 Gemfile
@@ -3,5 +3,9 @@ source 'https://rubygems.org'
group :development do
gem 'license_finder'
end
+
+group :test do
+ gem 'pry'
+end
# Specify your gem's dependencies in shutl_resource.gemspec
gemspec
View
12 lib/shutl/resource/rest_class_methods.rb
@@ -1,6 +1,7 @@
require 'open-uri'
module Shutl::Resource
module RestClassMethods
+
def base_uri uri
@base_uri = uri
end
@@ -216,8 +217,11 @@ def generate_collection_url *args
end
- private
+ def self.from_user= email
+ Thread.current[:user_email] = email
+ end
+ private
def headers
{
@@ -230,10 +234,14 @@ def headers
def header_options params
header_opts = params[:headers] || {}
header_opts.merge!(authorization: "Bearer #{params[:auth]}") if params[:auth]
- header_opts.merge!(from: params[:from]) if params[:from]
+ header_opts.merge!(from: current_user_email(params)) if current_user_email(params)
header_opts
end
+ def current_user_email params
+ params[:from] || Thread.current[:user_email]
+ end
+
def generate_request_header header_options = {}
header_options.inject(headers) do |h, (k,v)|
View
2 lib/shutl/resource/version.rb
@@ -1,5 +1,5 @@
module Shutl
module Resource
- VERSION = '1.5.3'
+ VERSION = '1.5.4'
end
end
View
76 spec/rest_resource_spec.rb
@@ -376,6 +376,41 @@ def stub_post status
request.should have_been_requested
end
+ context "send user email address for Audit" do
+ let(:user_email) { 'user@example.com' }
+
+ before do
+ Shutl::Resource::RestClassMethods.from_user = user_email
+ end
+
+ it "should override the email with 'from' option" do
+ request = stub_request(:post, 'http://host/test_rests').with(headers: headers.merge(from: 'new@example.com')).to_return(:status => 200)
+ TestRest.create({}, from: 'new@example.com')
+
+ request.should have_been_requested
+ end
+
+ it "should be thread-safe" do
+ t1 = Thread.new do
+ sleep 0.5
+ Shutl::Resource::RestClassMethods.from_user = 'thread1 user'
+ stub_request(:post, 'http://host/test_rests').with(headers: headers.merge(from: 'thread1 user')).to_return(:status => 200)
+ TestRest.create
+ end
+
+ t2 = Thread.new do
+ Shutl::Resource::RestClassMethods.from_user = 'thread2 user'
+ sleep 1
+ stub_request(:post, 'http://host/test_rests').with(headers: headers.merge(from: 'thread2 user')).to_return(:status => 200)
+ TestRest.create
+ end
+
+ t1.join; t2.join
+
+ Thread.current[:user_email].should be user_email
+ end
+ end
+
end
describe '#destroy' do
@@ -472,6 +507,47 @@ def stub_post status
request.should have_been_requested
end
+
+ context "send user email address for Audit" do
+ let(:user_email) { 'user@example.com' }
+
+ before do
+ Shutl::Resource::RestClassMethods.from_user = user_email
+ end
+
+ it "should override the email with 'from' option" do
+ request = stub_request(:put, 'http://host/test_rests/a').with(body: anything, headers: headers.merge(from: 'new@example.com')).to_return(:status => 200)
+
+ test_resource = TestRest.new
+ test_resource.update!({a: 'a', b: 'b'}, {from: 'new@example.com'})
+
+ request.should have_been_requested
+ end
+
+ it "should be thread-safe" do
+ t1 = Thread.new do
+ sleep 0.5
+ Shutl::Resource::RestClassMethods.from_user = 'thread1 user'
+ stub_request(:put, 'http://host/test_rests/a').with(body: anything, headers: headers.merge(from: 'thread1 user')).to_return(:status => 200)
+
+ test_resource = TestRest.new
+ test_resource.update!(a: 'a', b: 'b')
+ end
+
+ t2 = Thread.new do
+ Shutl::Resource::RestClassMethods.from_user = 'thread2 user'
+ sleep 1
+ stub_request(:put, 'http://host/test_rests/a').with(body: anything, headers: headers.merge(from: 'thread2 user')).to_return(:status => 200)
+
+ test_resource = TestRest.new
+ test_resource.update!(a: 'a', b: 'b')
+ end
+
+ t1.join; t2.join
+
+ Thread.current[:user_email].should be user_email
+ end
+ end
end
describe '#respond_to?' do
View
3 spec/support/test_resource.rb
@@ -2,5 +2,4 @@ class TestRest
include Shutl::Resource::Rest
base_uri 'http://host'
resource_id :a
-end
-
+end

0 comments on commit c63acde

Please sign in to comment.
Something went wrong with that request. Please try again.