Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #5994

Merged
merged 4 commits into from
Jun 23, 2024
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
2 changes: 1 addition & 1 deletion packages/twenty-chrome-extension/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ export type Captcha = {
};

export enum CaptchaDriverType {
GoogleRecatpcha = 'GoogleRecatpcha',
GoogleRecaptcha = 'GoogleRecaptcha',
Turnstile = 'Turnstile'
}

Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-front/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const jestConfig: JestConfigWithTsJest = {
coverageThreshold: {
global: {
statements: 65,
lines: 65,
lines: 64,
functions: 55,
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-front/src/generated-metadata/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export type Captcha = {
};

export enum CaptchaDriverType {
GoogleRecatpcha = 'GoogleRecatpcha',
GoogleRecaptcha = 'GoogleRecaptcha',
Turnstile = 'Turnstile'
}

Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export type Captcha = {
};

export enum CaptchaDriverType {
GoogleRecatpcha = 'GoogleRecatpcha',
GoogleRecaptcha = 'GoogleRecaptcha',
Turnstile = 'Turnstile'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const CaptchaProviderScriptLoaderEffect = () => {
scriptElement = document.createElement('script');
scriptElement.src = scriptUrl;
scriptElement.onload = () => {
if (captchaProvider.provider === CaptchaDriverType.GoogleRecatpcha) {
if (captchaProvider.provider === CaptchaDriverType.GoogleRecaptcha) {
window.grecaptcha?.ready(() => {
setIsCaptchaScriptLoaded(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useRequestFreshCaptchaToken = () => {
let captchaWidget: any;

switch (captchaProvider.provider) {
case CaptchaDriverType.GoogleRecatpcha:
case CaptchaDriverType.GoogleRecaptcha:
window.grecaptcha
.execute(captchaProvider.siteKey, {
action: 'submit',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from '@storybook/test';

import { CaptchaDriverType } from '~/generated/graphql';

import { getCaptchaUrlByProvider } from '../getCaptchaUrlByProvider';

describe('getCaptchaUrlByProvider', () => {
it('handles GoogleRecaptcha', async () => {
const captchaUrl = getCaptchaUrlByProvider(
CaptchaDriverType.GoogleRecaptcha,
'siteKey',
);

expect(captchaUrl).toEqual(
'https://www.google.com/recaptcha/api.js?render=siteKey',
);

expect(() =>
getCaptchaUrlByProvider(CaptchaDriverType.GoogleRecaptcha, ''),
).toThrow(
'SiteKey must be provided while generating url for GoogleRecaptcha provider',
);
});

it('handles Turnstile', async () => {
const captchaUrl = getCaptchaUrlByProvider(CaptchaDriverType.Turnstile, '');

expect(captchaUrl).toEqual(
'https://challenges.cloudflare.com/turnstile/v0/api.js',
);
});

it('handles unknown provider', async () => {
expect(() =>
getCaptchaUrlByProvider('Unknown' as CaptchaDriverType, ''),
).toThrow('Unknown captcha provider');
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { CaptchaDriverType } from '~/generated-metadata/graphql';
import { isNonEmptyString } from '@sniptt/guards';

export const getCaptchaUrlByProvider = (name: string, siteKey: string) => {
if (!name) {
return '';
}
import { CaptchaDriverType } from '~/generated-metadata/graphql';

export const getCaptchaUrlByProvider = (
name: CaptchaDriverType,
siteKey: string,
) => {
switch (name) {
case CaptchaDriverType.GoogleRecatpcha:
case CaptchaDriverType.GoogleRecaptcha:
if (!isNonEmptyString(siteKey)) {
throw new Error(
'SiteKey must be provided while generating url for GoogleRecaptcha provider',
);
}
return `https://www.google.com/recaptcha/api.js?render=${siteKey}`;
case CaptchaDriverType.Turnstile:
return 'https://challenges.cloudflare.com/turnstile/v0/api.js';
default:
return '';
throw new Error('Unknown captcha provider');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getForeignDataWrapperType } from '../getForeignDataWrapperType';

describe('getForeignDataWrapperType', () => {
it('should handle postgres', () => {
expect(getForeignDataWrapperType('postgresql')).toBe('postgres_fdw');
});

it('should handle stripe', () => {
expect(getForeignDataWrapperType('stripe')).toBe('stripe_fdw');
});

it('should return null for unknown', () => {
expect(getForeignDataWrapperType('unknown')).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AppPath } from '@/types/AppPath';

import indexAppPath from '../indexAppPath';

describe('getIndexAppPath', () => {
it('returns the index app path', () => {
const { getIndexAppPath } = indexAppPath;
expect(getIndexAppPath()).toEqual(AppPath.Index);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ export const SETTINGS_FIELD_CURRENCY_CODES: Record<
BRL: {
label: 'Brazilian real',
Icon: IconCurrencyReal,
}
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getSettingsIntegrationAll } from '../getSettingsIntegrationAll';

describe('getSettingsIntegrationAll', () => {
it('should return null if imageUrl is null', () => {
expect(
getSettingsIntegrationAll({
isAirtableIntegrationActive: true,
isAirtableIntegrationEnabled: true,
isPostgresqlIntegrationActive: true,
isPostgresqlIntegrationEnabled: true,
isStripeIntegrationActive: true,
isStripeIntegrationEnabled: true,
}),
).toStrictEqual({
integrations: [
{
from: {
image: '/images/integrations/airtable-logo.png',
key: 'airtable',
},
link: '/settings/integrations/airtable',
text: 'Airtable',
type: 'Active',
},
{
from: {
image: '/images/integrations/postgresql-logo.png',
key: 'postgresql',
},
link: '/settings/integrations/postgresql',
text: 'PostgreSQL',
type: 'Active',
},
{
from: {
image: '/images/integrations/stripe-logo.png',
key: 'stripe',
},
link: '/settings/integrations/stripe',
text: 'Stripe',
type: 'Active',
},
],
key: 'all',
title: 'All',
});
});
});
2 changes: 1 addition & 1 deletion packages/twenty-front/src/testing/mock-data/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const mockedClientConfig: ClientConfig = {
__typename: 'Billing',
},
captcha: {
provider: CaptchaDriverType.GoogleRecatpcha,
provider: CaptchaDriverType.GoogleRecaptcha,
siteKey: 'MOCKED_SITE_KEY',
__typename: 'Captcha',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getImageAbsoluteURIOrBase64 } from '../getImageAbsoluteURIOrBase64';

describe('getImageAbsoluteURIOrBase64', () => {
it('should return null if imageUrl is null', () => {
const imageUrl = null;
const result = getImageAbsoluteURIOrBase64(imageUrl);
expect(result).toBeNull();
});

it('should return base64 encoded string if prefixed with data', () => {
const imageUrl = 'data:XXX';
const result = getImageAbsoluteURIOrBase64(imageUrl);
expect(result).toBe(imageUrl);
});

it('should return absolute url if the imageUrl is an absolute url', () => {
const imageUrl = 'https://XXX';
const result = getImageAbsoluteURIOrBase64(imageUrl);
expect(result).toBe(imageUrl);
});

it('should return fully formed url if imageUrl is a relative url', () => {
const imageUrl = 'XXX';
const result = getImageAbsoluteURIOrBase64(imageUrl);
expect(result).toBe('http://localhost:3000/files/XXX');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CaptchaModule {
}

switch (config.type) {
case CaptchaDriverType.GoogleRecatpcha:
case CaptchaDriverType.GoogleRecaptcha:
return new GoogleRecaptchaDriver(config.options);
case CaptchaDriverType.Turnstile:
return new TurnstileDriver(config.options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FactoryProvider, ModuleMetadata } from '@nestjs/common';
import { registerEnumType } from '@nestjs/graphql';

export enum CaptchaDriverType {
GoogleRecatpcha = 'google-recaptcha',
GoogleRecaptcha = 'google-recaptcha',
Turnstile = 'turnstile',
}

Expand All @@ -15,8 +15,8 @@ export type CaptchaDriverOptions = {
secretKey: string;
};

export interface GoogleRecatpchaDriverFactoryOptions {
type: CaptchaDriverType.GoogleRecatpcha;
export interface GoogleRecaptchaDriverFactoryOptions {
type: CaptchaDriverType.GoogleRecaptcha;
options: CaptchaDriverOptions;
}

Expand All @@ -26,7 +26,7 @@ export interface TurnstileDriverFactoryOptions {
}

export type CaptchaModuleOptions =
| GoogleRecatpchaDriverFactoryOptions
| GoogleRecaptchaDriverFactoryOptions
| TurnstileDriverFactoryOptions;

export type CaptchaModuleAsyncOptions = {
Expand Down
Loading