|
| 1 | +const notifier = require("node-notifier"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +const BASE_URL = "http://localhost"; |
| 5 | +const BOUNCER_KEY = process.env.BOUNCER_KEY; |
| 6 | +const CLIENT_IP = process.env.CS_WP_HOST; |
| 7 | +const WORDPRESS_VERSION = process.env.WORDPRESS_VERSION; |
| 8 | +const ADMIN_LOGIN = "admin"; |
| 9 | +const ADMIN_PASSWORD = "my_very_very_secret_admin_password"; |
| 10 | +const LAPI_URL = process.env.LAPI_URL_FROM_CONTAINERS; |
| 11 | +const NOTIFY = !!process.env.DEBUG; |
| 12 | +const TIMEOUT = (!!process.env.DEBUG ? 5 * 60 : 5) * 1000; |
| 13 | + |
| 14 | +const notify = (message) => { |
| 15 | + if (NOTIFY) { |
| 16 | + notifier.notify({ |
| 17 | + title: "CrowdSec automation", |
| 18 | + message: message, |
| 19 | + icon: path.join(__dirname, "../icon.png"), |
| 20 | + }); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +const { addDecision, deleteAllDecisions } = require("../utils/watcherClient"); |
| 25 | + |
| 26 | +const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 27 | + |
| 28 | +jest.setTimeout(TIMEOUT); |
| 29 | + |
| 30 | +describe(`Install Stack: WordPress ${WORDPRESS_VERSION} + the CrowdSec plugin`, () => { |
| 31 | + beforeAll(async () => {}); |
| 32 | + |
| 33 | + afterAll(async () => { |
| 34 | + return await browser.close(); |
| 35 | + }); |
| 36 | + |
| 37 | + const waitForNavigation = page.waitForNavigation(); |
| 38 | + |
| 39 | + it('Should install wordpress"', async () => { |
| 40 | + notify("Install wordpress"); |
| 41 | + |
| 42 | + // Go to home |
| 43 | + await page.goto(`${BASE_URL}`); |
| 44 | + |
| 45 | + // "Language selection" page |
| 46 | + |
| 47 | + await page.click('option[lang="en"]'); |
| 48 | + await page.click("#language-continue"); |
| 49 | + await waitForNavigation; |
| 50 | + |
| 51 | + // "Account creation" page |
| 52 | + |
| 53 | + await page.fill("#weblog_title", "My website"); |
| 54 | + await page.fill("#user_login", ADMIN_LOGIN); |
| 55 | + await page.fill("#pass1", ADMIN_PASSWORD); |
| 56 | + await page.fill("#admin_email", "admin@admin.admin"); |
| 57 | + await page.click("#submit"); |
| 58 | + await waitForNavigation; |
| 59 | + |
| 60 | + // "Success" page |
| 61 | + |
| 62 | + await expect(page).toHaveText("h1", "Success!"); |
| 63 | + await page.click(".wp-core-ui > .step > .button"); |
| 64 | + await waitForNavigation; |
| 65 | + }); |
| 66 | + |
| 67 | + it('Should login to wp-admin"', async () => { |
| 68 | + notify("Login to wp-admin"); |
| 69 | + // "Login" page |
| 70 | + await page.fill("#user_login", ADMIN_LOGIN); |
| 71 | + await page.fill("#user_pass", ADMIN_PASSWORD); |
| 72 | + await page.waitForSelector("#wp-submit"); |
| 73 | + await page.click("#wp-submit"); |
| 74 | + await waitForNavigation; |
| 75 | + }); |
| 76 | + |
| 77 | + it('Should install CrowdSec plugin"', async () => { |
| 78 | + notify("Install CrowdSec plugin"); |
| 79 | + // "Plugins" page |
| 80 | + await page.goto(`${BASE_URL}/wp-admin/plugins.php'`); |
| 81 | + await page.click("#activate-crowdsec"); |
| 82 | + await waitForNavigation; |
| 83 | + await expect(page).toHaveText("#message", "Plugin activated."); |
| 84 | + }); |
| 85 | + |
| 86 | + it('Should configure the connection details"', async () => { |
| 87 | + notify("Configure the connection details"); |
| 88 | + // CrowdSec Menu |
| 89 | + await page.click( |
| 90 | + "#adminmenuwrap > #adminmenu > #toplevel_page_crowdsec_plugin > .wp-has-submenu > .wp-menu-name" |
| 91 | + ); |
| 92 | + await waitForNavigation; |
| 93 | + |
| 94 | + // CrowdSec Settings page: set connection details |
| 95 | + |
| 96 | + await page.fill("[name=crowdsec_api_url]", LAPI_URL); |
| 97 | + await page.fill("[name=crowdsec_api_key]", BOUNCER_KEY); |
| 98 | + await page.click("[type=submit]"); |
| 99 | + await waitForNavigation; |
| 100 | + |
| 101 | + await expect(page).toHaveText( |
| 102 | + "#setting-error-settings_updated", |
| 103 | + "Settings saved." |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('Should reduce the cache durations"', async () => { |
| 108 | + notify("Reduce the cache durations"); |
| 109 | + // CrowdSec Menu |
| 110 | + await page.click( |
| 111 | + "#toplevel_page_crowdsec_plugin > ul > li:nth-child(3) > a" |
| 112 | + ); |
| 113 | + await waitForNavigation; |
| 114 | + |
| 115 | + // CrowdSec Settings page: set connection details |
| 116 | + |
| 117 | + await page.fill("[name=crowdsec_clean_ip_cache_duration]", "1"); |
| 118 | + await page.fill("[name=crowdsec_bad_ip_cache_duration]", "1"); |
| 119 | + await page.click("[type=submit]"); |
| 120 | + await waitForNavigation; |
| 121 | + |
| 122 | + await expect(page).toHaveText( |
| 123 | + "#setting-error-settings_updated", |
| 124 | + "Settings saved." |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('Should display the homepage with no remediation"', async () => { |
| 129 | + notify("Display the homepage with no remediation"); |
| 130 | + |
| 131 | + await page.goto(`${BASE_URL}`); |
| 132 | + await waitForNavigation; |
| 133 | + |
| 134 | + const title = await page.title(); |
| 135 | + await expect(title).toContain("Just another WordPress site"); |
| 136 | + }); |
| 137 | + |
| 138 | + it('Should display a captcha wall"', async () => { |
| 139 | + notify("Display a captcha wall"); |
| 140 | + |
| 141 | + // add a captcha remediation |
| 142 | + await addDecision(CLIENT_IP, "captcha", "15m"); |
| 143 | + await wait(1000); |
| 144 | + |
| 145 | + await page.goto(`${BASE_URL}`); |
| 146 | + await waitForNavigation; |
| 147 | + |
| 148 | + const title = await page.title(); |
| 149 | + await expect(title).toContain("Oops.."); |
| 150 | + await expect(page).toHaveText( |
| 151 | + ".desc", |
| 152 | + "Please complete the security check." |
| 153 | + ); |
| 154 | + |
| 155 | + // Refresh the captcha 2 times |
| 156 | + await page.click("#refresh_link"); |
| 157 | + await waitForNavigation; |
| 158 | + |
| 159 | + await page.click("#refresh_link"); |
| 160 | + await waitForNavigation; |
| 161 | + }); |
| 162 | + |
| 163 | + it('Should display a ban wall"', async () => { |
| 164 | + notify("Display a ban wall"); |
| 165 | + |
| 166 | + // add a ban remediation |
| 167 | + await addDecision(CLIENT_IP, "ban", "15m"); |
| 168 | + await wait(1000); |
| 169 | + |
| 170 | + await page.goto(`${BASE_URL}`); |
| 171 | + await waitForNavigation; |
| 172 | + |
| 173 | + const title = await page.title(); |
| 174 | + await expect(title).toContain("Oops.."); |
| 175 | + await expect(page).toHaveText( |
| 176 | + ".desc", |
| 177 | + "This page is protected against cyber attacks and your IP has been banned by our system." |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it('Should display back the homepage with no remediation"', async () => { |
| 182 | + notify("Display back the homepage with no remediation"); |
| 183 | + |
| 184 | + // delete a ban remediation |
| 185 | + await deleteAllDecisions(); |
| 186 | + await wait(1000); |
| 187 | + |
| 188 | + await page.goto(`${BASE_URL}`); |
| 189 | + await waitForNavigation; |
| 190 | + |
| 191 | + const title = await page.title(); |
| 192 | + await expect(title).toContain("Just another WordPress site"); |
| 193 | + }); |
| 194 | + |
| 195 | + it('Should enable the stream mode"', async () => { |
| 196 | + notify("Enable the stream mode"); |
| 197 | + |
| 198 | + await page.goto( |
| 199 | + `${BASE_URL}/wp-admin/admin.php?page=crowdsec_advanced_settings` |
| 200 | + ); |
| 201 | + await waitForNavigation; |
| 202 | + |
| 203 | + const title = await page.title(); |
| 204 | + await expect(title).toContain("Advanced"); |
| 205 | + |
| 206 | + await page.click("[for=crowdsec_stream_mode]"); |
| 207 | + |
| 208 | + await page.fill("[name=crowdsec_stream_mode_refresh_frequency]", "1"); |
| 209 | + await page.click("[type=submit]"); |
| 210 | + await waitForNavigation; |
| 211 | + |
| 212 | + await expect(page).toHaveText( |
| 213 | + "#setting-error-settings_updated", |
| 214 | + "Settings saved." |
| 215 | + ); |
| 216 | + }); |
| 217 | + |
| 218 | + it('Should display a ban wall via stream mode"', async () => { |
| 219 | + notify("Display a ban wall via stream mode"); |
| 220 | + |
| 221 | + // add a ban remediation |
| 222 | + await addDecision(CLIENT_IP, "ban", "15m"); |
| 223 | + await wait(2000); |
| 224 | + // force WP Cron to run cache update as bouncing is done before cache updating |
| 225 | + // This could be fixed by running homemade call to cache update |
| 226 | + // if it's the time to update cache |
| 227 | + await page.goto(`${BASE_URL}/wp-cron.php`); |
| 228 | + |
| 229 | + await page.goto(`${BASE_URL}`); |
| 230 | + await waitForNavigation; |
| 231 | + |
| 232 | + const title = await page.title(); |
| 233 | + await expect(title).toContain("Oops.."); |
| 234 | + await expect(page).toHaveText( |
| 235 | + ".desc", |
| 236 | + "This page is protected against cyber attacks and your IP has been banned by our system." |
| 237 | + ); |
| 238 | + }); |
| 239 | + |
| 240 | + it('Should display back the homepage with no remediation via stream mode"', async () => { |
| 241 | + notify("Display back the homepage with no remediation via stream mode"); |
| 242 | + |
| 243 | + // delete a ban remediation |
| 244 | + await deleteAllDecisions(); |
| 245 | + await wait(2000); |
| 246 | + await page.goto(`${BASE_URL}/wp-cron.php`); |
| 247 | + |
| 248 | + await page.goto(`${BASE_URL}`); |
| 249 | + await waitForNavigation; |
| 250 | + |
| 251 | + const title = await page.title(); |
| 252 | + await expect(title).toContain("Just another WordPress site"); |
| 253 | + }); |
| 254 | +}); |
0 commit comments