admin-fe/test/views/users/index.test.js

380 lines
11 KiB
JavaScript
Raw Normal View History

2019-03-22 20:58:58 +00:00
import Vuex from 'vuex'
2019-03-22 21:27:31 +00:00
import { mount, createLocalVue, config } from '@vue/test-utils'
import flushPromises from 'flush-promises'
2019-03-22 20:58:58 +00:00
import Element from 'element-ui'
import Users from '@/views/users/index'
2019-06-11 15:13:48 +00:00
import NewAccountDialog from '@/views/users/components/NewAccountDialog'
import { storeConfig } from './store.conf'
2019-03-22 20:58:58 +00:00
import { cloneDeep } from 'lodash'
2019-03-22 21:27:31 +00:00
config.mocks["$t"] = () => {}
2019-03-29 14:25:53 +00:00
config.stubs['users-filter'] = '<div />'
2019-03-22 20:58:58 +00:00
const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(Element)
2020-04-17 22:27:00 +00:00
jest.mock('@/api/app')
2019-09-27 16:40:25 +00:00
jest.mock('@/api/nodeInfo')
2019-03-22 20:58:58 +00:00
jest.mock('@/api/users')
jest.mock('@/api/settings')
2019-03-22 20:58:58 +00:00
2019-03-22 21:18:57 +00:00
describe('Search and filter users', () => {
2019-03-22 20:58:58 +00:00
let store
beforeEach(() => {
store = new Vuex.Store(cloneDeep(storeConfig))
})
it('fetches initial list of users', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 20:58:58 +00:00
})
await flushPromises()
2020-08-05 22:53:23 +00:00
expect(wrapper.vm.usersCount).toEqual(4)
2019-03-22 20:58:58 +00:00
done()
})
it('starts a search on input change', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 20:58:58 +00:00
})
wrapper.vm.handleDebounceSearchInput = (query) => {
store.dispatch('SearchUsers', { query, page: 1 })
}
await flushPromises()
2020-08-05 22:53:23 +00:00
expect(wrapper.vm.usersCount).toEqual(4)
2019-03-29 14:25:53 +00:00
const input = wrapper.find('.search input.el-input__inner')
2019-03-22 20:58:58 +00:00
input.element.value = 'bob'
input.trigger('input')
await flushPromises()
2019-03-22 20:58:58 +00:00
expect(wrapper.vm.usersCount).toEqual(1)
input.element.value = ''
input.trigger('input')
await flushPromises()
2020-08-05 22:53:23 +00:00
expect(wrapper.vm.usersCount).toEqual(4)
2019-03-22 20:58:58 +00:00
done()
})
})
2019-03-22 21:18:57 +00:00
describe('Users actions', () => {
let store
const htmlElement = (trChild, liChild) =>
2020-08-23 16:37:40 +00:00
`.el-table__fixed-body-wrapper table tr:nth-child(${trChild}) ul.el-dropdown-menu > li:nth-child(${liChild})`
2019-03-22 21:18:57 +00:00
beforeEach(() => {
store = new Vuex.Store(cloneDeep(storeConfig))
})
2019-10-18 11:57:48 +00:00
it('grants admin right to a local user', async (done) => {
2019-03-22 21:18:57 +00:00
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2019-03-22 21:18:57 +00:00
const user = store.state.users.fetchedUsers[2]
expect(user.roles.admin).toBe(false)
expect(user.roles.moderator).toBe(false)
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(3, 2)).trigger('click')
2019-10-18 11:57:48 +00:00
const updatedUser = store.state.users.fetchedUsers[2]
expect(updatedUser.roles.admin).toBe(true)
done()
})
it('grants moderator right to a local user', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
})
await flushPromises()
2019-10-18 11:57:48 +00:00
const user = store.state.users.fetchedUsers[2]
expect(user.roles.admin).toBe(false)
expect(user.roles.moderator).toBe(false)
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(3, 3)).trigger('click')
2019-03-22 21:18:57 +00:00
const updatedUser = store.state.users.fetchedUsers[2]
expect(updatedUser.roles.moderator).toBe(true)
done()
})
it('does not show actions that grant admin and moderator rights to external users', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2019-03-22 21:18:57 +00:00
2019-03-22 21:27:31 +00:00
const dropdownMenuItems = wrapper.findAll(
2020-08-23 16:37:40 +00:00
`.el-table__fixed-body-wrapper table tr:nth-child(2) ul.el-dropdown-menu > li`
2019-03-22 21:27:31 +00:00
)
2020-08-23 16:37:40 +00:00
expect(dropdownMenuItems.length).toBe(7)
2019-03-22 21:18:57 +00:00
done()
})
it('toggles activation status', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2019-03-22 21:18:57 +00:00
const user = store.state.users.fetchedUsers[1]
expect(user.deactivated).toBe(false)
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(2, 2)).trigger('click')
2019-03-22 21:18:57 +00:00
const updatedUser = store.state.users.fetchedUsers[1]
expect(updatedUser.deactivated).toBe(true)
done()
})
2019-07-27 11:51:31 +00:00
it('deletes user', async (done) => {
2019-03-22 21:18:57 +00:00
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2020-07-17 21:13:06 +00:00
expect(store.state.users.fetchedUsers[1].deactivated).toBe(false)
2019-03-22 21:18:57 +00:00
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(2, 3)).trigger('click')
store.dispatch('DeleteUsers', { users: [{ active: true, deactivated: false, id: '10', nickname: 'bob', local: false, external: true, roles: { admin: false, moderator: false }, tags: ['mrf_tag:sandbox'] }] })
2020-07-17 21:13:06 +00:00
await flushPromises()
2020-07-17 21:13:06 +00:00
expect(store.state.users.fetchedUsers[1].deactivated).toBe(true)
2019-03-22 21:18:57 +00:00
done()
})
it('adds tags', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2019-03-22 21:18:57 +00:00
const user1 = store.state.users.fetchedUsers[0]
const user2 = store.state.users.fetchedUsers[1]
expect(user1.tags.length).toBe(0)
expect(user2.tags.length).toBe(1)
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(1, 6)).trigger('click')
wrapper.find(htmlElement(2, 6)).trigger('click')
2019-03-22 21:18:57 +00:00
const updatedUser1 = store.state.users.fetchedUsers[0]
const updatedUser2 = store.state.users.fetchedUsers[1]
expect(updatedUser1.tags.length).toBe(1)
expect(updatedUser2.tags.length).toBe(2)
done()
})
it('deletes tags', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2019-03-22 21:18:57 +00:00
const user = store.state.users.fetchedUsers[1]
expect(user.tags.length).toBe(1)
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(2, 7)).trigger('click')
2019-03-22 21:18:57 +00:00
const updatedUser = store.state.users.fetchedUsers[1]
expect(updatedUser.tags.length).toBe(0)
done()
})
it('does not change user index in array when tag is added', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-03-22 21:18:57 +00:00
})
await flushPromises()
2019-03-22 21:18:57 +00:00
const firstUserNickname = store.state.users.fetchedUsers[0].nickname
const secondUserNickname = store.state.users.fetchedUsers[1].nickname
expect(firstUserNickname).toBe('allis')
expect(secondUserNickname).toBe('bob')
wrapper.find(htmlElement(2, 5)).trigger('click')
await flushPromises()
2019-03-22 21:18:57 +00:00
const firstUserNicknameAfterToggle = store.state.users.fetchedUsers[0].nickname
const secondUserNicknameAfterToggle = store.state.users.fetchedUsers[1].nickname
expect(firstUserNicknameAfterToggle).toEqual('allis')
expect(secondUserNicknameAfterToggle).toEqual('bob')
done()
})
2019-09-23 19:00:28 +00:00
it('creates password revoke token', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
})
await flushPromises()
wrapper.setData({ resetPasswordDialogOpen: false })
2019-09-23 19:00:28 +00:00
const closeDialogButton = wrapper.find('.password-reset-token-dialog button')
expect(wrapper.vm.resetPasswordDialogOpen).toBe(false)
2019-09-23 19:00:28 +00:00
expect(store.state.users.passwordResetToken.token).toBe('')
2020-08-23 16:37:40 +00:00
wrapper.find(htmlElement(1, 12)).trigger('click')
2019-09-23 19:00:28 +00:00
await flushPromises()
expect(wrapper.vm.resetPasswordDialogOpen).toBe(true)
2019-09-23 19:00:28 +00:00
expect(store.state.users.passwordResetToken.token).toBe('g05lxnBJQnL')
expect(store.state.users.passwordResetToken.link).toBe('http://url/api/pleroma/password_reset/g05lxnBJQnL')
closeDialogButton.trigger('click')
await flushPromises()
expect(wrapper.vm.resetPasswordDialogOpen).toBe(false)
2019-09-23 19:00:28 +00:00
done()
})
2019-03-22 21:18:57 +00:00
})
2019-06-11 15:13:48 +00:00
describe('Creates new account', () => {
let store
const nicknameInput = 'input[name="nickname"]'
const emailInput = 'input[name="email"]'
const passwordInput = 'input[name="password"]'
beforeEach(async() => {
store = new Vuex.Store(cloneDeep(storeConfig))
})
it('opens and closes dialog window', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-06-11 15:13:48 +00:00
})
await flushPromises()
wrapper.setData({ createAccountDialogOpen: false })
2019-06-11 15:13:48 +00:00
const openDialogButton = wrapper.find('button.actions-button')
const closeDialogButton = wrapper.find('div.el-dialog__footer button')
expect(wrapper.vm.createAccountDialogOpen).toBe(false)
2019-06-11 15:13:48 +00:00
openDialogButton.trigger('click')
await flushPromises()
expect(wrapper.vm.createAccountDialogOpen).toBe(true)
2019-06-11 15:13:48 +00:00
closeDialogButton.trigger('click')
await flushPromises()
expect(wrapper.vm.createAccountDialogOpen).toBe(false)
2019-06-11 15:13:48 +00:00
done()
})
it('creates new account', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-06-11 15:13:48 +00:00
})
await flushPromises()
2020-08-05 22:53:23 +00:00
expect(wrapper.vm.usersCount).toEqual(4)
2019-06-11 15:13:48 +00:00
const openDialogButton = wrapper.find('button.actions-button')
openDialogButton.trigger('click')
await flushPromises()
const nickname = wrapper.find(nicknameInput)
nickname.element.value = 'marshall'
nickname.trigger('input')
const email = wrapper.find(emailInput)
email.element.value = 'marshall@marshall.com'
email.trigger('input')
const password = wrapper.find(passwordInput)
password.element.value = '1234'
password.trigger('input')
const createButton = wrapper.find('button.el-button--primary')
createButton.trigger('click')
await flushPromises()
2020-08-05 22:53:23 +00:00
expect(wrapper.vm.usersCount).toEqual(5)
2019-06-11 15:13:48 +00:00
done()
})
it('validates data', () => {
const wrapper = mount(NewAccountDialog, {
store,
localVue,
sync: false,
stubs: ['router-link']
2019-06-11 15:13:48 +00:00
})
const validateEmailRule = { validator: wrapper.vm.validateEmail, field: 'email', fullField: 'email', type: 'string' }
const validatePasswordRule = { validator: wrapper.vm.validatePassword, field: 'password', fullField: 'password', type: 'string' }
const validateUsernameRule = { validator: wrapper.vm.validateUsername, field: 'nickname', fullField: 'nickname', type: 'string' }
const identity = val => val
expect(wrapper.vm.validateUsername(validateUsernameRule, '', identity)).toBeInstanceOf(Error)
expect(wrapper.vm.validateUsername(validateUsernameRule, 'marshall%$', identity)).toBeInstanceOf(Error)
2019-06-11 15:13:48 +00:00
expect(wrapper.vm.validateUsername(validateUsernameRule, 'Marshall66', identity)).toBeUndefined()
expect(wrapper.vm.validateEmail(validateEmailRule, '', identity)).toBeInstanceOf(Error)
expect(wrapper.vm.validateEmail(validateEmailRule, 'test', identity)).toBeInstanceOf(Error)
2019-06-11 15:13:48 +00:00
expect(wrapper.vm.validateEmail(validateEmailRule, 'test@test.com', identity)).toBeUndefined()
expect(wrapper.vm.validatePassword(validatePasswordRule, '', identity)).toBeInstanceOf(Error)
2019-06-11 15:13:48 +00:00
expect(wrapper.vm.validatePassword(validatePasswordRule, '1234', identity)).toBeUndefined()
2020-08-23 21:31:38 +00:00
})
2019-06-11 15:13:48 +00:00
2020-08-23 21:31:38 +00:00
it('updates actor type', async (done) => {
const wrapper = mount(Users, {
store,
localVue,
sync: false,
stubs: ['router-link']
})
await flushPromises()
const user = store.state.users.fetchedUsers[0]
expect(user.actor_type).toBe('Person')
2019-06-11 15:13:48 +00:00
2020-08-23 21:31:38 +00:00
const findWrapper = (trChild, liChild1, liChild2) =>
`.el-table__fixed-body-wrapper table tr:nth-child(${trChild}) ul.el-dropdown-menu > li:nth-child(${liChild1}) ul li:nth-child(${liChild2})`
wrapper.find(findWrapper(1, 1, 1)).trigger('click')
const updatedUser = store.state.users.fetchedUsers[0]
expect(updatedUser.actor_type).toBe('Service')
done()
2019-06-11 15:13:48 +00:00
})
})