|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe DiscourseKofi::Admin::RewardsController do |
| 4 | + fab!(:admin) |
| 5 | + |
| 6 | + before do |
| 7 | + SiteSetting.kofi_enabled = true |
| 8 | + sign_in(admin) |
| 9 | + end |
| 10 | + |
| 11 | + fab!(:reward) |
| 12 | + fab!(:subscription_reward) |
| 13 | + |
| 14 | + describe "#index" do |
| 15 | + it "returns all rewards" do |
| 16 | + get "/ko-fi/admin/rewards.json" |
| 17 | + expect(response.status).to eq(200) |
| 18 | + parsed = response.parsed_body |
| 19 | + expect(parsed[:rewards]).to contain_exactly(include(id: reward.id)) |
| 20 | + expect(parsed[:subscriptions]).to contain_exactly( |
| 21 | + include(id: subscription_reward.id) |
| 22 | + ) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe "#show" do |
| 27 | + it "returns a reward" do |
| 28 | + get "/ko-fi/admin/rewards/#{reward.id}.json" |
| 29 | + expect(response.status).to eq(200) |
| 30 | + parsed = response.parsed_body |
| 31 | + expect(parsed[:reward][:id]).to eq reward.id |
| 32 | + expect(parsed[:reward][:subscription]).to be false |
| 33 | + expect(parsed[:reward][:amount]).to eq reward.amount.as_json |
| 34 | + |
| 35 | + expect(parsed[:reward][:tier_name]).to be_nil |
| 36 | + end |
| 37 | + |
| 38 | + it "returns a subscription reward" do |
| 39 | + get "/ko-fi/admin/rewards/#{subscription_reward.id}.json" |
| 40 | + expect(response.status).to eq(200) |
| 41 | + parsed = response.parsed_body |
| 42 | + expect(parsed[:reward][:id]).to eq subscription_reward.id |
| 43 | + expect(parsed[:reward][:subscription]).to be true |
| 44 | + expect(parsed[:reward][:tier_name]).to eq subscription_reward.tier_name |
| 45 | + |
| 46 | + expect(parsed[:reward][:amount]).to be_nil |
| 47 | + end |
| 48 | + |
| 49 | + it "cannot find an unknown reward" do |
| 50 | + get "/ko-fi/admin/rewards/99999999999999999.json" |
| 51 | + expect(response.status).to eq(404) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + describe "#create" do |
| 56 | + fab!(:badge) |
| 57 | + fab!(:group) |
| 58 | + |
| 59 | + it "can create a new reward" do |
| 60 | + StaffActionLogger |
| 61 | + .any_instance |
| 62 | + .expects(:log_custom) |
| 63 | + .with("kofi_reward_creation", anything) |
| 64 | + .once |
| 65 | + |
| 66 | + post "/ko-fi/admin/rewards.json", |
| 67 | + params: { |
| 68 | + subscription: false, |
| 69 | + badge_id: badge.id, |
| 70 | + group_id: group.id, |
| 71 | + amount: 123.45, |
| 72 | + payment_types: %i[donation subscription] |
| 73 | + } |
| 74 | + |
| 75 | + expect(response.status).to eq(200) |
| 76 | + parsed = response.parsed_body |
| 77 | + expect(parsed[:reward][:id]).not_to be_nil |
| 78 | + expect(parsed[:reward][:subscription]).to be false |
| 79 | + expect(parsed[:reward][:badge][:id]).to eq badge.id |
| 80 | + expect(parsed[:reward][:group][:id]).to eq group.id |
| 81 | + expect(parsed[:reward][:amount]).to eq "123.45" |
| 82 | + expect(parsed[:reward][:payment_types]).to match_array( |
| 83 | + %w[donation subscription] |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + it "can create a new subscription reward" do |
| 88 | + StaffActionLogger |
| 89 | + .any_instance |
| 90 | + .expects(:log_custom) |
| 91 | + .with("kofi_reward_creation", anything) |
| 92 | + .once |
| 93 | + |
| 94 | + post "/ko-fi/admin/rewards.json", |
| 95 | + params: { |
| 96 | + subscription: true, |
| 97 | + group_id: group.id, |
| 98 | + tier_name: "premium" |
| 99 | + } |
| 100 | + |
| 101 | + expect(response.status).to eq(200) |
| 102 | + parsed = response.parsed_body |
| 103 | + expect(parsed[:reward][:id]).not_to be_nil |
| 104 | + expect(parsed[:reward][:subscription]).to be true |
| 105 | + expect(parsed[:reward][:group][:id]).to eq group.id |
| 106 | + expect(parsed[:reward][:tier_name]).to eq "premium" |
| 107 | + end |
| 108 | + |
| 109 | + it "cannot create an invalid reward" do |
| 110 | + StaffActionLogger |
| 111 | + .any_instance |
| 112 | + .expects(:log_custom) |
| 113 | + .with("kofi_reward_creation", anything) |
| 114 | + .never |
| 115 | + |
| 116 | + post "/ko-fi/admin/rewards.json", |
| 117 | + params: { |
| 118 | + subscription: false, |
| 119 | + group_id: group.id, |
| 120 | + tier_name: "premium" |
| 121 | + } |
| 122 | + |
| 123 | + expect(response.status).to eq(422) |
| 124 | + parsed = response.parsed_body |
| 125 | + expect(parsed[:errors]).to match_array( |
| 126 | + [ |
| 127 | + "Tier name must be blank", |
| 128 | + "Payment types can't be blank", |
| 129 | + "Amount is not a number", |
| 130 | + "Payment types must be an array" |
| 131 | + ] |
| 132 | + ) |
| 133 | + end |
| 134 | + |
| 135 | + describe "#update" do |
| 136 | + it "can update a reward" do |
| 137 | + StaffActionLogger |
| 138 | + .any_instance |
| 139 | + .expects(:log_custom) |
| 140 | + .with("kofi_reward_change", anything) |
| 141 | + .once |
| 142 | + |
| 143 | + patch "/ko-fi/admin/rewards/#{reward.id}.json", |
| 144 | + params: { |
| 145 | + group_id: nil, |
| 146 | + amount: 123.45, |
| 147 | + payment_types: %w[donation subscription] |
| 148 | + } |
| 149 | + |
| 150 | + expect(response.status).to eq(200) |
| 151 | + parsed = response.parsed_body |
| 152 | + expect(parsed[:reward][:id]).to eq reward.id |
| 153 | + expect(parsed[:reward][:subscription]).to be false |
| 154 | + expect(parsed[:reward][:group]).to be_nil |
| 155 | + expect(parsed[:reward][:amount]).to eq "123.45" |
| 156 | + expect(parsed[:reward][:payment_types]).to match_array( |
| 157 | + %w[donation subscription] |
| 158 | + ) |
| 159 | + end |
| 160 | + |
| 161 | + it "cannot change the subscription kind" do |
| 162 | + patch "/ko-fi/admin/rewards/#{reward.id}.json", |
| 163 | + params: { |
| 164 | + subscription: true |
| 165 | + } |
| 166 | + |
| 167 | + expect(response.status).to eq(200) |
| 168 | + parsed = response.parsed_body |
| 169 | + expect(parsed[:reward][:subscription]).to be false |
| 170 | + end |
| 171 | + |
| 172 | + it "cannot make invalid changes" do |
| 173 | + patch "/ko-fi/admin/rewards/#{reward.id}.json", |
| 174 | + params: { |
| 175 | + tier_name: "something" |
| 176 | + } |
| 177 | + |
| 178 | + expect(response.status).to eq(422) |
| 179 | + end |
| 180 | + |
| 181 | + it "cannot unpdate an unknown reward" do |
| 182 | + patch "/ko-fi/admin/rewards/9999999999999.json", params: {} |
| 183 | + |
| 184 | + expect(response.status).to eq(404) |
| 185 | + end |
| 186 | + end |
| 187 | + |
| 188 | + describe "#destroy" do |
| 189 | + it "can delete a reward" do |
| 190 | + StaffActionLogger |
| 191 | + .any_instance |
| 192 | + .expects(:log_custom) |
| 193 | + .with("kofi_reward_deletion", anything) |
| 194 | + .once |
| 195 | + |
| 196 | + delete "/ko-fi/admin/rewards/#{reward.id}.json" |
| 197 | + expect(response.status).to eq(200) |
| 198 | + |
| 199 | + get "/ko-fi/admin/rewards/#{reward.id}.json" |
| 200 | + expect(response.status).to eq(404) |
| 201 | + end |
| 202 | + |
| 203 | + it "cannot delete an unknown rewared" do |
| 204 | + delete "/ko-fi/admin/rewards/9999999999999.json" |
| 205 | + expect(response.status).to eq(404) |
| 206 | + end |
| 207 | + end |
| 208 | + end |
| 209 | +end |
0 commit comments