Skip to content

Commit

Permalink
Hotfix/check email value with dot (#13)
Browse files Browse the repository at this point in the history
* replace function in helpers

* adapt testing

* adapt email regex to handle tiret and underscores

* hotfix test result
  • Loading branch information
tuxtux59 authored Sep 4, 2023
1 parent 80243e0 commit a81129a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 20 deletions.
79 changes: 79 additions & 0 deletions src/__tests__/utils/helpers.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Transaction from '../../models/Transaction';
import {
canProcessSloCode,
checkDomainValue,
checkEmailValue,
extractParamsFromUri,
logOutBody,
prepareConfig,
Expand Down Expand Up @@ -316,3 +318,80 @@ describe('helpers#canProcessSloCode/2', () => {
).toBeTruthy();
});
});

describe('helper#checkEmaiValue/1', () => {
it('returns false if empty string value', () => {
expect(() => checkEmailValue('')).toThrowError(
'Please provide non blank string for email'
);
});
it('returns false if wrong string value', () => {
expect(() => checkEmailValue('azerty')).toThrowError(
'Please provide valid email'
);
});

it('returns false if @ string value', () => {
expect(() => checkEmailValue('@')).toThrowError(
'Please provide valid email'
);
});

it('returns true if simple string value', () => {
expect(checkEmailValue('[email protected]')).toEqual('[email protected]');
});

it('returns true if dotted email string value', () => {
expect(checkEmailValue('[email protected]')).toEqual(
'[email protected]'
);
});

it('returns false if ending first part with dots email string value', () => {
expect(() => checkEmailValue('[email protected]')).toThrowError(
'Please provide valid email'
);
});

it('returns false if alias email string value', () => {
expect(() => checkEmailValue('[email protected]')).toThrowError(
'Please provide valid email'
);
});

it('returns truthy if tiret email string value', () => {
expect(checkEmailValue('[email protected]')).toEqual(
'[email protected]'
);
});

it('returns truthy if underscored email string value', () => {
expect(checkEmailValue('[email protected]')).toEqual(
'[email protected]'
);
});
});

describe('helpers#checkDomainValue/1', () => {
it('should throw error if empy input', () => {
expect(() => checkDomainValue(' ')).toThrowError(
'Please provide non blank string for domain'
);
});

it('should throw error if underscored input', () => {
expect(() => checkDomainValue('_')).toThrowError(
'Please provide valid domain (alphanumeric dashed separated)'
);
});

it('should throw error if standard account complex name input', () => {
expect(() => checkDomainValue("My awesome Company's name")).toThrowError(
'Please provide valid domain (alphanumeric dashed separated)'
);
});

it('should return value if proper domain input', () => {
expect(checkDomainValue('communitiz-app')).toEqual('communitiz-app');
});
});
21 changes: 1 addition & 20 deletions src/components/CryptrGatewayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,7 @@ import { Locale, useCryptr } from '..';
import { Pressable, Text, View } from 'react-native';
import type { StyleProp, TextStyle, ViewStyle } from 'react-native';
import { defaultStyles } from '../utils/defaultStypes';

const checkEmailValue = (emailValue: string) => {
if (emailValue.trim() === '') {
throw new Error('Please provide non blank string for email');
}
if (!/^[a-zA-Z0-9+]+@(?:[a-zA-Z0-9-]+\.)+[A-Za-z]+$/.test(emailValue)) {
throw new Error('Please provide valid email');
}
return emailValue;
};

const checkDomainValue = (domainValue?: string) => {
if (domainValue !== undefined && domainValue?.trim() !== '') {
if (!/^[a-z0-9-]*$/.test(domainValue)) {
throw new Error(
'Please provide valid domain (alphanumeric dashed separated)'
);
}
}
};
import { checkDomainValue, checkEmailValue } from '../utils/helpers';

type GatewayProps = {
domain?: string;
Expand Down
24 changes: 24 additions & 0 deletions src/utils/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,27 @@ export const canProcessSloCode = (
// ips !== 'google'
);
};

export const checkEmailValue = (emailValue: string) => {
if (emailValue.trim() === '') {
throw new Error('Please provide non blank string for email');
}
if (
!/^[a-z0-9]+[-_.]?[a-z0-9]+@(?:[a-zA-Z0-9-]+\.)+[A-Za-z]+$/.test(emailValue)
) {
throw new Error('Please provide valid email');
}
return emailValue;
};

export const checkDomainValue = (domainValue?: string) => {
if (domainValue === undefined || domainValue.trim() === '') {
throw new Error('Please provide non blank string for domain');
}
if (!/^[a-z0-9-]*$/.test(domainValue)) {
throw new Error(
'Please provide valid domain (alphanumeric dashed separated)'
);
}
return domainValue;
};

0 comments on commit a81129a

Please sign in to comment.