about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/about_controller_spec.rb11
-rw-r--r--spec/controllers/admin/reports_controller_spec.rb14
-rw-r--r--spec/controllers/admin/settings_controller_spec.rb14
-rw-r--r--spec/controllers/api/v1/accounts_controller_spec.rb39
-rw-r--r--spec/controllers/api/v1/notifications_controller_spec.rb62
-rw-r--r--spec/controllers/auth/registrations_controller_spec.rb2
-rw-r--r--spec/controllers/xrd_controller_spec.rb2
-rw-r--r--spec/fixtures/files/avatar.gifbin0 -> 85810 bytes
-rw-r--r--spec/helpers/about_helper_spec.rb5
-rw-r--r--spec/helpers/accounts_helper_spec.rb5
-rw-r--r--spec/helpers/admin/domain_blocks_helper_spec.rb5
-rw-r--r--spec/helpers/admin/pubsubhubbub_helper_spec.rb5
-rw-r--r--spec/helpers/application_helper_spec.rb16
-rw-r--r--spec/helpers/authorize_follow_helper_spec.rb5
-rw-r--r--spec/helpers/stream_entries_helper_spec.rb12
-rw-r--r--spec/helpers/tags_helper_spec.rb5
-rw-r--r--spec/helpers/xrd_helper_spec.rb5
-rw-r--r--spec/javascript/components/avatar.test.jsx12
-rw-r--r--spec/models/account_spec.rb99
-rw-r--r--spec/models/tag_spec.rb11
-rw-r--r--spec/presenters/instance_presenter_spec.rb74
-rw-r--r--spec/requests/catch_all_route_request_spec.rb21
22 files changed, 372 insertions, 52 deletions
diff --git a/spec/controllers/about_controller_spec.rb b/spec/controllers/about_controller_spec.rb
index 4282649e1..f49de9622 100644
--- a/spec/controllers/about_controller_spec.rb
+++ b/spec/controllers/about_controller_spec.rb
@@ -3,9 +3,16 @@ require 'rails_helper'
 RSpec.describe AboutController, type: :controller do
   render_views
 
-  describe 'GET #index' do
+  describe 'GET #show' do
     it 'returns http success' do
-      get :index
+      get :show
+      expect(response).to have_http_status(:success)
+    end
+  end
+
+  describe 'GET #more' do
+    it 'returns http success' do
+      get :more
       expect(response).to have_http_status(:success)
     end
   end
diff --git a/spec/controllers/admin/reports_controller_spec.rb b/spec/controllers/admin/reports_controller_spec.rb
new file mode 100644
index 000000000..622ea87c1
--- /dev/null
+++ b/spec/controllers/admin/reports_controller_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+RSpec.describe Admin::ReportsController, type: :controller do
+  describe 'GET #index' do
+    before do
+      sign_in Fabricate(:user, admin: true), scope: :user
+    end
+
+    it 'returns http success' do
+      get :index
+      expect(response).to have_http_status(:success)
+    end
+  end
+end
diff --git a/spec/controllers/admin/settings_controller_spec.rb b/spec/controllers/admin/settings_controller_spec.rb
new file mode 100644
index 000000000..c126b645b
--- /dev/null
+++ b/spec/controllers/admin/settings_controller_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+RSpec.describe Admin::SettingsController, type: :controller do
+  describe 'GET #index' do
+    before do
+      sign_in Fabricate(:user, admin: true), scope: :user
+    end
+
+    it 'returns http success' do
+      get :index
+      expect(response).to have_http_status(:success)
+    end
+  end
+end
diff --git a/spec/controllers/api/v1/accounts_controller_spec.rb b/spec/controllers/api/v1/accounts_controller_spec.rb
index 5d36b0159..ed49779b4 100644
--- a/spec/controllers/api/v1/accounts_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts_controller_spec.rb
@@ -24,6 +24,45 @@ RSpec.describe Api::V1::AccountsController, type: :controller do
     end
   end
 
+  describe 'PATCH #update_credentials' do
+    describe 'with valid data' do
+      before do
+        avatar = File.read(Rails.root.join('app', 'assets', 'images', 'logo.png'))
+        header = File.read(Rails.root.join('app', 'assets', 'images', 'mastodon-getting-started.png'))
+
+        patch :update_credentials, params: {
+          display_name: "Alice Isn't Dead",
+          note: "Hi!\n\nToot toot!",
+          avatar: "data:image/png;base64,#{Base64.encode64(avatar)}",
+          header: "data:image/png;base64,#{Base64.encode64(header)}",
+        }
+      end
+
+      it 'returns http success' do
+        expect(response).to have_http_status(:success)
+      end
+
+      it 'updates account info' do
+        user.account.reload
+
+        expect(user.account.display_name).to eq("Alice Isn't Dead")
+        expect(user.account.note).to eq("Hi!\n\nToot toot!")
+        expect(user.account.avatar).to exist
+        expect(user.account.header).to exist
+      end
+    end
+
+    describe 'with invalid data' do
+      before do
+        patch :update_credentials, params: { note: 'This is too long. ' * 10 }
+      end
+
+      it 'returns http unprocessable entity' do
+        expect(response).to have_http_status(:unprocessable_entity)
+      end
+    end
+  end
+
   describe 'GET #statuses' do
     it 'returns http success' do
       get :statuses, params: { id: user.account.id }
diff --git a/spec/controllers/api/v1/notifications_controller_spec.rb b/spec/controllers/api/v1/notifications_controller_spec.rb
index e5f7eec73..c390d4f01 100644
--- a/spec/controllers/api/v1/notifications_controller_spec.rb
+++ b/spec/controllers/api/v1/notifications_controller_spec.rb
@@ -5,15 +5,71 @@ RSpec.describe Api::V1::NotificationsController, type: :controller do
 
   let(:user)  { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
   let(:token) { double acceptable?: true, resource_owner_id: user.id }
+  let(:other) { Fabricate(:user, account: Fabricate(:account, username: 'bob')) }
 
   before do
     allow(controller).to receive(:doorkeeper_token) { token }
   end
 
   describe 'GET #index' do
-    it 'returns http success' do
-      get :index
-      expect(response).to have_http_status(:success)
+    before do
+      status     = PostStatusService.new.call(user.account, 'Test')
+      @reblog    = ReblogService.new.call(other.account, status)
+      @mention   = PostStatusService.new.call(other.account, 'Hello @alice')
+      @favourite = FavouriteService.new.call(other.account, status)
+      @follow    = FollowService.new.call(other.account, 'alice')
+    end
+
+    describe 'with no options' do
+      before do
+        get :index
+      end
+
+      it 'returns http success' do
+        expect(response).to have_http_status(:success)
+      end
+
+      it 'includes reblog' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@reblog.id)
+      end
+
+      it 'includes mention' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@mention.mentions.first.id)
+      end
+
+      it 'includes favourite' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@favourite.id)
+      end
+
+      it 'includes follow' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@follow.id)
+      end
+    end
+
+    describe 'with excluded mentions' do
+      before do
+        get :index, params: { exclude_types: ['mention'] }
+      end
+
+      it 'returns http success' do
+        expect(response).to have_http_status(:success)
+      end
+
+      it 'includes reblog' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@reblog.id)
+      end
+
+      it 'excludes mention' do
+        expect(assigns(:notifications).map(&:activity_id)).to_not include(@mention.mentions.first.id)
+      end
+
+      it 'includes favourite' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@favourite.id)
+      end
+
+      it 'includes follow' do
+        expect(assigns(:notifications).map(&:activity_id)).to include(@follow.id)
+      end
     end
   end
 end
diff --git a/spec/controllers/auth/registrations_controller_spec.rb b/spec/controllers/auth/registrations_controller_spec.rb
index 27ad6cbde..6b26e6693 100644
--- a/spec/controllers/auth/registrations_controller_spec.rb
+++ b/spec/controllers/auth/registrations_controller_spec.rb
@@ -5,6 +5,7 @@ RSpec.describe Auth::RegistrationsController, type: :controller do
 
   describe 'GET #new' do
     before do
+      Setting.open_registrations = true
       request.env["devise.mapping"] = Devise.mappings[:user]
     end
 
@@ -16,6 +17,7 @@ RSpec.describe Auth::RegistrationsController, type: :controller do
 
   describe 'POST #create' do
     before do
+      Setting.open_registrations = true
       request.env["devise.mapping"] = Devise.mappings[:user]
       post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678' } }
     end
diff --git a/spec/controllers/xrd_controller_spec.rb b/spec/controllers/xrd_controller_spec.rb
index e687cf9e0..b56c68f5c 100644
--- a/spec/controllers/xrd_controller_spec.rb
+++ b/spec/controllers/xrd_controller_spec.rb
@@ -14,7 +14,7 @@ RSpec.describe XrdController, type: :controller do
     let(:alice) { Fabricate(:account, username: 'alice') }
 
     it 'returns http success when account can be found' do
-      get :webfinger, params: { resource: "acct:#{alice.username}@#{Rails.configuration.x.local_domain}" }
+      get :webfinger, params: { resource: alice.to_webfinger_s }
       expect(response).to have_http_status(:success)
     end
 
diff --git a/spec/fixtures/files/avatar.gif b/spec/fixtures/files/avatar.gif
new file mode 100644
index 000000000..d929801e5
--- /dev/null
+++ b/spec/fixtures/files/avatar.gif
Binary files differdiff --git a/spec/helpers/about_helper_spec.rb b/spec/helpers/about_helper_spec.rb
deleted file mode 100644
index 6efc9f5bd..000000000
--- a/spec/helpers/about_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe AboutHelper, type: :helper do
-
-end
diff --git a/spec/helpers/accounts_helper_spec.rb b/spec/helpers/accounts_helper_spec.rb
deleted file mode 100644
index 3aea1f909..000000000
--- a/spec/helpers/accounts_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe AccountsHelper, type: :helper do
-
-end
diff --git a/spec/helpers/admin/domain_blocks_helper_spec.rb b/spec/helpers/admin/domain_blocks_helper_spec.rb
deleted file mode 100644
index cc7ead84e..000000000
--- a/spec/helpers/admin/domain_blocks_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe Admin::DomainBlocksHelper, type: :helper do
-
-end
diff --git a/spec/helpers/admin/pubsubhubbub_helper_spec.rb b/spec/helpers/admin/pubsubhubbub_helper_spec.rb
deleted file mode 100644
index 673236a7e..000000000
--- a/spec/helpers/admin/pubsubhubbub_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe Admin::PubsubhubbubHelper, type: :helper do
-
-end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index c2063c995..a2eeb443c 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -1,5 +1,19 @@
 require 'rails_helper'
 
-RSpec.describe ApplicationHelper, type: :helper do
+describe ApplicationHelper do
+  describe 'active_nav_class' do
+    it 'returns active when on the current page' do
+      allow(helper).to receive(:current_page?).and_return(true)
 
+      result = helper.active_nav_class("/test")
+      expect(result).to eq "active"
+    end
+
+    it 'returns empty string when not on current page' do
+      allow(helper).to receive(:current_page?).and_return(false)
+
+      result = helper.active_nav_class("/test")
+      expect(result).to eq ""
+    end
+  end
 end
diff --git a/spec/helpers/authorize_follow_helper_spec.rb b/spec/helpers/authorize_follow_helper_spec.rb
deleted file mode 100644
index ba5b0a70b..000000000
--- a/spec/helpers/authorize_follow_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe AuthorizeFollowHelper, type: :helper do
-
-end
diff --git a/spec/helpers/stream_entries_helper_spec.rb b/spec/helpers/stream_entries_helper_spec.rb
index 6227f9280..221e1e32d 100644
--- a/spec/helpers/stream_entries_helper_spec.rb
+++ b/spec/helpers/stream_entries_helper_spec.rb
@@ -2,7 +2,17 @@ require 'rails_helper'
 
 RSpec.describe StreamEntriesHelper, type: :helper do
   describe '#display_name' do
-    pending
+    it 'uses the display name when it exists' do
+      account = Account.new(display_name: "Display", username: "Username")
+
+      expect(helper.display_name(account)).to eq "Display"
+    end
+
+    it 'uses the username when display name is nil' do
+      account = Account.new(display_name: nil, username: "Username")
+
+      expect(helper.display_name(account)).to eq "Username"
+    end
   end
 
   describe '#avatar_for_status_url' do
diff --git a/spec/helpers/tags_helper_spec.rb b/spec/helpers/tags_helper_spec.rb
deleted file mode 100644
index f661e44ac..000000000
--- a/spec/helpers/tags_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe TagsHelper, type: :helper do
-
-end
diff --git a/spec/helpers/xrd_helper_spec.rb b/spec/helpers/xrd_helper_spec.rb
deleted file mode 100644
index 0bc71b657..000000000
--- a/spec/helpers/xrd_helper_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe XrdHelper, type: :helper do
-
-end
diff --git a/spec/javascript/components/avatar.test.jsx b/spec/javascript/components/avatar.test.jsx
index 852e13a89..7131bbec7 100644
--- a/spec/javascript/components/avatar.test.jsx
+++ b/spec/javascript/components/avatar.test.jsx
@@ -6,16 +6,10 @@ import Avatar from '../../../app/assets/javascripts/components/components/avatar
 describe('<Avatar />', () => {
   const src = '/path/to/image.jpg';
   const size = 100;
-  const wrapper = render(<Avatar src={src} size={size} />);
+  const wrapper = render(<Avatar src={src} animate size={size} />);
 
-  it('renders an img element with the given src', () => {
-    expect(wrapper.find('img')).to.have.attr('src', `${src}`);
-  });
-
-  it('renders an img element of the given size', () => {
-    ['width', 'height'].map((attr) => {
-      expect(wrapper.find('img')).to.have.attr(attr, `${size}`);
-    });
+  it('renders a div element with the given src as background', () => {
+    expect(wrapper.find('div')).to.have.style('background-image', `url(${src})`);
   });
 
   it('renders a div element of the given size', () => {
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 93a45459d..fb367ab7a 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -54,6 +54,30 @@ RSpec.describe Account, type: :model do
     end
   end
 
+  describe 'Local domain user methods' do
+    around do |example|
+      before = Rails.configuration.x.local_domain
+      example.run
+      Rails.configuration.x.local_domain = before
+    end
+
+    describe '#to_webfinger_s' do
+      it 'returns a webfinger string for the account' do
+        Rails.configuration.x.local_domain = 'example.com'
+
+        expect(subject.to_webfinger_s).to eq 'acct:alice@example.com'
+      end
+    end
+
+    describe '#local_username_and_domain' do
+      it 'returns the username and local domain for the account' do
+        Rails.configuration.x.local_domain = 'example.com'
+
+        expect(subject.local_username_and_domain).to eq 'alice@example.com'
+      end
+    end
+  end
+
   describe '#acct' do
     it 'returns username for local users' do
       expect(subject.acct).to eql 'alice'
@@ -170,6 +194,61 @@ RSpec.describe Account, type: :model do
     end
   end
 
+  describe '.search_for' do
+    before do
+      @match = Fabricate(
+        :account,
+        display_name: "Display Name",
+        username: "username",
+        domain: "example.com"
+      )
+      _missing = Fabricate(
+        :account,
+        display_name: "Missing",
+        username: "missing",
+        domain: "missing.com"
+      )
+    end
+
+    it 'finds accounts with matching display_name' do
+      results = Account.search_for("display")
+      expect(results).to eq [@match]
+    end
+
+    it 'finds accounts with matching username' do
+      results = Account.search_for("username")
+      expect(results).to eq [@match]
+    end
+
+    it 'finds accounts with matching domain' do
+      results = Account.search_for("example")
+      expect(results).to eq [@match]
+    end
+
+    it 'ranks multiple matches higher' do
+      account = Fabricate(
+        :account,
+        username: "username",
+        display_name: "username"
+      )
+      results = Account.search_for("username")
+      expect(results).to eq [account, @match]
+    end
+  end
+
+  describe '.advanced_search_for' do
+    it 'ranks followed accounts higher' do
+      account = Fabricate(:account)
+      match = Fabricate(:account, username: "Matching")
+      followed_match = Fabricate(:account, username: "Matcher")
+      Fabricate(:follow, account: account, target_account: followed_match)
+
+      results = Account.advanced_search_for("match", account)
+      expect(results).to eq [followed_match, match]
+      expect(results.first.rank).to be > results.last.rank
+    end
+  end
+
   describe '.find_local' do
     before do
       Fabricate(:account, username: 'Alice')
@@ -342,4 +421,24 @@ RSpec.describe Account, type: :model do
       end
     end
   end
+
+  describe 'static avatars' do
+    describe 'when GIF' do
+      it 'creates a png static style' do
+        subject.avatar = attachment_fixture('avatar.gif')
+        subject.save
+
+        expect(subject.avatar_static_url).to_not eq subject.avatar_original_url
+      end
+    end
+
+    describe 'when non-GIF' do
+      it 'does not create extra static style' do
+        subject.avatar = attachment_fixture('attachment.jpg')
+        subject.save
+
+        expect(subject.avatar_static_url).to eq subject.avatar_original_url
+      end
+    end
+  end
 end
diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb
index 360bbc16d..7a5b8ec89 100644
--- a/spec/models/tag_spec.rb
+++ b/spec/models/tag_spec.rb
@@ -12,4 +12,15 @@ RSpec.describe Tag, type: :model do
       expect(subject.match('https://en.wikipedia.org/wiki/Ghostbusters_(song)#Lawsuit')).to be_nil
     end
   end
+
+  describe '.search_for' do
+    it 'finds tag records with matching names' do
+      tag = Fabricate(:tag, name: "match")
+      _miss_tag = Fabricate(:tag, name: "miss")
+
+      results = Tag.search_for("match")
+
+      expect(results).to eq [tag]
+    end
+  end
 end
diff --git a/spec/presenters/instance_presenter_spec.rb b/spec/presenters/instance_presenter_spec.rb
new file mode 100644
index 000000000..0f318d9c3
--- /dev/null
+++ b/spec/presenters/instance_presenter_spec.rb
@@ -0,0 +1,74 @@
+require 'rails_helper'
+
+describe InstancePresenter do
+  let(:instance_presenter) { InstancePresenter.new }
+
+  it "delegates site_description to Setting" do
+    Setting.site_description = "Site desc"
+
+    expect(instance_presenter.site_description).to eq "Site desc"
+  end
+
+  it "delegates site_extended_description to Setting" do
+    Setting.site_extended_description = "Extended desc"
+
+    expect(instance_presenter.site_extended_description).to eq "Extended desc"
+  end
+
+  it "delegates open_registrations to Setting" do
+    Setting.open_registrations = false
+
+    expect(instance_presenter.open_registrations).to eq false
+  end
+
+  it "delegates closed_registrations_message to Setting" do
+    Setting.closed_registrations_message = "Closed message"
+
+    expect(instance_presenter.closed_registrations_message).to eq "Closed message"
+  end
+
+  it "delegates contact_email to Setting" do
+    Setting.contact_email = "admin@example.com"
+
+    expect(instance_presenter.contact_email).to eq "admin@example.com"
+  end
+
+  describe "contact_account" do
+    it "returns the account for the site contact username" do
+      Setting.site_contact_username = "aaa"
+      account = Fabricate(:account, username: "aaa")
+
+      expect(instance_presenter.contact_account).to eq(account)
+    end
+  end
+
+  describe "user_count" do
+    it "returns the number of site users" do
+      cache = double
+      allow(Rails).to receive(:cache).and_return(cache)
+      allow(cache).to receive(:fetch).with("user_count").and_return(123)
+
+      expect(instance_presenter.user_count).to eq(123)
+    end
+  end
+
+  describe "status_count" do
+    it "returns the number of local statuses" do
+      cache = double
+      allow(Rails).to receive(:cache).and_return(cache)
+      allow(cache).to receive(:fetch).with("local_status_count").and_return(234)
+
+      expect(instance_presenter.status_count).to eq(234)
+    end
+  end
+
+  describe "domain_count" do
+    it "returns the number of known domains" do
+      cache = double
+      allow(Rails).to receive(:cache).and_return(cache)
+      allow(cache).to receive(:fetch).with("distinct_domain_count").and_return(345)
+
+      expect(instance_presenter.domain_count).to eq(345)
+    end
+  end
+end
diff --git a/spec/requests/catch_all_route_request_spec.rb b/spec/requests/catch_all_route_request_spec.rb
new file mode 100644
index 000000000..22ce1cf59
--- /dev/null
+++ b/spec/requests/catch_all_route_request_spec.rb
@@ -0,0 +1,21 @@
+require "rails_helper"
+
+describe "The catch all route" do
+  describe "with a simple value" do
+    it "returns a 404 page as html" do
+      get "/test"
+
+      expect(response.status).to eq 404
+      expect(response.content_type).to eq "text/html"
+    end
+  end
+
+  describe "with an implied format" do
+    it "returns a 404 page as html" do
+      get "/test.test"
+
+      expect(response.status).to eq 404
+      expect(response.content_type).to eq "text/html"
+    end
+  end
+end