2020-02-05 10:37:14 +08:00
|
|
|
import React from 'react'
|
|
|
|
import { render, fireEvent, wait } from '@testing-library/react'
|
|
|
|
import * as fetch from '@/scripts/net'
|
2020-02-07 15:21:43 +08:00
|
|
|
import { t } from '@/scripts/i18n'
|
2020-02-05 10:37:14 +08:00
|
|
|
import OAuth from '@/views/user/OAuth'
|
|
|
|
import { App } from '@/views/user/OAuth/types'
|
|
|
|
|
|
|
|
jest.mock('@/scripts/net')
|
|
|
|
|
2020-02-07 15:15:38 +08:00
|
|
|
const fixture: Readonly<App> = Object.freeze({
|
2020-02-05 10:37:14 +08:00
|
|
|
id: 1,
|
|
|
|
name: 'My App',
|
|
|
|
redirect: 'http://url.test/',
|
|
|
|
secret: 'abc',
|
2020-02-07 15:15:38 +08:00
|
|
|
})
|
2020-02-05 10:37:14 +08:00
|
|
|
|
|
|
|
test('loading data', () => {
|
|
|
|
fetch.get.mockResolvedValue([])
|
|
|
|
const { queryByTitle } = render(<OAuth />)
|
|
|
|
expect(queryByTitle('Loading...')).toBeInTheDocument()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('create app', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
fetch.get.mockResolvedValue([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('succeeded', async () => {
|
2020-02-07 15:15:38 +08:00
|
|
|
fetch.post.mockResolvedValue(fixture)
|
2020-02-08 09:51:46 +08:00
|
|
|
const { getByLabelText, getByText, queryByText } = render(<OAuth />)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('user.oauth.create')))
|
2020-02-08 09:51:46 +08:00
|
|
|
fireEvent.input(getByLabelText(t('user.oauth.name')), {
|
2020-02-05 10:37:14 +08:00
|
|
|
target: { value: 'My App' },
|
|
|
|
})
|
2020-02-08 09:51:46 +08:00
|
|
|
fireEvent.input(getByLabelText(t('user.oauth.redirect')), {
|
2020-02-05 10:37:14 +08:00
|
|
|
target: { value: 'http://url.test/' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
|
|
|
expect(fetch.post).toBeCalledWith('/oauth/clients', {
|
|
|
|
name: 'My App',
|
|
|
|
redirect: 'http://url.test/',
|
|
|
|
})
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.id.toString())).toBeInTheDocument()
|
|
|
|
expect(queryByText(fixture.name)).toBeInTheDocument()
|
|
|
|
expect(queryByText(fixture.redirect)).toBeInTheDocument()
|
|
|
|
expect(queryByText(fixture.secret)).toBeInTheDocument()
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('failed', async () => {
|
|
|
|
fetch.post.mockResolvedValue({ message: 'exception' })
|
2020-02-08 09:51:46 +08:00
|
|
|
const { getByLabelText, getByText, getByRole, queryByText } = render(
|
2020-02-06 09:27:27 +08:00
|
|
|
<OAuth />,
|
|
|
|
)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('user.oauth.create')))
|
2020-02-08 09:51:46 +08:00
|
|
|
fireEvent.input(getByLabelText(t('user.oauth.name')), {
|
2020-02-05 10:37:14 +08:00
|
|
|
target: { value: 'My App' },
|
|
|
|
})
|
2020-02-08 09:51:46 +08:00
|
|
|
fireEvent.input(getByLabelText(t('user.oauth.redirect')), {
|
2020-02-05 10:37:14 +08:00
|
|
|
target: { value: 'http://url.test/' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
|
|
|
|
await wait()
|
|
|
|
expect(fetch.post).toBeCalledWith('/oauth/clients', {
|
|
|
|
name: 'My App',
|
|
|
|
redirect: 'http://url.test/',
|
|
|
|
})
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.name)).not.toBeInTheDocument()
|
|
|
|
expect(queryByText(fixture.redirect)).not.toBeInTheDocument()
|
2020-02-06 09:27:27 +08:00
|
|
|
expect(queryByText('exception')).toBeInTheDocument()
|
|
|
|
expect(getByRole('alert')).toHaveClass('alert-danger')
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('cancel dialog', async () => {
|
2020-02-08 09:51:46 +08:00
|
|
|
const { getByLabelText, getByText } = render(<OAuth />)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('user.oauth.create')))
|
2020-02-08 09:51:46 +08:00
|
|
|
fireEvent.input(getByLabelText(t('user.oauth.name')), {
|
2020-02-05 10:37:14 +08:00
|
|
|
target: { value: 'My App' },
|
|
|
|
})
|
2020-02-08 09:51:46 +08:00
|
|
|
fireEvent.input(getByLabelText(t('user.oauth.redirect')), {
|
2020-02-05 10:37:14 +08:00
|
|
|
target: { value: 'http://url.test/' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.cancel')))
|
2020-02-05 10:37:14 +08:00
|
|
|
|
|
|
|
await wait()
|
|
|
|
expect(fetch.post).not.toBeCalled()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('user.oauth.create')))
|
2020-02-08 09:51:46 +08:00
|
|
|
expect(getByLabelText(t('user.oauth.name'))).toHaveValue('')
|
|
|
|
expect(getByLabelText(t('user.oauth.redirect'))).toHaveValue('')
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('edit app', () => {
|
|
|
|
beforeEach(() => {
|
2020-02-07 15:15:38 +08:00
|
|
|
fetch.get.mockResolvedValue([fixture])
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('edit name', () => {
|
|
|
|
it('succeeded', async () => {
|
2020-02-07 15:15:38 +08:00
|
|
|
fetch.put.mockResolvedValue({ ...fixture, name: 'new name' })
|
2020-02-05 10:37:14 +08:00
|
|
|
|
2020-02-06 09:27:27 +08:00
|
|
|
const { getByTitle, getByText, getByDisplayValue, queryByText } = render(
|
|
|
|
<OAuth />,
|
|
|
|
)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByTitle(t('user.oauth.modifyName')))
|
2020-02-07 15:15:38 +08:00
|
|
|
fireEvent.input(getByDisplayValue(fixture.name), {
|
2020-02-06 09:27:27 +08:00
|
|
|
target: { value: 'new name' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
|
|
|
|
...fixture,
|
2020-02-05 10:37:14 +08:00
|
|
|
name: 'new name',
|
|
|
|
})
|
|
|
|
expect(queryByText('new name')).toBeInTheDocument()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('failed', async () => {
|
|
|
|
fetch.put.mockResolvedValue({ message: 'exception' })
|
|
|
|
|
2020-02-06 09:27:27 +08:00
|
|
|
const {
|
|
|
|
getByTitle,
|
|
|
|
getByText,
|
|
|
|
getByDisplayValue,
|
|
|
|
getByRole,
|
|
|
|
queryByText,
|
|
|
|
} = render(<OAuth />)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByTitle(t('user.oauth.modifyName')))
|
2020-02-07 15:15:38 +08:00
|
|
|
fireEvent.input(getByDisplayValue(fixture.name), {
|
2020-02-06 09:27:27 +08:00
|
|
|
target: { value: 'new name' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
|
|
|
|
...fixture,
|
2020-02-05 10:37:14 +08:00
|
|
|
name: 'new name',
|
|
|
|
})
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.name)).toBeInTheDocument()
|
2020-02-06 09:27:27 +08:00
|
|
|
expect(queryByText('exception')).toBeInTheDocument()
|
|
|
|
expect(getByRole('alert')).toHaveClass('alert-danger')
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('cancel dialog', async () => {
|
2020-02-06 09:27:27 +08:00
|
|
|
const { getByTitle, getByText, queryByText } = render(<OAuth />)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByTitle(t('user.oauth.modifyName')))
|
|
|
|
fireEvent.click(getByText(t('general.cancel')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
|
|
|
expect(fetch.put).not.toBeCalled()
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.name)).toBeInTheDocument()
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('edit redirect url', () => {
|
|
|
|
it('succeeded', async () => {
|
2020-02-07 15:15:38 +08:00
|
|
|
fetch.put.mockResolvedValue({ ...fixture, redirect: 'http://new.test/' })
|
2020-02-05 10:37:14 +08:00
|
|
|
|
2020-02-06 09:27:27 +08:00
|
|
|
const { getByTitle, getByDisplayValue, getByText, queryByText } = render(
|
|
|
|
<OAuth />,
|
|
|
|
)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByTitle(t('user.oauth.modifyUrl')))
|
2020-02-07 15:15:38 +08:00
|
|
|
fireEvent.input(getByDisplayValue(fixture.redirect), {
|
2020-02-06 09:27:27 +08:00
|
|
|
target: { value: 'http://new.test/' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
|
|
|
|
...fixture,
|
2020-02-05 10:37:14 +08:00
|
|
|
redirect: 'http://new.test/',
|
|
|
|
})
|
|
|
|
expect(queryByText('http://new.test/')).toBeInTheDocument()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('failed', async () => {
|
|
|
|
fetch.put.mockResolvedValue({ message: 'exception' })
|
|
|
|
|
2020-02-06 09:27:27 +08:00
|
|
|
const {
|
|
|
|
getByTitle,
|
|
|
|
getByDisplayValue,
|
|
|
|
getByText,
|
|
|
|
getByRole,
|
|
|
|
queryByText,
|
|
|
|
} = render(<OAuth />)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByTitle(t('user.oauth.modifyUrl')))
|
2020-02-07 15:15:38 +08:00
|
|
|
fireEvent.input(getByDisplayValue(fixture.redirect), {
|
2020-02-06 09:27:27 +08:00
|
|
|
target: { value: 'http://new.test/' },
|
|
|
|
})
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
|
|
|
|
...fixture,
|
2020-02-05 10:37:14 +08:00
|
|
|
redirect: 'http://new.test/',
|
|
|
|
})
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.redirect)).toBeInTheDocument()
|
2020-02-06 09:27:27 +08:00
|
|
|
expect(queryByText('exception')).toBeInTheDocument()
|
|
|
|
expect(getByRole('alert')).toHaveClass('alert-danger')
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('cancel dialog', async () => {
|
2020-02-06 09:27:27 +08:00
|
|
|
const { getByTitle, getByText, queryByText } = render(<OAuth />)
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByTitle(t('user.oauth.modifyUrl')))
|
|
|
|
fireEvent.click(getByText(t('general.cancel')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
|
|
|
expect(fetch.put).not.toBeCalled()
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.redirect)).toBeInTheDocument()
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('delete app', () => {
|
|
|
|
beforeEach(() => {
|
2020-02-07 15:15:38 +08:00
|
|
|
fetch.get.mockResolvedValue([fixture])
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('succeeded', async () => {
|
|
|
|
const { getByText, queryByText } = render(<OAuth />)
|
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('report.delete')))
|
|
|
|
fireEvent.click(getByText(t('general.confirm')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(fetch.del).toBeCalledWith(`/oauth/clients/${fixture.id}`)
|
|
|
|
expect(queryByText(fixture.name)).not.toBeInTheDocument()
|
|
|
|
expect(queryByText(fixture.redirect)).not.toBeInTheDocument()
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('cancel dialog', async () => {
|
|
|
|
const { getByText, queryByText } = render(<OAuth />)
|
|
|
|
await wait()
|
|
|
|
|
2020-02-07 15:21:43 +08:00
|
|
|
fireEvent.click(getByText(t('report.delete')))
|
|
|
|
fireEvent.click(getByText(t('general.cancel')))
|
2020-02-05 10:37:14 +08:00
|
|
|
await wait()
|
|
|
|
|
|
|
|
expect(fetch.post).not.toBeCalled()
|
2020-02-07 15:15:38 +08:00
|
|
|
expect(queryByText(fixture.name)).toBeInTheDocument()
|
|
|
|
expect(queryByText(fixture.redirect)).toBeInTheDocument()
|
2020-02-05 10:37:14 +08:00
|
|
|
})
|
|
|
|
})
|