Skip to content
Browse files

save avatar image from twitter omniauth using carrierwave, with specs…

… using webmock.
  • Loading branch information...
1 parent 62e1b04 commit 1e4a21d6b579232448cd0e2d87c608089689fb87 @shioyama shioyama committed
View
3 .gitignore
@@ -31,3 +31,6 @@
# Ignore local rbenv version
.rbenv-version
+
+# Ignore carrierwave uploads
+/public/uploads/
View
1 Gemfile
@@ -44,6 +44,7 @@ end
group :test do
# ref: http://datacodescotch.blogspot.jp/2011/11/warning-cucumber-rails-required-outside.html
gem 'cucumber-rails', '~> 1.0', require: false
+ gem 'webmock'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
View
6 Gemfile.lock
@@ -62,6 +62,7 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
+ addressable (2.2.8)
arel (3.0.2)
bootstrap-sass (2.0.1)
builder (3.0.0)
@@ -82,6 +83,7 @@ GEM
execjs
coffee-script-source (1.2.0)
columnize (0.3.6)
+ crack (0.3.1)
cucumber (1.1.6)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
@@ -213,6 +215,9 @@ GEM
uglifier (1.2.2)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ webmock (1.8.7)
+ addressable (>= 2.2.7)
+ crack (>= 0.1.7)
xpath (0.1.4)
nokogiri (~> 1.3)
@@ -244,3 +249,4 @@ DEPENDENCIES
sqlite3
twitter_bootstrap_form_for!
uglifier (>= 1.0.3)
+ webmock
View
12 app/models/user.rb
@@ -1,5 +1,12 @@
+require 'open-uri'
+
class User < ActiveRecord::Base
- attr_accessible :name, :fullname, :location, :profile, :mysite
+ attr_accessible :name, :fullname, :location, :profile, :mysite, :remote_avatar_url
+
+ # validations
+ validates :name, :presence => true, :uniqueness => true
+ validates :fullname, :presence => true
+ validates :avatar, :presence => true
# associations
has_many :cothreads
@@ -11,7 +18,8 @@ def self.new_from_hash(hash)
new(:name => hash['info']['nickname'],
:location => hash['info']['location'],
:profile => hash['info']['description'],
- :fullname => hash['info']['name'])
+ :fullname => hash['info']['name'],
+ :remote_avatar_url => hash['info']['image'])
end
def self.current_user=(user)
View
6 config/initializers/carrierwave.rb
@@ -0,0 +1,6 @@
+if Rails.env.test? or Rails.env.cucumber?
+ CarrierWave.configure do |config|
+ config.storage = :file
+ config.enable_processing = false
+ end
+end
View
2 config/initializers/omniauth.rb
@@ -8,7 +8,7 @@ def self.add_twitter_mock(uid, name, nickname)
OmniAuth.config.add_mock(:twitter,
{ :provider => 'twitter',
:uid => uid,
- :info => { :name => name, :nickname => nickname }})
+ :info => { :name => name, :nickname => nickname, :image => "http://a1.twimg.com/profile_images/1234567/csasaki_bigger.png" }})
end
def add_twitter_mock(uid, name, nickname)
OmniAuthHelpers.add_twitter_mock(uid, name, nickname)
View
1 spec/factories/users.rb
@@ -7,5 +7,6 @@
location "Tokyo, Japan"
mysite "http://www.mysite.com"
fullname "Cojiro Sasaki"
+ remote_avatar_url "http://a1.twimg.com/profile_images/1234567/csasaki_bigger.png"
end
end
View
BIN spec/fixtures/myavatar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
8 spec/models/authorization_spec.rb
@@ -10,10 +10,10 @@
describe ".create_from_hash" do
hash = {
- "provider"=>"twitter",
- "uid"=>"4138021",
- "info"=> { "name" => "Cojiro Sasaki",
- "nickname"=>"csasaki" }
+ "provider" => "twitter",
+ "uid" => "4138021",
+ "info" => { "name" => "Cojiro Sasaki",
+ "nickname"=>"csasaki", "image" => "http://a1.twimg.com/profile_images/1234567/csasaki_bigger.png" }
}
subject { Authorization.create_from_hash!(hash) }
View
57 spec/models/user_spec.rb
@@ -1,23 +1,58 @@
require 'spec_helper'
describe User do
+ before do
+ I18n.locale = 'en'
+ end
- describe ".new_from_hash" do
-
+ describe "validation with factory" do
before do
- hash = {
- "provider"=>"twitter",
- "uid"=>"4138021",
- "info"=> { "name" => "Cojiro Sasaki",
- "nickname" => "csasaki",
- "description" => "I like dicing blue chickens.",
- "location" => "Fukui" }
- }
- @user = User.new_from_hash(hash)
+ @user = FactoryGirl.create(:user)
+ FactoryGirl.create(:user, :name => "alice")
end
subject { @user }
+ it "is valid created by factory" do
+ should be_valid
+ end
+
+ it "is invalid without a name" do
+ subject.name = ""
+ should_not be_valid
+ end
+
+ it "is invalid with a duplicate name" do
+ subject.name = "alice"
+ should_not be_valid
+ end
+
+ it "is invalid without a fullname" do
+ subject.fullname = ""
+ should_not be_valid
+ end
+
+ it "is invalid without an avatar" do
+ subject.remove_avatar!
+ should_not be_valid
+ end
+
+ end
+
+ describe ".new_from_hash" do
+
+ hash = {
+ "provider"=>"twitter",
+ "uid"=>"4138021",
+ "info"=> { "name" => "Cojiro Sasaki",
+ "nickname" => "csasaki",
+ "description" => "I like dicing blue chickens.",
+ "location" => "Fukui",
+ "image" => "http://a1.twimg.com/profile_images/1234567/csasaki_bigger.png" }
+ }
+
+ subject { User.new_from_hash(hash) }
+
its(:name) { should == "csasaki" }
its(:fullname) { should == "Cojiro Sasaki" }
its(:location) { should == "Fukui" }
View
7 spec/spec_helper.rb
@@ -12,12 +12,15 @@
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
+ require 'webmock/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
+ include CojiroRequestStubs
+
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
@@ -42,6 +45,10 @@
# for testing sign-in through Twitter, Facebook, etc.
OmniAuth.config.test_mode = true
+ config.before(:each) do
+ load_request_stubs
+ end
+
end
end
View
10 spec/support/webmocks.rb
@@ -0,0 +1,10 @@
+module CojiroRequestStubs
+
+ def fixture(file)
+ File.open(File.expand_path(fixture_path + '/' + file))
+ end
+
+ def load_request_stubs
+ stub_request(:get, "http://a1.twimg.com/profile_images/1234567/csasaki_bigger.png").to_return(:status => 200, :body => fixture("myavatar.png"), :headers => {})
+ end
+end

0 comments on commit 1e4a21d

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