Create an app only when needed
This commit is contained in:
parent
1bdf67c08d
commit
3a4cdbef15
12 changed files with 449 additions and 34 deletions
62
test/fixtures/mock_api.js
vendored
Normal file
62
test/fixtures/mock_api.js
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { test as testBase } from 'vitest'
|
||||
import { setupWorker } from 'msw/browser'
|
||||
import { http, HttpResponse } from 'msw'
|
||||
|
||||
// https://mswjs.io/docs/recipes/vitest-browser-mode
|
||||
export const injectMswToTest = (defaultHandlers) => {
|
||||
const worker = setupWorker(...defaultHandlers)
|
||||
|
||||
return testBase.extend({
|
||||
worker: [
|
||||
async ({}, use) => {
|
||||
await worker.start()
|
||||
|
||||
await use(worker)
|
||||
|
||||
worker.resetHandlers()
|
||||
worker.stop()
|
||||
},
|
||||
{
|
||||
auto: true
|
||||
}
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export const testServer = 'https://test.server.example'
|
||||
|
||||
export const authApis = [
|
||||
http.post(`${testServer}/api/v1/apps`, () => {
|
||||
return HttpResponse.json({
|
||||
client_id: 'test-id',
|
||||
client_secret: 'test-secret'
|
||||
})
|
||||
}),
|
||||
http.get(`${testServer}/api/v1/apps/verify_credentials`, ({ request }) => {
|
||||
const authHeader = request.headers.get('Authorization')
|
||||
if (authHeader === 'Bearer test-app-token' ||
|
||||
authHeader === 'Bearer also-good-app-token') {
|
||||
return HttpResponse.json({})
|
||||
} else {
|
||||
// Pleroma 2.9.0 gives the following respoonse upon error
|
||||
return HttpResponse.json({ error: { detail: 'Internal server error' } }, {
|
||||
status: 400
|
||||
})
|
||||
}
|
||||
}),
|
||||
http.post(`${testServer}/oauth/token`, async ({ request }) => {
|
||||
const data = await request.formData()
|
||||
|
||||
if (data.get('client_id') === 'test-id' &&
|
||||
data.get('client_secret') === 'test-secret' &&
|
||||
data.get('grant_type') === 'client_credentials' &&
|
||||
data.has('redirect_uri')) {
|
||||
return HttpResponse.json({ access_token: 'test-app-token' })
|
||||
} else {
|
||||
// Pleroma 2.9.0 gives the following respoonse upon error
|
||||
return HttpResponse.json({ error: 'Invalid credentials' }, {
|
||||
status: 400
|
||||
})
|
||||
}
|
||||
})
|
||||
]
|
||||
199
test/unit/specs/modules/oauth.spec.js
Normal file
199
test/unit/specs/modules/oauth.spec.js
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
import { createStore } from 'vuex'
|
||||
import { http, HttpResponse } from 'msw'
|
||||
import oauth from 'src/modules/oauth.js'
|
||||
import { injectMswToTest, authApis, testServer } from '/test/fixtures/mock_api.js'
|
||||
|
||||
const test = injectMswToTest(authApis)
|
||||
|
||||
const getStore = (defaultStateInjection) => {
|
||||
const stateFunction = defaultStateInjection ? () => {
|
||||
return {
|
||||
...oauth.state(),
|
||||
...defaultStateInjection
|
||||
}
|
||||
} : oauth.state
|
||||
|
||||
return createStore({
|
||||
modules: {
|
||||
instance: {
|
||||
state: () => ({ server: testServer })
|
||||
},
|
||||
oauth: {
|
||||
...oauth,
|
||||
state: stateFunction
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
describe('createApp', () => {
|
||||
test('it should use create an app and record client id and secret', async () => {
|
||||
const store = getStore()
|
||||
const app = await store.dispatch('createApp')
|
||||
expect(store.state.oauth.clientId).to.eql('test-id')
|
||||
expect(store.state.oauth.clientSecret).to.eql('test-secret')
|
||||
expect(app.clientId).to.eql('test-id')
|
||||
expect(app.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
test('it should throw and not update if failed', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(`${testServer}/api/v1/apps`, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
})
|
||||
)
|
||||
|
||||
const store = getStore()
|
||||
const res = store.dispatch('createApp')
|
||||
await expect(res).rejects.toThrowError('Throttled')
|
||||
expect(store.state.oauth.clientId).to.eql(false)
|
||||
expect(store.state.oauth.clientSecret).to.eql(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureApp', () => {
|
||||
test('it should create an app if it does not exist', async () => {
|
||||
const store = getStore()
|
||||
const app = await store.dispatch('ensureApp')
|
||||
expect(store.state.oauth.clientId).to.eql('test-id')
|
||||
expect(store.state.oauth.clientSecret).to.eql('test-secret')
|
||||
expect(app.clientId).to.eql('test-id')
|
||||
expect(app.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
test('it should not create an app if it exists', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(`${testServer}/api/v1/apps`, () => {
|
||||
return HttpResponse.text('Should not call this API', { status: 400 })
|
||||
})
|
||||
)
|
||||
|
||||
const store = getStore({
|
||||
clientId: 'another-id',
|
||||
clientSecret: 'another-secret'
|
||||
})
|
||||
const app = await store.dispatch('ensureApp')
|
||||
expect(store.state.oauth.clientId).to.eql('another-id')
|
||||
expect(store.state.oauth.clientSecret).to.eql('another-secret')
|
||||
expect(app.clientId).to.eql('another-id')
|
||||
expect(app.clientSecret).to.eql('another-secret')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAppToken', () => {
|
||||
test('it should get app token and set it in state', async () => {
|
||||
const store = getStore({
|
||||
clientId: 'test-id',
|
||||
clientSecret: 'test-secret'
|
||||
})
|
||||
const token = await store.dispatch('getAppToken')
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
test('it should throw and not set state if it cannot get app token', async () => {
|
||||
const store = getStore({
|
||||
clientId: 'bad-id',
|
||||
clientSecret: 'bad-secret'
|
||||
})
|
||||
await expect(store.dispatch('getAppToken')).rejects.toThrowError('400')
|
||||
expect(store.state.oauth.appToken).to.eql(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureAppToken', () => {
|
||||
test('it should work if the state is empty', async () => {
|
||||
const store = getStore()
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
test('it should work if we already have a working token', async () => {
|
||||
const store = getStore({
|
||||
appToken: 'also-good-app-token'
|
||||
})
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
expect(token).to.eql('also-good-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('also-good-app-token')
|
||||
})
|
||||
|
||||
test('it should work if we have a bad token but good app credentials', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(`${testServer}/api/v1/apps`, () => {
|
||||
return HttpResponse.text('Should not call this API', { status: 400 })
|
||||
})
|
||||
)
|
||||
const store = getStore({
|
||||
appToken: 'bad-app-token',
|
||||
clientId: 'test-id',
|
||||
clientSecret: 'test-secret'
|
||||
})
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
test('it should work if we have no token but good app credentials', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(`${testServer}/api/v1/apps`, () => {
|
||||
return HttpResponse.text('Should not call this API', { status: 400 })
|
||||
})
|
||||
)
|
||||
const store = getStore({
|
||||
clientId: 'test-id',
|
||||
clientSecret: 'test-secret'
|
||||
})
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
test('it should work if we have no token and bad app credentials', async () => {
|
||||
const store = getStore({
|
||||
clientId: 'bad-id',
|
||||
clientSecret: 'bad-secret'
|
||||
})
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('test-app-token')
|
||||
expect(store.state.oauth.clientId).to.eql('test-id')
|
||||
expect(store.state.oauth.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
test('it should work if we have bad token and bad app credentials', async () => {
|
||||
const store = getStore({
|
||||
appToken: 'bad-app-token',
|
||||
clientId: 'bad-id',
|
||||
clientSecret: 'bad-secret'
|
||||
})
|
||||
const token = await store.dispatch('ensureAppToken')
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.state.oauth.appToken).to.eql('test-app-token')
|
||||
expect(store.state.oauth.clientId).to.eql('test-id')
|
||||
expect(store.state.oauth.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
test('it should throw if we cannot create an app', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(`${testServer}/api/v1/apps`, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
})
|
||||
)
|
||||
|
||||
const store = getStore()
|
||||
await expect(store.dispatch('ensureAppToken')).rejects.toThrowError('Throttled')
|
||||
})
|
||||
|
||||
test('it should throw if we cannot obtain app token', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(`${testServer}/oauth/token`, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
})
|
||||
)
|
||||
|
||||
const store = getStore()
|
||||
await expect(store.dispatch('ensureAppToken')).rejects.toThrowError('Throttled')
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue