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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import userEvent from '@testing-library/user-event';
import { navigateMock, renderWithProviders } from '../../__helpers__/test-utils';
import { mockBitbucketAccount } from '../../__mocks__/account-mocks';

import type { Hostname, Token } from '../../types';

import {
type PersonalAccessTokenFormData,
validatePersonalAccessTokenForm,
} from '../../utils/auth/loginValidation';
import * as comms from '../../utils/system/comms';
import { BitbucketLoginWithPersonalAccessTokenRoute } from './LoginWithPersonalAccessToken';

Expand All @@ -23,30 +17,6 @@ describe('renderer/routes/bitbucket/LoginWithPersonalAccessToken.tsx', () => {
expect(tree.container).toMatchSnapshot();
});

describe('form validation', () => {
it('requires username (Atlassian email) for bitbucket', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'bitbucket.org' as Hostname,
token: 'my-api-token' as Token,
username: '',
};

expect(validatePersonalAccessTokenForm(values, 'bitbucket').username).toBe(
'Atlassian email is required',
);
});

it('passes validation with username and token', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'bitbucket.org' as Hostname,
token: 'my-api-token' as Token,
username: 'user@example.com',
};

expect(validatePersonalAccessTokenForm(values, 'bitbucket')).toEqual({});
});
});

it('renders the username field', () => {
renderWithProviders(<BitbucketLoginWithPersonalAccessTokenRoute />);
expect(screen.getByTestId('login-username')).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import userEvent from '@testing-library/user-event';
import { navigateMock, renderWithProviders } from '../../__helpers__/test-utils';
import { mockGiteaAccount } from '../../__mocks__/account-mocks';

import type { Forge, Hostname, Token } from '../../types';
import type { Hostname } from '../../types';

import {
type PersonalAccessTokenFormData,
validatePersonalAccessTokenForm,
} from '../../utils/auth/loginValidation';
import * as comms from '../../utils/system/comms';
import { GiteaLoginWithPersonalAccessTokenRoute } from './LoginWithPersonalAccessToken';

Expand All @@ -34,76 +30,6 @@ describe('renderer/routes/gitea/LoginWithPersonalAccessToken.tsx', () => {
expect(screen.getByText('Hostname format is invalid')).toBeInTheDocument();
});

describe('form validation', () => {
it.each(['gitea.example.com:0', 'gitea.example.com:65536', 'gitea.example.com:https'])(
'rejects invalid port in %s',
(hostname) => {
expect(
validatePersonalAccessTokenForm(
{
hostname: hostname as Hostname,
token: 'abcdef1234567890abcdef1234567890abcdef12' as Token,
},
'gitea',
).hostname,
).toBe('Hostname format is invalid');
},
);

it.each<Forge>(['github', 'gitlab', 'bitbucket'])('keeps ports invalid for %s', (forge) => {
expect(
validatePersonalAccessTokenForm(
{
hostname: 'git.example.com:3000' as Hostname,
token: 'abcdef1234567890abcdef1234567890abcdef12' as Token,
},
forge,
).hostname,
).toBe('Hostname format is invalid');
});

it.each(['http://git.internal', 'http://git.internal:3000', 'gitea.example.com:3000'])(
'accepts Gitea origin %s',
(hostname) => {
expect(
validatePersonalAccessTokenForm(
{ hostname: hostname as Hostname, token: 'a'.repeat(40) as Token },
'gitea',
),
).toEqual({});
},
);

it.each<Forge>(['github', 'gitlab', 'bitbucket'])('keeps %s hostnames HTTPS-only', (forge) => {
expect(
validatePersonalAccessTokenForm(
{ hostname: 'http://git.internal:3000' as Hostname, token: '' as Token },
forge,
).hostname,
).toBe('Hostname format is invalid');
});

it('should validate the token matches the Gitea format', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'gitea.example.com' as Hostname,
token: 'abcdef1234567890abcdef1234567890abcdef12' as Token,
};

expect(validatePersonalAccessTokenForm(values, 'gitea')).toEqual({});
});

it('should reject tokens that are not 40-character lowercase hex', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'gitea.example.com' as Hostname,
token: 'GHT_INVALID_TOKEN' as Token,
};

expect(validatePersonalAccessTokenForm(values, 'gitea').token).toBe(
'Token format is invalid',
);
});
});

it.each([
['gitea.example.com', 'https://gitea.example.com'],
['gitea.example.com:3000', 'https://gitea.example.com:3000'],
Expand Down
29 changes: 0 additions & 29 deletions src/renderer/routes/github/LoginWithOAuthApp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import userEvent from '@testing-library/user-event';

import { navigateMock, renderWithProviders } from '../../__helpers__/test-utils';

import type { ClientID, ClientSecret, Hostname } from '../../types';

import { type OAuthAppFormData, validateOAuthAppForm } from '../../utils/auth/loginValidation';
import * as logger from '../../utils/core/logger';
import * as comms from '../../utils/system/comms';
import { GitHubLoginWithOAuthAppRoute } from './LoginWithOAuthApp';
Expand All @@ -30,32 +27,6 @@ describe('renderer/routes/github/LoginWithOAuthApp.tsx', () => {
expect(navigateMock).toHaveBeenCalledWith(-1);
});

describe('form validation', () => {
it('should validate the form values are not empty', () => {
const values: OAuthAppFormData = {
hostname: null as unknown as Hostname,
clientId: null as unknown as ClientID,
clientSecret: null as unknown as ClientSecret,
};

expect(validateOAuthAppForm(values).hostname).toBe('Hostname is required');
expect(validateOAuthAppForm(values).clientId).toBe('Client ID is required');
expect(validateOAuthAppForm(values).clientSecret).toBe('Client Secret is required');
});

it('should validate the form values are correct format', () => {
const values: OAuthAppFormData = {
hostname: 'hello' as Hostname,
clientId: '!@£INVALID-.1' as ClientID,
clientSecret: '!@£INVALID-.1' as ClientSecret,
};

expect(validateOAuthAppForm(values).hostname).toBe('Hostname format is invalid');
expect(validateOAuthAppForm(values).clientId).toBe('Client ID format is invalid');
expect(validateOAuthAppForm(values).clientSecret).toBe('Client Secret format is invalid');
});
});

describe("'Create new OAuth App' button", () => {
it('should be disabled if no hostname configured', async () => {
renderWithProviders(<GitHubLoginWithOAuthAppRoute />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import userEvent from '@testing-library/user-event';
import { navigateMock, renderWithProviders } from '../../__helpers__/test-utils';
import { mockPersonalAccessTokenAccount } from '../../__mocks__/account-mocks';

import type { Hostname, Token } from '../../types';
import type { Hostname } from '../../types';

import {
type PersonalAccessTokenFormData,
validatePersonalAccessTokenForm,
} from '../../utils/auth/loginValidation';
import * as logger from '../../utils/core/logger';
import * as comms from '../../utils/system/comms';
import { GitHubLoginWithPersonalAccessTokenRoute } from './LoginWithPersonalAccessToken';
Expand All @@ -33,27 +29,6 @@ describe('renderer/routes/github/LoginWithPersonalAccessToken.tsx', () => {
expect(navigateMock).toHaveBeenCalledWith(-1);
});

describe('form validation', () => {
it('should validate the form values are not empty', () => {
const values: PersonalAccessTokenFormData = {
hostname: null as unknown as Hostname,
token: null as unknown as Token,
};
expect(validatePersonalAccessTokenForm(values).hostname).toBe('Hostname is required');
expect(validatePersonalAccessTokenForm(values).token).toBe('Token is required');
});

it('should validate the form values are correct format', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'hello' as Hostname,
token: '!@£INVALID-.1' as Token,
};

expect(validatePersonalAccessTokenForm(values).hostname).toBe('Hostname format is invalid');
expect(validatePersonalAccessTokenForm(values).token).toBe('Token format is invalid');
});
});

describe("'Generate a PAT' button", () => {
it('should be disabled if no hostname configured', async () => {
renderWithProviders(<GitHubLoginWithPersonalAccessTokenRoute />, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { mockGitLabAccount } from '../../__mocks__/account-mocks';

import type { Hostname, Token } from '../../types';

import {
type PersonalAccessTokenFormData,
validatePersonalAccessTokenForm,
} from '../../utils/auth/loginValidation';
import * as comms from '../../utils/system/comms';
import { GitLabLoginWithPersonalAccessTokenRoute } from './LoginWithPersonalAccessToken';

Expand All @@ -31,37 +27,6 @@ describe('renderer/routes/gitlab/LoginWithPersonalAccessToken.tsx', () => {
expect(screen.getByTestId('login-hostname')).toHaveValue('gitlab.com');
});

describe('form validation', () => {
it('accepts the modern routing-prefixed token format', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'gitlab.com' as Hostname,
token: MODERN_TOKEN,
};

expect(validatePersonalAccessTokenForm(values, 'gitlab')).toEqual({});
});

it('rejects an empty token', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'gitlab.com' as Hostname,
token: '' as Token,
};

expect(validatePersonalAccessTokenForm(values, 'gitlab').token).toBe('Token is required');
});

it('rejects an invalid hostname', () => {
const values: PersonalAccessTokenFormData = {
hostname: 'not a hostname' as Hostname,
token: MODERN_TOKEN,
};

expect(validatePersonalAccessTokenForm(values, 'gitlab').hostname).toBe(
'Hostname format is invalid',
);
});
});

it('should open the token settings for the configured hostname', async () => {
renderWithProviders(<GitLabLoginWithPersonalAccessTokenRoute />, {
loginWithPersonalAccessToken: loginWithPersonalAccessTokenMock,
Expand Down
Loading
Loading