Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

3.0 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { githubProvider, githubUserInfo, verifiedPrimaryEmail } from './github-sign-in.ts'

const profile = {
  id: 4242,
  login: 'mike-b',
  name: 'Mike Brennan',
  avatar_url: 'https://avatars.example/4242',
  email: '[email protected]',
}

/** A fake GitHub API: /user answers `profile`, /user/emails answers `emails`. */
function fakeGitHub(emails: unknown, status = 200) {
  return vi.fn(async (url: string | URL | Request) => {
    const body = String(url).endsWith('/user/emails') ? emails : profile
    return new Response(JSON.stringify(body), { status: String(url).endsWith('/user/emails') ? status : 200 })
  }) as unknown as typeof fetch
}

describe('verifiedPrimaryEmail', () => {
  it('takes the primary address only when GitHub verified it', () => {
    expect(
      verifiedPrimaryEmail([
        { email: '[email protected]', primary: false, verified: true },
        { email: '[email protected]', primary: true, verified: true },
      ]),
    ).toBe('[email protected]')
    expect(
      verifiedPrimaryEmail([
        { email: '[email protected]', primary: false, verified: true },
        { email: '[email protected]', primary: true, verified: false },
      ]),
    ).toBeNull()
    expect(verifiedPrimaryEmail([{ email: '[email protected]', primary: false, verified: true }])).toBeNull()
    expect(verifiedPrimaryEmail(null)).toBeNull()
    expect(verifiedPrimaryEmail({ message: 'Not Found' })).toBeNull()
  })
})

describe('githubUserInfo', () => {
  it('reports the verified primary address, never the public profile address', async () => {
    const info = await githubUserInfo(
      { accessToken: 't' },
      fakeGitHub([
        { email: '[email protected]', primary: false, verified: true },
        { email: '[email protected]', primary: true, verified: true },
      ]),
    )
    expect(info?.user).toEqual({
      name: 'Mike Brennan',
      email: '[email protected]',
      image: profile.avatar_url,
      emailVerified: true,
    })
    expect(info?.data.id).toBe(4242)
  })

  it('reports no email when the primary address is unverified, so Better Auth refuses the sign-in', async () => {
    const info = await githubUserInfo(
      { accessToken: 't' },
      fakeGitHub([{ email: '[email protected]', primary: true, verified: false }]),
    )
    expect(info?.user).toMatchObject({ email: null, emailVerified: false })
    expect(info?.data.email).toBeNull()
  })

  it('reports no email when the emails could not be read (scope not granted)', async () => {
    const info = await githubUserInfo({ accessToken: 't' }, fakeGitHub({ message: 'Forbidden' }, 403))
    expect(info?.user).toMatchObject({ email: null, emailVerified: false })
  })

  it('gives up without an access token', async () => {
    expect(await githubUserInfo({}, fakeGitHub([]))).toBeNull()
  })

  it('asks only for read:user and user:email', () => {
    expect(githubProvider({ clientId: 'id', clientSecret: 's' })).toMatchObject({
      scope: ['read:user', 'user:email'],
      disableDefaultScope: true,
    })
  })
})