about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-03-22 21:05:23 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-03-22 21:05:23 +0100
commit02e4fb2e06f424c16ab25ea294a4af6490a5f7e3 (patch)
treea63f863ed677eb789646ab468681001628e29490
parent921f40c1873883e8ce476e04de1f3f1bd5ccbf91 (diff)
Only re-download avatar if URL changed (fix #19)
-rw-r--r--app/models/account.rb8
-rw-r--r--db/migrate/20160322193748_add_avatar_remote_url_to_accounts.rb5
-rw-r--r--db/schema.rb3
-rw-r--r--spec/services/update_remote_profile_service_spec.rb54
4 files changed, 66 insertions, 4 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 06f858c7c..c686a47ed 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -5,7 +5,6 @@ class Account < ActiveRecord::Base
   validates :username, uniqueness: { scope: :domain, case_sensitive: true },  unless: 'local?'
 
   # Avatar upload
-  attr_reader :avatar_remote_url
   has_attached_file :avatar, styles: { large: '300x300#', medium: '96x96#', small: '48x48#' }, default_url: 'avatars/missing.png'
   validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
 
@@ -91,8 +90,11 @@ class Account < ActiveRecord::Base
   end
 
   def avatar_remote_url=(url)
-    self.avatar = URI.parse(url)
-    @avatar_remote_url = url
+    unless self[:avatar_remote_url] == url
+      self.avatar = URI.parse(url)
+    end
+
+    self[:avatar_remote_url] = url
   end
 
   def to_param
diff --git a/db/migrate/20160322193748_add_avatar_remote_url_to_accounts.rb b/db/migrate/20160322193748_add_avatar_remote_url_to_accounts.rb
new file mode 100644
index 000000000..9dc4b027f
--- /dev/null
+++ b/db/migrate/20160322193748_add_avatar_remote_url_to_accounts.rb
@@ -0,0 +1,5 @@
+class AddAvatarRemoteUrlToAccounts < ActiveRecord::Migration
+  def change
+    add_column :accounts, :avatar_remote_url, :string, null: true, default: nil
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 250b69f6d..d6702b36e 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20160316103650) do
+ActiveRecord::Schema.define(version: 20160322193748) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -40,6 +40,7 @@ ActiveRecord::Schema.define(version: 20160316103650) do
     t.string   "header_content_type"
     t.integer  "header_file_size"
     t.datetime "header_updated_at"
+    t.string   "avatar_remote_url"
   end
 
   add_index "accounts", ["username", "domain"], name: "index_accounts_on_username_and_domain", unique: true, using: :btree
diff --git a/spec/services/update_remote_profile_service_spec.rb b/spec/services/update_remote_profile_service_spec.rb
index f09f60ada..1ffcfbfac 100644
--- a/spec/services/update_remote_profile_service_spec.rb
+++ b/spec/services/update_remote_profile_service_spec.rb
@@ -1,5 +1,59 @@
 require 'rails_helper'
 
 RSpec.describe UpdateRemoteProfileService do
+  let(:xml) { Nokogiri::XML(File.read(File.join(Rails.root, 'spec', 'fixtures', 'push', 'feed.atom'))).at_xpath('//xmlns:author') }
+
   subject { UpdateRemoteProfileService.new }
+
+  before do
+    stub_request(:get, 'https://quitter.no/avatar/7477-300-20160211190340.png').to_return(request_fixture('avatar.txt'))
+  end
+
+  context 'with updated details' do
+    let(:remote_account) { Fabricate(:account, username: 'bob', domain: 'example.com') }
+
+    before do
+      subject.(xml, remote_account)
+    end
+
+    it 'downloads new avatar' do
+      expect(a_request(:get, 'https://quitter.no/avatar/7477-300-20160211190340.png')).to have_been_made
+    end
+
+    it 'sets the avatar remote url' do
+      expect(remote_account.reload.avatar_remote_url).to eq 'https://quitter.no/avatar/7477-300-20160211190340.png'
+    end
+
+    it 'sets display name' do
+      expect(remote_account.reload.display_name).to eq 'DIGITAL CAT'
+    end
+
+    it 'sets note' do
+      expect(remote_account.reload.note).to eq 'Software engineer, free time musician and DIGITAL SPORTS enthusiast. Likes cats. Warning: May contain memes'
+    end
+  end
+
+  context 'with unchanged details' do
+    let(:remote_account) { Fabricate(:account, username: 'bob', domain: 'example.com',display_name: 'DIGITAL CAT', note: 'Software engineer, free time musician and DIGITAL SPORTS enthusiast. Likes cats. Warning: May contain memes', avatar_remote_url: 'https://quitter.no/avatar/7477-300-20160211190340.png') }
+
+    before do
+      subject.(xml, remote_account)
+    end
+
+    it 'does not re-download avatar' do
+      expect(a_request(:get, 'https://quitter.no/avatar/7477-300-20160211190340.png')).to have_been_made.once
+    end
+
+    it 'sets the avatar remote url' do
+      expect(remote_account.reload.avatar_remote_url).to eq 'https://quitter.no/avatar/7477-300-20160211190340.png'
+    end
+
+    it 'sets display name' do
+      expect(remote_account.reload.display_name).to eq 'DIGITAL CAT'
+    end
+
+    it 'sets note' do
+      expect(remote_account.reload.note).to eq 'Software engineer, free time musician and DIGITAL SPORTS enthusiast. Likes cats. Warning: May contain memes'
+    end
+  end
 end