-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Error when NEXT_PUBLIC_WORDPRESS_URL same as headless site U…
…RL (#1809) * Feature: Error when NEXT_PUBLIC_WORDPRESS_URL pointing to headless site * Update packages/faustwp-cli/src/healthCheck/validateNextWordPressUrl.ts Co-authored-by: John Parris <[email protected]> * Chore: Fix PHP Lint issues * Chore: PHP Lint issues * Feature: handle older versions of FaustWP Plugin * Tests: Add unit tests for domains_match * PHPCS: Fix * PHPCS: Lint issues * Chore: Remove WP_Rest_Response messages. * Feat: Only perform the check on valid secret key --------- Co-authored-by: John Parris <[email protected]>
- Loading branch information
Showing
11 changed files
with
290 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@faustwp/cli': patch | ||
'@faustwp/wordpress-plugin': patch | ||
--- | ||
|
||
Faust now errors if the NEXT_PUBLIC_WORDPRESS_URL matches the Headless URL in Faust Plugin settings. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/faustwp-cli/src/healthCheck/validateNextWordPressUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getWpSecret, getWpUrl } from '../utils/index.js'; | ||
import { errorLog, warnLog } from '../stdout/index.js'; | ||
|
||
/** | ||
* Validates the NEXT_PUBLIC_WORDPRESS_URL environment variable by sending a POST request to the Faust Plugin API. | ||
* If the URL matches the Faust Plugin Headless URL, the validation fails, and an error is logged. | ||
*/ | ||
export async function validateNextWordPressUrl(): Promise<void> { | ||
const apiUrl = `${getWpUrl()}/wp-json/faustwp/v1/validate_public_wordpress_url`; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'x-faustwp-secret': getWpSecret() || '', | ||
}; | ||
|
||
const postData = { | ||
public_wordpress_url: getWpUrl(), | ||
}; | ||
try { | ||
const response = await fetch(apiUrl, { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify(postData), | ||
}); | ||
|
||
if (!response.ok) { | ||
if (response.status === 404) { | ||
// Handle the case when the route does not exist | ||
warnLog( | ||
'Route not found: Please update your FaustWP plugin to the latest version.', | ||
); | ||
} else { | ||
errorLog( | ||
'Validation Failed: Your Faust front-end site URL value is misconfigured. It should NOT match the `NEXT_PUBLIC_WORDPRESS_URL.`', | ||
); | ||
process.exit(1); | ||
} | ||
} | ||
} catch (error) { | ||
console.log('error', error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/faustwp-cli/tests/healthCheck/validateNextWordPressUrl.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import fetchMock from 'fetch-mock-jest'; | ||
import { validateNextWordPressUrl } from '../../src/healthCheck/validateNextWordPressUrl'; | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
describe('healthCheck/validateNextWordPressUrl', () => { | ||
const envBackup = process.env; | ||
|
||
beforeEach(() => { | ||
process.env = { ...envBackup }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = envBackup; | ||
}); | ||
|
||
it('exits with a 1 exit code when the WordPress URL matches the Headless URL', async () => { | ||
// @ts-ignore | ||
const mockExit = jest.spyOn(process, 'exit').mockImplementation((code) => { | ||
if (code && code !== 0) { | ||
throw new Error(`Exit code: ${code}`); | ||
} | ||
}); | ||
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
|
||
process.env.NEXT_PUBLIC_WORDPRESS_URL = 'https://headless.local'; | ||
process.env.FAUST_SECRET_KEY = 'invalid-secret-key'; | ||
|
||
const headers = { | ||
'Content-Type': 'application/json', | ||
'x-faustwp-secret': process.env.FAUST_SECRET_KEY, | ||
}; | ||
|
||
fetchMock.post( | ||
'https://headless.local/wp-json/faustwp/v1/validate_public_wordpress_url', | ||
{ | ||
headers, | ||
body: JSON.stringify({ | ||
public_wordpress_url: process.env.NEXT_PUBLIC_WORDPRESS_URL, | ||
}), | ||
status: 400, | ||
}, | ||
); | ||
|
||
await validateNextWordPressUrl(); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Validation Failed: Your Faust front-end site URL value is misconfigured. It should NOT match the `NEXT_PUBLIC_WORDPRESS_URL.', | ||
), | ||
); | ||
expect(mockExit).toHaveBeenCalledWith(1); | ||
expect(fetchMock).toHaveFetched( | ||
'https://headless.local/wp-json/faustwp/v1/validate_public_wordpress_url', | ||
); | ||
|
||
consoleLogSpy.mockClear(); | ||
}); | ||
|
||
it('continues silently when the route does not exist', async () => { | ||
// @ts-ignore | ||
const mockExit = jest.spyOn(process, 'exit').mockImplementation((code) => { | ||
if (code && code !== 0) { | ||
throw new Error(`Exit code: ${code}`); | ||
} | ||
}); | ||
// Mock environment variables | ||
process.env.NEXT_PUBLIC_WORDPRESS_URL = 'http://mysite.local'; | ||
process.env.FAUST_SECRET_KEY = 'e9d5963e-bb41-4c94-a3f3-292e8903d5ea'; | ||
|
||
const headers = { | ||
'Content-Type': 'application/json', | ||
'x-faustwp-secret': process.env.FAUST_SECRET_KEY, | ||
}; | ||
|
||
fetchMock.postOnce( | ||
'http://mysite.local/wp-json/faustwp/v1/validate_public_wordpress_url', | ||
{ | ||
status: 404, | ||
}, | ||
); | ||
|
||
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
|
||
await validateNextWordPressUrl(); | ||
|
||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Route not found: Please update your FaustWP plugin to the latest version.', | ||
), | ||
); | ||
|
||
expect(mockExit).not.toHaveBeenCalled() | ||
expect(fetchMock).toHaveFetched( | ||
'http://mysite.local/wp-json/faustwp/v1/validate_public_wordpress_url', | ||
); | ||
|
||
consoleLogSpy.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace WPE\FaustWP\Tests\Unit; | ||
|
||
use function WPE\FaustWP\Utilities\{domains_match}; | ||
|
||
class FunctionsTests extends FaustUnitTest { | ||
public function test_domains_match() { | ||
// Test case 1: Same domains with different protocols | ||
$this->assertTrue(domains_match("http://example.com", "https://example.com")); | ||
|
||
// Test case 2: Same domains with trailing slashes | ||
$this->assertTrue(domains_match("http://example.com/", "http://example.com")); | ||
|
||
// Test case 3: Same domains with www prefix | ||
$this->assertTrue(domains_match("http://www.example.com", "http://example.com")); | ||
|
||
// Test case 4: Different domains | ||
$this->assertFalse(domains_match("http://example1.com", "http://example2.com")); | ||
|
||
// Test case 5: Same domains with different subdomains | ||
$this->assertFalse(domains_match("http://1.example.com", "http://2.example.com")); | ||
} | ||
} |