get rid of msw, use fetch mock instead
This commit is contained in:
parent
98908a3b60
commit
341803dac2
8 changed files with 199 additions and 475 deletions
|
|
@ -1,9 +1,6 @@
|
|||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import { setActivePinia } from 'pinia'
|
||||
|
||||
import { test as it } from '/test/fixtures/mock_api.js'
|
||||
|
||||
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||
|
||||
import {
|
||||
|
|
@ -12,51 +9,67 @@ import {
|
|||
OAUTH_TOKEN_URL,
|
||||
} from 'src/api/oauth.js'
|
||||
|
||||
const authApis = () => [
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.json({
|
||||
client_id: 'test-id',
|
||||
client_secret: 'test-secret',
|
||||
})
|
||||
}),
|
||||
http.get(MASTODON_APP_VERIFY_URL, ({ 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(OAUTH_TOKEN_URL, async ({ request }) => {
|
||||
const data = await request.formData()
|
||||
const response = (data, extra = {}) =>
|
||||
new Response(JSON.stringify(data), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
...extra,
|
||||
})
|
||||
|
||||
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' })
|
||||
const defaultMockAppURL = () => {
|
||||
return response({
|
||||
client_id: 'test-id',
|
||||
client_secret: 'test-secret',
|
||||
})
|
||||
}
|
||||
|
||||
const defaultMockAppVerifyURL = (headers) => {
|
||||
const authHeader = headers.Authorization
|
||||
if (
|
||||
authHeader === 'Bearer test-app-token' ||
|
||||
authHeader === 'Bearer also-good-app-token'
|
||||
) {
|
||||
return response({})
|
||||
} else {
|
||||
return response(
|
||||
{ error: { detail: 'Internal server error' } },
|
||||
{ status: 400 },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const defaultMockOAuthTokenURL = (params) => {
|
||||
const data = params.body
|
||||
|
||||
if (
|
||||
data.get('client_id') === 'test-id' &&
|
||||
data.get('client_secret') === 'test-secret' &&
|
||||
data.get('grant_type') === 'client_credentials' &&
|
||||
data.has('redirect_uri')
|
||||
) {
|
||||
return response({ access_token: 'test-app-token' })
|
||||
} else {
|
||||
// Pleroma 2.9.0 gives the following respoonse upon error
|
||||
return response({ error: 'Invalid credentials' }, { status: 400 })
|
||||
}
|
||||
}
|
||||
|
||||
const authApis = ({ mockAppURL, mockAppVerifyURL, mockOAuthTokenURL } = {}) => {
|
||||
const mockFetch = vi.fn().mockImplementation((url, params) => {
|
||||
const { method = 'GET', headers } = params
|
||||
|
||||
if (url === MASTODON_APP_URL && method === 'POST') {
|
||||
return (mockAppURL ?? defaultMockAppURL)()
|
||||
} else if (url === MASTODON_APP_VERIFY_URL && method === 'GET') {
|
||||
return (mockAppVerifyURL ?? defaultMockAppVerifyURL)(headers)
|
||||
} else if (url === OAUTH_TOKEN_URL && method === 'POST') {
|
||||
return (mockOAuthTokenURL ?? defaultMockOAuthTokenURL)(params)
|
||||
} else {
|
||||
// Pleroma 2.9.0 gives the following respoonse upon error
|
||||
return HttpResponse.json(
|
||||
{ error: 'Invalid credentials' },
|
||||
{
|
||||
status: 400,
|
||||
},
|
||||
)
|
||||
return response({ error: 'Invalid request for test' }, { status: 401 })
|
||||
}
|
||||
}),
|
||||
]
|
||||
})
|
||||
vi.stubGlobal('fetch', mockFetch)
|
||||
return mockFetch
|
||||
}
|
||||
|
||||
describe('oauth store', () => {
|
||||
beforeEach(() => {
|
||||
|
|
@ -64,17 +77,10 @@ describe('oauth store', () => {
|
|||
})
|
||||
|
||||
describe('createApp', () => {
|
||||
it('should use create an app and record client id and secret', async ({
|
||||
worker,
|
||||
}) => {
|
||||
worker.use(
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
}),
|
||||
)
|
||||
it('should use create an app and record client id and secret', async () => {
|
||||
authApis()
|
||||
|
||||
const store = useOAuthStore()
|
||||
worker.use(...authApis())
|
||||
const app = await store.createApp()
|
||||
expect(store.clientId).to.eql('test-id')
|
||||
expect(store.clientSecret).to.eql('test-secret')
|
||||
|
|
@ -82,10 +88,13 @@ describe('oauth store', () => {
|
|||
expect(app.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
it('should throw and not update if failed', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
it('should throw and not update if failed', async () => {
|
||||
const mockFetch = authApis()
|
||||
mockFetch.mockResolvedValueOnce(
|
||||
new Response('Throttled', {
|
||||
status: 429,
|
||||
statusText: 'Throttled',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -98,8 +107,8 @@ describe('oauth store', () => {
|
|||
})
|
||||
|
||||
describe('ensureApp', () => {
|
||||
it('should create an app if it does not exist', async ({ worker }) => {
|
||||
worker.use(...authApis())
|
||||
it('should create an app if it does not exist', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
const app = await store.ensureApp()
|
||||
expect(store.clientId).to.eql('test-id')
|
||||
|
|
@ -108,12 +117,15 @@ describe('oauth store', () => {
|
|||
expect(app.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
it('should not create an app if it exists', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.text('Should not call this API', { status: 400 })
|
||||
}),
|
||||
)
|
||||
it('should not create an app if it exists', async () => {
|
||||
authApis({
|
||||
mockAppURL: () =>
|
||||
new Response('Throttled', {
|
||||
status: 429,
|
||||
statusText: 'Throttled',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
}),
|
||||
})
|
||||
|
||||
const store = useOAuthStore()
|
||||
store.clientId = 'another-id'
|
||||
|
|
@ -128,8 +140,8 @@ describe('oauth store', () => {
|
|||
})
|
||||
|
||||
describe('getAppToken', () => {
|
||||
it('should get app token and set it in state', async ({ worker }) => {
|
||||
worker.use(...authApis())
|
||||
it('should get app token and set it in state', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
store.clientId = 'test-id'
|
||||
store.clientSecret = 'test-secret'
|
||||
|
|
@ -139,10 +151,8 @@ describe('oauth store', () => {
|
|||
expect(store.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
it('should throw and not set state if it cannot get app token', async ({
|
||||
worker,
|
||||
}) => {
|
||||
worker.use(...authApis())
|
||||
it('should throw and not set state if it cannot get app token', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
store.clientId = 'bad-id'
|
||||
store.clientSecret = 'bad-secret'
|
||||
|
|
@ -153,16 +163,16 @@ describe('oauth store', () => {
|
|||
})
|
||||
|
||||
describe('ensureAppToken', () => {
|
||||
it('should work if the state is empty', async ({ worker }) => {
|
||||
worker.use(...authApis())
|
||||
it('should work if the state is empty', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
const token = await store.ensureAppToken()
|
||||
expect(token).to.eql('test-app-token')
|
||||
expect(store.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
it('should work if we already have a working token', async ({ worker }) => {
|
||||
worker.use(...authApis())
|
||||
it('should work if we already have a working token', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
store.appToken = 'also-good-app-token'
|
||||
|
||||
|
|
@ -171,15 +181,16 @@ describe('oauth store', () => {
|
|||
expect(store.appToken).to.eql('also-good-app-token')
|
||||
})
|
||||
|
||||
it('should work if we have a bad token but good app credentials', async ({
|
||||
worker,
|
||||
}) => {
|
||||
worker.use(
|
||||
...authApis(),
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.text('Should not call this API', { status: 400 })
|
||||
}),
|
||||
)
|
||||
it('should work if we have a bad token but good app credentials', async () => {
|
||||
authApis({
|
||||
mockAppURL: () =>
|
||||
new Response('Should not call this API', {
|
||||
status: 400,
|
||||
statusText: 'Should not call this API',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
}),
|
||||
})
|
||||
|
||||
const store = useOAuthStore()
|
||||
store.appToken = 'bad-app-token'
|
||||
store.clientId = 'test-id'
|
||||
|
|
@ -190,15 +201,16 @@ describe('oauth store', () => {
|
|||
expect(store.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
it('should work if we have no token but good app credentials', async ({
|
||||
worker,
|
||||
}) => {
|
||||
worker.use(
|
||||
...authApis(),
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.text('Should not call this API', { status: 400 })
|
||||
}),
|
||||
)
|
||||
it('should work if we have no token but good app credentials', async () => {
|
||||
authApis({
|
||||
mockAppURL: () =>
|
||||
new Response('Should not call this API', {
|
||||
status: 400,
|
||||
statusText: 'Should not call this API',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
}),
|
||||
})
|
||||
|
||||
const store = useOAuthStore()
|
||||
store.clientId = 'test-id'
|
||||
store.clientSecret = 'test-secret'
|
||||
|
|
@ -208,10 +220,8 @@ describe('oauth store', () => {
|
|||
expect(store.appToken).to.eql('test-app-token')
|
||||
})
|
||||
|
||||
it('should work if we have no token and bad app credentials', async ({
|
||||
worker,
|
||||
}) => {
|
||||
worker.use(...authApis())
|
||||
it('should work if we have no token and bad app credentials', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
store.clientId = 'bad-id'
|
||||
store.clientSecret = 'bad-secret'
|
||||
|
|
@ -223,10 +233,8 @@ describe('oauth store', () => {
|
|||
expect(store.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
it('should work if we have bad token and bad app credentials', async ({
|
||||
worker,
|
||||
}) => {
|
||||
worker.use(...authApis())
|
||||
it('should work if we have bad token and bad app credentials', async () => {
|
||||
authApis()
|
||||
const store = useOAuthStore()
|
||||
store.appToken = 'bad-app-token'
|
||||
store.clientId = 'bad-id'
|
||||
|
|
@ -239,23 +247,29 @@ describe('oauth store', () => {
|
|||
expect(store.clientSecret).to.eql('test-secret')
|
||||
})
|
||||
|
||||
it('should throw if we cannot create an app', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(MASTODON_APP_URL, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
}),
|
||||
)
|
||||
it('should throw if we cannot create an app', async () => {
|
||||
authApis({
|
||||
mockAppURL: () =>
|
||||
new Response('Throttled', {
|
||||
status: 429,
|
||||
statusText: 'Throttled',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
}),
|
||||
})
|
||||
|
||||
const store = useOAuthStore()
|
||||
await expect(store.ensureAppToken()).rejects.toThrowError('Throttled')
|
||||
})
|
||||
|
||||
it('should throw if we cannot obtain app token', async ({ worker }) => {
|
||||
worker.use(
|
||||
http.post(OAUTH_TOKEN_URL, () => {
|
||||
return HttpResponse.text('Throttled', { status: 429 })
|
||||
}),
|
||||
)
|
||||
it('should throw if we cannot obtain app token', async () => {
|
||||
authApis({
|
||||
mockOAuthTokenURL: () =>
|
||||
new Response('Throttled', {
|
||||
status: 429,
|
||||
statusText: 'Throttled',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
}),
|
||||
})
|
||||
|
||||
const store = useOAuthStore()
|
||||
await expect(store.getAppToken()).rejects.toThrowError('Throttled')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue