xref: /expo/apps/test-suite/tests/AuthSession.js (revision 55a1a37f)
1import * as AuthSession from 'expo-auth-session';
2import * as PKCE from 'expo-auth-session/build/PKCE';
3
4export const name = 'AuthSession';
5
6// Open ID RP cert testing server http://openid.net/certification/rp_testing
7// TODO(Bacon): Test exchanges
8// const testUri = "https://rp.certification.openid.net:8080/expo-auth-session/";
9
10export async function test({ describe, it, expect, jasmine }) {
11  describe('OpenID Connect Auto Discovery', () => {
12    const issuer = 'https://accounts.google.com';
13
14    it('fetches a discovery document from an issuer', async () => {
15      const discovery = await AuthSession.fetchDiscoveryAsync(issuer);
16      expect(discovery.authorizationEndpoint).toEqual(
17        'https://accounts.google.com/o/oauth2/v2/auth'
18      );
19      expect(discovery.tokenEndpoint).toEqual('https://oauth2.googleapis.com/token');
20      expect(discovery.revocationEndpoint).toEqual('https://oauth2.googleapis.com/revoke');
21      expect(discovery.userInfoEndpoint).toEqual(
22        'https://openidconnect.googleapis.com/v1/userinfo'
23      );
24    });
25  });
26
27  describe('PKCE', () => {
28    it(`creates the expected challenge for a valid code`, async () => {
29      const code = PKCE.generateRandom(43);
30      const challenge = await PKCE.deriveChallengeAsync(code);
31      expect(challenge).toBeTruthy();
32      // No `==` in the base64 encoded result.
33      expect(challenge.indexOf('=') < 0);
34    });
35
36    it(`generateRandom produces different values`, async () => {
37      const code1 = PKCE.generateRandom(10);
38      const code2 = PKCE.generateRandom(10);
39      expect(code1).not.toEqual(code2);
40    });
41
42    it(`produces the right base64 encoded challenge`, async () => {
43      const CODE = new Array(12).join('expo');
44      const challenge = await PKCE.deriveChallengeAsync(CODE);
45      expect(challenge).toEqual('J0cfOVZk6FKn67XzdP8stPFJ7bw2UY0hACisxoIefkU');
46    });
47  });
48}
49