Repository: cloudflare/wildebeest
File: backend/test/mastodon/oauth.spec.ts (TypeScript)

Code before:
			assert.equal(count, 0)
		})

		test('first login creates the user and redirects', async () => {
			const db = await makeDB()

			const params = new URLSearchParams({
				redirect_uri: 'https://redirect.com/a',
				email: 'a@cloudflare.com',
			})

			const formData = new FormData()
			formData.set('username', 'username')
			formData.set('name', 'name')

			const req = new Request('https://example.com/first-login?' + params, {
				method: 'POST',
				body: formData,
			})
			const res = await first_login.handlePostRequest(req, db, userKEK)
			assert.equal(res.status, 302)

			const location = res.headers.get('location')
			assert.equal(location, 'https://redirect.com/a')

			const actor = await db.prepare('SELECT * FROM actors').first<{ properties: string; email: string; id: string }>()
			const properties = JSON.parse(actor.properties)

			assert.equal(actor.email, 'a@cloudflare.com')
			assert.equal(properties.preferredUsername, 'username')
			assert.equal(properties.name, 'name')
			assert(isUrlValid(actor.id))
			// ensure that we generate a correct key pairs for the user
			assert((await getSigningKey(userKEK, db, actor as unknown as Actor)) instanceof CryptoKey)
		})

		test('token error on unknown client', async () => {
			const db = await makeDB()
			const body = new URLSearchParams({ code: 'some-code' })

			const req = new Request('https://example.com/oauth/token', {
				method: 'POST',
				body,
			})
			const res = await oauth_token.handleRequest(db, req)
			assert.equal(res.status, 403)
		})

		test('token returns auth infos', async () => {
			const db = await makeDB()
			const testScope = 'test abcd'

Review comment (suggestion):
How about 

```
   .first<{ properties: string; email: string; id: string }&Actor>()
```

then we don't need the `as unknown as Actor` below.

Code after:
			assert.equal(count, 0)
		})

		test('first login creates the user and redirects', async () => {
			const db = await makeDB()

			const params = new URLSearchParams({
				redirect_uri: 'https://redirect.com/a',
				email: 'a@cloudflare.com',
			})

			const formData = new FormData()
			formData.set('username', 'username')
			formData.set('name', 'name')

			const req = new Request('https://example.com/first-login?' + params, {
				method: 'POST',
				body: formData,
			})
			const res = await first_login.handlePostRequest(req, db, userKEK)
			assert.equal(res.status, 302)

			const location = res.headers.get('location')
			assert.equal(location, 'https://redirect.com/a')

			const actor = await db
				.prepare('SELECT * FROM actors')
				.first<{ properties: string; email: string; id: string } & Actor>()
			const properties = JSON.parse(actor.properties)

			assert.equal(actor.email, 'a@cloudflare.com')
			assert.equal(properties.preferredUsername, 'username')
			assert.equal(properties.name, 'name')
			assert(isUrlValid(actor.id))
			// ensure that we generate a correct key pairs for the user
			assert((await getSigningKey(userKEK, db, actor as Actor)) instanceof CryptoKey)
		})

		test('token error on unknown client', async () => {
			const db = await makeDB()
			const body = new URLSearchParams({ code: 'some-code' })

			const req = new Request('https://example.com/oauth/token', {
				method: 'POST',
				body,
			})
			const res = await oauth_token.handleRequest(db, req)
			assert.equal(res.status, 403)
		})

		test('token returns auth infos', async () => {