|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../../../../../spec_helper' |
| 4 | +require 'wpxf/modules' |
| 5 | + |
| 6 | +describe Wpxf::Auxiliary::WpMarketplaceV24FileDownload do |
| 7 | + let(:subject) { described_class.new } |
| 8 | + let(:post_res) { Wpxf::Net::HttpResponse.new(nil) } |
| 9 | + let(:session_cookie) { double('cookie') } |
| 10 | + |
| 11 | + before :each, 'setup subject' do |
| 12 | + allow(subject).to receive(:check_plugin_version_from_changelog) |
| 13 | + allow(subject).to receive(:emit_error) |
| 14 | + allow(subject).to receive(:emit_info) |
| 15 | + allow(subject).to receive(:execute_post_request).and_return(post_res) |
| 16 | + allow(subject).to receive(:session_cookie).and_return(session_cookie) |
| 17 | + allow(subject).to receive(:fetch_ajax_nonce).and_call_original |
| 18 | + end |
| 19 | + |
| 20 | + it 'should return a Wpxf::Module' do |
| 21 | + expect(subject).to be_a Wpxf::Module |
| 22 | + end |
| 23 | + |
| 24 | + it 'should check the plugin version is < 2.4.1' do |
| 25 | + subject.check |
| 26 | + expect(subject).to have_received(:check_plugin_version_from_changelog) |
| 27 | + .with('wpmarketplace', 'readme.txt', '2.4.1') |
| 28 | + .exactly(1).times |
| 29 | + end |
| 30 | + |
| 31 | + it 'should require authentication' do |
| 32 | + expect(subject.requires_authentication).to be true |
| 33 | + end |
| 34 | + |
| 35 | + it 'should configure the default remote file path' do |
| 36 | + expected = '../../../wp-config.php' |
| 37 | + expect(subject.default_remote_file_path).to eql expected |
| 38 | + end |
| 39 | + |
| 40 | + it 'should configure the working directory' do |
| 41 | + expected = 'wp-content/plugins/wpmarketplace' |
| 42 | + expect(subject.working_directory).to eql expected |
| 43 | + end |
| 44 | + |
| 45 | + it 'should make a POST request to start the download' do |
| 46 | + expect(subject.download_request_method).to eql :post |
| 47 | + end |
| 48 | + |
| 49 | + it 'should configure the downloader url' do |
| 50 | + expect(subject.downloader_url).to eql subject.full_uri |
| 51 | + end |
| 52 | + |
| 53 | + describe 'before the download begins' do |
| 54 | + it 'should attempt to modify the plugin permissions' do |
| 55 | + subject.before_download |
| 56 | + expect(subject).to have_received(:execute_post_request) |
| 57 | + .with( |
| 58 | + url: subject.full_uri, |
| 59 | + body: { |
| 60 | + 'action' => 'wpmp_pp_ajax_call', |
| 61 | + 'execute' => 'wpmp_save_settings', |
| 62 | + '_wpmp_settings[user_role][]' => 'subscriber' |
| 63 | + }, |
| 64 | + cookie: session_cookie |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + context 'if the plugin permissions cannot be changed' do |
| 69 | + it 'should emit an error' do |
| 70 | + post_res.code = 404 |
| 71 | + subject.before_download |
| 72 | + expect(subject).to have_received(:emit_error) |
| 73 | + .with('Failed to modify the plugin permissions') |
| 74 | + .exactly(1).times |
| 75 | + end |
| 76 | + |
| 77 | + it 'should fail the module execution' do |
| 78 | + post_res.code = 404 |
| 79 | + res = subject.before_download |
| 80 | + expect(res).to be false |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context 'if the plugin permissions are successfully changed' do |
| 85 | + before :each, 'setup mock' do |
| 86 | + post_res.code = 200 |
| 87 | + post_res.body = 'Settings Saved Successfully' |
| 88 | + end |
| 89 | + |
| 90 | + it 'should emit a verbose info message' do |
| 91 | + subject.before_download |
| 92 | + expect(subject).to have_received(:emit_info) |
| 93 | + .with('Modified plugin permissions successfully', true) |
| 94 | + .exactly(1).times |
| 95 | + end |
| 96 | + |
| 97 | + it 'should attempt to acquire an AJAX nonce' do |
| 98 | + subject.before_download |
| 99 | + expect(subject).to have_received(:fetch_ajax_nonce) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context 'if an AJAX nonce cannot be acquired' do |
| 104 | + before :each, 'setup stub responses' do |
| 105 | + plugin_modification_res = Wpxf::Net::HttpResponse.new(nil) |
| 106 | + nonce_res = Wpxf::Net::HttpResponse.new(nil) |
| 107 | + |
| 108 | + plugin_modification_res.code = 200 |
| 109 | + plugin_modification_res.body = 'Settings Saved Successfully' |
| 110 | + |
| 111 | + nonce_res.code = 404 |
| 112 | + |
| 113 | + allow(subject).to receive(:execute_post_request) |
| 114 | + .and_return(plugin_modification_res, nonce_res) |
| 115 | + end |
| 116 | + |
| 117 | + it 'should emit an error' do |
| 118 | + post_res.code = 404 |
| 119 | + subject.before_download |
| 120 | + expect(subject).to have_received(:emit_error) |
| 121 | + .with('Failed to acquire a download nonce') |
| 122 | + .exactly(1).times |
| 123 | + end |
| 124 | + |
| 125 | + it 'should fail the module execution' do |
| 126 | + res = subject.before_download |
| 127 | + expect(res).to be false |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + context 'if an AJAX nonce is acquired' do |
| 132 | + before :each, 'setup stub responses' do |
| 133 | + plugin_modification_res = Wpxf::Net::HttpResponse.new(nil) |
| 134 | + nonce_res = Wpxf::Net::HttpResponse.new(nil) |
| 135 | + |
| 136 | + plugin_modification_res.code = 200 |
| 137 | + plugin_modification_res.body = 'Settings Saved Successfully' |
| 138 | + |
| 139 | + nonce_res.code = 200 |
| 140 | + nonce_res.body = '<input name="__product_wpmp" value="nonce_value">' |
| 141 | + |
| 142 | + allow(subject).to receive(:execute_post_request) |
| 143 | + .and_return(plugin_modification_res, nonce_res) |
| 144 | + end |
| 145 | + |
| 146 | + it 'should emit a verbose info message containing the nonce' do |
| 147 | + subject.before_download |
| 148 | + expect(subject).to have_received(:emit_info) |
| 149 | + .with('Acquired nonce "nonce_value"', true) |
| 150 | + .exactly(1).times |
| 151 | + end |
| 152 | + |
| 153 | + it 'should attempt to create a product that exposes the target file' do |
| 154 | + allow(subject).to receive(:create_product).and_call_original |
| 155 | + subject.before_download |
| 156 | + expect(subject).to have_received(:create_product) |
| 157 | + end |
| 158 | + |
| 159 | + it 'should create a random download ID to be used in the download request parameters' do |
| 160 | + subject.before_download |
| 161 | + expect(subject.download_id).to match(/1[0-9]{5}/) |
| 162 | + expect(subject.download_request_params).to eql('wpmpfile' => subject.download_id) |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + context 'if the product creation fails' do |
| 167 | + before :each, 'setup stub responses' do |
| 168 | + plugin_modification_res = Wpxf::Net::HttpResponse.new(nil) |
| 169 | + nonce_res = Wpxf::Net::HttpResponse.new(nil) |
| 170 | + product_res = Wpxf::Net::HttpResponse.new(nil) |
| 171 | + |
| 172 | + plugin_modification_res.code = 200 |
| 173 | + plugin_modification_res.body = 'Settings Saved Successfully' |
| 174 | + |
| 175 | + nonce_res.code = 200 |
| 176 | + nonce_res.body = '<input name="__product_wpmp" value="nonce_value">' |
| 177 | + |
| 178 | + product_res.code = 404 |
| 179 | + |
| 180 | + allow(subject).to receive(:execute_post_request) |
| 181 | + .and_return( |
| 182 | + plugin_modification_res, |
| 183 | + nonce_res, |
| 184 | + product_res |
| 185 | + ) |
| 186 | + end |
| 187 | + |
| 188 | + it 'should emit an error' do |
| 189 | + subject.before_download |
| 190 | + expect(subject).to have_received(:emit_error) |
| 191 | + .with('Failed to create dummy product') |
| 192 | + .exactly(1).times |
| 193 | + end |
| 194 | + |
| 195 | + it 'should fail the module execution' do |
| 196 | + res = subject.before_download |
| 197 | + expect(res).to be false |
| 198 | + end |
| 199 | + end |
| 200 | + |
| 201 | + context 'if the product is crated' do |
| 202 | + before :each, 'setup stub responses' do |
| 203 | + plugin_modification_res = Wpxf::Net::HttpResponse.new(nil) |
| 204 | + nonce_res = Wpxf::Net::HttpResponse.new(nil) |
| 205 | + product_res = Wpxf::Net::HttpResponse.new(nil) |
| 206 | + |
| 207 | + plugin_modification_res.code = 200 |
| 208 | + plugin_modification_res.body = 'Settings Saved Successfully' |
| 209 | + |
| 210 | + nonce_res.code = 200 |
| 211 | + nonce_res.body = '<input name="__product_wpmp" value="nonce_value">' |
| 212 | + |
| 213 | + product_res.code = 200 |
| 214 | + |
| 215 | + allow(subject).to receive(:execute_post_request) |
| 216 | + .and_return( |
| 217 | + plugin_modification_res, |
| 218 | + nonce_res, |
| 219 | + product_res |
| 220 | + ) |
| 221 | + end |
| 222 | + |
| 223 | + it 'should pass the pre-download checks' do |
| 224 | + res = subject.before_download |
| 225 | + expect(res).to be true |
| 226 | + end |
| 227 | + end |
| 228 | + end |
| 229 | +end |
0 commit comments