| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require "rails_helper" |
| 4 | |
| 5 | # Covers the admin console: the require_admin! gate, that each page renders, and |
| 6 | # the grant/revoke-admin flows including the self-revoke guard. Auth normally |
| 7 | # runs through smbCloud's native gem, so we stub current_user rather than log in. |
| 8 | RSpec.describe "Admin console", type: :request do |
| 9 | let(:admin) do |
| 10 | User.create!(smbcloud_id: 900_001, email: "admin@example.com", username: "bossadmin", admin: true) |
| 11 | end |
| 12 | let(:member) do |
| 13 | User.create!(smbcloud_id: 900_002, email: "member@example.com", username: "plainuser") |
| 14 | end |
| 15 | |
| 16 | def sign_in_as(user) |
| 17 | allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) |
| 18 | end |
| 19 | |
| 20 | describe "the require_admin! gate" do |
| 21 | it "redirects a signed-out visitor to sign in" do |
| 22 | get "/admin" |
| 23 | expect(response).to redirect_to(signin_path) |
| 24 | end |
| 25 | |
| 26 | it "redirects a signed-in non-admin to root" do |
| 27 | sign_in_as(member) |
| 28 | get "/admin" |
| 29 | expect(response).to redirect_to(root_path) |
| 30 | end |
| 31 | |
| 32 | it "lets an admin in" do |
| 33 | sign_in_as(admin) |
| 34 | get "/admin" |
| 35 | expect(response).to have_http_status(:success) |
| 36 | expect(response.body).to include("Overview") |
| 37 | end |
| 38 | end |
| 39 | |
| 40 | describe "pages render for an admin" do |
| 41 | before { sign_in_as(admin) } |
| 42 | |
| 43 | it "renders the users index and finds a user by search" do |
| 44 | member |
| 45 | get admin_users_path(q: "plainuser") |
| 46 | expect(response).to have_http_status(:success) |
| 47 | expect(response.body).to include("plainuser") |
| 48 | end |
| 49 | |
| 50 | it "renders a user detail page" do |
| 51 | get admin_user_path(member.username) |
| 52 | expect(response).to have_http_status(:success) |
| 53 | expect(response.body).to include(member.email) |
| 54 | end |
| 55 | |
| 56 | it "renders the repositories index" do |
| 57 | get admin_repositories_path |
| 58 | expect(response).to have_http_status(:success) |
| 59 | end |
| 60 | |
| 61 | it "renders the subscriptions index" do |
| 62 | Subscription.create!(user: member, plan: :pro, status: :active) |
| 63 | get admin_subscriptions_path |
| 64 | expect(response).to have_http_status(:success) |
| 65 | expect(response.body).to include("plainuser") |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | describe "granting and revoking admin" do |
| 70 | before { sign_in_as(admin) } |
| 71 | |
| 72 | it "grants admin to a member" do |
| 73 | patch grant_admin_admin_user_path(member.username) |
| 74 | expect(response).to redirect_to(admin_user_path(member.username)) |
| 75 | expect(member.reload.admin?).to be(true) |
| 76 | end |
| 77 | |
| 78 | it "revokes admin from another admin" do |
| 79 | other = User.create!(smbcloud_id: 900_003, email: "o@example.com", username: "otheradmin", admin: true) |
| 80 | patch revoke_admin_admin_user_path(other.username) |
| 81 | expect(other.reload.admin?).to be(false) |
| 82 | end |
| 83 | |
| 84 | it "refuses to let an admin revoke their own access" do |
| 85 | patch revoke_admin_admin_user_path(admin.username) |
| 86 | expect(admin.reload.admin?).to be(true) |
| 87 | expect(flash[:alert]).to match(/your own admin/i) |
| 88 | end |
| 89 | end |
| 90 | |
| 91 | describe BillingMetrics do |
| 92 | it "sums list prices over active paid plans only" do |
| 93 | User.create!(smbcloud_id: 910_001, email: "p1@example.com", username: "payer-one") |
| 94 | .create_subscription!(plan: :pro, status: :active) |
| 95 | User.create!(smbcloud_id: 910_002, email: "p2@example.com", username: "payer-two") |
| 96 | .create_subscription!(plan: :team, status: :active) |
| 97 | # Trials and free plans are excluded from MRR. |
| 98 | User.create!(smbcloud_id: 910_003, email: "p3@example.com", username: "trialer") |
| 99 | .create_subscription!(plan: :pro, status: :trialing) |
| 100 | |
| 101 | expect(BillingMetrics.estimated_mrr).to eq(20 + 40) |
| 102 | end |
| 103 | end |
| 104 | end |