Back to Repositories

Validating Git Synchronization Workflows in Insomnia

This test suite validates Git synchronization functionality in Insomnia, covering both generic Git server cloning and GitHub authentication workflows. The tests ensure proper repository connection, OAuth flow handling, and synchronization capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of Git integration features in Insomnia.

Key functionality tested includes:
  • Generic Git repository cloning with credentials
  • GitHub OAuth authentication flow
  • Repository selection and synchronization
  • Form input validation and submission
Edge cases covered include OAuth redirect handling and GitHub app authorization simulation.

Implementation Analysis

The tests utilize Playwright’s page object model for UI interaction testing. The implementation employs async/await patterns for handling asynchronous operations and custom event listeners for OAuth flow simulation.

Framework-specific features include:
  • Custom test fixtures for app and page objects
  • Electron evaluation for OAuth flow interception
  • Playwright selector strategies for UI elements

Technical Details

Testing tools and configuration:
  • Playwright Test Runner
  • TypeScript for type safety
  • Custom Electron helpers for OAuth simulation
  • Data-testid and role-based selectors
  • Custom wait conditions for API responses
  • Event prevention and URL manipulation utilities

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through systematic validation of user workflows.

Notable practices include:
  • Isolation of OAuth authentication flow
  • Meaningful test descriptions and organization
  • Consistent selector strategies
  • Proper async/await usage
  • Effective mocking of external services

kong/insomnia

packages/insomnia-smoke-test/tests/smoke/git-sync.test.ts

            
import { test } from '../../playwright/test';

test('Clone from generic Git server', async ({ page }) => {
  // waitting for the /features api request to finish
  await page.waitForSelector('[data-test-git-enable="true"]');
  await page.getByLabel('Clone git repository').click();
  await page.getByRole('tab', { name: ' Git' }).click();
  await page.getByPlaceholder('https://github.com/org/repo.git').fill('https://github.com/Kong/insomnia-git-example.git');
  await page.getByPlaceholder('Name').fill('J');
  await page.getByPlaceholder('Email').fill('J');
  await page.getByPlaceholder('MyUser').fill('J');
  await page.getByPlaceholder('88e7ee63b254e4b0bf047559eafe86ba9dd49507').fill('J');
  await page.getByTestId('git-repository-settings-modal__sync-btn').click();
  await page.getByLabel('Toggle preview').click();
});

test('Sign in with GitHub', async ({ app, page }) => {
  await page.getByRole('button', { name: 'New Document' }).click();
  await page.getByRole('dialog').getByRole('button', { name: 'Create' }).click();
  await page.getByLabel('Insomnia Sync').click();
  await page.getByRole('menuitemradio', { name: 'Switch to Git Repository' }).click();

  await page.getByRole('tab', { name: 'GitHub' }).click();

  // Prevent the app from opening the browser to the authorization page
  // and return the url that would be created by following the GitHub OAuth flow.
  // https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#web-application-flow
  const fakeGitHubAppOAuthWebFlow = app.evaluate(electron => {
    return new Promise<{ redirectUrl: string }>(resolve => {
      const webContents = electron.BrowserWindow.getAllWindows()?.find(w => w.title === 'Insomnia')?.webContents;
      // Remove all navigation listeners so that only the one we inject will run
      webContents?.removeAllListeners('will-navigate');
      webContents?.on('will-navigate' as any, (event: Event, url: string) => {
        event.preventDefault();
        const parsedUrl = new URL(url);
        // We use the same state parameter that the app created to assert that we prevent CSRF
        const stateSearchParam = parsedUrl.searchParams.get('state') || '';
        const redirectUrl = `insomnia://oauth/github-app/authenticate?state=${stateSearchParam}&code=12345`;
        resolve({ redirectUrl });
      });
    });
  });

  const [{ redirectUrl }] = await Promise.all([
    fakeGitHubAppOAuthWebFlow,
    page.getByText('Authenticate with GitHub').click({
      // When playwright clicks a link it waits for navigation to finish.
      // In our case we are stubbing the navigation and we don't want to wait for it.
      noWaitAfter: true,
    }),
  ]);

  await page.locator('input[name="link"]').click();

  await page.locator('input[name="link"]').fill(redirectUrl);

  await page.getByRole('button', { name: 'Authenticate' }).click();

  await page.locator('button[id="github_repo_select_dropdown_button"]').click();

  await page.getByLabel('kong-test/sleepless').click();

  await page.locator('data-testid=git-repository-settings-modal__sync-btn').click();
});