summaryrefslogtreecommitdiff
path: root/spec/lib/solidus_subscriptions/permission_sets/subscription_management_spec.rb
blob: 779fc9b6b0a4cc02b29dc5f60297aef320ee7546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

RSpec.describe SolidusSubscriptions::PermissionSets::SubscriptionManagement do
  it 'is allowed to manage their subscriptions' do
    user = create(:user)
    subscription = create(:subscription, user: user)

    ability = Spree::Ability.new(user)
    permission_set = described_class.new(ability)
    permission_set.activate!

    expect(ability).to be_able_to(:manage, subscription)
  end

  it "is allowed to manage someone else's subscriptions" do
    user = create(:user)
    subscription = create(:subscription)

    ability = Spree::Ability.new(user)
    permission_set = described_class.new(ability)
    permission_set.activate!

    expect(ability).not_to be_able_to(:manage, subscription)
  end

  it 'is allowed to manage line items on their orders' do
    user = create(:user)
    order = create(:order, user: user)
    line_item = create(
      :subscription_line_item,
      spree_line_item: create(:line_item, order: create(:order, user: user)),
    )

    ability = Spree::Ability.new(user)
    permission_set = described_class.new(ability)
    permission_set.activate!

    expect(ability).to be_able_to(:manage, line_item, order)
  end

  it 'is allowed to manage line items on the given order' do
    user = create(:user)
    order = create(:order, user: user)
    line_item = create(
      :subscription_line_item,
      spree_line_item: create(:line_item, order: order),
    )

    ability = Spree::Ability.new(user)
    permission_set = described_class.new(ability)
    permission_set.activate!

    expect(ability).to be_able_to(:manage, line_item, order)
  end

  it "is not allowed to manage line items on someone else's orders" do
    user = create(:user)
    order = create(:order)
    line_item = create(
      :subscription_line_item,
      spree_line_item: create(:line_item),
    )

    ability = Spree::Ability.new(user)
    permission_set = described_class.new(ability)
    permission_set.activate!

    expect(ability).not_to be_able_to(:manage, line_item, order)
  end
end