about summary refs log tree commit diff
path: root/spec/models
diff options
context:
space:
mode:
authorSamy KACIMI <samy.kacimi@gmail.com>2017-04-05 00:29:56 +0200
committerSamy KACIMI <samy.kacimi@gmail.com>2017-04-05 00:29:56 +0200
commit81c76fe375d9342e5a436db05c8e25305c650e8d (patch)
tree9d81c36f8745de3d5ab8827a82a14be369f0d23e /spec/models
parent6fd865c0004efbf11ee87c06fea9f48af567fabe (diff)
add more tests to models
Diffstat (limited to 'spec/models')
-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.rb44
7 files changed, 203 insertions, 0 deletions
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 91c8d75cf..fbc9a7d40 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 is 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 64de06749..1a3254185 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -1,5 +1,49 @@
 require 'rails_helper'
 
 RSpec.describe User, type: :model do
+  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
 end