Created
April 18, 2012 08:35
-
-
Save partydrone/2412068 to your computer and use it in GitHub Desktop.
How do I test a third-party API with Rspec?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Failures: | |
1) SessionsController fetches a user from Active Directory | |
Failure/Error: post :create, username: "brubble", password: "secret" | |
NoMethodError: | |
undefined method `amount' for nil:NilClass | |
# ./app/models/user.rb:128:in `set_account_balance' | |
# ./app/models/user.rb:110:in `create_user_with_adauth' | |
# ./app/models/user.rb:97:in `return_and_create_with_adauth' | |
# ./app/controllers/sessions_controller.rb:11:in `create' | |
# ./spec/controllers/sessions_controller_spec.rb:11:in `block (2 levels) in <top (required)>' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController < ApplicationController | |
skip_before_filter :authenticate | |
def new | |
redirect_to root_path if current_user | |
end | |
def create | |
ldap_user = Adauth.authenticate(params[:username], params[:password]) | |
if ldap_user | |
user = User.return_and_create_with_adauth(ldap_user) | |
if params[:remember_me] | |
cookies.permanent[:auth_token] = user.auth_token | |
else | |
cookies[:auth_token] = user.auth_token | |
end | |
path = session[:return_to] | |
session[:return_to] = nil | |
redirect_to path rescue redirect_to root_path | |
else | |
redirect_to sign_in_path, error: "Invalid Login" | |
end | |
end | |
def destroy | |
cookies.delete(:auth_token) | |
redirect_to sign_in_path | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe SessionsController do | |
it "fetches a user from Active Directory" do | |
remote_user = stub(login: "brubble", | |
first_name: "Barney", | |
last_name: "Rubble", | |
email: "[email protected]") | |
ldap_user = Adauth.stub(:authenticate).and_return(remote_user) | |
post :create, username: "brubble", password: "secret" | |
ldap_user.should == remote_user | |
end | |
context "when there is an Active Directory user" do | |
it "creates a local user" | |
context "when the remember me box is checked" do | |
it "sets a permanent cookie" | |
end | |
context "when the remember me box is not checked" do | |
it "sets a session cookie" | |
end | |
context "when there is a return path in the session" do | |
it "resets the return path in the session" | |
it "redirects to the return path" | |
end | |
context "when there is not a return path in the session" do | |
it "redirects to the home page" | |
end | |
end | |
context "when there is not an Active Directory user" do | |
it "sets a login error" | |
it "redirects to the sign in page" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :claims | |
has_many :comments | |
has_many :donations | |
before_create { generate_token :auth_token } | |
before_create :set_account_balance | |
scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0"} } | |
ROLES = %w[ admin approver editor processor ] | |
def credit_account_balance(amount) | |
self[:account_balance] += amount | |
end | |
def debit_account_balance(amount) | |
self[:account_balance] -= amount | |
end | |
def name | |
"#{first_name} #{last_name}" | |
end | |
def role?(role_sym) | |
roles.any? { |r| r.to_sym == role_sym } | |
end | |
def roles | |
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? } | |
end | |
def roles=(roles) | |
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum | |
end | |
def to_s | |
name.blank? ? login : name | |
end | |
def groups | |
group_strings.split(", ") | |
end | |
def ous | |
ou_strings.split(", ") | |
end | |
def update_from_adauth(adauth_user) | |
self.first_name = adauth_user.first_name.gsub(/\"|\[|\]/, "") | |
self.last_name = adauth_user.last_name.gsub(/\"|\[|\]/, "") | |
self.email = adauth_user.email.gsub(/\"|\[|\]/, "") | |
self.save | |
end | |
def self.return_and_create_with_adauth(adauth_user) | |
user = (find_by_login(adauth_user.login.gsub(/\"|\[|\]/, "")) || create_user_with_adauth(adauth_user)) | |
user.update_from_adauth(adauth_user) | |
return user | |
end | |
def self.create_user_with_adauth(adauth_user) | |
create! do |user| | |
user.login = adauth_user.login.gsub(/\"|\[|\]/, "") | |
user.first_name = adauth_user.first_name.gsub(/\"|\[|\]/, "") | |
user.last_name = adauth_user.last_name.gsub(/\"|\[|\]/, "") | |
user.email = adauth_user.email.gsub(/\"|\[|\]/, "") | |
end | |
end | |
private | |
def generate_token(column) | |
begin | |
self[column] = SecureRandom.urlsafe_base64 | |
end while User.exists?(column => self[column]) | |
end | |
def set_account_balance | |
allowance = Allowance.where("year = ?", Time.now.year).first | |
self[:account_balance] = allowance.amount | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment