| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require "rails_helper" |
| 4 | |
| 5 | # Unit coverage for the admin read models — exercised directly, without HTTP, so |
| 6 | # the query/rollup logic is pinned independently of the controllers and views. |
| 7 | RSpec.describe "Admin read models" do |
| 8 | def make_user(n, **attrs) |
| 9 | User.create!(smbcloud_id: 800_000 + n, email: "u#{n}@example.com", username: "user-#{n}", **attrs) |
| 10 | end |
| 11 | |
| 12 | describe Admin::UserSearch do |
| 13 | it "filters to admins only" do |
| 14 | make_user(1) |
| 15 | admin = make_user(2, admin: true) |
| 16 | result = described_class.new(filter: "admins") |
| 17 | expect(result.records).to contain_exactly(admin) |
| 18 | expect(result.total).to eq(1) |
| 19 | end |
| 20 | |
| 21 | it "matches on username, email, or display name (case-insensitive)" do |
| 22 | match = make_user(3, display_name: "Ada Lovelace") |
| 23 | make_user(4) |
| 24 | expect(described_class.new(q: "ADA").records).to include(match) |
| 25 | expect(described_class.new(q: "user-3").records).to include(match) |
| 26 | end |
| 27 | |
| 28 | it "paginates and reports has_next?" do |
| 29 | (1..(Admin::UserSearch::PER_PAGE + 5)).each { |n| make_user(n) } |
| 30 | page1 = described_class.new({}) |
| 31 | expect(page1.records.size).to eq(Admin::UserSearch::PER_PAGE) |
| 32 | expect(page1.has_next?).to be(true) |
| 33 | expect(described_class.new(page: 2).has_next?).to be(false) |
| 34 | end |
| 35 | end |
| 36 | |
| 37 | describe Admin::RepositorySearch do |
| 38 | it "filters by kind and privacy" do |
| 39 | owner = make_user(10) |
| 40 | code = owner.repositories.create!(name: "code-repo", disk_path: "/tmp/a.git", kind: "code") |
| 41 | model = owner.repositories.create!(name: "model-repo", disk_path: "/tmp/b.git", kind: "model") |
| 42 | priv = owner.repositories.create!(name: "secret", disk_path: "/tmp/c.git", kind: "code", is_private: true) |
| 43 | |
| 44 | expect(described_class.new(kind: "model").records).to contain_exactly(model) |
| 45 | expect(described_class.new(filter: "private").records).to contain_exactly(priv) |
| 46 | expect(described_class.new(q: "code-repo").records).to contain_exactly(code) |
| 47 | end |
| 48 | end |
| 49 | |
| 50 | describe Admin::SubscriptionSearch do |
| 51 | it "breaks down by plan and status and filters" do |
| 52 | make_user(20).create_subscription!(plan: :pro, status: :active) |
| 53 | make_user(21).create_subscription!(plan: :team, status: :trialing) |
| 54 | make_user(22).create_subscription!(plan: :pro, status: :canceled) |
| 55 | |
| 56 | search = described_class.new({}) |
| 57 | expect(search.by_plan).to eq("pro" => 2, "team" => 1) |
| 58 | expect(search.by_status["active"]).to eq(1) |
| 59 | expect(described_class.new(status: "active").records.size).to eq(1) |
| 60 | expect(described_class.new(plan: "team").records.size).to eq(1) |
| 61 | end |
| 62 | end |
| 63 | |
| 64 | describe Admin::DashboardMetrics do |
| 65 | it "rolls up growth, catalog, and paying counts" do |
| 66 | make_user(30) |
| 67 | make_user(31, admin: true) |
| 68 | make_user(32).create_subscription!(plan: :pro, status: :active) |
| 69 | |
| 70 | m = described_class.new |
| 71 | expect(m.total_users).to eq(3) |
| 72 | expect(m.admins_count).to eq(1) |
| 73 | expect(m.paying("pro")).to eq(1) |
| 74 | expect(m.estimated_mrr).to eq(20) |
| 75 | end |
| 76 | end |
| 77 | end |