File size: 1,883 Bytes
21dd449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { describe, expect, it } from "vitest";
import { TEST_COOKIE, TEST_HUB_URL } from "../test/consts";
import { oauthLoginUrl } from "./oauth-login-url";
import { oauthHandleRedirect } from "./oauth-handle-redirect";

describe("oauthHandleRedirect", () => {
	it("should work", async () => {
		const localStorage = {
			nonce: undefined,
			codeVerifier: undefined,
		};
		const url = await oauthLoginUrl({
			clientId: "dummy-app",
			redirectUrl: "http://localhost:3000",
			localStorage,
			scopes: "openid profile email",
			hubUrl: TEST_HUB_URL,
		});
		const resp = await fetch(url, {
			method: "POST",
			headers: {
				Cookie: `token=${TEST_COOKIE}`,
			},
			redirect: "manual",
		});
		if (resp.status !== 303) {
			throw new Error(`Failed to fetch url ${url}: ${resp.status} ${resp.statusText}`);
		}
		const location = resp.headers.get("Location");
		if (!location) {
			throw new Error(`No location header in response`);
		}
		const result = await oauthHandleRedirect({
			redirectedUrl: location,
			codeVerifier: localStorage.codeVerifier,
			nonce: localStorage.nonce,
			hubUrl: TEST_HUB_URL,
		});

		if (!result) {
			throw new Error("Expected result to be defined");
		}
		expect(result.accessToken).toEqual(expect.any(String));
		expect(result.accessTokenExpiresAt).toBeInstanceOf(Date);
		expect(result.accessTokenExpiresAt.getTime()).toBeGreaterThan(Date.now());
		expect(result.scope).toEqual(expect.any(String));
		expect(result.userInfo).toEqual({
			sub: "62f264b9f3c90f4b6514a269",
			name: "@huggingface/hub CI bot",
			preferred_username: "hub.js",
			email_verified: true,
			email: "eliott@huggingface.co",
			isPro: false,
			picture: "https://hub-ci.huggingface.co/avatars/934b830e9fdaa879487852f79eef7165.svg",
			profile: "https://hub-ci.huggingface.co/hub.js",
			website: "https://github.com/huggingface/hub.js",
			orgs: [],
		});
	});
});