fix tests. msw has issues on firefox with vitest isolation.

This commit is contained in:
Henry Jameson 2026-06-18 19:58:50 +03:00
commit 9d24782cd8
11 changed files with 592 additions and 863 deletions

View file

@ -1,12 +1,64 @@
import { createTestingPinia } from '@pinia/testing'
import { HttpResponse, http } from 'msw'
import { createPinia, setActivePinia } from 'pinia'
import { setActivePinia } from 'pinia'
import { authApis, injectMswToTest } from '/test/fixtures/mock_api.js'
import { test as it } from '/test/fixtures/mock_api.js'
import { useOAuthStore } from 'src/stores/oauth.js'
const test = injectMswToTest(authApis)
import {
MASTODON_APP_URL,
MASTODON_APP_VERIFY_URL,
OAUTH_MFA_CHALLENGE_URL,
OAUTH_REVOKE_URL,
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()
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,
},
)
}
}),
]
describe('oauth store', () => {
beforeEach(() => {
@ -14,8 +66,11 @@ describe('oauth store', () => {
})
describe('createApp', () => {
test('it should use create an app and record client id and secret', async () => {
it('should use create an app and record client id and secret', async ({
worker,
}) => {
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')
@ -23,9 +78,9 @@ describe('oauth store', () => {
expect(app.clientSecret).to.eql('test-secret')
})
test('it should throw and not update if failed', async ({ worker }) => {
it('should throw and not update if failed', async ({ worker }) => {
worker.use(
http.post('/api/v1/apps', () => {
http.post(MASTODON_APP_URL, () => {
return HttpResponse.text('Throttled', { status: 429 })
}),
)
@ -39,7 +94,8 @@ describe('oauth store', () => {
})
describe('ensureApp', () => {
test('it should create an app if it does not exist', async () => {
it('should create an app if it does not exist', async ({ worker }) => {
worker.use(...authApis())
const store = useOAuthStore()
const app = await store.ensureApp()
expect(store.clientId).to.eql('test-id')
@ -48,9 +104,9 @@ describe('oauth store', () => {
expect(app.clientSecret).to.eql('test-secret')
})
test('it should not create an app if it exists', async ({ worker }) => {
it('should not create an app if it exists', async ({ worker }) => {
worker.use(
http.post('/api/v1/apps', () => {
http.post(MASTODON_APP_URL, () => {
return HttpResponse.text('Should not call this API', { status: 400 })
}),
)
@ -68,7 +124,8 @@ describe('oauth store', () => {
})
describe('getAppToken', () => {
test('it should get app token and set it in state', async () => {
it('should get app token and set it in state', async ({ worker }) => {
worker.use(...authApis())
const store = useOAuthStore()
store.clientId = 'test-id'
store.clientSecret = 'test-secret'
@ -78,7 +135,10 @@ describe('oauth store', () => {
expect(store.appToken).to.eql('test-app-token')
})
test('it should throw and not set state if it cannot get app token', async () => {
it('should throw and not set state if it cannot get app token', async ({
worker,
}) => {
worker.use(...authApis())
const store = useOAuthStore()
store.clientId = 'bad-id'
store.clientSecret = 'bad-secret'
@ -89,14 +149,17 @@ describe('oauth store', () => {
})
describe('ensureAppToken', () => {
test('it should work if the state is empty', async () => {
it('should work if the state is empty', async ({ worker }) => {
worker.use(...authApis())
console.log('=========', worker.listHandlers())
const store = useOAuthStore()
const token = await store.ensureAppToken()
expect(token).to.eql('test-app-token')
expect(store.appToken).to.eql('test-app-token')
})
test('it should work if we already have a working token', async () => {
it('should work if we already have a working token', async ({ worker }) => {
worker.use(...authApis())
const store = useOAuthStore()
store.appToken = 'also-good-app-token'
@ -105,11 +168,12 @@ describe('oauth store', () => {
expect(store.appToken).to.eql('also-good-app-token')
})
test('it should work if we have a bad token but good app credentials', async ({
it('should work if we have a bad token but good app credentials', async ({
worker,
}) => {
worker.use(
http.post('/api/v1/apps', () => {
...authApis(),
http.post(MASTODON_APP_URL, () => {
return HttpResponse.text('Should not call this API', { status: 400 })
}),
)
@ -123,11 +187,12 @@ describe('oauth store', () => {
expect(store.appToken).to.eql('test-app-token')
})
test('it should work if we have no token but good app credentials', async ({
it('should work if we have no token but good app credentials', async ({
worker,
}) => {
worker.use(
http.post('/api/v1/apps', () => {
...authApis(),
http.post(MASTODON_APP_URL, () => {
return HttpResponse.text('Should not call this API', { status: 400 })
}),
)
@ -140,7 +205,10 @@ describe('oauth store', () => {
expect(store.appToken).to.eql('test-app-token')
})
test('it should work if we have no token and bad app credentials', async () => {
it('should work if we have no token and bad app credentials', async ({
worker,
}) => {
worker.use(...authApis())
const store = useOAuthStore()
store.clientId = 'bad-id'
store.clientSecret = 'bad-secret'
@ -152,7 +220,10 @@ describe('oauth store', () => {
expect(store.clientSecret).to.eql('test-secret')
})
test('it should work if we have bad token and bad app credentials', async () => {
it('should work if we have bad token and bad app credentials', async ({
worker,
}) => {
worker.use(...authApis())
const store = useOAuthStore()
store.appToken = 'bad-app-token'
store.clientId = 'bad-id'
@ -165,9 +236,9 @@ describe('oauth store', () => {
expect(store.clientSecret).to.eql('test-secret')
})
test('it should throw if we cannot create an app', async ({ worker }) => {
it('should throw if we cannot create an app', async ({ worker }) => {
worker.use(
http.post('/api/v1/apps', () => {
http.post(MASTODON_APP_URL, () => {
return HttpResponse.text('Throttled', { status: 429 })
}),
)
@ -176,17 +247,15 @@ describe('oauth store', () => {
await expect(store.ensureAppToken()).rejects.toThrowError('Throttled')
})
test('it should throw if we cannot obtain app token', async ({
worker,
}) => {
it('should throw if we cannot obtain app token', async ({ worker }) => {
worker.use(
http.post('/oauth/token', () => {
http.post(OAUTH_TOKEN_URL, () => {
return HttpResponse.text('Throttled', { status: 429 })
}),
)
const store = useOAuthStore()
await expect(store.ensureAppToken()).rejects.toThrowError('Throttled')
await expect(store.getAppToken()).rejects.toThrowError('Throttled')
})
})
})