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

Commit d030e27

Browse files
committed
Update unit tests
1 parent 386e877 commit d030e27

File tree

2 files changed

+124
-38
lines changed

2 files changed

+124
-38
lines changed

lib/wpxf/wordpress/plugin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def upload_payload_as_plugin(name, payload_name, cookie)
2424
res&.code == 200 && res.body !~ /plugin installation failed/i
2525
end
2626

27-
# Create and upload the payload without packaging it in a ZIP file.
27+
# Upload the payload via the plugin form without packaging it in a ZIP file.
2828
# @param payload_name [String] the name the payload should use on the server.
2929
# @param cookie [String] a valid admin session cookie.
3030
# @return [Boolean] true on success, false on error.

spec/lib/wpxf/wordpress/plugin_spec.rb

Lines changed: 123 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020
subject
2121
end
2222

23+
let(:post_res) { Wpxf::Net::HttpResponse.new(nil) }
24+
2325
before :each do
2426
res = Wpxf::Net::HttpResponse.new(nil)
2527
res.body = body
2628
res.code = code
2729

2830
allow(subject).to receive(:execute_get_request).and_return(res)
31+
allow(subject).to receive(:upload_payload_using_plugin_form).and_call_original
32+
allow(subject).to receive(:execute_post_request).and_return(post_res)
33+
allow(subject).to receive(:emit_error)
2934
end
3035

3136
describe '#fetch_plugin_upload_nonce' do
@@ -44,64 +49,145 @@
4449
expect(script).to match(/\*\sPlugin\sName:\stest/)
4550
expect(script).to match(/\*\sVersion:\s[0-9]\.[0-9]\.[0-9]{2}/)
4651
expect(script).to match(/\*\sAuthor:\s[a-zA-Z]{10}/)
47-
expect(script).to match(/\*\sAuthor\sURI:\shttp:\/\/[a-zA-Z]{10}\.com/)
52+
expect(script).to match(%r{\*\sAuthor\sURI:\shttp://[a-zA-Z]{10}\.com})
4853
end
4954
end
5055

51-
describe '#wordpress_upload_plugin' do
52-
it 'returns false if an upload nonce cannot be retrieved' do
53-
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return nil
54-
res = subject.upload_payload_as_plugin('test', 'test', 'cookie')
55-
expect(res).to be false
56+
describe '#upload_payload_as_plugin' do
57+
context 'if an upload nonce cannot be retrieved' do
58+
it 'should return false' do
59+
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return nil
60+
res = subject.upload_payload_as_plugin('test', 'test', 'cookie')
61+
expect(res).to be false
62+
end
63+
end
64+
65+
context 'if an upload is successful' do
66+
it 'should return true ' do
67+
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return 'a'
68+
allow(subject).to receive(:execute_post_request) do |opts|
69+
expect(opts[:url]).to eq subject.wordpress_url_admin_update
70+
expect(opts[:params]).to eq('action' => 'upload-plugin')
71+
expect(opts[:cookie]).to eq 'cookie'
72+
expect(opts[:body]).to include(
73+
'_wpnonce',
74+
'_wp_http_referer',
75+
'pluginzip',
76+
'install-plugin-submit'
77+
)
78+
79+
res = Wpxf::Net::HttpResponse.new(nil)
80+
res.code = 200
81+
res
82+
end
83+
84+
res = subject.upload_payload_as_plugin('test', 'test', 'cookie')
85+
expect(res).to be true
86+
end
5687
end
5788

58-
it 'returns true if an upload is successful' do
59-
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return 'a'
60-
allow(subject).to receive(:execute_post_request) do |opts|
61-
expect(opts[:url]).to eq subject.wordpress_url_admin_update
62-
expect(opts[:params]).to eq('action' => 'upload-plugin')
63-
expect(opts[:cookie]).to eq 'cookie'
64-
expect(opts[:body]).to include(
65-
'_wpnonce',
66-
'_wp_http_referer',
67-
'pluginzip',
68-
'install-plugin-submit'
69-
)
70-
71-
res = Wpxf::Net::HttpResponse.new(nil)
72-
res.code = 200
73-
res
89+
context 'if the response code is not 200' do
90+
it 'should return false' do
91+
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return 'a'
92+
post_res.code = 404
93+
res = subject.upload_payload_as_plugin('test', 'test', 'cookie')
94+
expect(res).to be false
7495
end
96+
end
97+
end
7598

76-
res = subject.upload_payload_as_plugin('test', 'test', 'cookie')
77-
expect(res).to be true
99+
describe '#upload_payload_using_plugin_form' do
100+
context 'if an upload nonce cannot be retrieved' do
101+
it 'should return false' do
102+
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return nil
103+
res = subject.upload_payload_using_plugin_form('test', 'cookie')
104+
expect(res).to be false
105+
end
78106
end
79107

80-
it 'returns false if the response code is not 200' do
81-
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return 'a'
82-
allow(subject).to receive(:execute_post_request) do
83-
res = Wpxf::Net::HttpResponse.new(nil)
84-
res.code = 404
85-
res
108+
context 'if an upload is successful' do
109+
it 'should return true ' do
110+
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return 'a'
111+
allow(subject).to receive(:execute_post_request) do |opts|
112+
expect(opts[:url]).to eq subject.wordpress_url_admin_update
113+
expect(opts[:params]).to eq('action' => 'upload-plugin')
114+
expect(opts[:cookie]).to eq 'cookie'
115+
expect(opts[:body]).to include(
116+
'_wpnonce',
117+
'_wp_http_referer',
118+
'pluginzip',
119+
'install-plugin-submit'
120+
)
121+
122+
res = Wpxf::Net::HttpResponse.new(nil)
123+
res.code = 200
124+
res
125+
end
126+
127+
res = subject.upload_payload_using_plugin_form('test', 'cookie')
128+
expect(res).to be true
86129
end
130+
end
87131

88-
res = subject.upload_payload_as_plugin('test', 'test', 'cookie')
89-
expect(res).to be false
132+
context 'if the response code is not 200' do
133+
it 'should return false' do
134+
allow(subject).to receive(:fetch_plugin_upload_nonce).and_return 'a'
135+
post_res.code = 404
136+
res = subject.upload_payload_using_plugin_form('test', 'cookie')
137+
expect(res).to be false
138+
end
90139
end
91140
end
92141

93142
describe '#upload_payload_as_plugin_and_execute' do
94143
context 'when the plugin fails to upload' do
95-
it 'returns nil' do
96-
res = subject.upload_payload_as_plugin_and_execute('', '', '')
97-
expect(res).to be_nil
144+
it 'should attempt to upload the unpackaged payload' do
145+
subject.upload_payload_as_plugin_and_execute('plugin_name', 'payload_name', 'cookie')
146+
expect(subject).to have_received(:upload_payload_using_plugin_form)
147+
.with('payload_name', 'cookie')
148+
.exactly(1).times
149+
end
150+
151+
context 'if both upload attempts fail' do
152+
it 'should return nil' do
153+
res = subject.upload_payload_as_plugin_and_execute('', '', '')
154+
expect(res).to be_nil
155+
end
156+
157+
it 'should emit an error' do
158+
subject.upload_payload_as_plugin_and_execute('', '', '')
159+
expect(subject).to have_received(:emit_error)
160+
.with('Failed to upload the payload')
161+
.exactly(1).times
162+
end
163+
end
164+
end
165+
166+
context 'if the payload was not packaged as a plugin' do
167+
it 'should attempt to execute it from the uploads directory' do
168+
expected_url = "http://127.0.0.1/wp/wp-content/uploads/#{Time.now.strftime('%Y')}/#{Time.now.strftime('%m')}/test.php"
169+
allow(subject).to receive(:upload_payload_using_plugin_form).and_return(true)
170+
subject.upload_payload_as_plugin_and_execute('test', 'test', 'cookie')
171+
expect(subject).to have_received(:execute_get_request)
172+
.with(url: expected_url)
173+
end
174+
end
175+
176+
context 'if the payload was packaged as a plugin' do
177+
it 'should attempt to execute it from the plugins directory' do
178+
expected_url = 'http://127.0.0.1/wp/wp-content/plugins/plugin_name/payload_name.php'
179+
allow(subject).to receive(:upload_payload_as_plugin).and_return(true)
180+
subject.upload_payload_as_plugin_and_execute('plugin_name', 'payload_name', 'cookie')
181+
expect(subject).to have_received(:execute_get_request)
182+
.with(url: expected_url)
98183
end
99184
end
100185

101186
context 'when the execution returns status 200' do
102187
let(:code) { 200 }
103188
let(:body) { 'res content' }
104-
it 'emits the response content' do
189+
190+
it 'should emit the response content' do
105191
allow(subject).to receive(:upload_payload_as_plugin).and_return true
106192

107193
emitted_content = false
@@ -115,7 +201,7 @@
115201
end
116202

117203
context 'when the payload is executed' do
118-
it 'returns the HttpResponse of the payload request' do
204+
it 'should return the HttpResponse of the payload request' do
119205
allow(subject).to receive(:upload_payload_as_plugin).and_return true
120206
res = subject.upload_payload_as_plugin_and_execute('', '', '')
121207
expect(res).to be_kind_of Wpxf::Net::HttpResponse

0 commit comments

Comments
 (0)