Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
env:
FRONT_DOOR_USERNAME: ${{ secrets.FRONT_DOOR_USERNAME }}
FRONT_DOOR_PASSWORD: ${{ secrets.FRONT_DOOR_PASSWORD }}
COVEO_CREDENTIALS_BASE_URL: ${{ secrets.COVEO_CREDENTIALS_BASE_URL }}
jobs:
playwright:
name: Run Playwright
Expand Down
50 changes: 30 additions & 20 deletions assets/js/coveo.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,38 @@ async function getValidSearchCredentials() {
}

async function atomicCoveo() {
await customElements.whenDefined('atomic-search-interface');
const credentials = await getValidSearchCredentials();

document.querySelectorAll('atomic-search-interface').forEach(async (el) => {
await el.initialize({
...credentials,
analytics: { analyticsMode: 'legacy' },
preprocessRequest: (request) => {
const body = JSON.parse(request.body);
body.q = `<@- ${body.q} -@>`;
request.body = JSON.stringify(body);
return request;
},
try {
await customElements.whenDefined('atomic-search-interface');
const credentials = await getValidSearchCredentials();

document.querySelectorAll('atomic-search-interface').forEach(async (el) => {
await el.initialize({
...credentials,
analytics: { analyticsMode: 'legacy' },
preprocessRequest: (request) => {
const body = JSON.parse(request.body);
body.q = `<@- ${body.q} -@>`;
request.body = JSON.stringify(body);
return request;
},
});

// No standalone searchboxes should be getting executing first search.
if (el.id === 'search-v2') await el.executeFirstSearch();
});

// No standalone searchboxes should be getting executing first search.
if (el.id === 'search-v2') await el.executeFirstSearch();
});

const headerSearchBar = document.querySelector('#search-standalone-header');
if (headerSearchBar?.shadowRoot) {
hideShadowElement(headerSearchBar.shadowRoot, 'atomic-relevance-inspector');
const headerSearchBar = document.querySelector('#search-standalone-header');
if (headerSearchBar?.shadowRoot) {
hideShadowElement(
headerSearchBar.shadowRoot,
'atomic-relevance-inspector'
);
}
} catch (error) {
// Handle coveo error from only a LACK of credentials.
// INCORRECT credentials will cause the page to load but spin waiting.
const coveoErrorContainer = document.getElementById('coveo-error-content');
coveoErrorContainer.style.display = 'block';
}
}

Expand Down
10 changes: 10 additions & 0 deletions layouts/partials/search-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ $coveoEnabled := partial "get-feature-flags.html" "disable_coveo" }}

<div class="content" data-testid="coveo-error-content" id="coveo-error-content" style="{{- if $coveoEnabled -}}display: none;{{- else -}}{{- end -}}">
<div class="not-found-container" data-testid="not-found-container">
<h1 class="info-header">
Search functionality is unavailable.
</h1>
<a href="{{ site.BaseURL | relLangURL }}" aria-label="Return home">Return to the {{ site.Title }} homepage.</a>
</div>
</div>
12 changes: 3 additions & 9 deletions layouts/search/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
{{ $coveoEnabled := partial "get-feature-flags.html" "disable_coveo" }}
{{ if $coveoEnabled }}
<section class="search no-sidebar">
{{ partial "coveo-atomic.html" .}}
{{ partial "coveo-atomic.html" . }}
</section>
{{ partial "search-error.html" }}
{{ else }}
<div class="content" data-testid="content">
<div class="not-found-container" data-testid="not-found-container">
<h1 class="info-header">
Search functionality is unavailable.
</h1>
<a href="{{ .Site.BaseURL | relLangURL }}" aria-label="Return home">Return to the {{ .Site.Title }} homepage.</a>
</div>
</div>
{{ partial "search-error.html" }}
{{ end }}
{{ end }}
3 changes: 2 additions & 1 deletion tests/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FRONT_DOOR_USERNAME=
FRONT_DOOR_PASSWORD=
FRONT_DOOR_PASSWORD=
COVEO_CREDENTIALS_BASE_URL=
1 change: 1 addition & 0 deletions tests/src/constants/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const TIMEOUT = 4000;
export const COVEO_CREDENTIALS_ENDPOINT = 'api/v1/auth/search_token';
21 changes: 18 additions & 3 deletions tests/src/coveo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ async function submitSearchQuery(page, query) {

test.describe('Coveo test', () => {
test.beforeEach(async ({ page, request }) => {
// Setup to start on landing page
await page.goto('/');
await page.waitForLoadState('load');
await waitFor(async () => await handleConsentPopup(page));
await mockCoveoCredentials(page, request);

// Conditionally mock credentials
const excludedTests = ['missing coveo credentials'];
if (!excludedTests.includes(test.info().title)) {
await mockCoveoCredentials(page, request);
}
});

test.afterEach(async ({ page }) => {
// Run basic smoke tests on all valid queries
if (!test.info().title.includes('invalid search query')) {
// Conditionally run a smoke test only on valid queries
const excludedTests = ['invalid search query', 'missing coveo credentials'];
if (!excludedTests.includes(test.info().title)) {
await runSmokeTestCoveo(page);
}
});
Expand Down Expand Up @@ -57,4 +64,12 @@ test.describe('Coveo test', () => {
await page.reload();
expect(page.url()).toContain(endpoint);
});

test('missing coveo credentials', async ({ page }) => {
const searchEndpoint = 'search.html';
await page.goto(`/${searchEndpoint}`);

const coveoErrorContent = page.getByTestId('coveo-error-content');
await expect(coveoErrorContent).toBeVisible();
});
});
21 changes: 12 additions & 9 deletions tests/src/mock/coveo.mock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from '@playwright/test';
import { COVEO_CREDENTIALS_ENDPOINT } from '../constants';

export const mockCoveoData = {
validQuery: 'proxy',
Expand All @@ -8,24 +9,26 @@ export const mockCoveoData = {

export async function mockCoveoCredentials(page, request) {
// Get credentials
const tokenBaseURL = 'https://docs-dev.nginx.com';
const tokenEndpoint = '/api/v1/auth/search_token';
const username = process.env.FRONT_DOOR_USERNAME;
const password = process.env.FRONT_DOOR_PASSWORD;
const response = await request.get(tokenBaseURL + tokenEndpoint, {
headers: {
Authorization:
'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'),
},
});
const baseURL = process.env.COVEO_CREDENTIALS_BASE_URL;
const response = await request.get(
`${baseURL}/${COVEO_CREDENTIALS_ENDPOINT}`,
{
headers: {
Authorization:
'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'),
},
}
);

expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);

const credentials = await response.json();

// Mock the local request to be successful, then reload the page.
await page.route(`**${tokenEndpoint}`, async (route) => {
await page.route(`**/${COVEO_CREDENTIALS_ENDPOINT}`, async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
Expand Down
Loading