Skip to content

Commit

Permalink
PB-34298 WP4.2 - Auto-save password resource from quickaccess in v5 f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
Benj1er committed Aug 12, 2024
1 parent 15dce42 commit 6845679
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ class SaveResource extends React.Component {
return;
}

const resourceTypeId = this.resourceTypesSettings.findResourceTypeIdBySlug(this.resourceTypesSettings.DEFAULT_RESOURCE_TYPES_SLUGS.PASSWORD_AND_DESCRIPTION);
const resourceDto = {
name: this.state.name,
username: this.state.username,
uri: this.state.uri,
resource_type_id: this.resourceTypesSettings.findResourceTypeIdBySlug(this.resourceTypesSettings.DEFAULT_RESOURCE_TYPES_SLUGS.PASSWORD_AND_DESCRIPTION),
metadata: {
name: this.state.name,
username: this.state.username,
uris: [this.state.uri],
resource_type_id: resourceTypeId,
},
resource_type_id: resourceTypeId,
expired: this.props.passwordExpiryContext.getDefaultExpirationDate(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,36 @@
* @returns {any}
*/
import {defaultUserAppContext} from "../../../react-extension/contexts/ExtAppContext.test.data";
import {
defaultPasswordExpirySettingsContext
} from "../../../react-extension/contexts/PasswordExpirySettingsContext.test.data";
import {
overridenPasswordExpirySettingsEntityDto
} from "../../../shared/models/passwordExpirySettings/PasswordExpirySettingsDto.test.data";
import {
defaultPasswordPoliciesContext
} from "../../../shared/context/PasswordPoliciesContext/PasswordPoliciesContext.test.data";
import {defaultPasswordPoliciesDto} from "../../../shared/models/passwordPolicies/PasswordPoliciesDto.test.data";

export function defaultAppContext(appContext) {
const defaultAppContext = defaultUserAppContext();
return Object.assign(defaultAppContext, appContext || {});
}

export function defaultProps(props = {}) {
const defaultProps = {
context: defaultAppContext(props?.appContext),
passwordExpiryContext: defaultPasswordExpirySettingsContext({
getSettings: () => overridenPasswordExpirySettingsEntityDto()
}),
passwordPoliciesContext: defaultPasswordPoliciesContext({
getPolicies: jest.fn(() => defaultPasswordPoliciesDto())
}),
};
return Object.assign(defaultProps, props);
}


export function mockExtensionCall(context) {
context.port.addRequestListener("passbolt.resources.create", () => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,72 @@
* @link https://www.passbolt.com Passbolt(tm)
* @since 3.9.0
*/
import SaveResourcePage from "./SaveResource.test.page";
import {defaultProps} from "./SaveResource.test.data";
import {waitFor} from "@testing-library/react";

beforeEach(() => {
jest.resetModules();
});

describe("See the Create Resource - save resource", () => {
it.todo("As a signed-in user creating a password on the quickaccess, I should fill the form with meta received");
it("As a signed-in user creating a password on the quickaccess, I should fill the form with meta received", async() => {
expect.assertions(4);
// data mocked
const props = defaultProps(); // The props to pass
const resourceMetaFromTab = {
name: "Passbolt",
uri: "https://passbolt.com",
username: "username",
secret_clear: "secret"
};
// functions mocked
jest.spyOn(props.context.port, 'request').mockImplementationOnce(() => resourceMetaFromTab);
// process
const page = new SaveResourcePage(props);
await waitFor(() => {});
// expectations
expect(page.name.value).toBe(resourceMetaFromTab.name);
expect(page.username.value).toBe(resourceMetaFromTab.username);
expect(page.uri.value).toBe(resourceMetaFromTab.uri);
expect(page.password.value).toBe(resourceMetaFromTab.secret_clear);
});

it.todo("As a signed-in user creating a password on the quickaccess, I should be able to save resource");
it("As a signed-in user creating a password on the quickaccess, I should be able to save resource", async() => {
expect.assertions(2);
// data mocked
const props = defaultProps(); // The props to pass
const resourceMetaFromTab = {
name: "Passbolt",
uri: "https://passbolt.com",
username: "username",
secret_clear: "secret"
};
// functions mocked
jest.spyOn(props.context.port, 'request').mockImplementationOnce(() => resourceMetaFromTab);
jest.spyOn(window, 'close').mockImplementation(jest.fn());
// process
const page = new SaveResourcePage(props);
await waitFor(() => {});
await page.save();
// expected data
const resourceTypeId = props.context.resourceTypesSettings.findResourceTypeIdBySlug(props.context.resourceTypesSettings.DEFAULT_RESOURCE_TYPES_SLUGS.PASSWORD_AND_DESCRIPTION);
const resourceDto = {
metadata: {
name: resourceMetaFromTab.name,
username: resourceMetaFromTab.username,
uris: [resourceMetaFromTab.uri],
resource_type_id: resourceTypeId,
},
resource_type_id: resourceTypeId,
expired: props.passwordExpiryContext.getDefaultExpirationDate(),
};
const secretDto = {
password: resourceMetaFromTab.secret_clear,
description: ""
};
// expectations
expect(props.context.port.request).toHaveBeenCalledWith("passbolt.resources.create", resourceDto, secretDto);
expect(window.close).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
import React from "react";
import {render, waitFor} from "@testing-library/react";
import {StaticRouter} from 'react-router-dom';
import {BrowserRouter as Router} from "react-router-dom";
import MockTranslationProvider from '../../../react-extension/test/mock/components/Internationalisation/MockTranslationProvider';
import {fireEvent} from '@testing-library/react';
import SaveResource from "./SaveResource";
Expand All @@ -27,35 +27,42 @@ export default class SaveResourcePage {
* Default constructor
* @param props Props to attach
*/
constructor(context, props) {
constructor(props) {
this._page = render(
<MockTranslationProvider>
<StaticRouter context={context}>
<SaveResource context={context} {...props} debug />
</StaticRouter>
<Router>
<SaveResource {...props}/>
</Router>
</MockTranslationProvider>
);
}

/**
* Returns the password input element
* Returns the name input element
*/
get password() {
return this._page.container.querySelector('#password');
get name() {
return this._page.container.querySelector('#name');
}

/**
* Returns the username input element
*/
get username() {
return this._page.container.querySelector('#username');
}

/**
* Returns the pwned warning message
* Returns the uri input element
*/
get pwnedWarningMessage() {
return this._page.container.querySelector('.pwned-password.warning-message');
get uri() {
return this._page.container.querySelector('#uri');
}

/**
* Returns the password complexity
* Returns the password input element
*/
get passwordComplexity() {
return this._page.container.querySelector('.complexity-text');
get password() {
return this._page.container.querySelector('#password');
}

/**
Expand All @@ -65,17 +72,15 @@ export default class SaveResourcePage {
return this._page.container.querySelector('.submit-wrapper button[type=\"submit\"]');
}

/** fill the input password with data */
async fillInputPassword(data) {
const dataInputEvent = {target: {value: data}};
fireEvent.change(this.password, dataInputEvent);
jest.runAllTimers();
}

/** Click on the element */
async click(element) {
const leftClick = {button: 0};
fireEvent.click(element, leftClick);
await waitFor(() => {});
}

/** Submit teh form */
async save() {
await this.click(this.saveButton);
}
}

0 comments on commit 6845679

Please sign in to comment.