docbrown/spec/models/organization_spec.rb
rhymes c8abbe9d60 Pro: memberships (#3461)
* Add ProMembership model

* Implement ProMembershipsController#create

* Implement basic ProMembershipsController#show

* Add ProMembership to ledger

* Populate user history after pro subscription is created

* Add fields for expiration notifications

* Add ProMemberships::ExpirationNotifier to notify users of expiring memberships

* Add tasks for recurring jobs to notify users of expiration

* Add auto_recharge column to ProMembership

* Add ProMemberships::Biller (incomplete)

* Fix specs

* Add ProMembership to Administrate

* Fix spec

* Add has_enough_credits? to User and Organization

* Add Payments::Customer class

* Finish ProMembership::Biller functionality

* Fix ProMemberships::Creator check for credits

* Disable destroy actions for ProMembershipsController

* Correctly authenticate ProMembershipsController actions

* Make sure only pro user's history can be indexed

* Add ProMembershipsController#update action for auto recharge

* Use regular AR to save new credits and add touch to the purchaser

* Clarify Pro membership create policy

* Display information about an existing pro membership

* Add UI to show page

* Add system test for Pro membership creation

* Implement edit membership

* Make sure users with pro memberships can access history and dashboard pro

* Fix padding issue

* Show a different text for a user that has credits but not enough for Pro

* Move Pro Membership functionality inside settings

* Update Pro Membership link in email notifications

* Bust all relevant caches

* Add the Pro checkmark around the website

* Use Users::ResaveArticlesJob instead of delay

* Add/remove user from pro-members chat channel

* Use the appropriate Pro checkmark

* Remove unfinished pro elements

* Remove checkmark JS
2019-09-24 10:38:54 -07:00

193 lines
6 KiB
Ruby

require "rails_helper"
RSpec.describe Organization, type: :model do
let(:user) { create(:user) }
let(:organization) { create(:organization) }
it { is_expected.to have_many(:sponsorships) }
it { is_expected.to have_many(:organization_memberships).dependent(:delete_all) }
describe "#name" do
it "rejects names with over 50 characters" do
organization.name = Faker::Lorem.characters(number: 51)
expect(organization).not_to be_valid
end
it "accepts names with 50 or less characters" do
expect(organization).to be_valid
end
end
describe "#summary" do
it "rejects summaries with over 1000 characters" do
organization.summary = Faker::Lorem.characters(number: 1001)
expect(organization).not_to be_valid
end
it "accepts summaries with 1000 or less characters" do
expect(organization).to be_valid
end
end
describe "#text_color_hex" do
it "accepts hex color codes" do
organization.text_color_hex = Faker::Color.hex_color
expect(organization).to be_valid
end
it "rejects color names" do
organization.text_color_hex = Faker::Color.color_name
expect(organization).not_to be_valid
end
it "rejects RGB colors" do
organization.text_color_hex = Faker::Color.rgb_color
expect(organization).not_to be_valid
end
it "rejects wrong color format" do
organization.text_color_hex = "##{Faker::Lorem.words(number: 4)}"
expect(organization).not_to be_valid
end
end
describe "#slug" do
it "accepts properly formatted slug" do
organization.slug = "heyho"
expect(organization).to be_valid
end
it "accepts properly formatted slug with numbers" do
organization.slug = "HeyHo2"
expect(organization).to be_valid
end
it "rejects slug with spaces" do
organization.slug = "hey ho"
expect(organization).not_to be_valid
end
it "rejects slug with unacceptable character" do
organization.slug = "Hey&Ho"
expect(organization).not_to be_valid
end
it "downcases slug" do
organization.slug = "HaHaHa"
organization.save
expect(organization.slug).to eq("hahaha")
end
it "rejects reserved slug" do
organization = build(:organization, slug: "settings")
expect(organization).not_to be_valid
expect(organization.errors[:slug].to_s.include?("reserved")).to be true
end
it "takes organization slug into account " do
create(:user, username: "lightalloy")
organization = build(:organization, slug: "lightalloy")
expect(organization).not_to be_valid
expect(organization.errors[:slug].to_s.include?("taken")).to be true
end
it "takes podcast slug into account" do
create(:podcast, slug: "devpodcast")
organization = build(:organization, slug: "devpodcast")
expect(organization).not_to be_valid
expect(organization.errors[:slug].to_s.include?("taken")).to be true
end
it "takes page slug into account" do
create(:page, slug: "needed_info_for_site")
organization = build(:organization, slug: "needed_info_for_site")
expect(organization).not_to be_valid
expect(organization.errors[:slug].to_s.include?("taken")).to be true
end
it "triggers cache busting on save" do
expect { build(:organization).save }.to have_enqueued_job.on_queue("organizations_bust_cache")
end
end
describe "#url" do
it "accepts valid http url" do
organization.url = "http://ben.com"
expect(organization).to be_valid
end
it "accepts valid secure http url" do
organization.url = "https://ben.com"
expect(organization).to be_valid
end
it "rejects invalid http url" do
organization.url = "ben.com"
expect(organization).not_to be_valid
end
end
describe "#check_for_slug_change" do
def create_article_for_organization
user.update(organization_id: organization.id, org_admin: true)
create(:article, organization_id: organization.id, user_id: user.id)
end
it "properly updates the slug/username" do
random_new_slug = "slug_#{rand(10_000)}"
organization.update(slug: random_new_slug)
expect(organization.slug).to eq random_new_slug
end
it "updates old_slug to original slug if slug was changed" do
original_slug = organization.slug
organization.update(slug: "slug_#{rand(10_000)}")
expect(organization.old_slug).to eq original_slug
end
it "updates old_old_slug properly if slug was changed and there was an old_slug" do
original_slug = organization.slug
organization.update(slug: "something_else")
organization.update(slug: "another_slug")
expect(organization.old_old_slug).to eq original_slug
end
it "updates the paths of the organization's articles" do
create_article_for_organization
new_slug = "slug_#{rand(10_000)}"
organization.update(slug: new_slug)
article = Article.find_by(organization_id: organization.id)
expect(article.path).to include new_slug
end
it "updates article cached_organizations" do
create_article_for_organization
new_slug = "slug_#{rand(10_000)}"
organization.update(slug: new_slug)
article = Article.find_by(organization_id: organization.id)
expect(article.cached_organization.slug).to eq new_slug
end
end
describe "#has_enough_credits?" do
it "returns false if the user has less unspent credits than neeed" do
expect(organization.has_enough_credits?(1)).to be(false)
end
it "returns true if the user has the exact amount of unspent credits" do
create(:credit, organization: organization, spent: false)
expect(organization.has_enough_credits?(1)).to be(true)
end
it "returns true if the user has more unspent credits than needed" do
create_list(:credit, 2, organization: organization, spent: false)
expect(organization.has_enough_credits?(1)).to be(true)
end
end
describe "#pro?" do
it "always returns false" do
expect(organization.pro?).to be(false)
end
end
end