Commit b80197e9

mo khan <mo@mokhan.ca>
2014-09-23 00:37:50
refactor disqus auth.
1 parent b60acc8
Changed files (2)
app/helpers/application_helper.rb
@@ -10,20 +10,13 @@ module ApplicationHelper
   end
 
   def full_title(title)
-    if title.blank?
-      "CakeSide - for cake lovers!"
-    else
-      "#{title} - CakeSide"
-    end
+    title.blank? ?  "CakeSide - for cake lovers!" : "#{title} - CakeSide"
   end
 
   def disqus_auth(user = current_user)
-    if user.has_avatar?
-      data = { id: user.id, username: user.name, email: user.email, :avatar => user.avatar.url_for(:thumb), :url => "https://#{request.host_with_port}/profiles/#{user.to_param}" }.to_json
-    else
-      data = { id: user.id, username: user.name, email: user.email, :url => "https://#{request.host_with_port}/profiles/#{user.to_param}" }.to_json
-    end
-    message = Base64.encode64(data).gsub("\n", "")
+    data = { id: user.id, username: user.name, email: user.email, url: "https://#{request.host_with_port}/profiles/#{user.to_param}" }
+    data[:avatar] = user.avatar.url_for(:thumb) if user.has_avatar?
+    message = Base64.encode64(data.to_json).gsub("\n", "")
     timestamp = Time.now.to_i
     signature = OpenSSL::HMAC.hexdigest('sha1', ENV['DISQUS_SECRET_KEY'] || '', "#{message} #{timestamp}")
     "#{message} #{signature} #{timestamp}"
spec/helpers/application_helper_spec.rb
@@ -2,21 +2,21 @@ require "rails_helper"
 
 describe ApplicationHelper do
   describe "#disqus_auth" do
-    let(:user) { OpenStruct.new(id: 1, name: 'mo', email: 'test@cakeside.com', :to_param => '1-mo') }
+    let(:user) { create(:user) }
+    let(:secret) { 'secret' }
 
-    before :each do
-      data = { id: user.id, username: user.name, email: user.email, :url => "https://test.host/profiles/#{user.to_param}" }.to_json
+    let(:expected) do
+      data = { id: user.id, username: user.name, email: user.email, url: "https://test.host/profiles/#{user.to_param}" }.to_json
       message = Base64.encode64(data).gsub("\n", "")
       timestamp = Time.now.to_i
-      secret = ENV['DISQUS_SECRET_KEY'] = 'secret'
       signature = OpenSSL::HMAC.hexdigest('sha1', secret, "#{message} #{timestamp}")
-      @expected = "#{message} #{signature} #{timestamp}"
+      "#{message} #{signature} #{timestamp}"
     end
 
+    before { ENV['DISQUS_SECRET_KEY'] = secret }
+
     it "should generate a single sign on token" do
-      result = helper.disqus_auth(user)
-      result.should_not be_nil
-      result.should == @expected
+      expect(helper.disqus_auth(user)).to eql(expected)
     end
   end
 end