2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
require 'spec_helper'
|
2016-02-12 09:58:39 -05:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Subscribable, 'Subscribable' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2016-10-31 15:19:14 -04:00
|
|
|
let(:resource) { create(:issue, project: project) }
|
2016-11-14 17:42:22 -05:00
|
|
|
let(:user_1) { create(:user) }
|
2016-02-12 09:58:39 -05:00
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
describe '#subscribed?' do
|
2017-10-31 12:15:03 -04:00
|
|
|
context 'without user' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(resource.subscribed?(nil, project)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
context 'without project' do
|
|
|
|
it 'returns false when no subscription exists' do
|
|
|
|
expect(resource.subscribed?(user_1)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when a subcription exists and subscribed is true' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, subscribed: true)
|
2016-11-14 17:42:22 -05:00
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1)).to be_truthy
|
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
it 'returns false when a subcription exists and subscribed is false' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, subscribed: false)
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
expect(resource.subscribed?(user_1)).to be_falsey
|
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
end
|
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
context 'with project' do
|
|
|
|
it 'returns false when no subscription exists' do
|
|
|
|
expect(resource.subscribed?(user_1, project)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when a subcription exists and subscribed is true' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, project: project, subscribed: true)
|
2016-11-14 17:42:22 -05:00
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1, project)).to be_truthy
|
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
it 'returns false when a subcription exists and subscribed is false' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, project: project, subscribed: false)
|
2016-11-14 17:42:22 -05:00
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1, project)).to be_falsey
|
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
end
|
|
|
|
end
|
2016-10-31 15:19:14 -04:00
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
describe '#subscribers' do
|
|
|
|
it 'returns [] when no subcribers exists' do
|
2016-10-31 15:19:14 -04:00
|
|
|
expect(resource.subscribers(project)).to be_empty
|
2016-03-01 11:33:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the subscribed users' do
|
2016-11-14 17:42:22 -05:00
|
|
|
user_2 = create(:user)
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, subscribed: true)
|
|
|
|
resource.subscriptions.create!(user: user_2, project: project, subscribed: true)
|
|
|
|
resource.subscriptions.create!(user: create(:user), project: project, subscribed: false)
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
expect(resource.subscribers(project)).to contain_exactly(user_1, user_2)
|
2016-03-01 11:33:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#toggle_subscription' do
|
2016-11-14 17:42:22 -05:00
|
|
|
context 'without project' do
|
|
|
|
it 'toggles the current subscription state for the given user' do
|
|
|
|
expect(resource.subscribed?(user_1)).to be_falsey
|
|
|
|
|
|
|
|
resource.toggle_subscription(user_1)
|
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
context 'with project' do
|
|
|
|
it 'toggles the current subscription state for the given user' do
|
|
|
|
expect(resource.subscribed?(user_1, project)).to be_falsey
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
resource.toggle_subscription(user_1, project)
|
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1, project)).to be_truthy
|
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-12 16:48:09 -04:00
|
|
|
describe '#subscribe' do
|
2016-11-14 17:42:22 -05:00
|
|
|
context 'without project' do
|
|
|
|
it 'subscribes the given user' do
|
|
|
|
expect(resource.subscribed?(user_1)).to be_falsey
|
|
|
|
|
|
|
|
resource.subscribe(user_1)
|
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with project' do
|
|
|
|
it 'subscribes the given user' do
|
|
|
|
expect(resource.subscribed?(user_1, project)).to be_falsey
|
2016-05-12 16:48:09 -04:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
resource.subscribe(user_1, project)
|
2016-05-12 16:48:09 -04:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
expect(resource.subscribed?(user_1, project)).to be_truthy
|
|
|
|
end
|
2016-05-12 16:48:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
describe '#unsubscribe' do
|
2016-11-14 17:42:22 -05:00
|
|
|
context 'without project' do
|
|
|
|
it 'unsubscribes the given current user' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, subscribed: true)
|
2016-11-14 17:42:22 -05:00
|
|
|
expect(resource.subscribed?(user_1)).to be_truthy
|
|
|
|
|
|
|
|
resource.unsubscribe(user_1)
|
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with project' do
|
|
|
|
it 'unsubscribes the given current user' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, project: project, subscribed: true)
|
2016-11-14 17:42:22 -05:00
|
|
|
expect(resource.subscribed?(user_1, project)).to be_truthy
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
resource.unsubscribe(user_1, project)
|
2016-03-01 11:33:13 -05:00
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
expect(resource.subscribed?(user_1, project)).to be_falsey
|
|
|
|
end
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
|
|
|
end
|
2019-10-31 14:06:53 -04:00
|
|
|
|
|
|
|
describe '#set_subscription' do
|
|
|
|
shared_examples 'setting subscriptions' do
|
|
|
|
context 'when desired_state is set to true' do
|
|
|
|
context 'when a user is subscribed to the resource' do
|
|
|
|
it 'keeps the user subscribed' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, subscribed: true, project: resource_project)
|
2019-10-31 14:06:53 -04:00
|
|
|
|
|
|
|
resource.set_subscription(user_1, true, resource_project)
|
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1, resource_project)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is not subscribed to the resource' do
|
|
|
|
it 'subscribes the user to the resource' do
|
|
|
|
expect { resource.set_subscription(user_1, true, resource_project) }
|
|
|
|
.to change { resource.subscribed?(user_1, resource_project) }
|
|
|
|
.from(false).to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when desired_state is set to false' do
|
|
|
|
context 'when a user is subscribed to the resource' do
|
|
|
|
it 'unsubscribes the user from the resource' do
|
2020-09-22 14:09:54 -04:00
|
|
|
resource.subscriptions.create!(user: user_1, subscribed: true, project: resource_project)
|
2019-10-31 14:06:53 -04:00
|
|
|
|
|
|
|
expect { resource.set_subscription(user_1, false, resource_project) }
|
|
|
|
.to change { resource.subscribed?(user_1, resource_project) }
|
|
|
|
.from(true).to(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is not subscribed to the resource' do
|
|
|
|
it 'keeps the user unsubscribed' do
|
|
|
|
resource.set_subscription(user_1, false, resource_project)
|
|
|
|
|
|
|
|
expect(resource.subscribed?(user_1, resource_project)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without project' do
|
|
|
|
let(:resource_project) { nil }
|
|
|
|
|
|
|
|
it_behaves_like 'setting subscriptions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with project' do
|
|
|
|
let(:resource_project) { project }
|
|
|
|
|
|
|
|
it_behaves_like 'setting subscriptions'
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|