|
30 | 30 |
|
31 | 31 | before do |
32 | 32 | described_class.reset! |
33 | | - # Stub the public key to use our test key |
34 | | - allow(ReactOnRailsPro::LicensePublicKey).to receive(:KEY).and_return(test_public_key) |
| 33 | + # Stub the public key constant to use our test key |
| 34 | + stub_const("ReactOnRailsPro::LicensePublicKey::KEY", test_public_key) |
35 | 35 | # Clear ENV variable |
36 | 36 | ENV.delete("REACT_ON_RAILS_PRO_LICENSE") |
37 | 37 | end |
|
65 | 65 | ENV["REACT_ON_RAILS_PRO_LICENSE"] = expired_token |
66 | 66 | end |
67 | 67 |
|
68 | | - it "returns false in production" do |
| 68 | + it "raises error in production" do |
69 | 69 | allow(Rails.env).to receive(:development?).and_return(false) |
70 | 70 | allow(Rails.env).to receive(:test?).and_return(false) |
71 | | - expect(described_class.valid?).to be false |
| 71 | + expect { described_class.valid? }.to raise_error(ReactOnRailsPro::Error, /License has expired/) |
72 | 72 | end |
73 | 73 |
|
74 | 74 | it "returns true in development with warning" do |
|
78 | 78 | end |
79 | 79 | end |
80 | 80 |
|
| 81 | + context "with license missing exp field" do |
| 82 | + let(:payload_without_exp) do |
| 83 | + { |
| 84 | + |
| 85 | + iat: Time.now.to_i |
| 86 | + # exp field is missing |
| 87 | + } |
| 88 | + end |
| 89 | + |
| 90 | + before do |
| 91 | + token_without_exp = JWT.encode(payload_without_exp, test_private_key, "RS256") |
| 92 | + ENV["REACT_ON_RAILS_PRO_LICENSE"] = token_without_exp |
| 93 | + end |
| 94 | + |
| 95 | + it "raises error in production" do |
| 96 | + allow(Rails.env).to receive(:development?).and_return(false) |
| 97 | + allow(Rails.env).to receive(:test?).and_return(false) |
| 98 | + expect { described_class.valid? }.to raise_error(ReactOnRailsPro::Error, /License is missing required expiration field/) |
| 99 | + end |
| 100 | + |
| 101 | + it "returns true in development with warning" do |
| 102 | + allow(Rails.env).to receive(:development?).and_return(true) |
| 103 | + expect(Rails.logger).to receive(:warn).with(/License is missing required expiration field/) |
| 104 | + expect(described_class.valid?).to be true |
| 105 | + end |
| 106 | + |
| 107 | + it "sets appropriate validation error in development" do |
| 108 | + allow(Rails.env).to receive(:development?).and_return(true) |
| 109 | + allow(Rails.logger).to receive(:warn) |
| 110 | + described_class.valid? |
| 111 | + expect(described_class.validation_error).to eq("License is missing required expiration field") |
| 112 | + end |
| 113 | + end |
| 114 | + |
81 | 115 | context "with invalid signature" do |
82 | 116 | before do |
83 | 117 | wrong_key = OpenSSL::PKey::RSA.new(2048) |
84 | 118 | invalid_token = JWT.encode(valid_payload, wrong_key, "RS256") |
85 | 119 | ENV["REACT_ON_RAILS_PRO_LICENSE"] = invalid_token |
86 | 120 | end |
87 | 121 |
|
88 | | - it "returns false in production" do |
| 122 | + it "raises error in production" do |
89 | 123 | allow(Rails.env).to receive(:development?).and_return(false) |
90 | 124 | allow(Rails.env).to receive(:test?).and_return(false) |
91 | | - expect(described_class.valid?).to be false |
| 125 | + expect { described_class.valid? }.to raise_error(ReactOnRailsPro::Error, /Invalid license signature/) |
92 | 126 | end |
93 | 127 |
|
94 | 128 | it "returns true in development with warning" do |
|
99 | 133 | end |
100 | 134 |
|
101 | 135 | context "with missing license" do |
| 136 | + let(:config_path) { double("Pathname", exist?: false) } |
| 137 | + |
102 | 138 | before do |
103 | 139 | ENV.delete("REACT_ON_RAILS_PRO_LICENSE") |
104 | | - allow(File).to receive(:read).and_raise(Errno::ENOENT) |
| 140 | + allow(Rails.root).to receive(:join).with("config", "react_on_rails_pro_license.key").and_return(config_path) |
105 | 141 | end |
106 | 142 |
|
107 | 143 | it "returns false in production with error" do |
|
152 | 188 | before do |
153 | 189 | expired_token = JWT.encode(expired_payload, test_private_key, "RS256") |
154 | 190 | ENV["REACT_ON_RAILS_PRO_LICENSE"] = expired_token |
155 | | - allow(Rails.env).to receive(:development?).and_return(false) |
156 | | - allow(Rails.env).to receive(:test?).and_return(false) |
| 191 | + allow(Rails.env).to receive(:development?).and_return(true) |
| 192 | + allow(Rails.logger).to receive(:warn) |
157 | 193 | end |
158 | 194 |
|
159 | 195 | it "returns the error message" do |
|
0 commit comments