Skip to content

Commit

Permalink
fix: check if ssh key has newline at the end
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Kurinnyi <[email protected]>
  • Loading branch information
akurinnoy authored and ibuziuk committed Aug 19, 2024
1 parent d0c3248 commit 2ae50bc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ describe('TextFileUpload', () => {
/* Upload a file */

const fileContent = 'file-content';
const fileContentBase64 = Buffer.from(fileContent).toString('base64');
const file = new File([fileContent], 'file.txt', { type: 'text/plain' });

const fileUploader = screen.getByTestId(fieldId);
const fileUploadInput = fileUploader.querySelector('input[type="file"]');
userEvent.upload(fileUploadInput!, file);

await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(fileContentBase64, true));
await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(fileContent, true));

expect(fileInput).toHaveValue(file.name);
expect(clearButton).toBeEnabled();
Expand Down Expand Up @@ -118,10 +117,9 @@ describe('TextFileUpload', () => {
/* Paste a content */

const content = 'content';
const contentBase64 = Buffer.from(content).toString('base64');
userEvent.paste(contentInput, content);

await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(contentBase64, false));
await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(content, false));

expect(fileInput).toHaveValue('');
expect(clearButton).toBeEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export type Props = {
fileNamePlaceholder?: string;
textAreaPlaceholder?: string;
validated: ValidatedOptions;
/**
* @param content base64 encoded file content
*/
onChange: (content: string, isUpload: boolean) => void;
};

Expand Down Expand Up @@ -66,7 +63,7 @@ export class TextFileUpload extends React.PureComponent<Props, State> {

private handleDataChange(content: string): void {
this.setState({ content });
this.props.onChange(btoa(content), true);
this.props.onChange(content, true);
}

private handleTextChange(content: string): void {
Expand All @@ -75,7 +72,7 @@ export class TextFileUpload extends React.PureComponent<Props, State> {
file: undefined,
filename: undefined,
});
this.props.onChange(btoa(content), false);
this.props.onChange(content, false);
}

public render(): React.ReactElement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe('SshPrivateKey', () => {
const sshPrivateKey = 'ssh-private-key';
userEvent.paste(input, sshPrivateKey);

expect(mockOnChange).toHaveBeenCalledWith(sshPrivateKey, true);
const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, true);
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy();
Expand Down Expand Up @@ -81,7 +83,9 @@ describe('SshPrivateKey', () => {
const sshPrivateKey = 'ssh-private-key'.repeat(5000);
userEvent.paste(input, sshPrivateKey);

expect(mockOnChange).toHaveBeenCalledWith(sshPrivateKey, false);
const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, false);
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy();
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
Expand All @@ -97,10 +101,12 @@ describe('SshPrivateKey', () => {
const input = screen.getByPlaceholderText('Or paste the PRIVATE key');

// fill the SSH key data field
const sshKeyData = 'ssh-key-data';
userEvent.paste(input, sshKeyData);
const sshPrivateKey = 'ssh-key-data';
userEvent.paste(input, sshPrivateKey);

expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, true);
const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, true);
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy();
Expand All @@ -114,8 +120,8 @@ describe('SshPrivateKey', () => {
const input = screen.getByPlaceholderText('Or paste the PRIVATE key');

// fill the SSH key data field
const sshKeyData = 'ssh-key-data';
userEvent.paste(input, sshKeyData);
const sshPrivateKey = 'ssh-key-data';
userEvent.paste(input, sshPrivateKey);

mockOnChange.mockClear();

Expand All @@ -137,10 +143,12 @@ describe('SshPrivateKey', () => {
const input = screen.getByPlaceholderText('Or paste the PRIVATE key');

// fill the SSH key data field
const sshKeyData = 'ssh-key-data'.repeat(5000);
userEvent.paste(input, sshKeyData);
const sshPrivateKey = 'ssh-key-data'.repeat(5000);
userEvent.paste(input, sshPrivateKey);

const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, false);
expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, false);
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy();

expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class SshPrivateKey extends React.Component<Props, State> {

private onChange(privateKey: string, isUpload: boolean): void {
const { onChange } = this.props;

privateKey = privateKey.trim()
? btoa(
// expect the only new line at the end
privateKey.trim() + '\n',
)
: '';

const validated = this.validate(privateKey);
const isValid = validated === ValidatedOptions.success;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe('SshPublicKey', () => {
const sshPublicKey = 'ssh-public-key';
userEvent.paste(input, sshPublicKey);

expect(mockOnChange).toHaveBeenCalledWith(sshPublicKey, true);
const expectedSshPrivateKey = btoa(sshPublicKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, true);
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy();
Expand All @@ -67,7 +69,7 @@ describe('SshPublicKey', () => {
const sshPublicKey = '';
fireEvent.change(input, { target: { value: sshPublicKey } });

expect(mockOnChange).toHaveBeenCalledWith(sshPublicKey, false);
expect(mockOnChange).toHaveBeenCalledWith('', false);
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeTruthy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy();
Expand All @@ -84,7 +86,9 @@ describe('SshPublicKey', () => {
const sshPublicKey = 'ssh-public-key'.repeat(1000);
userEvent.paste(input, sshPublicKey);

expect(mockOnChange).toHaveBeenCalledWith(sshPublicKey, false);
const expectedSshPublicKey = btoa(sshPublicKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(expectedSshPublicKey, false);
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy();
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
Expand All @@ -99,10 +103,12 @@ describe('SshPublicKey', () => {

const input = screen.getByPlaceholderText('Or paste the PUBLIC key');

const sshKeyData = 'ssh-key-data';
userEvent.paste(input, sshKeyData);
const sshPublicKey = 'ssh-key-data';
userEvent.paste(input, sshPublicKey);

expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, true);
const expectedSshPublicKey = btoa(sshPublicKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(expectedSshPublicKey, true);
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy();
Expand Down Expand Up @@ -137,10 +143,12 @@ describe('SshPublicKey', () => {
const input = screen.getByPlaceholderText('Or paste the PUBLIC key');

// fill the SSH key data field
const sshKeyData = 'ssh-key-data'.repeat(1000);
userEvent.paste(input, sshKeyData);
const sshPublicKey = 'ssh-key-data'.repeat(1000);
userEvent.paste(input, sshPublicKey);

const expectedSshPublicKey = btoa(sshPublicKey.trim() + '\n');

expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, false);
expect(mockOnChange).toHaveBeenCalledWith(expectedSshPublicKey, false);
expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy();
expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy();
expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class SshPublicKey extends React.Component<Props, State> {

private onChange(publicKey: string, isUpload: boolean): void {
const { onChange } = this.props;

publicKey = publicKey.trim()
? btoa(
// expect the only new line at the end
publicKey.trim() + '\n',
)
: '';

const validated = this.validate(publicKey);
const isValid = validated === ValidatedOptions.success;

Expand Down

0 comments on commit 2ae50bc

Please sign in to comment.