From 8fd174298df3fea60a4efe958fca9c1946633e10 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Tue, 23 May 2017 09:53:01 +0900 Subject: [PATCH] Cover AccountsController more in spec (#3229) * Introduce recent scope to Status and StreamEntry Introduce recent scope to Status and StreamEntry as Account has. * Cover AccountsController more in AccountsController --- app/controllers/accounts_controller.rb | 4 +- app/models/status.rb | 3 +- app/models/stream_entry.rb | 1 + spec/controllers/accounts_controller_spec.rb | 39 +++++++++++++++++--- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index dd7c46b4e1..3e580448ec 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -6,12 +6,12 @@ class AccountsController < ApplicationController def show respond_to do |format| format.html do - @statuses = @account.statuses.permitted_for(@account, current_account).order(id: :desc).paginate_by_max_id(20, params[:max_id], params[:since_id]) + @statuses = @account.statuses.permitted_for(@account, current_account).recent.paginate_by_max_id(20, params[:max_id], params[:since_id]) @statuses = cache_collection(@statuses, Status) end format.atom do - @entries = @account.stream_entries.order(id: :desc).where(hidden: false).with_includes.paginate_by_max_id(20, params[:max_id], params[:since_id]) + @entries = @account.stream_entries.recent.where(hidden: false).with_includes.paginate_by_max_id(20, params[:max_id], params[:since_id]) render xml: AtomSerializer.render(AtomSerializer.new.feed(@account, @entries.to_a)) end diff --git a/app/models/status.rb b/app/models/status.rb index d7304152f5..a3dbce9f13 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -55,8 +55,9 @@ class Status < ApplicationRecord validates_with StatusLengthValidator validates :reblog, uniqueness: { scope: :account }, if: :reblog? - default_scope { order(id: :desc) } + default_scope { recent } + scope :recent, -> { reorder(id: :desc) } scope :remote, -> { where.not(uri: nil) } scope :local, -> { where(uri: nil) } diff --git a/app/models/stream_entry.rb b/app/models/stream_entry.rb index fb349f35c2..44aac39b3f 100644 --- a/app/models/stream_entry.rb +++ b/app/models/stream_entry.rb @@ -25,6 +25,7 @@ class StreamEntry < ApplicationRecord STATUS_INCLUDES = [:account, :stream_entry, :conversation, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :conversation, :media_attachments, :tags, mentions: :account], thread: [:stream_entry, :account]].freeze default_scope { where(activity_type: 'Status') } + scope :recent, -> { reorder(id: :desc) } scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES) } delegate :target, :title, :content, :thread, diff --git a/spec/controllers/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb index 94d10d78f1..447e2dd53c 100644 --- a/spec/controllers/accounts_controller_spec.rb +++ b/spec/controllers/accounts_controller_spec.rb @@ -6,17 +6,29 @@ RSpec.describe AccountsController, type: :controller do let(:alice) { Fabricate(:account, username: 'alice') } describe 'GET #show' do + let!(:status1) { Status.create!(account: alice, text: 'Hello world') } + let!(:status2) { Status.create!(account: alice, text: 'Boop', thread: status1) } + let!(:status3) { Status.create!(account: alice, text: 'Picture!') } + let!(:status4) { Status.create!(account: alice, text: 'Mentioning @alice') } + before do - status1 = Status.create!(account: alice, text: 'Hello world') - Status.create!(account: alice, text: 'Boop', thread: status1) - status3 = Status.create!(account: alice, text: 'Picture!') status3.media_attachments.create!(account: alice, file: fixture_file_upload('files/attachment.jpg', 'image/jpeg')) - Status.create!(account: alice, text: 'Mentioning @alice') end context 'atom' do before do - get :show, params: { username: alice.username }, format: 'atom' + get :show, params: { username: alice.username, max_id: status4.stream_entry.id, since_id: status1.stream_entry.id }, format: 'atom' + end + + it 'assigns @account' do + expect(assigns(:account)).to eq alice + end + + it 'assigns @entries' do + entries = assigns(:entries).to_a + expect(entries.size).to eq 2 + expect(entries[0].status).to eq status3 + expect(entries[1].status).to eq status2 end it 'returns http success with Atom' do @@ -29,6 +41,10 @@ RSpec.describe AccountsController, type: :controller do get :show, params: { username: alice.username }, format: 'activitystreams2' end + it 'assigns @account' do + expect(assigns(:account)).to eq alice + end + it 'returns http success with Activity Streams 2.0' do expect(response).to have_http_status(:success) end @@ -36,7 +52,18 @@ RSpec.describe AccountsController, type: :controller do context 'html' do before do - get :show, params: { username: alice.username } + get :show, params: { username: alice.username, max_id: status4.id, since_id: status1.id } + end + + it 'assigns @account' do + expect(assigns(:account)).to eq alice + end + + it 'assigns @statuses' do + statuses = assigns(:statuses).to_a + expect(statuses.size).to eq 2 + expect(statuses[0]).to eq status3 + expect(statuses[1]).to eq status2 end it 'returns http success' do