|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Internal Organizations', type: :request do |
| 6 | + let(:organization) { create(:organization, tier: 'tier_3_amateur', subscription_plan: 'free', subscription_status: 'trial') } |
| 7 | + let(:user) { create(:user, organization: organization) } |
| 8 | + let(:secret) { ENV.fetch('INTERNAL_JWT_SECRET', 'test_internal_secret') } |
| 9 | + let(:auth_headers) { { 'Authorization' => "Bearer #{secret}", 'Content-Type' => 'application/json' } } |
| 10 | + |
| 11 | + describe 'PATCH /internal/organizations/by_user/:user_id/tier' do |
| 12 | + context 'without Authorization header' do |
| 13 | + it 'returns 401' do |
| 14 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 15 | + params: { tier: 'tier_2_semi_pro', subscription_plan: 'semi_pro', subscription_status: 'active' }.to_json, |
| 16 | + headers: { 'Content-Type' => 'application/json' } |
| 17 | + |
| 18 | + expect(response).to have_http_status(:unauthorized) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context 'with wrong secret' do |
| 23 | + it 'returns 401' do |
| 24 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 25 | + params: { tier: 'tier_2_semi_pro', subscription_plan: 'semi_pro', subscription_status: 'active' }.to_json, |
| 26 | + headers: auth_headers.merge('Authorization' => 'Bearer wrong_secret') |
| 27 | + |
| 28 | + expect(response).to have_http_status(:unauthorized) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context 'when user does not exist' do |
| 33 | + it 'returns 404' do |
| 34 | + patch '/internal/organizations/by_user/99999999/tier', |
| 35 | + params: { tier: 'tier_2_semi_pro', subscription_plan: 'semi_pro', subscription_status: 'active' }.to_json, |
| 36 | + headers: auth_headers |
| 37 | + |
| 38 | + expect(response).to have_http_status(:not_found) |
| 39 | + expect(json_response[:error]).to eq('user not found') |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context 'with invalid tier' do |
| 44 | + it 'returns 422' do |
| 45 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 46 | + params: { tier: 'tier_9_invalid', subscription_plan: 'semi_pro', subscription_status: 'active' }.to_json, |
| 47 | + headers: auth_headers |
| 48 | + |
| 49 | + expect(response).to have_http_status(:unprocessable_entity) |
| 50 | + expect(json_response[:error]).to include('invalid tier') |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context 'on subscription activation (pro_monthly)' do |
| 55 | + it 'upgrades the organization to tier_2_semi_pro' do |
| 56 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 57 | + params: { tier: 'tier_2_semi_pro', subscription_plan: 'semi_pro', subscription_status: 'active' }.to_json, |
| 58 | + headers: auth_headers |
| 59 | + |
| 60 | + expect(response).to have_http_status(:ok) |
| 61 | + |
| 62 | + org = organization.reload |
| 63 | + expect(org.tier).to eq('tier_2_semi_pro') |
| 64 | + expect(org.subscription_plan).to eq('semi_pro') |
| 65 | + expect(org.subscription_status).to eq('active') |
| 66 | + end |
| 67 | + |
| 68 | + it 'returns the updated organization data' do |
| 69 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 70 | + params: { tier: 'tier_2_semi_pro', subscription_plan: 'semi_pro', subscription_status: 'active' }.to_json, |
| 71 | + headers: auth_headers |
| 72 | + |
| 73 | + data = json_response[:data] |
| 74 | + expect(data[:id]).to eq(organization.id) |
| 75 | + expect(data[:tier]).to eq('tier_2_semi_pro') |
| 76 | + expect(data[:subscription_status]).to eq('active') |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + context 'on subscription cancellation' do |
| 81 | + before do |
| 82 | + organization.update!(tier: 'tier_2_semi_pro', subscription_plan: 'semi_pro', subscription_status: 'active') |
| 83 | + end |
| 84 | + |
| 85 | + it 'downgrades the organization to tier_3_amateur' do |
| 86 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 87 | + params: { tier: 'tier_3_amateur', subscription_plan: 'free', subscription_status: 'cancelled' }.to_json, |
| 88 | + headers: auth_headers |
| 89 | + |
| 90 | + expect(response).to have_http_status(:ok) |
| 91 | + |
| 92 | + org = organization.reload |
| 93 | + expect(org.tier).to eq('tier_3_amateur') |
| 94 | + expect(org.subscription_plan).to eq('free') |
| 95 | + expect(org.subscription_status).to eq('cancelled') |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context 'on enterprise activation' do |
| 100 | + it 'upgrades the organization to tier_1_professional' do |
| 101 | + patch "/internal/organizations/by_user/#{user.id}/tier", |
| 102 | + params: { tier: 'tier_1_professional', subscription_plan: 'enterprise', subscription_status: 'active' }.to_json, |
| 103 | + headers: auth_headers |
| 104 | + |
| 105 | + expect(response).to have_http_status(:ok) |
| 106 | + expect(organization.reload.tier).to eq('tier_1_professional') |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments