about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-04-05 03:31:45 +0200
committerEugen Rochko <eugen@zeonfederated.com>2017-04-05 03:31:45 +0200
commitccb6a658fd1a2e596c95d5b8e7a39f72a5f5b14b (patch)
tree7b846b51e3c91fb93aa379361dfe7a811bf5a01c /spec
parent94d00f278819d34ef97d59d17eb289e9be217dfe (diff)
parent667ffafef8c8b7956cdd31b8f65d5e82778211d8 (diff)
Merge branch 'fakenine-add_more_tests_to_models'
Diffstat (limited to 'spec')
-rw-r--r--spec/fabricators/account_fabricator.rb2
-rw-r--r--spec/fabricators/block_fabricator.rb3
-rw-r--r--spec/fabricators/follow_fabricator.rb3
-rw-r--r--spec/fabricators/follow_request_fabricator.rb3
-rw-r--r--spec/fabricators/mention_fabricator.rb4
-rw-r--r--spec/fabricators/user_fabricator.rb2
-rw-r--r--spec/models/account_spec.rb69
-rw-r--r--spec/models/block_spec.rb17
-rw-r--r--spec/models/domain_block_spec.rb18
-rw-r--r--spec/models/follow_request_spec.rb19
-rw-r--r--spec/models/follow_spec.rb19
-rw-r--r--spec/models/mention_spec.rb17
-rw-r--r--spec/models/user_spec.rb52
-rw-r--r--spec/rails_helper.rb2
-rw-r--r--spec/support/matchers/model/model_have_error_on_field.rb15
15 files changed, 237 insertions, 8 deletions
diff --git a/spec/fabricators/account_fabricator.rb b/spec/fabricators/account_fabricator.rb
index 3a7c00bf5..567de05f4 100644
--- a/spec/fabricators/account_fabricator.rb
+++ b/spec/fabricators/account_fabricator.rb
@@ -1,3 +1,3 @@
 Fabricator(:account) do
-  username "alice"
+  username { Faker::Internet.user_name(nil, %w(_)) }
 end
diff --git a/spec/fabricators/block_fabricator.rb b/spec/fabricators/block_fabricator.rb
index 9a5a6808f..379931ba6 100644
--- a/spec/fabricators/block_fabricator.rb
+++ b/spec/fabricators/block_fabricator.rb
@@ -1,3 +1,4 @@
 Fabricator(:block) do
-
+  account
+  target_account { Fabricate(:account) }
 end
diff --git a/spec/fabricators/follow_fabricator.rb b/spec/fabricators/follow_fabricator.rb
index 9d9d06f12..9b25dc547 100644
--- a/spec/fabricators/follow_fabricator.rb
+++ b/spec/fabricators/follow_fabricator.rb
@@ -1,3 +1,4 @@
 Fabricator(:follow) do
-
+  account
+  target_account { Fabricate(:account) }
 end
diff --git a/spec/fabricators/follow_request_fabricator.rb b/spec/fabricators/follow_request_fabricator.rb
index 9c3733cef..78a057919 100644
--- a/spec/fabricators/follow_request_fabricator.rb
+++ b/spec/fabricators/follow_request_fabricator.rb
@@ -1,3 +1,4 @@
 Fabricator(:follow_request) do
-
+  account
+  target_account { Fabricate(:account) }
 end
diff --git a/spec/fabricators/mention_fabricator.rb b/spec/fabricators/mention_fabricator.rb
new file mode 100644
index 000000000..cb5fe4299
--- /dev/null
+++ b/spec/fabricators/mention_fabricator.rb
@@ -0,0 +1,4 @@
+Fabricator(:mention) do
+  account
+  status
+end
diff --git a/spec/fabricators/user_fabricator.rb b/spec/fabricators/user_fabricator.rb
index c08559137..16b3b1f6f 100644
--- a/spec/fabricators/user_fabricator.rb
+++ b/spec/fabricators/user_fabricator.rb
@@ -1,6 +1,6 @@
 Fabricator(:user) do
   account
-  email        "alice@example.com"
+  email        { Faker::Internet.email }
   password     "123456789"
   confirmed_at { Time.now }
 end
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 91c8d75cf..d7f59adb8 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -209,4 +209,73 @@ RSpec.describe Account, type: :model do
       expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
     end
   end
+
+  describe 'validations' do
+    it 'has a valid fabricator' do
+      account = Fabricate.build(:account)
+      account.valid?
+      expect(account).to be_valid
+    end
+
+    it 'is invalid without a username' do
+      account = Fabricate.build(:account, username: nil)
+      account.valid?
+      expect(account).to model_have_error_on_field(:username)
+    end
+
+    it 'is invalid if the username already exists' do
+      account_1 = Fabricate(:account, username: 'the_doctor')
+      account_2 = Fabricate.build(:account, username: 'the_doctor')
+      account_2.valid?
+      expect(account_2).to model_have_error_on_field(:username)
+    end
+
+    context 'when is local' do
+      it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
+        account = Fabricate.build(:account, username: 'the-doctor')
+        account.valid?
+        expect(account).to model_have_error_on_field(:username)
+      end
+
+      it 'is invalid if the username is longer then 30 characters' do
+        account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
+        account.valid?
+        expect(account).to model_have_error_on_field(:username)
+      end
+    end
+  end
+
+  describe 'scopes' do
+    describe 'remote' do
+      it 'returns an array of accounts who have a domain' do
+        account_1 = Fabricate(:account, domain: nil)
+        account_2 = Fabricate(:account, domain: 'example.com')
+        expect(Account.remote).to match_array([account_2])
+      end
+    end
+
+    describe 'local' do
+      it 'returns an array of accounts who do not have a domain' do
+        account_1 = Fabricate(:account, domain: nil)
+        account_2 = Fabricate(:account, domain: 'example.com')
+        expect(Account.local).to match_array([account_1])
+      end
+    end
+
+    describe 'silenced' do
+      it 'returns an array of accounts who are silenced' do
+        account_1 = Fabricate(:account, silenced: true)
+        account_2 = Fabricate(:account, silenced: false)
+        expect(Account.silenced).to match_array([account_1])
+      end
+    end
+
+    describe 'suspended' do
+      it 'returns an array of accounts who are suspended' do
+        account_1 = Fabricate(:account, suspended: true)
+        account_2 = Fabricate(:account, suspended: false)
+        expect(Account.suspended).to match_array([account_1])
+      end
+    end
+  end
 end
diff --git a/spec/models/block_spec.rb b/spec/models/block_spec.rb
index 6862de6fc..cabb41c3e 100644
--- a/spec/models/block_spec.rb
+++ b/spec/models/block_spec.rb
@@ -1,5 +1,22 @@
 require 'rails_helper'
 
 RSpec.describe Block, type: :model do
+  describe 'validations' do
+    it 'has a valid fabricator' do
+      block = Fabricate.build(:block)
+      expect(block).to be_valid
+    end
 
+    it 'is invalid without an account' do
+      block = Fabricate.build(:block, account: nil)
+      block.valid?
+      expect(block).to model_have_error_on_field(:account)
+    end
+
+    it 'is invalid without a target_account' do
+      block = Fabricate.build(:block, target_account: nil)
+      block.valid?
+      expect(block).to model_have_error_on_field(:target_account)
+    end
+  end
 end
diff --git a/spec/models/domain_block_spec.rb b/spec/models/domain_block_spec.rb
index ad5403110..b19c8083e 100644
--- a/spec/models/domain_block_spec.rb
+++ b/spec/models/domain_block_spec.rb
@@ -1,5 +1,23 @@
 require 'rails_helper'
 
 RSpec.describe DomainBlock, type: :model do
+  describe 'validations' do
+    it 'has a valid fabricator' do
+      domain_block = Fabricate.build(:domain_block)
+      expect(domain_block).to be_valid
+    end
 
+    it 'is invalid without a domain' do
+      domain_block = Fabricate.build(:domain_block, domain: nil)
+      domain_block.valid?
+      expect(domain_block).to model_have_error_on_field(:domain)
+    end
+
+    it 'is invalid if the domain already exists' do
+      domain_block_1 = Fabricate(:domain_block, domain: 'dalek.com')
+      domain_block_2 = Fabricate.build(:domain_block, domain: 'dalek.com')
+      domain_block_2.valid?
+      expect(domain_block_2).to model_have_error_on_field(:domain)
+    end
+  end
 end
diff --git a/spec/models/follow_request_spec.rb b/spec/models/follow_request_spec.rb
index f2ec642d8..cc6f8ee62 100644
--- a/spec/models/follow_request_spec.rb
+++ b/spec/models/follow_request_spec.rb
@@ -3,4 +3,23 @@ require 'rails_helper'
 RSpec.describe FollowRequest, type: :model do
   describe '#authorize!'
   describe '#reject!'
+
+  describe 'validations' do
+    it 'has a valid fabricator' do
+      follow_request = Fabricate.build(:follow_request)
+      expect(follow_request).to be_valid
+    end
+
+    it 'is invalid without an account' do
+      follow_request = Fabricate.build(:follow_request, account: nil)
+      follow_request.valid?
+      expect(follow_request).to model_have_error_on_field(:account)
+    end
+
+    it 'is invalid without a target account' do
+      follow_request = Fabricate.build(:follow_request, target_account: nil)
+      follow_request.valid?
+      expect(follow_request).to model_have_error_on_field(:target_account)      
+    end
+  end
 end
diff --git a/spec/models/follow_spec.rb b/spec/models/follow_spec.rb
index eb21f3e18..0fae25352 100644
--- a/spec/models/follow_spec.rb
+++ b/spec/models/follow_spec.rb
@@ -5,4 +5,23 @@ RSpec.describe Follow, type: :model do
   let(:bob)   { Fabricate(:account, username: 'bob') }
 
   subject { Follow.new(account: alice, target_account: bob) }
+
+  describe 'validations' do
+    it 'has a valid fabricator' do
+      follow = Fabricate.build(:follow)
+      expect(follow).to be_valid
+    end
+
+    it 'is invalid without an account' do
+      follow = Fabricate.build(:follow, account: nil)
+      follow.valid?
+      expect(follow).to model_have_error_on_field(:account)
+    end
+
+    it 'is invalid without a target_account' do
+      follow = Fabricate.build(:follow, target_account: nil)
+      follow.valid?
+      expect(follow).to model_have_error_on_field(:target_account)
+    end
+  end
 end
diff --git a/spec/models/mention_spec.rb b/spec/models/mention_spec.rb
index 5c91fda02..dbcf6a32c 100644
--- a/spec/models/mention_spec.rb
+++ b/spec/models/mention_spec.rb
@@ -1,5 +1,22 @@
 require 'rails_helper'
 
 RSpec.describe Mention, type: :model do
+  describe 'validations' do
+    it 'has a valid fabricator' do
+      mention = Fabricate.build(:mention)
+      expect(mention).to be_valid
+    end
 
+    it 'is invalid without an account' do
+      mention = Fabricate.build(:mention, account: nil)
+      mention.valid?
+      expect(mention).to model_have_error_on_field(:account)
+    end
+
+    it 'is invalid without a status' do
+      mention = Fabricate.build(:mention, status: nil)
+      mention.valid?
+      expect(mention).to model_have_error_on_field(:status)
+    end
+  end
 end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index aa777fd39..eb2a4aaea 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -1,7 +1,53 @@
 require 'rails_helper'
 
 RSpec.describe User, type: :model do
-  let(:account) { Fabricate(:account, username: 'alice') }  
+  describe 'validations' do
+    it 'is invalid without an account' do
+      user = Fabricate.build(:user, account: nil)
+      user.valid?
+      expect(user).to model_have_error_on_field(:account)
+    end
+
+    it 'is invalid without a valid locale' do
+      user = Fabricate.build(:user, locale: 'toto')
+      user.valid?
+      expect(user).to model_have_error_on_field(:locale)
+    end
+
+    it 'is invalid without a valid email' do
+      user = Fabricate.build(:user, email: 'john@')
+      user.valid?
+      expect(user).to model_have_error_on_field(:email)
+    end
+  end
+
+  describe 'scopes' do
+    describe 'recent' do
+      it 'returns an array of recent users ordered by id' do
+        user_1 = Fabricate(:user)
+        user_2 = Fabricate(:user)
+        expect(User.recent).to match_array([user_2, user_1])
+      end
+    end
+
+    describe 'admins' do
+      it 'returns an array of users who are admin' do
+        user_1 = Fabricate(:user, admin: false)
+        user_2 = Fabricate(:user, admin: true)
+        expect(User.admins).to match_array([user_2])
+      end
+    end
+
+    describe 'confirmed' do
+      it 'returns an array of users who are confirmed' do
+        user_1 = Fabricate(:user, confirmed_at: nil)
+        user_2 = Fabricate(:user, confirmed_at: Time.now)
+        expect(User.confirmed).to match_array([user_2])
+      end
+    end
+  end
+
+  let(:account) { Fabricate(:account, username: 'alice') }
   let(:password) { 'abcd1234' }
 
   describe 'blacklist' do
@@ -10,7 +56,7 @@ RSpec.describe User, type: :model do
 
       expect(user.valid?).to be_truthy
     end
-    
+
     it 'should not allow a blacklisted user to be created' do
       user = User.new(email: 'foo@mvrht.com', account: account, password: password)
 
@@ -37,6 +83,6 @@ RSpec.describe User, type: :model do
     it 'should allow a user to be created if they are whitelisted' do
       user = User.new(email: 'foo@mastodon.space', account: account, password: password)
       expect(user.valid?).to be_truthy
-    end    
+    end
   end
 end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 977c7bdc0..faac96982 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -8,6 +8,8 @@ require 'rspec/rails'
 require 'webmock/rspec'
 require 'paperclip/matchers'
 
+Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
+
 ActiveRecord::Migration.maintain_test_schema!
 WebMock.disable_net_connect!(allow: 'localhost:7575')
 Sidekiq::Testing.inline!
diff --git a/spec/support/matchers/model/model_have_error_on_field.rb b/spec/support/matchers/model/model_have_error_on_field.rb
new file mode 100644
index 000000000..5d5fe1c7b
--- /dev/null
+++ b/spec/support/matchers/model/model_have_error_on_field.rb
@@ -0,0 +1,15 @@
+RSpec::Matchers.define :model_have_error_on_field do |expected|
+  match do |record|
+    if record.errors.empty?
+      record.valid?
+    end
+
+    record.errors.has_key?(expected)
+  end
+
+  failure_message do |record|
+    keys = record.errors.keys
+    
+    "expect record.errors(#{keys}) to include #{expected}"
+  end
+end