|
20 | 20 | subject
|
21 | 21 | end
|
22 | 22 |
|
| 23 | + let(:post_res) { Wpxf::Net::HttpResponse.new(nil) } |
| 24 | + |
23 | 25 | before :each do
|
24 | 26 | res = Wpxf::Net::HttpResponse.new(nil)
|
25 | 27 | res.body = body
|
26 | 28 | res.code = code
|
27 | 29 |
|
28 | 30 | 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) |
29 | 34 | end
|
30 | 35 |
|
31 | 36 | describe '#fetch_plugin_upload_nonce' do
|
|
44 | 49 | expect(script).to match(/\*\sPlugin\sName:\stest/)
|
45 | 50 | expect(script).to match(/\*\sVersion:\s[0-9]\.[0-9]\.[0-9]{2}/)
|
46 | 51 | 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}) |
48 | 53 | end
|
49 | 54 | end
|
50 | 55 |
|
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 |
56 | 87 | end
|
57 | 88 |
|
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 |
74 | 95 | end
|
| 96 | + end |
| 97 | + end |
75 | 98 |
|
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 |
78 | 106 | end
|
79 | 107 |
|
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 |
86 | 129 | end
|
| 130 | + end |
87 | 131 |
|
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 |
90 | 139 | end
|
91 | 140 | end
|
92 | 141 |
|
93 | 142 | describe '#upload_payload_as_plugin_and_execute' do
|
94 | 143 | 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) |
98 | 183 | end
|
99 | 184 | end
|
100 | 185 |
|
101 | 186 | context 'when the execution returns status 200' do
|
102 | 187 | let(:code) { 200 }
|
103 | 188 | let(:body) { 'res content' }
|
104 |
| - it 'emits the response content' do |
| 189 | + |
| 190 | + it 'should emit the response content' do |
105 | 191 | allow(subject).to receive(:upload_payload_as_plugin).and_return true
|
106 | 192 |
|
107 | 193 | emitted_content = false
|
|
115 | 201 | end
|
116 | 202 |
|
117 | 203 | 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 |
119 | 205 | allow(subject).to receive(:upload_payload_as_plugin).and_return true
|
120 | 206 | res = subject.upload_payload_as_plugin_and_execute('', '', '')
|
121 | 207 | expect(res).to be_kind_of Wpxf::Net::HttpResponse
|
|
0 commit comments