2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-07-25 01:22:29 -04:00
|
|
|
require_dependency 'badge'
|
|
|
|
|
|
|
|
describe Badge do
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to belong_to(:badge_type) }
|
|
|
|
it { is_expected.to belong_to(:badge_grouping) }
|
|
|
|
it { is_expected.to have_many(:user_badges).dependent(:destroy) }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:name) }
|
|
|
|
it { is_expected.to validate_presence_of(:badge_type) }
|
|
|
|
it { is_expected.to validate_uniqueness_of(:name) }
|
2014-07-25 01:22:29 -04:00
|
|
|
|
|
|
|
it 'has a valid system attribute for new badges' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Badge.create!(name: "test", badge_type_id: 1).system?).to be false
|
2014-07-25 01:22:29 -04:00
|
|
|
end
|
|
|
|
|
2016-03-28 03:38:38 -04:00
|
|
|
it 'auto translates name' do
|
|
|
|
badge = Badge.find_by_name("Basic User")
|
|
|
|
name_english = badge.name
|
|
|
|
|
2016-09-29 01:42:56 -04:00
|
|
|
begin
|
|
|
|
I18n.locale = 'fr'
|
2016-03-28 03:38:38 -04:00
|
|
|
|
2016-09-29 01:42:56 -04:00
|
|
|
expect(badge.display_name).not_to eq(name_english)
|
|
|
|
ensure
|
|
|
|
I18n.locale = :en
|
|
|
|
end
|
2016-03-28 03:38:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles changes on badge description and long description correctly for system badges' do
|
|
|
|
badge = Badge.find_by_name("Basic User")
|
|
|
|
badge.description = badge.description.dup
|
|
|
|
badge.long_description = badge.long_description.dup
|
|
|
|
badge.save
|
|
|
|
badge.reload
|
|
|
|
|
|
|
|
expect(badge[:description]).to eq(nil)
|
|
|
|
expect(badge[:long_description]).to eq(nil)
|
|
|
|
|
|
|
|
badge.description = "testing"
|
|
|
|
badge.long_description = "testing it"
|
|
|
|
|
|
|
|
badge.save
|
|
|
|
badge.reload
|
|
|
|
|
|
|
|
expect(badge[:description]).to eq("testing")
|
|
|
|
expect(badge[:long_description]).to eq("testing it")
|
|
|
|
end
|
|
|
|
|
2016-03-29 01:41:03 -04:00
|
|
|
it 'can ensure consistency' do
|
|
|
|
b = Badge.first
|
|
|
|
b.grant_count = 100
|
|
|
|
b.save
|
|
|
|
|
|
|
|
UserBadge.create!(user_id: -100, badge_id: b.id, granted_at: 1.minute.ago, granted_by_id: -1)
|
2016-03-29 08:54:18 -04:00
|
|
|
UserBadge.create!(user_id: User.first.id, badge_id: b.id, granted_at: 1.minute.ago, granted_by_id: -1)
|
2016-03-29 01:41:03 -04:00
|
|
|
|
|
|
|
Badge.ensure_consistency!
|
|
|
|
|
|
|
|
b.reload
|
2016-03-29 08:54:18 -04:00
|
|
|
expect(b.grant_count).to eq(1)
|
2016-03-29 01:41:03 -04:00
|
|
|
end
|
|
|
|
|
2018-01-21 22:10:53 -05:00
|
|
|
describe '#manually_grantable?' do
|
|
|
|
let(:badge) { Fabricate(:badge, name: 'Test Badge') }
|
|
|
|
subject { badge.manually_grantable? }
|
|
|
|
|
|
|
|
context 'when system badge' do
|
|
|
|
before { badge.system = true }
|
|
|
|
it { is_expected.to be false }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has query' do
|
|
|
|
before { badge.query = 'SELECT id FROM users' }
|
|
|
|
it { is_expected.to be false }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when neither system nor has query' do
|
|
|
|
before { badge.update_columns(system: false, query: nil) }
|
|
|
|
it { is_expected.to be true }
|
|
|
|
end
|
|
|
|
end
|
2014-07-25 01:22:29 -04:00
|
|
|
end
|