From edca759a8b0943013c12871477aa82d5e9ddfef8 Mon Sep 17 00:00:00 2001 From: Grégoire Aubert Date: Mon, 20 Nov 2017 14:26:25 +0100 Subject: Add Unit tests for users permission page --- .../main/js/apps/users/__tests__/Header-test.tsx | 37 ++++++++ .../main/js/apps/users/__tests__/UsersApp-test.tsx | 92 +++++++++++++++++++ .../src/main/js/apps/users/__tests__/UsersList.tsx | 70 +++++++++++++++ .../__tests__/__snapshots__/Header-test.tsx.snap | 33 +++++++ .../__tests__/__snapshots__/UsersApp-test.tsx.snap | 100 +++++++++++++++++++++ .../__tests__/__snapshots__/UsersList.tsx.snap | 64 +++++++++++++ .../components/__tests__/UserActions-test.tsx | 60 +++++++++++++ .../users/components/__tests__/UserGroups-test.tsx | 54 +++++++++++ .../components/__tests__/UserListItem-test.tsx | 61 +++++++++++++ .../__snapshots__/UserActions-test.tsx.snap | 23 +++++ .../__snapshots__/UserGroups-test.tsx.snap | 36 ++++++++ .../__snapshots__/UserListItem-test.tsx.snap | 71 +++++++++++++++ 12 files changed, 701 insertions(+) create mode 100644 server/sonar-web/src/main/js/apps/users/__tests__/Header-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/users/__tests__/UsersApp-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/users/__tests__/UsersList.tsx create mode 100644 server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/Header-test.tsx.snap create mode 100644 server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersApp-test.tsx.snap create mode 100644 server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersList.tsx.snap create mode 100644 server/sonar-web/src/main/js/apps/users/components/__tests__/UserActions-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/users/components/__tests__/UserGroups-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/users/components/__tests__/UserListItem-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserActions-test.tsx.snap create mode 100644 server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserGroups-test.tsx.snap create mode 100644 server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserListItem-test.tsx.snap (limited to 'server/sonar-web/src/main/js/apps/users') diff --git a/server/sonar-web/src/main/js/apps/users/__tests__/Header-test.tsx b/server/sonar-web/src/main/js/apps/users/__tests__/Header-test.tsx new file mode 100644 index 00000000000..4cfe8b1b42a --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/__tests__/Header-test.tsx @@ -0,0 +1,37 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { shallow } from 'enzyme'; +import Header from '../Header'; +import { click } from '../../../helpers/testUtils'; + +it('should render correctly', () => { + expect(getWrapper()).toMatchSnapshot(); +}); + +it('should open the user creation form', () => { + const wrapper = getWrapper(); + click(wrapper.find('#users-create')); + expect(wrapper.find('UserForm').exists()).toBeTruthy(); +}); + +function getWrapper(props = {}) { + return shallow(
); +} diff --git a/server/sonar-web/src/main/js/apps/users/__tests__/UsersApp-test.tsx b/server/sonar-web/src/main/js/apps/users/__tests__/UsersApp-test.tsx new file mode 100644 index 00000000000..eae63060e53 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/__tests__/UsersApp-test.tsx @@ -0,0 +1,92 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +/* eslint-disable import/order */ +import * as React from 'react'; +import { Location } from 'history'; +import { shallow } from 'enzyme'; +import UsersApp from '../UsersApp'; + +jest.mock('../../../api/users', () => ({ + getIdentityProviders: jest.fn(() => + Promise.resolve({ + identityProviders: [ + { + backgroundColor: 'blue', + iconPath: 'icon/path', + key: 'foo', + name: 'Foo Provider' + } + ] + }) + ), + searchUsers: jest.fn(() => + Promise.resolve({ + paging: { + pageIndex: 1, + pageSize: 1, + total: 2 + }, + users: [ + { + login: 'luke', + name: 'Luke', + active: true, + scmAccounts: [], + local: false + } + ] + }) + ) +})); + +const getIdentityProviders = require('../../../api/users').getIdentityProviders as jest.Mock; +const searchUsers = require('../../../api/users').searchUsers as jest.Mock; + +const currentUser = { isLoggedIn: true, login: 'luke' }; +const location = { pathname: '', query: {} } as Location; + +beforeEach(() => { + getIdentityProviders.mockClear(); + searchUsers.mockClear(); +}); + +it('should render correctly', async () => { + const wrapper = getWrapper(); + expect(wrapper).toMatchSnapshot(); + expect(getIdentityProviders).toHaveBeenCalled(); + expect(searchUsers).toHaveBeenCalled(); + await new Promise(setImmediate); + wrapper.update(); + expect(wrapper).toMatchSnapshot(); +}); + +function getWrapper(props = {}) { + return shallow( + , + { + context: { router: {} } + } + ); +} diff --git a/server/sonar-web/src/main/js/apps/users/__tests__/UsersList.tsx b/server/sonar-web/src/main/js/apps/users/__tests__/UsersList.tsx new file mode 100644 index 00000000000..aa3ed1b718a --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/__tests__/UsersList.tsx @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { shallow } from 'enzyme'; +import UsersList from '../UsersList'; + +const users = [ + { + login: 'luke', + name: 'Luke', + active: true, + scmAccounts: [], + local: false + }, + { + login: 'obi', + name: 'One', + active: true, + scmAccounts: [], + local: false + } +]; + +it('should render correctly', () => { + expect(getWrapper()).toMatchSnapshot(); +}); + +it('should show a group column', () => { + const wrapper = getWrapper({ organizationsEnabled: false }); + expect(wrapper.find('th').filterWhere(elem => elem.text() === 'my_profile.groups')).toHaveLength( + 1 + ); +}); + +function getWrapper(props = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/Header-test.tsx.snap b/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/Header-test.tsx.snap new file mode 100644 index 00000000000..6e38124302c --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/Header-test.tsx.snap @@ -0,0 +1,33 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` +
+

+ users.page +

+ +
+ +
+

+ users.page.description +

+
+`; diff --git a/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersApp-test.tsx.snap b/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersApp-test.tsx.snap new file mode 100644 index 00000000000..8aff49f07ed --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersApp-test.tsx.snap @@ -0,0 +1,100 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` +
+ +
+ + +
+`; + +exports[`should render correctly 2`] = ` +
+ +
+ + + +
+`; diff --git a/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersList.tsx.snap b/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersList.tsx.snap new file mode 100644 index 00000000000..4b732ab0f36 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/__tests__/__snapshots__/UsersList.tsx.snap @@ -0,0 +1,64 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + + + + + + + + + + + + +
+ + + my_profile.scm_accounts + + users.tokens + +   +
+`; diff --git a/server/sonar-web/src/main/js/apps/users/components/__tests__/UserActions-test.tsx b/server/sonar-web/src/main/js/apps/users/components/__tests__/UserActions-test.tsx new file mode 100644 index 00000000000..4e215347a51 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/components/__tests__/UserActions-test.tsx @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { shallow } from 'enzyme'; +import { click } from '../../../../helpers/testUtils'; +import UserActions from '../UserActions'; + +const user = { + login: 'obi', + name: 'One', + active: true, + scmAccounts: [], + local: false +}; + +it('should render correctly', () => { + expect(getWrapper()).toMatchSnapshot(); +}); + +it('should display change password action', () => { + expect( + getWrapper({ user: { ...user, local: true } }) + .find('.js-user-change-password') + .exists() + ).toBeTruthy(); +}); + +it('should open the update form', () => { + const wrapper = getWrapper(); + click(wrapper.find('.js-user-update')); + expect( + wrapper + .first() + .find('UserForm') + .exists() + ).toBeTruthy(); +}); + +function getWrapper(props = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/apps/users/components/__tests__/UserGroups-test.tsx b/server/sonar-web/src/main/js/apps/users/components/__tests__/UserGroups-test.tsx new file mode 100644 index 00000000000..bc87f01dad8 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/components/__tests__/UserGroups-test.tsx @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { shallow } from 'enzyme'; +import { click } from '../../../../helpers/testUtils'; +import UserGroups from '../UserGroups'; + +const user = { + login: 'obi', + name: 'One', + active: true, + scmAccounts: [], + local: false +}; + +const groups = ['foo', 'bar', 'baz', 'plop']; + +it('should render correctly', () => { + expect(getWrapper()).toMatchSnapshot(); +}); + +it('should show all groups', () => { + const wrapper = getWrapper(); + expect(wrapper.find('li')).toHaveLength(3); + click(wrapper.find('.js-user-more-groups')); + expect(wrapper.find('li')).toHaveLength(5); +}); + +it('should open the groups form', () => { + const wrapper = getWrapper(); + click(wrapper.find('.js-user-groups')); + expect(wrapper.find('GroupsForm').exists()).toBeTruthy(); +}); + +function getWrapper(props = {}) { + return shallow(); +} diff --git a/server/sonar-web/src/main/js/apps/users/components/__tests__/UserListItem-test.tsx b/server/sonar-web/src/main/js/apps/users/components/__tests__/UserListItem-test.tsx new file mode 100644 index 00000000000..632020a9068 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/components/__tests__/UserListItem-test.tsx @@ -0,0 +1,61 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { shallow } from 'enzyme'; +import { click } from '../../../../helpers/testUtils'; +import UserListItem from '../UserListItem'; + +const user = { + login: 'obi', + name: 'One', + active: true, + scmAccounts: [], + local: false +}; + +it('should render correctly', () => { + expect(getWrapper()).toMatchSnapshot(); +}); + +it('should display a change password button', () => { + expect( + getWrapper({ organizationsEnabled: true }) + .find('UserGroups') + .exists() + ).toBeFalsy(); +}); + +it('should open the correct forms', () => { + const wrapper = getWrapper(); + click(wrapper.find('.js-user-tokens')); + expect(wrapper.find('TokensForm').exists()).toBeTruthy(); +}); + +function getWrapper(props = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserActions-test.tsx.snap b/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserActions-test.tsx.snap new file mode 100644 index 00000000000..6328daaf8e0 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserActions-test.tsx.snap @@ -0,0 +1,23 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + + + update_details + + + + users.deactivate + + +`; diff --git a/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserGroups-test.tsx.snap b/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserGroups-test.tsx.snap new file mode 100644 index 00000000000..cbc45cc9034 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserGroups-test.tsx.snap @@ -0,0 +1,36 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + +`; diff --git a/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserListItem-test.tsx.snap b/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserListItem-test.tsx.snap new file mode 100644 index 00000000000..c7e647b57dd --- /dev/null +++ b/server/sonar-web/src/main/js/apps/users/components/__tests__/__snapshots__/UserListItem-test.tsx.snap @@ -0,0 +1,71 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + + + + + + + + + + + + + + + + + + + + +`; -- cgit v1.2.3