Skip to content

Instantly share code, notes, and snippets.

View julesfern's full-sized avatar

Jules Glegg julesfern

View GitHub Profile
it "should redirect users to the login screen with the correct return_to if return_to is set" do
return_to_param = MerbAuthSliceFullfat[:return_to_param]
@controller = get("/sekkrit", :format=>"html", return_to_param=>"/returnedsdfsdf")
@controller.status.should == 302
@controller.should redirect_to(@controller.url(:merb_auth_slice_fullfat_login, return_to_param=>"/returned"))
end
raises:
Expected exceptions#unauthenticated to redirect to </auth/login?return_to=%2Freturned>, but it redirected to </auth/login?return_to=%2Freturnedsdfsdf>
it "should redirect users to the login screen with the correct return_to if return_to is set" do
return_to_param = MerbAuthSliceFullfat[:return_to_param]
@controller = get("/sekkrit", :format=>"html", return_to_param=>"/returnedsdfsdf")
@controller.status.should == 302
@controller.should redirect_to(@controller.url(:merb_auth_slice_fullfat_login, return_to_param=>"/returned"))
end
raises:
Expected exceptions#unauthenticated to redirect to </auth/login?return_to=%2Freturned>, but it redirected to </auth/login?return_to=%2Freturnedsdfsdf>
protected
def validate
errors.add("Uploaded file", "is not a valid image file.") unless uploaded_file_has_valid_extension? if new_record?
end
def uploaded_file_has_valid_extension?
%w(.jpg .png).include?(File.extname(self.file.path)) if self.file
end
@controller = get("/sekkrit", :format=>"html", @return_to_param=>"returned")
# the test:
@controller.should redirect_to(@controller.url(:merb_auth_slice_fullfat_login, @return_to_param=>"DSFARGEG"))
# fails with:
#Expected exceptions#unauthenticated to redirect to </auth/login?return_to=DSFARGEG>, but it redirected to #</auth/login?return_to=returned>
# while the test:
@controller.should redirect_to(@controller.url(:merb_auth_slice_fullfat_login, @return_to_param=>"returned"))
# fails with:
# Override for buggy freaking redirect_to assertion in merb 0.9.11.
# duplicates syntax of old version, so can be safely removed once
# http://merb.lighthouseapp.com/projects/7433-merb/tickets/949-redirect_to-assertion-errors-on-success-under-some-setups
# is fixed.
def redirect_to(url)
simple_matcher("redirect to #{url.inspect}") do |controller, matcher|
actual_url = controller.rack_response[1]["Location"]
matcher.failure_message = "expected to be redirected to #{url.inspect} but instead was redirected to #{actual_url.inspect}"
actual_url == url
end
if defined?(Merb::Plugins)
$:.unshift File.dirname(__FILE__)
load_dependency 'merb-auth-core'
load_dependency 'merb-auth-more'
load_dependency 'merb-slices'
Merb::Plugins.add_rakefiles "merb-auth-slice-fullfat/merbtasks", "merb-auth-slice-fullfat/slicetasks", "merb-auth-slice-fullfat/spectasks"
# Register the Slice for the current host application
def self.setup_router(scope)
scope.identify MerbAuthSliceFullfat::PasswordReset => :identifier do |identification|
identification.resources :sessions
identification.resources :password_resets, :keys=>[:identifier]
identification.resources :authenticating_clients
end
scope.default_routes
end
# The code:
class MerbAuthSliceFullfat::AuthenticatingClient
include DataMapper::Resource
# Key it
property :id, Serial
# The registration will belong to a user, who will be able to edit the client properties.
property :user_id, Integer, :writer => :protected
end
it "should verify that a correctly-signed GET request is signed using GET parameters" do
# set the api key to a known value for the purposes of this test
@authenticating_client.send(:"api_key=", "fishsticks")
@authenticating_client.save!
get_params = {
"a"=>"1", "BBB"=>"3", "c"=>"2",
"1"=>"a", "2"=>"b",
"api_key"=>"fishsticks"
}
get_params[:api_signature] = Digest::SHA1.hexdigest("#{@authenticating_client.secret}httptest.fullfat.com/secret/#{get_params.keys.sort.join("")}#{get_params.values.sort.join("")}")
# mixed in to Merb::Request
# Attempts to verify the request's signature using the strategy covered in signing.markdown.
# Takes one argument, which is the authenticating client you wish to check the signature against.
# Returns a true on success, false on fail.
def signed?
# Fail immediately if the request is not signed at all
return false unless api_request? and authenticating_client
# Prepare the verification string for comparison
correct_sig = "#{authenticating_client.secret}#{method}#{protocol}#{host}#{uri}"