* Allow user to have many orgs * Allow users to handle multi orgs in settings * Make rounded buttons inline * Add multi org function to dashboards * Fix merge conflicts * Fix mistake in merge conflict fix oops * Display the correct membership level * Fix accessibility issues * Display organizations for article editors * Handle submitting org id with preact editors * Make listings work with multiple organizations * Allow listings to have multiple orgs on create * Display the correct number of credits for each org * Move script tag to Webpack * Allow multi orgs for purchasing and viewing credits * Use OrganizationMembership as authorization check * Display multiple organizations for notifications * Allow dashboard to be viewable under multi-orgs * Remove unused method * Add multi-org functionality for article editors * Show pro dashboard buttons for member+ org levels * Leave the correct organization * Allow article API to change org id * Add left-out authorization method oops * Make nav buttons a bit more clear * Fix merge conflict * Fix adding org id for /api/articles and tests * Fix tests for org policy * Use proper logic for displaying org members * Update org actions with new authorization * Use correct org when creating a listing * Remove additional payment charge oops * Mark org notifications as read with authorization * Remove deprecated post_as_organization attribute * Use new org_admin syntax * Remove deprecated org logic for article create and update * Default all RSS posts to not belong to any org * Render org_member page for guest users * Update org policy spec to work with multi orgs * Use org_membership for org traits and move identity code * Use org_member trait * Update to work with multi-orgs * Validate article's org_id if param org_id is blank * Make a let variable * Remove unnecessary eager load for credits * Fix HTML structure and org logic for non-org users * Update credits spec for multi-org * Add test for failed payment when purchased by org * Lint listings_spec * Test that the listing was created under the user * Add tests for POST /listings multi-org * Use double quotes for classes * Fix /manage and a few other multi-org bugs * Fix test for multi org * Use correct method SQL exists? not Rails exist? * Fix reads spec for multi-org * Fix org_controller actions to work with multi org * Test only multi org and not old usage and fix leave_org * Fix org showing user profile img test for multi-org * Fix org logic for users with no orgs * Remove switch org functionality * Update tests and add hidden param for org id * Redirect to the specific organization * Test other org button actions * Use settings_notice instead of legacy notice and refactor * Fix weird extra end issue prob from merge conflicts * Test for with new flash key * Fix user_views_org tests for multi-org * Test for new flash message * Update snapshot with new a11y html * Move styling to stylesheet * Add site admins functionality * Move org_member? method in user model and refactor * Use unspent_credits_count for organizations * Add tests for /listings/new and minor bug fixes * Use .present? in case of empty array * Fix a lingering deprecated method * Use greater than 1 for random numbers * Add tests for counting spent and unspent credits
173 lines
5.1 KiB
Ruby
173 lines
5.1 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe "Credits", type: :request do
|
|
describe "GET /credits" do
|
|
let(:user) { create(:user) }
|
|
let(:org_member) { create(:user, :org_member) }
|
|
let(:org_admin) { create(:user, :org_admin) }
|
|
|
|
it "shows credits page" do
|
|
sign_in user
|
|
get "/credits"
|
|
expect(response.body).to include("You have")
|
|
end
|
|
|
|
it "shows credits page if user belongs to an org" do
|
|
org = org_member.organizations.first
|
|
sign_in org_member
|
|
get "/credits"
|
|
expect(response.body).to include("You have")
|
|
expect(response.body).not_to include(CGI.escapeHTML(org.name))
|
|
end
|
|
|
|
it "shows credits page if user belongs to an org and is org admin" do
|
|
org = org_admin.organizations.first
|
|
sign_in org_admin
|
|
get "/credits"
|
|
expect(response.body).to include(CGI.escapeHTML(org.name))
|
|
end
|
|
end
|
|
|
|
describe "POST credits" do
|
|
let(:user) { create(:user) }
|
|
let(:org_admin) { create(:user, :org_admin) }
|
|
let(:admin_org_id) { org_admin.organizations.first.id }
|
|
let(:stripe_helper) { StripeMock.create_test_helper }
|
|
|
|
before do
|
|
StripeMock.start
|
|
sign_in user
|
|
end
|
|
|
|
after do
|
|
StripeMock.stop
|
|
end
|
|
|
|
it "creates unspent credits" do
|
|
post "/credits", params: {
|
|
credit: {
|
|
number_to_purchase: 25
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
expect(user.credits.where(spent: false).size).to eq(25)
|
|
end
|
|
|
|
it "makes a valid Stripe charge" do
|
|
post "/credits", params: {
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
customer = Stripe::Customer.retrieve(user.stripe_id_code)
|
|
expect(customer.charges.first.amount).to eq 8000
|
|
end
|
|
|
|
context "when a user already has a card" do
|
|
before do
|
|
customer = Stripe::Customer.create(email: user.email)
|
|
user.update_column(:stripe_id_code, customer.id)
|
|
customer.sources.create(source: stripe_helper.generate_card_token)
|
|
end
|
|
|
|
it "makes a valid Stripe charge" do
|
|
customer = Stripe::Customer.retrieve(user.stripe_id_code)
|
|
post "/credits", params: {
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
selected_card: customer.sources.first.id
|
|
}
|
|
expect(customer.charges.first.amount).to eq 8000
|
|
end
|
|
|
|
it "creates unspent credits" do
|
|
customer = Stripe::Customer.retrieve(user.stripe_id_code)
|
|
post "/credits", params: {
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
selected_card: customer.sources.first.id
|
|
}
|
|
expect(user.credits.where(spent: false).size).to eq(20)
|
|
end
|
|
|
|
it "charges a new card if given one" do
|
|
post "/credits", params: {
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
customer = Stripe::Customer.retrieve(user.stripe_id_code)
|
|
card_id = customer.sources.data.last.id
|
|
expect(customer.charges.first.source.id).to eq card_id
|
|
end
|
|
end
|
|
|
|
context "when purchasing as an organization" do
|
|
before { sign_in org_admin }
|
|
|
|
it "creates unspent credits for the organization" do
|
|
post "/credits", params: {
|
|
organization_id: admin_org_id,
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
expect(Credit.where(organization_id: admin_org_id, spent: false).size).to eq 20
|
|
end
|
|
|
|
it "makes a valid Stripe charge" do
|
|
post "/credits", params: {
|
|
organization_id: admin_org_id,
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
customer = Stripe::Customer.retrieve(org_admin.stripe_id_code)
|
|
expect(customer.charges.first.amount).to eq 8000
|
|
end
|
|
|
|
it "does not create unspent credits for the current_user" do
|
|
post "/credits", params: {
|
|
organization_id: admin_org_id,
|
|
credit: {
|
|
number_to_purchase: 20
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
expect(org_admin.credits.where(spent: false).size).to eq 0
|
|
end
|
|
end
|
|
|
|
context "when payment fails" do
|
|
it "does not reward credits" do
|
|
StripeMock.prepare_error(Stripe::CardError.new(2, 3, 4), :new_charge)
|
|
post "/credits", params: {
|
|
credit: {
|
|
number_to_purchase: 25
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
expect(user.credits.where(spent: false).size).to eq(0)
|
|
end
|
|
|
|
it "does not reward credits for orgs" do
|
|
sign_in org_admin
|
|
StripeMock.prepare_error(Stripe::CardError.new(2, 3, 4), :new_charge)
|
|
post "/credits", params: {
|
|
organization_id: admin_org_id,
|
|
credit: {
|
|
number_to_purchase: 25
|
|
},
|
|
stripe_token: stripe_helper.generate_card_token
|
|
}
|
|
expect(Credit.where(organization_id: admin_org_id, spent: false).size).to eq(0)
|
|
end
|
|
end
|
|
end
|
|
end
|